|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 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 | + * http://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.google.android.fhir.datacapture.views.compose |
| 18 | + |
| 19 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 20 | +import androidx.compose.foundation.interaction.PressInteraction |
| 21 | +import androidx.compose.foundation.text.KeyboardActions |
| 22 | +import androidx.compose.foundation.text.KeyboardOptions |
| 23 | +import androidx.compose.material3.Icon |
| 24 | +import androidx.compose.material3.IconButton |
| 25 | +import androidx.compose.material3.OutlinedTextField |
| 26 | +import androidx.compose.material3.Text |
| 27 | +import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.runtime.LaunchedEffect |
| 29 | +import androidx.compose.runtime.getValue |
| 30 | +import androidx.compose.runtime.mutableStateOf |
| 31 | +import androidx.compose.runtime.remember |
| 32 | +import androidx.compose.runtime.setValue |
| 33 | +import androidx.compose.ui.Modifier |
| 34 | +import androidx.compose.ui.focus.FocusDirection |
| 35 | +import androidx.compose.ui.focus.onFocusChanged |
| 36 | +import androidx.compose.ui.platform.LocalFocusManager |
| 37 | +import androidx.compose.ui.platform.LocalSoftwareKeyboardController |
| 38 | +import androidx.compose.ui.platform.testTag |
| 39 | +import androidx.compose.ui.res.painterResource |
| 40 | +import androidx.compose.ui.res.stringResource |
| 41 | +import androidx.compose.ui.semantics.error |
| 42 | +import androidx.compose.ui.semantics.semantics |
| 43 | +import androidx.compose.ui.text.input.ImeAction |
| 44 | +import androidx.compose.ui.text.input.KeyboardType |
| 45 | +import androidx.compose.ui.tooling.preview.Preview |
| 46 | +import com.google.android.fhir.datacapture.R |
| 47 | +import java.time.LocalTime |
| 48 | + |
| 49 | +@Composable |
| 50 | +internal fun TimePickerItem( |
| 51 | + modifier: Modifier = Modifier, |
| 52 | + selectedTime: String?, |
| 53 | + enabled: Boolean, |
| 54 | + hint: String, |
| 55 | + supportingHelperText: String?, |
| 56 | + isError: Boolean, |
| 57 | + onTimeChanged: (LocalTime) -> Unit, |
| 58 | +) { |
| 59 | + val focusManager = LocalFocusManager.current |
| 60 | + val keyboardController = LocalSoftwareKeyboardController.current |
| 61 | + var selectedTimeText by remember(selectedTime) { mutableStateOf(selectedTime ?: "") } |
| 62 | + var showTimePickerModal by remember { mutableStateOf(false) } |
| 63 | + |
| 64 | + OutlinedTextField( |
| 65 | + value = selectedTimeText, |
| 66 | + onValueChange = {}, |
| 67 | + singleLine = true, |
| 68 | + label = { Text(hint) }, |
| 69 | + modifier = |
| 70 | + modifier |
| 71 | + .testTag(TIME_PICKER_INPUT_FIELD) |
| 72 | + .onFocusChanged { |
| 73 | + if (!it.isFocused) { |
| 74 | + keyboardController?.hide() |
| 75 | + } |
| 76 | + } |
| 77 | + .semantics { |
| 78 | + if (isError && !supportingHelperText.isNullOrBlank()) error(supportingHelperText) |
| 79 | + }, |
| 80 | + supportingText = { supportingHelperText?.let { Text(it) } }, |
| 81 | + isError = isError, |
| 82 | + trailingIcon = { |
| 83 | + IconButton( |
| 84 | + onClick = { |
| 85 | + // todo: show with time picker input |
| 86 | + showTimePickerModal = true |
| 87 | + }, |
| 88 | + enabled = enabled, |
| 89 | + ) { |
| 90 | + Icon( |
| 91 | + painterResource(R.drawable.gm_schedule_24), |
| 92 | + contentDescription = stringResource(R.string.select_time), |
| 93 | + ) |
| 94 | + } |
| 95 | + }, |
| 96 | + readOnly = true, |
| 97 | + enabled = enabled, |
| 98 | + keyboardOptions = |
| 99 | + KeyboardOptions( |
| 100 | + autoCorrectEnabled = false, |
| 101 | + keyboardType = KeyboardType.Number, |
| 102 | + imeAction = ImeAction.Done, |
| 103 | + ), |
| 104 | + keyboardActions = |
| 105 | + KeyboardActions( |
| 106 | + onNext = { focusManager.moveFocus(FocusDirection.Down) }, |
| 107 | + ), |
| 108 | + interactionSource = |
| 109 | + remember { MutableInteractionSource() } |
| 110 | + .also { interactionSource -> |
| 111 | + LaunchedEffect(interactionSource) { |
| 112 | + interactionSource.interactions.collect { |
| 113 | + if (it is PressInteraction.Release) { |
| 114 | + // todo: show with keyboard input |
| 115 | + showTimePickerModal = true |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + }, |
| 120 | + ) |
| 121 | + |
| 122 | + if (showTimePickerModal) { |
| 123 | + TimePickerSwitchable(onDismiss = { showTimePickerModal = false }) { hour, min -> |
| 124 | + val localTime = LocalTime.of(hour, min) |
| 125 | + onTimeChanged(localTime) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +@Composable |
| 131 | +@Preview |
| 132 | +fun PreviewTimePickerItem() { |
| 133 | + TimePickerItem(Modifier, null, true, stringResource(R.string.time), null, false) {} |
| 134 | +} |
| 135 | + |
| 136 | +const val TIME_PICKER_INPUT_FIELD = "time_picker_text_field" |
0 commit comments