Improve settings composable. Remove delay before setting the theme. as this did not help [#11]
Some checks are pending
continuous-integration/drone/push Build is pending

This commit is contained in:
2023-01-27 12:03:36 +01:00
parent ca88bd1054
commit 4346af3d2b
2 changed files with 52 additions and 24 deletions

View File

@@ -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,13 +92,9 @@ 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)
}
}
}
override fun onScreenOnChanged(keepOn: KeepScreenOn) {
if (keepOn.value) {

View File

@@ -119,7 +119,12 @@ fun <T> StringSetting(name: String, map: Map<T, Int>, 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 <T> StringSetting(name: String, map: Map<T, Int>, selected: T, onSelected: (
)
}
DropDownMenu(
map,
selected,
expanded,
) {
expanded = false
it?.let { onSelected(it) }
}
}
}
@Composable
fun <T> DropDownMenu(map: Map<T, Int>, selected: T, expanded: Boolean, onSelected: (T?) -> Unit) {
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
onDismissRequest = { onSelected(null) }
) {
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)
}
})
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,
) {}
}
}
}