|
| 1 | +/* |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.compose.snippets.components |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.Arrangement |
| 20 | +import androidx.compose.foundation.layout.Column |
| 21 | +import androidx.compose.foundation.layout.fillMaxSize |
| 22 | +import androidx.compose.foundation.layout.padding |
| 23 | +import androidx.compose.material3.AlertDialog |
| 24 | +import androidx.compose.material3.Button |
| 25 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 26 | +import androidx.compose.material3.Text |
| 27 | +import androidx.compose.material3.TextButton |
| 28 | +import androidx.compose.material3.TimeInput |
| 29 | +import androidx.compose.material3.TimePicker |
| 30 | +import androidx.compose.material3.TimePickerState |
| 31 | +import androidx.compose.material3.rememberTimePickerState |
| 32 | +import androidx.compose.runtime.Composable |
| 33 | +import androidx.compose.runtime.getValue |
| 34 | +import androidx.compose.runtime.mutableStateOf |
| 35 | +import androidx.compose.runtime.remember |
| 36 | +import androidx.compose.runtime.setValue |
| 37 | +import androidx.compose.ui.Alignment |
| 38 | +import androidx.compose.ui.Modifier |
| 39 | +import androidx.compose.ui.unit.dp |
| 40 | +import java.text.SimpleDateFormat |
| 41 | +import java.util.Calendar |
| 42 | +import java.util.Locale |
| 43 | + |
| 44 | +@OptIn(ExperimentalMaterial3Api::class) |
| 45 | +@Composable |
| 46 | +fun TimePickerExamples() { |
| 47 | + var showDialExample by remember { mutableStateOf(false) } |
| 48 | + var showInputExample by remember { mutableStateOf(false) } |
| 49 | + |
| 50 | + var selectedTime: TimePickerState? by remember { mutableStateOf(null) } |
| 51 | + |
| 52 | + val formatter = remember { SimpleDateFormat("hh:mm a", Locale.getDefault()) } |
| 53 | + |
| 54 | + Column( |
| 55 | + modifier = Modifier |
| 56 | + .fillMaxSize() |
| 57 | + .padding(32.dp), |
| 58 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 59 | + verticalArrangement = Arrangement.spacedBy(24.dp), |
| 60 | + ) { |
| 61 | + Button(onClick = { |
| 62 | + showDialExample = true |
| 63 | + showInputExample = false |
| 64 | + }) { |
| 65 | + Text("Dial time picker") |
| 66 | + } |
| 67 | + Button(onClick = { |
| 68 | + showDialExample = false |
| 69 | + showInputExample = true |
| 70 | + }) { |
| 71 | + Text("Input time picker") |
| 72 | + } |
| 73 | + if (selectedTime != null) { |
| 74 | + val cal = Calendar.getInstance() |
| 75 | + cal.set(Calendar.HOUR_OF_DAY, selectedTime!!.hour) |
| 76 | + cal.set(Calendar.MINUTE, selectedTime!!.minute) |
| 77 | + cal.isLenient = false |
| 78 | + Text("Selected time = ${formatter.format(cal.time)}") |
| 79 | + } else { |
| 80 | + Text("No time selected.") |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + when { |
| 85 | + showDialExample -> DialExample( |
| 86 | + onDismiss = { showDialExample = false }, |
| 87 | + onConfirm = { |
| 88 | + time -> |
| 89 | + selectedTime = time |
| 90 | + showDialExample = false |
| 91 | + }, |
| 92 | + ) |
| 93 | + showInputExample -> InputExample( |
| 94 | + onDismiss = { showInputExample = false }, |
| 95 | + onConfirm = { |
| 96 | + time -> |
| 97 | + selectedTime = time |
| 98 | + showInputExample = false |
| 99 | + }, |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +@OptIn(ExperimentalMaterial3Api::class) |
| 105 | +// [START android_compose_components_dial] |
| 106 | +@Composable |
| 107 | +fun DialExample( |
| 108 | + onConfirm: (TimePickerState) -> Unit, |
| 109 | + onDismiss: () -> Unit, |
| 110 | +) { |
| 111 | + val currentTime = Calendar.getInstance() |
| 112 | + |
| 113 | + val timePickerState = rememberTimePickerState( |
| 114 | + initialHour = currentTime.get(Calendar.HOUR_OF_DAY), |
| 115 | + initialMinute = currentTime.get(Calendar.MINUTE), |
| 116 | + is24Hour = true, |
| 117 | + ) |
| 118 | + |
| 119 | + TimePickerDialog( |
| 120 | + onDismiss = { onDismiss() }, |
| 121 | + onConfirm = { onConfirm(timePickerState) } |
| 122 | + ) { |
| 123 | + TimePicker( |
| 124 | + state = timePickerState, |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
| 128 | +// [END android_compose_components_dial] |
| 129 | + |
| 130 | +@OptIn(ExperimentalMaterial3Api::class) |
| 131 | +// [START android_compose_components_input] |
| 132 | +@Composable |
| 133 | +fun InputExample( |
| 134 | + onConfirm: (TimePickerState) -> Unit, |
| 135 | + onDismiss: () -> Unit, |
| 136 | +) { |
| 137 | + val currentTime = Calendar.getInstance() |
| 138 | + |
| 139 | + val timePickerState = rememberTimePickerState( |
| 140 | + initialHour = currentTime.get(Calendar.HOUR_OF_DAY), |
| 141 | + initialMinute = currentTime.get(Calendar.MINUTE), |
| 142 | + is24Hour = true, |
| 143 | + ) |
| 144 | + |
| 145 | + TimePickerDialog( |
| 146 | + onDismiss = { onDismiss() }, |
| 147 | + onConfirm = { onConfirm(timePickerState) } |
| 148 | + ) { |
| 149 | + TimeInput( |
| 150 | + state = timePickerState, |
| 151 | + ) |
| 152 | + } |
| 153 | +} |
| 154 | +// [END android_compose_components_input] |
| 155 | + |
| 156 | +// [START android_compose_components_timepickerdialog] |
| 157 | +@Composable |
| 158 | +fun TimePickerDialog( |
| 159 | + onDismiss: () -> Unit, |
| 160 | + onConfirm: () -> Unit, |
| 161 | + content: @Composable () -> Unit |
| 162 | +) { |
| 163 | + AlertDialog( |
| 164 | + onDismissRequest = onDismiss, |
| 165 | + dismissButton = { |
| 166 | + TextButton(onClick = { onDismiss() }) { |
| 167 | + Text("Dismiss") |
| 168 | + } |
| 169 | + }, |
| 170 | + confirmButton = { |
| 171 | + TextButton(onClick = { onConfirm() }) { |
| 172 | + Text("OK") |
| 173 | + } |
| 174 | + }, |
| 175 | + text = { content() } |
| 176 | + ) |
| 177 | +} |
| 178 | +// [END android_compose_components_timepickerdialog] |
0 commit comments