? = null,
32 | isSelectFromFutureEnable: Boolean = false,
33 | textStyle: TextStyle = LocalTextStyle.current,
34 | onSelectedDateChange: (SelectedDate) -> Unit
35 | ) {
36 | val currentDate = Date(System.currentTimeMillis())
37 | val currentDateShamsi = ShamsiDatePickerUtill()
38 | currentDateShamsi.gregorianToPersian(currentDate)
39 | val heroDatePickerUtil: HeroDatePickerUtil by lazy {
40 | with(currentDateShamsi) {
41 | val maxMonth = if (isSelectFromFutureEnable) 12 else month
42 | val maxDay = if (isSelectFromFutureEnable) 31 else day
43 | val maxYear = when {
44 | selectableYearRange != null -> selectableYearRange.last()
45 | isSelectFromFutureEnable -> year + 100
46 | else -> year
47 | }
48 | ShamsiHeroDatePicker(
49 | maxYear,
50 | maxMonth,
51 | maxDay,
52 | selectableYearRange
53 | )
54 | }
55 | }
56 |
57 | val selectedDateShamsi = ShamsiDatePickerUtill()
58 | selectedDateShamsi.gregorianToPersian(selectedDate)
59 |
60 | LaunchedEffect(Unit) {
61 | onSelectedDateChange(
62 | SelectedDate(
63 | selectedDateShamsi.year,
64 | selectedDateShamsi.month,
65 | selectedDateShamsi.day
66 | )
67 | )
68 | }
69 |
70 | var selectedYear by remember { mutableStateOf(selectedDateShamsi.year) }
71 | var selectedMonth by remember { mutableStateOf(selectedDateShamsi.month) }
72 | var selectedDay by remember { mutableStateOf(selectedDateShamsi.day) }
73 |
74 |
75 |
76 | Column(modifier = modifier) {
77 | val context = LocalContext.current
78 | Row(modifier = Modifier.fillMaxWidth()) {
79 | NumberPicker(
80 | modifier = Modifier.weight(0.25f),
81 | textStyle = textStyle,
82 | range = heroDatePickerUtil.getYearRange(),
83 | value = selectedYear,
84 | onValueChange = {
85 | selectedYear = it
86 | onSelectedDateChange(SelectedDate(selectedYear, selectedMonth, selectedDay))
87 | }
88 | )
89 |
90 | NumberPicker(
91 | modifier = Modifier.weight(0.5f),
92 | textStyle = textStyle,
93 | range = heroDatePickerUtil.getMonthRange(selectedYear),
94 | value = selectedMonth,
95 | label = { value ->
96 | heroDatePickerUtil.getMonthName(value, context) + " / " + value.toString()
97 | },
98 | onValueChange = {
99 | selectedMonth = it
100 | onSelectedDateChange(SelectedDate(selectedYear, selectedMonth, selectedDay))
101 | }
102 | )
103 |
104 | NumberPicker(
105 | modifier = Modifier.weight(0.25f),
106 | textStyle = textStyle,
107 | range = heroDatePickerUtil.getDayRange(selectedMonth, selectedYear),
108 | value = selectedDay,
109 | onValueChange = {
110 | selectedDay = it
111 | onSelectedDateChange(SelectedDate(selectedYear, selectedMonth, selectedDay))
112 | }
113 | )
114 | }
115 | }
116 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/HeroDatePicker/src/main/java/com/hamid97m/herodatepicker/utils/ShamsiDatePickerUtill.java:
--------------------------------------------------------------------------------
1 | package com.hamid97m.herodatepicker.utils;
2 |
3 | import android.annotation.SuppressLint;
4 |
5 | import java.util.Calendar;
6 | import java.util.Date;
7 |
8 | public class ShamsiDatePickerUtill {
9 |
10 | private int day, month, year;
11 | private int jYear, jMonth, jDay;
12 | private int gYear, gMonth, gDay;
13 | private int leap, march;
14 |
15 | /**
16 | * Calculates the Julian Day number (JG2JD) from Gregorian or Julian
17 | *
18 | * calendar dates. This integer number corresponds to the noon of the date
19 | *
20 | * (i.e. 12 hours of Universal Time). The procedure was tested to be good
21 | *
22 | * since 1 March, -100100 (of both the calendars) up to a few millions
23 | *
24 | * (10**6) years into the future. The algorithm is based on D.A. Hatcher,
25 | *
26 | * Q.Jl.R.Astron.Soc. 25(1984), 53-55 slightly modified by me (K.M.
27 | *
28 | * Borkowski, Post.Astron. 25(1987), 275-279).
29 | *
30 | * @param year int
31 | * @param month int
32 | * @param day int
33 | * @param J1G0 to be set to 1 for Julian and to 0 for Gregorian calendar
34 | * @return Julian Day number
35 | */
36 | private int JG2JD(int year, int month, int day, int J1G0) {
37 | int jd = (1461 * (year + 4800 + (month - 14) / 12)) / 4
38 | + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12
39 | - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4 + day
40 | - 32075;
41 | if (J1G0 == 0) {
42 | jd = jd - (year + 100100 + (month - 8) / 6) / 100 * 3 / 4 + 752;
43 | }
44 | return jd;
45 | }
46 |
47 | /**
48 | * Calculates Gregorian and Julian calendar dates from the Julian Day number
49 | *
50 | * (JD) for the period since JD=-34839655 (i.e. the year -100100 of both the
51 | *
52 | * calendars) to some millions (10**6) years ahead of the present. The
53 | *
54 | * algorithm is based on D.A. Hatcher, Q.Jl.R.Astron.Soc. 25(1984), 53-55
55 | *
56 | * slightly modified by me (K.M. Borkowski, Post.Astron. 25(1987), 275-279).
57 | *
58 | * @param JD Julian day number as int
59 | * @param J1G0 to be set to 1 for Julian and to 0 for Gregorian calendar
60 | */
61 | private void JD2JG(int JD, int J1G0) {
62 | int i, j;
63 | j = 4 * JD + 139361631;
64 | if (J1G0 == 0) {
65 | j = j + (4 * JD + 183187720) / 146097 * 3 / 4 * 4 - 3908;
66 | }
67 | i = (j % 1461) / 4 * 5 + 308;
68 | gDay = (i % 153) / 5 + 1;
69 | gMonth = ((i / 153) % 12) + 1;
70 | gYear = j / 1461 - 100100 + (8 - gMonth) / 6;
71 | }
72 |
73 | /**
74 | * Converts the Julian Day number to a date in the Jalali calendar
75 | *
76 | * @param JDN the Julian Day number
77 | */
78 | private void JD2Jal(int JDN) {
79 | JD2JG(JDN, 0);
80 | jYear = gYear - 621;
81 | JalCal(jYear);
82 | int JDN1F = JG2JD(gYear, 3, march, 0);
83 | int k = JDN - JDN1F;
84 | if (k >= 0) {
85 | if (k <= 185) {
86 | jMonth = 1 + k / 31;
87 | jDay = (k % 31) + 1;
88 | return;
89 | } else {
90 | k = k - 186;
91 | }
92 | } else {
93 | jYear = jYear - 1;
94 | k = k + 179;
95 | if (leap == 1) {
96 | k = k + 1;
97 | }
98 | }
99 | jMonth = 7 + k / 30;
100 | jDay = (k % 30) + 1;
101 | }
102 |
103 | /**
104 | * Converts a date of the Jalali calendar to the Julian Day Number
105 | *
106 | * @param jY Jalali year as int
107 | * @param jM Jalali month as int
108 | * @param jD Jalali day as int
109 | * @return Julian day number
110 | */
111 | private int Jal2JD(int jY, int jM, int jD) {
112 | JalCal(jY);
113 | return JG2JD(gYear, 3, march, 1) + (jM - 1) * 31 - jM / 7 * (jM - 7) + jD - 1;
114 | }
115 |
116 | /**
117 | * This procedure determines if the Jalali (Persian) year is leap (366-day
118 | *
119 | * long) or is the common year (365 days), and finds the day in March
120 | *
121 | * (Gregorian calendar) of the first day of the Jalali year (jYear)
122 | *
123 | * @param jY Jalali calendar year (-61 to 3177)
124 | */
125 | private void JalCal(int jY) {
126 | march = 0;
127 | leap = 0;
128 | int[] breaks = {-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210,
129 | 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178};
130 | gYear = jY + 621;
131 | int leapJ = -14;
132 | int jp = breaks[0];
133 | int jump = 0;
134 | for (int j = 1; j <= 19; j++) {
135 | int jm = breaks[j];
136 | jump = jm - jp;
137 | if (jY < jm) {
138 | int N = jY - jp;
139 | leapJ = leapJ + N / 33 * 8 + (N % 33 + 3) / 4;
140 | if ((jump % 33) == 4 && (jump - N) == 4) {
141 | leapJ = leapJ + 1;
142 | }
143 | int leapG = (gYear / 4) - (gYear / 100 + 1) * 3 / 4 - 150;
144 | march = 20 + leapJ - leapG;
145 | if ((jump - N) < 6) {
146 | N = N - jump + (jump + 4) / 33 * 33;
147 | }
148 | leap = ((((N + 1) % 33) - 1) % 4);
149 | if (leap == -1) {
150 | leap = 4;
151 | }
152 | break;
153 | }
154 | leapJ = leapJ + jump / 33 * 8 + (jump % 33) / 4;
155 | jp = jm;
156 | }
157 | }
158 |
159 | public static boolean isFarsiLeap(int jY) {
160 | int march = 0;
161 | int leap = 0;
162 | int[] breaks = {-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210,
163 | 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178};
164 | int gYear = jY + 621;
165 | int leapJ = -14;
166 | int jp = breaks[0];
167 | int jump = 0;
168 | for (int j = 1; j <= 19; j++) {
169 | int jm = breaks[j];
170 | jump = jm - jp;
171 | if (jY < jm) {
172 | int N = jY - jp;
173 | leapJ = leapJ + N / 33 * 8 + (N % 33 + 3) / 4;
174 | if ((jump % 33) == 4 && (jump - N) == 4) {
175 | leapJ = leapJ + 1;
176 | }
177 | int leapG = (gYear / 4) - (gYear / 100 + 1) * 3 / 4 - 150;
178 | march = 20 + leapJ - leapG;
179 | if ((jump - N) < 6) {
180 | N = N - jump + (jump + 4) / 33 * 33;
181 | }
182 | leap = ((((N + 1) % 33) - 1) % 4);
183 | if (leap == -1) {
184 | leap = 4;
185 | }
186 | break;
187 | }
188 | leapJ = leapJ + jump / 33 * 8 + (jump % 33) / 4;
189 | jp = jm;
190 | }
191 | return leap == 1;
192 | }
193 |
194 | /**
195 | * Modified toString() method that represents date string
196 | *
197 | * @return Date as String
198 | */
199 | @SuppressLint("DefaultLocale")
200 | @Override
201 | public String toString() {
202 | return String.format("%04d-%02d-%02d", getYear(), getMonth(), getDay());
203 | }
204 |
205 | /**
206 | * Converts Gregorian date to Persian(Jalali) date
207 | *
208 | * @param year int
209 | * @param month int
210 | * @param day int
211 | */
212 | public void gregorianToPersian(int year, int month, int day) {
213 | int jd = JG2JD(year, month, day, 0);
214 | JD2Jal(jd);
215 | this.year = jYear;
216 | this.month = jMonth;
217 | this.day = jDay;
218 | }
219 |
220 | public void gregorianToPersian(Date date) {
221 | Calendar cal = Calendar.getInstance();
222 | cal.setTime(date);
223 | gregorianToPersian(cal.get(Calendar.YEAR),
224 | cal.get(Calendar.MONTH) + 1,
225 | cal.get(Calendar.DAY_OF_MONTH));
226 | }
227 |
228 | /**
229 | * Converts Persian(Jalali) date to Gregorian date
230 | *
231 | * @param year int
232 | * @param month int
233 | * @param day int
234 | */
235 | public void persianToGregorian(int year, int month, int day) {
236 | int jd = Jal2JD(year, month, day);
237 | JD2JG(jd, 0);
238 | this.year = gYear;
239 | this.month = gMonth;
240 | this.day = gDay;
241 | }
242 |
243 | /**
244 | * Get manipulated day
245 | *
246 | * @return Day as int
247 | */
248 | public int getDay() {
249 | return day;
250 | }
251 |
252 | /**
253 | * Get manipulated month
254 | *
255 | * @return Month as int
256 | */
257 | public int getMonth() {
258 | return month;
259 | }
260 |
261 | /**
262 | * Get manipulated year
263 | *
264 | * @return Year as int
265 | */
266 | public int getYear() {
267 | return year;
268 | }
269 | }
--------------------------------------------------------------------------------
/HeroDatePicker/src/main/java/com/hamid97m/herodatepicker/view/ListItemPicker.kt:
--------------------------------------------------------------------------------
1 | package com.hamid97m.herodatepicker.view
2 |
3 | import androidx.compose.animation.core.Animatable
4 | import androidx.compose.animation.core.AnimationResult
5 | import androidx.compose.animation.core.AnimationVector1D
6 | import androidx.compose.animation.core.DecayAnimationSpec
7 | import androidx.compose.animation.core.calculateTargetValue
8 | import androidx.compose.animation.core.exponentialDecay
9 | import androidx.compose.foundation.background
10 | import androidx.compose.foundation.gestures.Orientation
11 | import androidx.compose.foundation.gestures.detectTapGestures
12 | import androidx.compose.foundation.gestures.draggable
13 | import androidx.compose.foundation.gestures.rememberDraggableState
14 | import androidx.compose.foundation.layout.Box
15 | import androidx.compose.foundation.layout.height
16 | import androidx.compose.foundation.layout.offset
17 | import androidx.compose.foundation.layout.padding
18 | import androidx.compose.foundation.layout.width
19 | import androidx.compose.material.LocalTextStyle
20 | import androidx.compose.material.MaterialTheme
21 | import androidx.compose.material.ProvideTextStyle
22 | import androidx.compose.material.Text
23 | import androidx.compose.runtime.Composable
24 | import androidx.compose.runtime.getValue
25 | import androidx.compose.runtime.mutableStateOf
26 | import androidx.compose.runtime.remember
27 | import androidx.compose.runtime.rememberCoroutineScope
28 | import androidx.compose.runtime.setValue
29 | import androidx.compose.ui.Alignment
30 | import androidx.compose.ui.Modifier
31 | import androidx.compose.ui.draw.alpha
32 | import androidx.compose.ui.graphics.Color
33 | import androidx.compose.ui.input.pointer.pointerInput
34 | import androidx.compose.ui.layout.Layout
35 | import androidx.compose.ui.platform.LocalDensity
36 | import androidx.compose.ui.text.TextStyle
37 | import androidx.compose.ui.text.style.TextAlign
38 | import androidx.compose.ui.unit.IntOffset
39 | import androidx.compose.ui.unit.dp
40 | import kotlinx.coroutines.launch
41 | import kotlin.math.abs
42 | import kotlin.math.roundToInt
43 |
44 | private fun getItemIndexForOffset(
45 | range: List,
46 | value: T,
47 | offset: Float,
48 | halfNumbersColumnHeightPx: Float
49 | ): Int {
50 | val indexOf = range.indexOf(value) - (offset / halfNumbersColumnHeightPx).toInt()
51 | return maxOf(0, minOf(indexOf, range.count() - 1))
52 | }
53 |
54 | @Composable
55 | internal fun ListItemPicker(
56 | modifier: Modifier = Modifier,
57 | label: (T) -> String = { it.toString() },
58 | value: T,
59 | onValueChange: (T) -> Unit,
60 | dividersColor: Color = MaterialTheme.colors.primary,
61 | list: List,
62 | textStyle: TextStyle = LocalTextStyle.current,
63 | ) {
64 | val minimumAlpha = 0.3f
65 | val verticalMargin = 8.dp
66 | val numbersColumnHeight = 80.dp
67 | val halfNumbersColumnHeight = numbersColumnHeight / 2
68 | val halfNumbersColumnHeightPx = with(LocalDensity.current) { halfNumbersColumnHeight.toPx() }
69 |
70 | val coroutineScope = rememberCoroutineScope()
71 |
72 | val animatedOffset = remember { Animatable(0f) }
73 | .apply {
74 | val index = list.indexOf(value)
75 | val offsetRange = remember(value, list) {
76 | -((list.count() - 1) - index) * halfNumbersColumnHeightPx to
77 | index * halfNumbersColumnHeightPx
78 | }
79 | updateBounds(offsetRange.first, offsetRange.second)
80 | }
81 |
82 | val coercedAnimatedOffset = animatedOffset.value % halfNumbersColumnHeightPx
83 |
84 | val indexOfElement =
85 | getItemIndexForOffset(list, value, animatedOffset.value, halfNumbersColumnHeightPx)
86 |
87 | var dividersWidth by remember { mutableStateOf(0.dp) }
88 |
89 | Layout(
90 | modifier = modifier
91 | .draggable(
92 | orientation = Orientation.Vertical,
93 | state = rememberDraggableState { deltaY ->
94 | coroutineScope.launch {
95 | animatedOffset.snapTo(animatedOffset.value + deltaY)
96 | }
97 | },
98 | onDragStopped = { velocity ->
99 | coroutineScope.launch {
100 | val endValue = animatedOffset.fling(
101 | initialVelocity = velocity,
102 | animationSpec = exponentialDecay(frictionMultiplier = 20f),
103 | adjustTarget = { target ->
104 | val coercedTarget = target % halfNumbersColumnHeightPx
105 | val coercedAnchors =
106 | listOf(
107 | -halfNumbersColumnHeightPx,
108 | 0f,
109 | halfNumbersColumnHeightPx
110 | )
111 | val coercedPoint =
112 | coercedAnchors.minByOrNull { abs(it - coercedTarget) }!!
113 | val base =
114 | halfNumbersColumnHeightPx * (target / halfNumbersColumnHeightPx).toInt()
115 | coercedPoint + base
116 | }
117 | ).endState.value
118 |
119 | val result = list.elementAt(
120 | getItemIndexForOffset(list, value, endValue, halfNumbersColumnHeightPx)
121 | )
122 | onValueChange(result)
123 | animatedOffset.snapTo(0f)
124 | }
125 | }
126 | )
127 | .padding(vertical = numbersColumnHeight / 3 + verticalMargin * 2),
128 | content = {
129 | Box(
130 | modifier
131 | .width(dividersWidth)
132 | .height(2.dp)
133 | .background(color = dividersColor)
134 | )
135 | Box(
136 | modifier = Modifier
137 | .padding(vertical = verticalMargin, horizontal = 20.dp)
138 | .offset { IntOffset(x = 0, y = coercedAnimatedOffset.roundToInt()) }
139 | ) {
140 | val baseLabelModifier = Modifier.align(Alignment.Center)
141 | ProvideTextStyle(textStyle) {
142 | if (indexOfElement > 0)
143 | Label(
144 | text = label(list.elementAt(indexOfElement - 1)),
145 | modifier = baseLabelModifier
146 | .offset(y = -halfNumbersColumnHeight)
147 | .alpha(
148 | maxOf(
149 | minimumAlpha,
150 | coercedAnimatedOffset / halfNumbersColumnHeightPx
151 | )
152 | )
153 | )
154 | Label(
155 | text = label(list.elementAt(indexOfElement)),
156 | modifier = baseLabelModifier
157 | .alpha(
158 | (maxOf(
159 | minimumAlpha,
160 | 1 - abs(coercedAnimatedOffset) / halfNumbersColumnHeightPx
161 | ))
162 | )
163 | )
164 | if (indexOfElement < list.count() - 1)
165 | Label(
166 | text = label(list.elementAt(indexOfElement + 1)),
167 | modifier = baseLabelModifier
168 | .offset(y = halfNumbersColumnHeight)
169 | .alpha(
170 | maxOf(
171 | minimumAlpha,
172 | -coercedAnimatedOffset / halfNumbersColumnHeightPx
173 | )
174 | )
175 | )
176 | }
177 | }
178 | Box(
179 | modifier
180 | .width(dividersWidth)
181 | .height(2.dp)
182 | .background(color = dividersColor)
183 | )
184 | }
185 | ) { measurables, constraints ->
186 | // Don't constrain child views further, measure them with given constraints
187 | // List of measured children
188 | val placeables = measurables.map { measurable ->
189 | // Measure each children
190 | measurable.measure(constraints)
191 | }
192 |
193 | dividersWidth = placeables
194 | .drop(1)
195 | .first()
196 | .width
197 | .toDp()
198 |
199 | // Set the size of the layout as big as it can
200 | layout(dividersWidth.toPx().toInt(), placeables
201 | .sumOf {
202 | it.height
203 | }
204 | ) {
205 | // Track the y co-ord we have placed children up to
206 | var yPosition = 0
207 |
208 | // Place children in the parent layout
209 | placeables.forEach { placeable ->
210 |
211 | // Position item on the screen
212 | placeable.placeRelative(x = 0, y = yPosition)
213 |
214 | // Record the y co-ord placed up to
215 | yPosition += placeable.height
216 | }
217 | }
218 | }
219 | }
220 |
221 | @Composable
222 | private fun Label(text: String, modifier: Modifier) {
223 | Text(
224 | modifier = modifier.pointerInput(Unit) {
225 | detectTapGestures(onLongPress = {
226 | // FIXME: Empty to disable text selection
227 | })
228 | },
229 | text = text,
230 | textAlign = TextAlign.Center,
231 | )
232 | }
233 |
234 | private suspend fun Animatable.fling(
235 | initialVelocity: Float,
236 | animationSpec: DecayAnimationSpec,
237 | adjustTarget: ((Float) -> Float)?,
238 | block: (Animatable.() -> Unit)? = null,
239 | ): AnimationResult {
240 | val targetValue = animationSpec.calculateTargetValue(value, initialVelocity)
241 | val adjustedTarget = adjustTarget?.invoke(targetValue)
242 | return if (adjustedTarget != null) {
243 | animateTo(
244 | targetValue = adjustedTarget,
245 | initialVelocity = initialVelocity,
246 | block = block
247 | )
248 | } else {
249 | animateDecay(
250 | initialVelocity = initialVelocity,
251 | animationSpec = animationSpec,
252 | block = block,
253 | )
254 | }
255 | }
--------------------------------------------------------------------------------