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.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.os.LocaleListCompat import androidx.core.os.LocaleListCompat
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavDestination.Companion.hierarchy import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
@@ -29,7 +28,6 @@ import androidx.navigation.compose.rememberNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController import com.google.accompanist.systemuicontroller.rememberSystemUiController
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.zobrist.tichucounter.domain.* import me.zobrist.tichucounter.domain.*
import me.zobrist.tichucounter.repository.GameRepository import me.zobrist.tichucounter.repository.GameRepository
@@ -94,11 +92,7 @@ class MainActivity : AppCompatActivity(), ISettingsChangeListener {
} }
if (themeValue != AppCompatDelegate.getDefaultNightMode()) { if (themeValue != AppCompatDelegate.getDefaultNightMode()) {
lifecycleScope.launch { AppCompatDelegate.setDefaultNightMode(themeValue)
// Give compose a bit of time to update the state.
delay(50)
AppCompatDelegate.setDefaultNightMode(themeValue)
}
} }
} }

View File

@@ -119,7 +119,12 @@ fun <T> StringSetting(name: String, map: Map<T, Int>, selected: T, onSelected: (
.clickable { expanded = true }) { .clickable { expanded = true }) {
Column(Modifier.weight(5f)) { Column(Modifier.weight(5f)) {
Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis) 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)) { Column(Modifier.weight(1f)) {
@@ -130,23 +135,35 @@ fun <T> StringSetting(name: String, map: Map<T, Int>, selected: T, onSelected: (
) )
} }
DropdownMenu( DropDownMenu(
expanded = expanded, map,
onDismissRequest = { expanded = false } selected,
expanded,
) { ) {
map.forEach { expanded = false
DropdownMenuItem( it?.let { onSelected(it) }
onClick = { }
expanded = false }
onSelected(it.key) }
},
text = { Text(stringResource(it.value)) }, @Composable
trailingIcon = { fun <T> DropDownMenu(map: Map<T, Int>, selected: T, expanded: Boolean, onSelected: (T?) -> Unit) {
if (it.key == selected) { DropdownMenu(
Icon(Icons.Outlined.Check, contentDescription = null) 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,
) {}
}
}
}