Refactor watch face selector

This commit is contained in:
MM20 2024-04-01 16:45:05 +02:00
parent b89c2810c6
commit 0fe5dd9029
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -15,8 +15,10 @@ import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowDropDown import androidx.compose.material.icons.rounded.ArrowDropDown
import androidx.compose.material.icons.rounded.Check
import androidx.compose.material.icons.rounded.ChevronLeft import androidx.compose.material.icons.rounded.ChevronLeft
import androidx.compose.material.icons.rounded.ChevronRight import androidx.compose.material.icons.rounded.ChevronRight
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material.icons.rounded.Style import androidx.compose.material.icons.rounded.Style
import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenu
@ -40,13 +42,13 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import de.mm20.launcher2.preferences.ClockWidgetColors import de.mm20.launcher2.preferences.ClockWidgetColors
import de.mm20.launcher2.preferences.ClockWidgetStyle import de.mm20.launcher2.preferences.ClockWidgetStyle
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.base.ProvideClockTime
import de.mm20.launcher2.ui.locals.LocalDarkTheme import de.mm20.launcher2.ui.locals.LocalDarkTheme
import de.mm20.launcher2.ui.locals.LocalPreferDarkContentOverWallpaper import de.mm20.launcher2.ui.locals.LocalPreferDarkContentOverWallpaper
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -74,62 +76,60 @@ fun WatchFaceSelector(
modifier = Modifier, modifier = Modifier,
) { ) {
val styles = remember { val styles = remember {
mapOf( listOf(
ClockStyle.DigitalClock1 to 0, ClockWidgetStyle.Digital1(),
ClockStyle.DigitalClock1_Outlined to 0, ClockWidgetStyle.Digital2,
ClockStyle.DigitalClock2 to 1, ClockWidgetStyle.Analog,
ClockStyle.AnalogClock to 2, ClockWidgetStyle.Orbit,
ClockStyle.OrbitClock to 3, ClockWidgetStyle.Segment,
ClockStyle.BinaryClock to 4, ClockWidgetStyle.Binary,
ClockStyle.SegmentClock to 5, ClockWidgetStyle.Empty,
ClockStyle.EmptyClock to 6,
) )
} }
val pagerState = rememberPagerState( val pagerState = rememberPagerState(
initialPage = styles.getOrDefault(selected, 0) initialPage = styles.indexOfFirst { it.javaClass == selected?.javaClass }.coerceAtLeast(0),
) { ) {
styles.values.max() + 1 styles.size
} }
LaunchedEffect(pagerState.currentPage) { LaunchedEffect(pagerState.currentPage) {
if (styles.getOrDefault(selected, 0) == pagerState.currentPage) { val newStyle = styles[pagerState.currentPage]
return@LaunchedEffect if (newStyle.javaClass == selected?.javaClass) return@LaunchedEffect
} onSelect(newStyle)
onSelect(styles.entries.first { it.value == pagerState.currentPage }.key)
} }
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
Box { Box {
androidx.compose.animation.AnimatedVisibility( androidx.compose.animation.AnimatedVisibility(
styles.entries.count { it.value == pagerState.currentPage } > 1, selected is ClockWidgetStyle.Digital1,
modifier = Modifier modifier = Modifier
.align(Alignment.TopEnd) .align(Alignment.TopEnd)
.zIndex(1f), .zIndex(1f),
enter = scaleIn(), enter = scaleIn(),
exit = scaleOut(), exit = scaleOut(),
) { ) {
var showVariantDropdown by remember { mutableStateOf(false) } var showStyleSettings by remember { mutableStateOf(false) }
IconButton( IconButton(
onClick = { showVariantDropdown = true }, onClick = { showStyleSettings = true },
modifier = Modifier modifier = Modifier
.padding(4.dp) .padding(4.dp)
) { ) {
Icon(Icons.Rounded.Style, null) Icon(Icons.Rounded.Settings, null)
} }
DropdownMenu( DropdownMenu(
expanded = showVariantDropdown, expanded = showStyleSettings,
onDismissRequest = { showVariantDropdown = false }) { onDismissRequest = { showStyleSettings = false }) {
for (variant in styles.filter { it.value == pagerState.currentPage }) { if (selected is ClockWidgetStyle.Digital1) {
DropdownMenuItem( DropdownMenuItem(
onClick = { text = { Text(stringResource(R.string.clock_variant_outlined)) },
onSelect(variant.key) leadingIcon = {
showVariantDropdown = false if (selected.outlined) {
Icon(Icons.Rounded.Check, null)
}
}, },
text = { onClick = {
Text( onSelect(selected.copy(outlined = !selected.outlined))
text = getVariantName(context, variant.key),
)
} }
) )
} }
@ -155,13 +155,11 @@ fun WatchFaceSelector(
.padding(top = 24.dp, bottom = 8.dp), .padding(top = 24.dp, bottom = 8.dp),
contentAlignment = Alignment.TopCenter, contentAlignment = Alignment.TopCenter,
) { ) {
val currentPageStyles = remember { val currentPageStyle = styles[pageIndex]
styles.filter { it.value == pageIndex } if (currentPageStyle.javaClass == selected?.javaClass) {
}
if (currentPageStyles.containsKey(selected)) {
Clock(selected, compact) Clock(selected, compact)
} else { } else {
Clock(currentPageStyles.keys.first(), compact) Clock(currentPageStyle, compact)
} }
} }
} }
@ -200,9 +198,9 @@ fun WatchFaceSelector(
), ),
) { ) {
Text( Text(
text = getClockstyleName( text = getClockStyleName(
context, context,
styles.entries.first { (k, v) -> v == pagerState.currentPage }.key styles[pagerState.currentPage]
), ),
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
) )
@ -217,19 +215,19 @@ fun WatchFaceSelector(
expanded = showStyleDropdown, expanded = showStyleDropdown,
onDismissRequest = { showStyleDropdown = false } onDismissRequest = { showStyleDropdown = false }
) { ) {
for (style in styles.entries.distinctBy { it.value }.sortedBy { it.value }) { for (style in styles.withIndex()) {
DropdownMenuItem( DropdownMenuItem(
onClick = { onClick = {
scope.launch { scope.launch {
pagerState.animateScrollToPage( pagerState.animateScrollToPage(
style.value, style.index,
) )
} }
showStyleDropdown = false showStyleDropdown = false
}, },
text = { text = {
Text( Text(
text = getClockstyleName(context, style.key), text = getClockStyleName(context, style.value),
) )
} }
) )
@ -253,35 +251,19 @@ fun WatchFaceSelector(
} }
} }
fun getClockstyleName(context: Context, style: ClockWidgetStyle): String { fun getClockStyleName(context: Context, style: ClockWidgetStyle): String {
return when (style) { return when (style) {
ClockStyle.DigitalClock1, is ClockWidgetStyle.Digital1 -> context.getString(R.string.clock_style_digital1)
ClockStyle.DigitalClock1_Outlined, is ClockWidgetStyle.Digital2 -> context.getString(R.string.clock_style_digital2)
ClockStyle.DigitalClock2 -> context.getString(R.string.clock_style_digital2) is ClockWidgetStyle.Orbit -> context.getString(R.string.clock_style_orbit)
ClockStyle.OrbitClock -> context.getString(R.string.clock_style_orbit) is ClockWidgetStyle.Binary -> context.getString(R.string.clock_style_binary)
ClockStyle.BinaryClock -> context.getString(R.string.clock_style_binary) is ClockWidgetStyle.Analog -> context.getString(R.string.clock_style_analog)
ClockStyle.AnalogClock -> context.getString(R.string.clock_style_analog) is ClockWidgetStyle.Segment -> context.getString(R.string.clock_style_segment)
ClockStyle.SegmentClock -> context.getString(R.string.clock_style_segment) is ClockWidgetStyle.Empty -> context.getString(R.string.clock_style_empty)
ClockStyle.EmptyClock -> context.getString(R.string.clock_style_empty)
else -> "" else -> ""
} }
} }
fun getVariantName(context: Context, style: ClockWidgetStyle): String {
return when (style) {
ClockStyle.DigitalClock1,
ClockStyle.DigitalClock2,
ClockStyle.OrbitClock,
ClockStyle.BinaryClock,
ClockStyle.AnalogClock,
ClockStyle.SegmentClock,
ClockStyle.EmptyClock -> context.getString(R.string.clock_variant_standard)
ClockStyle.DigitalClock1_Outlined -> context.getString(R.string.clock_variant_outlined)
else -> ""
}
}
// Compat for old enum names, TODO refactor this screen // Compat for old enum names, TODO refactor this screen
object ClockStyle { object ClockStyle {
val DigitalClock1 = ClockWidgetStyle.Digital1() val DigitalClock1 = ClockWidgetStyle.Digital1()