@@ -2,25 +2,17 @@ package com.commandiron.wheel_picker_compose
22
33import android.os.Build
44import androidx.annotation.RequiresApi
5- import androidx.compose.foundation.BorderStroke
6- import androidx.compose.foundation.layout.Box
7- import androidx.compose.foundation.layout.Row
8- import androidx.compose.foundation.layout.size
9- import androidx.compose.foundation.shape.RoundedCornerShape
105import androidx.compose.material3.LocalContentColor
116import androidx.compose.material3.MaterialTheme
12- import androidx.compose.material3.Surface
137import androidx.compose.runtime.Composable
14- import androidx.compose.runtime.mutableStateOf
15- import androidx.compose.runtime.remember
16- import androidx.compose.ui.Alignment
178import androidx.compose.ui.Modifier
189import androidx.compose.ui.graphics.Color
19- import androidx.compose.ui.graphics.Shape
2010import androidx.compose.ui.text.TextStyle
2111import androidx.compose.ui.unit.DpSize
2212import androidx.compose.ui.unit.dp
23- import java.text.DateFormatSymbols
13+ import com.commandiron.wheel_picker_compose.core.DefaultWheelDatePicker
14+ import com.commandiron.wheel_picker_compose.core.SelectorProperties
15+ import com.commandiron.wheel_picker_compose.core.WheelPickerDefaults
2416import java.time.LocalDate
2517
2618@RequiresApi(Build .VERSION_CODES .O )
@@ -30,175 +22,26 @@ fun WheelDatePicker(
3022 startDate : LocalDate = LocalDate .now(),
3123 minYear : Int = 1922,
3224 maxYear : Int = 2122,
33- disablePastDate : Boolean = false,
25+ backwardsDisabled : Boolean = false,
3426 size : DpSize = DpSize (256.dp, 128.dp),
3527 textStyle : TextStyle = MaterialTheme .typography.titleMedium,
3628 textColor : Color = LocalContentColor .current,
37- selectorEnabled : Boolean = true,
38- selectorShape : Shape = RoundedCornerShape (16.dp),
39- selectorColor : Color = MaterialTheme .colorScheme.primary.copy(alpha = 0.2f),
40- selectorBorder : BorderStroke ? = BorderStroke (1.dp, MaterialTheme .colorScheme.primary),
41- onScrollFinished : (snappedDate: LocalDate ) -> Unit = {},
29+ selectorProperties : SelectorProperties = WheelPickerDefaults .selectorProperties(),
30+ onSnappedDate : (snappedDate: LocalDate ) -> Unit = {}
4231) {
43- val dayTexts = remember { mutableStateOf((1 .. 31 ).toList().map { it.toString() }) }
44- val selectedDayOfMonth = remember { mutableStateOf(startDate.dayOfMonth)}
45-
46- val monthTexts: List <String > = if (size.width / 3 < 55 .dp){
47- DateFormatSymbols ().shortMonths.toList()
48- }else {
49- DateFormatSymbols ().months.toList()
50- }
51- val selectedMonth = remember { mutableStateOf(startDate.month.value)}
52-
53- val years = IntRange (
54- start = minYear,
55- endInclusive = maxYear,
56- )
57- val yearTexts = years.map { it.toString() }
58- val selectedYear = remember { mutableStateOf(startDate.year)}
59-
60- Box (modifier = modifier, contentAlignment = Alignment .Center ){
61- if (selectorEnabled){
62- Surface (
63- modifier = Modifier
64- .size(size.width, size.height / 3 ),
65- shape = selectorShape,
66- color = selectorColor,
67- border = selectorBorder
68- ) {}
32+ DefaultWheelDatePicker (
33+ modifier,
34+ startDate,
35+ minYear,
36+ maxYear,
37+ backwardsDisabled,
38+ size,
39+ textStyle,
40+ textColor,
41+ selectorProperties,
42+ onSnappedDate = { snappedDate ->
43+ onSnappedDate(snappedDate.snappedLocalDate)
44+ snappedDate.snappedIndex
6945 }
70- Row {
71- WheelTextPicker (
72- size = DpSize (size.width / 3 , size.height),
73- texts = dayTexts.value,
74- textStyle = textStyle,
75- textColor = textColor,
76- selectorEnabled = false ,
77- startIndex = startDate.dayOfMonth - 1 ,
78- onScrollFinished = { selectedIndex ->
79- try {
80-
81- val selectedDate = LocalDate .of(
82- selectedYear.value,
83- selectedMonth.value,
84- selectedIndex + 1
85- )
86- val isDateBefore = isDateBefore(selectedDate, startDate)
87-
88- if (disablePastDate){
89- if (! isDateBefore){
90- selectedDayOfMonth.value = selectedIndex + 1
91- }
92- }else {
93- selectedDayOfMonth.value = selectedIndex + 1
94- }
95-
96- onScrollFinished(
97- LocalDate .of(
98- selectedYear.value,
99- selectedMonth.value,
100- selectedDayOfMonth.value
101- )
102- )
103- }catch (e: Exception ){
104- e.printStackTrace()
105- }
106- return @WheelTextPicker selectedDayOfMonth.value - 1
107- }
108- )
109- WheelTextPicker (
110- size = DpSize (size.width / 3 , size.height),
111- texts = monthTexts,
112- textStyle = textStyle,
113- textColor = textColor,
114- selectorEnabled = false ,
115- startIndex = startDate.month.value - 1 ,
116- onScrollFinished = { selectedIndex ->
117-
118- dayTexts.value = calculateMonthDayTexts(selectedIndex + 1 , selectedYear.value)
119-
120- try {
121- val selectedDate = LocalDate .of(
122- selectedYear.value,
123- selectedIndex + 1 ,
124- selectedDayOfMonth.value
125- )
126-
127- val isDateBefore = isDateBefore(selectedDate, startDate)
128-
129- if (disablePastDate){
130- if (! isDateBefore){
131- selectedMonth.value = selectedIndex + 1
132- }else {
133- dayTexts.value = calculateMonthDayTexts(selectedMonth.value, selectedYear.value)
134- }
135- }else {
136- selectedMonth.value = selectedIndex + 1
137- }
138-
139- onScrollFinished(
140- LocalDate .of(
141- selectedYear.value,
142- selectedMonth.value,
143- selectedDayOfMonth.value
144- )
145- )
146- }catch (e: Exception ){
147- selectedMonth.value = selectedIndex + 1
148- e.printStackTrace()
149- }
150- return @WheelTextPicker selectedMonth.value - 1
151- }
152- )
153- WheelTextPicker (
154- size = DpSize (size.width / 3 , size.height),
155- texts = yearTexts,
156- textStyle = textStyle,
157- textColor = textColor,
158- selectorEnabled = false ,
159- startIndex = if (years.indexOf(years.find { it == startDate.year }) == - 1 ) {
160- throw IllegalArgumentException (
161- " startDate.year should greater than minYear and smaller than maxYear"
162- )
163- } else years.indexOf(years.find { it == startDate.year }),
164- onScrollFinished = { selectedIndex ->
165- dayTexts.value = calculateMonthDayTexts(selectedMonth.value, yearTexts[selectedIndex].toInt())
166- try {
167- val selectedDate = LocalDate .of(
168- yearTexts[selectedIndex].toInt(),
169- selectedMonth.value,
170- selectedDayOfMonth.value
171- )
172-
173- val isDateBefore = isDateBefore(selectedDate, startDate)
174-
175- if (disablePastDate){
176- if (! isDateBefore){
177- selectedYear.value = yearTexts[selectedIndex].toInt()
178- }
179- }else {
180- selectedYear.value = yearTexts[selectedIndex].toInt()
181- }
182-
183- onScrollFinished(
184- LocalDate .of(
185- selectedYear.value,
186- selectedMonth.value,
187- selectedDayOfMonth.value
188- )
189- )
190- }catch (e: Exception ){
191- selectedYear.value = yearTexts[selectedIndex].toInt()
192- e.printStackTrace()
193- }
194- return @WheelTextPicker yearTexts.indexOf(selectedYear.value.toString())
195- }
196- )
197- }
198- }
199- }
200-
201- @RequiresApi(Build .VERSION_CODES .O )
202- private fun isDateBefore (date : LocalDate , currentDate : LocalDate ): Boolean {
203- return date.isBefore(currentDate)
46+ )
20447}
0 commit comments