Skip to content

Commit

Permalink
Make the 12h TimerPicker more responsive in larger screens. (#2096)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: ssancho14 <ssancho14@users.noreply.github.com>
  • Loading branch information
ssancho14 and ssancho14 authored Mar 6, 2024
1 parent dfd2e9d commit cc395bc
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
Expand Down Expand Up @@ -169,7 +168,6 @@ public fun TimePicker(
(0..9).maxOf { mm.getBoundingBox(it).width }
}
val pickerWidth = with(LocalDensity.current) { (digitWidth * 2).toDp() + 6.dp }
val usableWidthRatio = 1 - 2 * 0.0728f

val optionColor = MaterialTheme.colors.secondary
val pickerOption = pickerTextOption(textStyle, { "%02d".format(it) })
Expand Down Expand Up @@ -238,7 +236,7 @@ public fun TimePicker(
Spacer(Modifier.height(4.dp))
Row(
// Horizontal padding is 2.08% + 5.2%
modifier = Modifier.fillMaxWidth(usableWidthRatio).weight(1f),
modifier = Modifier.fillMaxWidth(1 - 2 * 0.0728f).weight(1f),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
) {
Expand Down Expand Up @@ -385,8 +383,17 @@ public fun TimePickerWith12HourClock(
rememberPickerGroupState(FocusableElement12Hour.HOURS.index)
}

val isLargeScreen = LocalConfiguration.current.screenWidthDp > 225
val textStyle =
with(LocalDensity.current) { fontScaleIndependent(MaterialTheme.typography.display3) }
with(LocalDensity.current) {
fontScaleIndependent(
if (isLargeScreen) {
MaterialTheme.typography.display2
} else {
MaterialTheme.typography.display3
},
)
}

val focusRequesterConfirmButton = remember { FocusRequester() }

Expand Down Expand Up @@ -425,6 +432,24 @@ public fun TimePickerWith12HourClock(
}
}

val measurer = rememberTextMeasurer()
val density = LocalDensity.current
val (digitWidth, amPmWidth) = remember(
density.density,
LocalConfiguration.current.screenWidthDp,
) {
val mm = measurer.measure(
"0123456789\n$amString\n$pmString",
style = textStyle,
density = density,
)

(0..9).maxOf { mm.getBoundingBox(it).width } to
(1..2).maxOf { mm.getLineRight(it) - mm.getLineLeft(it) }
}
val pickerWidth = with(LocalDensity.current) { (digitWidth * 2).toDp() + 6.dp }
val pickerWidth2 = with(LocalDensity.current) { amPmWidth.toDp() + 6.dp }

ScreenScaffold(
modifier = modifier
.fillMaxSize()
Expand All @@ -436,7 +461,7 @@ public fun TimePickerWith12HourClock(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(Modifier.height(12.dp))
Spacer(Modifier.height(14.dp))
Text(
text = when (FocusableElement12Hour[pickerGroupState.selectedIndex]) {
FocusableElement12Hour.HOURS -> hourString
Expand All @@ -447,14 +472,9 @@ public fun TimePickerWith12HourClock(
style = MaterialTheme.typography.button,
maxLines = 1,
)
val weightsToCenterVertically = 0.5f
Spacer(
Modifier
.fillMaxWidth()
.weight(weightsToCenterVertically),
)
Spacer(Modifier.height(4.dp))
Row(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.fillMaxWidth(1 - 2 * 0.0728f).weight(1f),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
) {
Expand All @@ -470,15 +490,10 @@ public fun TimePickerWith12HourClock(
}
}

// TODO remove the offsets below, once checked with UX
// these are to refactor the code, remove spacers without changing
// visible layout at the same time.
PickerGroup(
pickerGroupItemWithRSB(
pickerState = hourState,
modifier = Modifier
.size(57.dp, 100.dp)
.offset(x = (2.5).dp),
modifier = Modifier.width(pickerWidth).fillMaxHeight(),
onSelected = {
doubleTapToNext(
FocusableElement12Hour.HOURS,
Expand All @@ -490,9 +505,7 @@ public fun TimePickerWith12HourClock(
),
pickerGroupItemWithRSB(
pickerState = minuteState,
modifier = Modifier
.size(53.dp, 100.dp)
.offset(x = (-0.5).dp),
modifier = Modifier.width(pickerWidth).fillMaxHeight(),
onSelected = {
doubleTapToNext(
FocusableElement12Hour.MINUTES,
Expand All @@ -504,9 +517,7 @@ public fun TimePickerWith12HourClock(
),
pickerGroupItemWithRSB(
pickerState = periodState,
modifier = Modifier
.size(68.dp, 100.dp)
.offset(x = (3).dp),
modifier = Modifier.width(pickerWidth2).fillMaxHeight(),
contentDescription = periodContentDescription,
onSelected = {
doubleTapToNext(
Expand All @@ -518,21 +529,17 @@ public fun TimePickerWith12HourClock(
if (it == 0) amString else pmString
}),
),
modifier = Modifier.fillMaxSize(),
autoCenter = false,
expandToFillWidth = true,
pickerGroupState = pickerGroupState,
separator = {
if (it == 0) {
Separator(textStyle)
}
if (it == 0) Separator(textStyle)
},
touchExplorationStateProvider = touchExplorationStateProvider,
)
}
Spacer(
Modifier
.fillMaxWidth()
.weight(weightsToCenterVertically),
)
Spacer(Modifier.height(4.dp))
Button(
onClick = {
val confirmedTime = LocalTime.of(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cc395bc

Please sign in to comment.