1
1
package com.spendesk.grapes.compose.calendar
2
2
3
+ import android.text.format.DateFormat
3
4
import androidx.compose.material3.ExperimentalMaterial3Api
4
5
import androidx.compose.material3.TimeInput
5
6
import androidx.compose.material3.TimePickerDefaults
7
+ import androidx.compose.material3.TimePickerState
6
8
import androidx.compose.material3.rememberTimePickerState
7
9
import androidx.compose.runtime.Composable
8
10
import androidx.compose.runtime.LaunchedEffect
9
- import androidx.compose.runtime.getValue
10
- import androidx.compose.runtime.mutableStateOf
11
- import androidx.compose.runtime.remember
12
- import androidx.compose.runtime.setValue
13
11
import androidx.compose.ui.Modifier
12
+ import androidx.compose.ui.platform.LocalContext
14
13
import androidx.compose.ui.tooling.preview.Preview
15
14
import com.spendesk.grapes.compose.theme.GrapesTheme
16
- import java.util.Calendar
17
- import java.util.Date
18
-
19
- /* *
20
- * @author : dany
21
- * @since : 07/03/2023, Tue
22
- **/
15
+ import java.time.LocalTime
23
16
24
17
/* *
25
18
* Grapes time picker which lets the user selects a specific time by providing the hour, minutes and the am/pm format.
26
19
*
27
20
* @param initialHour Initial hour to be displayed in the picker
28
21
* @param initialMinute Initial minute to be displayed in the picker
29
22
* @param modifier The [Modifier] to be applied to this time picker
30
- * @param onTimeSelected Callback when an hour or minute is changed in the picker
23
+ * @param onTimeChange Callback when an hour or minute is changed in the picker
24
+ * @param is24HourFormat Whether the picker should be in 24-hour format or not. Default to the device setting
31
25
*/
32
- @OptIn(ExperimentalMaterial3Api ::class )
33
26
@Composable
27
+ @OptIn(ExperimentalMaterial3Api ::class )
34
28
fun GrapesTimePicker (
35
29
initialHour : Int ,
36
30
initialMinute : Int ,
31
+ onTimeChange : (LocalTime ) -> Unit ,
37
32
modifier : Modifier = Modifier ,
38
- onTimeSelected : (( Int , Int ) -> Unit ) ? = null
33
+ is24HourFormat : Boolean = DateFormat .is24HourFormat( LocalContext .current),
39
34
) {
40
- val timerPickerState = rememberTimePickerState(
35
+ val timerPickerState: TimePickerState = rememberTimePickerState(
41
36
initialHour = initialHour,
42
37
initialMinute = initialMinute,
43
- is24Hour = false
38
+ is24Hour = is24HourFormat,
44
39
)
45
40
46
41
TimeInput (
@@ -61,27 +56,38 @@ fun GrapesTimePicker(
61
56
)
62
57
63
58
LaunchedEffect (timerPickerState.hour, timerPickerState.minute) {
64
- if (timerPickerState.hour != initialHour || timerPickerState.minute != initialMinute) {
65
- onTimeSelected?.invoke(timerPickerState.hour, timerPickerState.minute)
66
- }
59
+ onTimeChange(LocalTime .of(timerPickerState.hour, timerPickerState.minute))
67
60
}
68
61
}
69
62
63
+ @Composable
70
64
@Preview(showBackground = true )
65
+ @Suppress(" MagicNumber" )
66
+ private fun PreviewGrapesTimePicker24Hour () {
67
+ val now = LocalTime .of(16 , 44 , 0 )
68
+
69
+ GrapesTheme {
70
+ GrapesTimePicker (
71
+ initialHour = now.hour,
72
+ initialMinute = now.minute,
73
+ is24HourFormat = true ,
74
+ onTimeChange = {},
75
+ )
76
+ }
77
+ }
78
+
71
79
@Composable
72
- private fun GrapesTimePickerPreview () {
73
- var itemHour by remember { mutableStateOf(Calendar .getInstance().apply { time = Date () }.get(Calendar .HOUR_OF_DAY )) }
74
- var itemMinutes by remember { mutableStateOf(Calendar .getInstance().apply { time = Date () }.get(Calendar .MINUTE )) }
80
+ @Preview(showBackground = true )
81
+ @Suppress(" MagicNumber" )
82
+ private fun PreviewGrapesTimePicker12Hour () {
83
+ val now = LocalTime .of(16 , 44 , 0 )
75
84
76
85
GrapesTheme {
77
86
GrapesTimePicker (
78
- initialHour = itemHour,
79
- initialMinute = itemMinutes,
80
- onTimeSelected = { hour, minute ->
81
- itemHour = hour
82
- itemMinutes = minute
83
- println (" Selected hour: $hour and minute: $minute " )
84
- }
87
+ initialHour = now.hour,
88
+ initialMinute = now.minute,
89
+ is24HourFormat = false ,
90
+ onTimeChange = {},
85
91
)
86
92
}
87
93
}
0 commit comments