Skip to content

Commit 0280de8

Browse files
authored
Adding advanced examples (#289)
* Adding advanced examples * Apply Spotless * Adding advanced examples * Rename AdvancedTimePickerExample --------- Co-authored-by: jakeroseman <[email protected]>
1 parent 5a16f7c commit 0280de8

File tree

1 file changed

+134
-2
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/components

1 file changed

+134
-2
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/components/TimePickers.kt

+134-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@
1616

1717
package com.example.compose.snippets.components
1818

19+
import androidx.compose.foundation.background
1920
import androidx.compose.foundation.layout.Arrangement
2021
import androidx.compose.foundation.layout.Column
22+
import androidx.compose.foundation.layout.IntrinsicSize
23+
import androidx.compose.foundation.layout.Row
24+
import androidx.compose.foundation.layout.Spacer
2125
import androidx.compose.foundation.layout.fillMaxSize
26+
import androidx.compose.foundation.layout.fillMaxWidth
27+
import androidx.compose.foundation.layout.height
2228
import androidx.compose.foundation.layout.padding
29+
import androidx.compose.foundation.layout.width
30+
import androidx.compose.material.icons.Icons
31+
import androidx.compose.material.icons.filled.AccessTime
32+
import androidx.compose.material.icons.filled.EditCalendar
2333
import androidx.compose.material3.AlertDialog
2434
import androidx.compose.material3.Button
2535
import androidx.compose.material3.ExperimentalMaterial3Api
36+
import androidx.compose.material3.Icon
37+
import androidx.compose.material3.IconButton
38+
import androidx.compose.material3.MaterialTheme
39+
import androidx.compose.material3.Surface
2640
import androidx.compose.material3.Text
2741
import androidx.compose.material3.TextButton
2842
import androidx.compose.material3.TimeInput
@@ -37,6 +51,8 @@ import androidx.compose.runtime.setValue
3751
import androidx.compose.ui.Alignment
3852
import androidx.compose.ui.Modifier
3953
import androidx.compose.ui.unit.dp
54+
import androidx.compose.ui.window.Dialog
55+
import androidx.compose.ui.window.DialogProperties
4056
import java.text.SimpleDateFormat
4157
import java.util.Calendar
4258
import java.util.Locale
@@ -46,6 +62,7 @@ import java.util.Locale
4662
fun TimePickerExamples() {
4763
var showDialExample by remember { mutableStateOf(false) }
4864
var showInputExample by remember { mutableStateOf(false) }
65+
var showAdvancedExample by remember { mutableStateOf(false) }
4966

5067
var selectedTime: TimePickerState? by remember { mutableStateOf(null) }
5168

@@ -60,16 +77,19 @@ fun TimePickerExamples() {
6077
) {
6178
Button(onClick = {
6279
showDialExample = true
63-
showInputExample = false
6480
}) {
6581
Text("Dial time picker")
6682
}
6783
Button(onClick = {
68-
showDialExample = false
6984
showInputExample = true
7085
}) {
7186
Text("Input time picker")
7287
}
88+
Button(onClick = {
89+
showAdvancedExample = true
90+
}) {
91+
Text("Time picker with custom dialog")
92+
}
7393
if (selectedTime != null) {
7494
val cal = Calendar.getInstance()
7595
cal.set(Calendar.HOUR_OF_DAY, selectedTime!!.hour)
@@ -98,6 +118,14 @@ fun TimePickerExamples() {
98118
showInputExample = false
99119
},
100120
)
121+
showAdvancedExample -> AdvancedTimePickerExample(
122+
onDismiss = { showAdvancedExample = false },
123+
onConfirm = {
124+
time ->
125+
selectedTime = time
126+
showAdvancedExample = false
127+
},
128+
)
101129
}
102130
}
103131

@@ -153,6 +181,57 @@ fun InputExample(
153181
}
154182
// [END android_compose_components_input]
155183

184+
@OptIn(ExperimentalMaterial3Api::class)
185+
// [START android_compose_components_advanced]
186+
@Composable
187+
fun AdvancedTimePickerExample(
188+
onConfirm: (TimePickerState) -> Unit,
189+
onDismiss: () -> Unit,
190+
) {
191+
192+
val currentTime = Calendar.getInstance()
193+
194+
val timePickerState = rememberTimePickerState(
195+
initialHour = currentTime.get(Calendar.HOUR_OF_DAY),
196+
initialMinute = currentTime.get(Calendar.MINUTE),
197+
is24Hour = true,
198+
)
199+
200+
/** Determines whether the time picker is dial or input */
201+
var showDial by remember { mutableStateOf(true) }
202+
203+
/** The icon used for the icon button that switches from dial to input */
204+
val toggleIcon = if (showDial) {
205+
Icons.Filled.EditCalendar
206+
} else {
207+
Icons.Filled.AccessTime
208+
}
209+
210+
AdvancedTimePickerDialog(
211+
onDismiss = { onDismiss() },
212+
onConfirm = { onConfirm(timePickerState) },
213+
toggle = {
214+
IconButton(onClick = { showDial = !showDial }) {
215+
Icon(
216+
imageVector = toggleIcon,
217+
contentDescription = "Time picker type toggle",
218+
)
219+
}
220+
},
221+
) {
222+
if (showDial) {
223+
TimePicker(
224+
state = timePickerState,
225+
)
226+
} else {
227+
TimeInput(
228+
state = timePickerState,
229+
)
230+
}
231+
}
232+
}
233+
// [END android_compose_components_advanced]
234+
156235
// [START android_compose_components_timepickerdialog]
157236
@Composable
158237
fun TimePickerDialog(
@@ -176,3 +255,56 @@ fun TimePickerDialog(
176255
)
177256
}
178257
// [END android_compose_components_timepickerdialog]
258+
259+
// [START android_compose_components_advanceddialog]
260+
@Composable
261+
fun AdvancedTimePickerDialog(
262+
title: String = "Select Time",
263+
onDismiss: () -> Unit,
264+
onConfirm: () -> Unit,
265+
toggle: @Composable () -> Unit = {},
266+
content: @Composable () -> Unit,
267+
) {
268+
Dialog(
269+
onDismissRequest = onDismiss,
270+
properties = DialogProperties(usePlatformDefaultWidth = false),
271+
) {
272+
Surface(
273+
shape = MaterialTheme.shapes.extraLarge,
274+
tonalElevation = 6.dp,
275+
modifier =
276+
Modifier
277+
.width(IntrinsicSize.Min)
278+
.height(IntrinsicSize.Min)
279+
.background(
280+
shape = MaterialTheme.shapes.extraLarge,
281+
color = MaterialTheme.colorScheme.surface
282+
),
283+
) {
284+
Column(
285+
modifier = Modifier.padding(24.dp),
286+
horizontalAlignment = Alignment.CenterHorizontally
287+
) {
288+
Text(
289+
modifier = Modifier
290+
.fillMaxWidth()
291+
.padding(bottom = 20.dp),
292+
text = title,
293+
style = MaterialTheme.typography.labelMedium
294+
)
295+
content()
296+
Row(
297+
modifier = Modifier
298+
.height(40.dp)
299+
.fillMaxWidth()
300+
) {
301+
toggle()
302+
Spacer(modifier = Modifier.weight(1f))
303+
TextButton(onClick = onDismiss) { Text("Cancel") }
304+
TextButton(onClick = onConfirm) { Text("OK") }
305+
}
306+
}
307+
}
308+
}
309+
}
310+
// [END android_compose_components_advanceddialog]

0 commit comments

Comments
 (0)