Adaptive time selector.

Date selector will be fixed by Google (hopefully). Users can switch to text input for convenience.

closes #64
This commit is contained in:
sadellie 2023-07-21 22:55:38 +03:00
parent cce8799ce5
commit ac4c6665fa
2 changed files with 18 additions and 7 deletions

View File

@ -36,6 +36,7 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.material3.TimePicker import androidx.compose.material3.TimePicker
import androidx.compose.material3.TimePickerLayoutType
import androidx.compose.material3.rememberDatePickerState import androidx.compose.material3.rememberDatePickerState
import androidx.compose.material3.rememberTimePickerState import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -63,6 +64,7 @@ fun TimePickerDialog(
dismissLabel: String = stringResource(R.string.cancel_label), dismissLabel: String = stringResource(R.string.cancel_label),
onDismiss: () -> Unit = {}, onDismiss: () -> Unit = {},
onConfirm: (LocalDateTime) -> Unit, onConfirm: (LocalDateTime) -> Unit,
vertical: Boolean
) { ) {
val pickerState = rememberTimePickerState( val pickerState = rememberTimePickerState(
localDateTime.hour, localDateTime.hour,
@ -73,7 +75,7 @@ fun TimePickerDialog(
AlertDialog( AlertDialog(
onDismissRequest = onDismiss, onDismissRequest = onDismiss,
modifier = modifier.wrapContentHeight(), modifier = modifier.wrapContentHeight(),
properties = DialogProperties() properties = DialogProperties(usePlatformDefaultWidth = vertical)
) { ) {
Surface( Surface(
modifier = modifier, modifier = modifier,
@ -94,7 +96,8 @@ fun TimePickerDialog(
TimePicker( TimePicker(
state = pickerState, state = pickerState,
modifier = Modifier.padding(top = 20.dp) modifier = Modifier.padding(top = 20.dp),
layoutType = if (vertical) TimePickerLayoutType.Vertical else TimePickerLayoutType.Horizontal
) )
Row( Row(
@ -153,6 +156,7 @@ fun DatePickerDialog(
Box(modifier = Modifier Box(modifier = Modifier
.align(Alignment.End) .align(Alignment.End)
.padding(DialogButtonsPadding)) { .padding(DialogButtonsPadding)) {
AlertDialogFlowRow( AlertDialogFlowRow(
mainAxisSpacing = DialogButtonsMainAxisSpacing, mainAxisSpacing = DialogButtonsMainAxisSpacing,
crossAxisSpacing = DialogButtonsCrossAxisSpacing crossAxisSpacing = DialogButtonsCrossAxisSpacing

View File

@ -18,6 +18,7 @@
package com.sadellie.unitto.feature.datedifference package com.sadellie.unitto.feature.datedifference
import android.content.res.Configuration
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically import androidx.compose.animation.shrinkVertically
@ -33,6 +34,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -72,9 +74,10 @@ internal fun DateDifferenceScreen(
navigateToSettings: () -> Unit, navigateToSettings: () -> Unit,
updateStart: (LocalDateTime) -> Unit, updateStart: (LocalDateTime) -> Unit,
updateEnd: (LocalDateTime) -> Unit, updateEnd: (LocalDateTime) -> Unit,
uiState: UIState uiState: UIState,
) { ) {
var dialogState by remember { mutableStateOf(DialogState.NONE) } var dialogState by remember { mutableStateOf(DialogState.NONE) }
val isVertical = LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT
UnittoScreenWithTopBar( UnittoScreenWithTopBar(
title = { Text(stringResource(R.string.date_difference)) }, title = { Text(stringResource(R.string.date_difference)) },
@ -141,7 +144,8 @@ internal fun DateDifferenceScreen(
updateStart(it) updateStart(it)
dialogState = DialogState.FROM_DATE dialogState = DialogState.FROM_DATE
}, },
confirmLabel = stringResource(R.string.next_label) confirmLabel = stringResource(R.string.next_label),
vertical = isVertical,
) )
} }
@ -152,7 +156,8 @@ internal fun DateDifferenceScreen(
onConfirm = { onConfirm = {
updateStart(it) updateStart(it)
resetDialog() resetDialog()
} },
vertical = isVertical,
) )
} }
@ -175,7 +180,8 @@ internal fun DateDifferenceScreen(
updateEnd(it) updateEnd(it)
dialogState = DialogState.TO_DATE dialogState = DialogState.TO_DATE
}, },
confirmLabel = stringResource(R.string.next_label) confirmLabel = stringResource(R.string.next_label),
vertical = isVertical,
) )
} }
@ -186,7 +192,8 @@ internal fun DateDifferenceScreen(
onConfirm = { onConfirm = {
updateEnd(it) updateEnd(it)
resetDialog() resetDialog()
} },
vertical = isVertical,
) )
} }