From 4346af3d2b11dc911b831c516f779f07873df61c Mon Sep 17 00:00:00 2001 From: Fabian Zobrist Date: Fri, 27 Jan 2023 12:03:36 +0100 Subject: [PATCH] Improve settings composable. Remove delay before setting the theme. as this did not help [#11] --- .../me/zobrist/tichucounter/MainActivity.kt | 8 +-- .../tichucounter/ui/settings/SettingsView.kt | 68 ++++++++++++++----- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index a6b1ad3..3d62e81 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -18,7 +18,6 @@ import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.core.os.LocaleListCompat -import androidx.lifecycle.lifecycleScope import androidx.navigation.NavDestination.Companion.hierarchy import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.NavHostController @@ -29,7 +28,6 @@ import androidx.navigation.compose.rememberNavController import com.google.accompanist.systemuicontroller.rememberSystemUiController import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.delay import kotlinx.coroutines.launch import me.zobrist.tichucounter.domain.* import me.zobrist.tichucounter.repository.GameRepository @@ -94,11 +92,7 @@ class MainActivity : AppCompatActivity(), ISettingsChangeListener { } if (themeValue != AppCompatDelegate.getDefaultNightMode()) { - lifecycleScope.launch { - // Give compose a bit of time to update the state. - delay(50) - AppCompatDelegate.setDefaultNightMode(themeValue) - } + AppCompatDelegate.setDefaultNightMode(themeValue) } } diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/settings/SettingsView.kt b/app/src/main/java/me/zobrist/tichucounter/ui/settings/SettingsView.kt index bb02db3..28d5d3e 100644 --- a/app/src/main/java/me/zobrist/tichucounter/ui/settings/SettingsView.kt +++ b/app/src/main/java/me/zobrist/tichucounter/ui/settings/SettingsView.kt @@ -119,7 +119,12 @@ fun StringSetting(name: String, map: Map, selected: T, onSelected: ( .clickable { expanded = true }) { Column(Modifier.weight(5f)) { Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis) - Text(stringResource(map[selected]!!), style = MaterialTheme.typography.labelLarge) + map[selected]?.let { + Text( + stringResource(it), + style = MaterialTheme.typography.labelLarge + ) + } } Column(Modifier.weight(1f)) { @@ -130,23 +135,35 @@ fun StringSetting(name: String, map: Map, selected: T, onSelected: ( ) } - DropdownMenu( - expanded = expanded, - onDismissRequest = { expanded = false } + DropDownMenu( + map, + selected, + expanded, ) { - map.forEach { - DropdownMenuItem( - onClick = { - expanded = false - onSelected(it.key) - }, - text = { Text(stringResource(it.value)) }, - trailingIcon = { - if (it.key == selected) { - Icon(Icons.Outlined.Check, contentDescription = null) - } - }) - } + expanded = false + it?.let { onSelected(it) } + } + } +} + +@Composable +fun DropDownMenu(map: Map, selected: T, expanded: Boolean, onSelected: (T?) -> Unit) { + DropdownMenu( + expanded = expanded, + onDismissRequest = { onSelected(null) } + ) { + map.forEach { + DropdownMenuItem( + onClick = { + onSelected(it.key) + }, + trailingIcon = { + if (it.key == selected) { + Icon(Icons.Outlined.Check, null) + } + }, + text = { Text(stringResource(it.value)) }, + ) } } } @@ -162,3 +179,20 @@ fun SettingsViewPreview() { } } } + +@Preview(name = "Light Mode") +@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true) +@Composable +fun StringSettingPreview() { + + AppTheme { + Surface { + DropDownMenu( + themeMap, + Theme.LIGHT, + true, + ) {} + } + } +} +