Hoist UiModeManager lookup out of collect block

Move getSystemService call outside the flow collector so
the system service is resolved once rather than on every emission.
pull/2060/head
Steven Elliott 4 weeks ago
parent ed3f34e579
commit 8b7b4f1a0b

@ -135,13 +135,13 @@ class MainActivity : ComponentActivity() {
// Sync the user's dark theme preference to the system-level UiModeManager so the
// splash screen uses the correct theme on the next cold start.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val uiModeManager = getSystemService(UiModeManager::class.java)
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.mapNotNull { (it as? Success)?.userData?.darkThemeConfig }
.distinctUntilChanged()
.collect { darkThemeConfig ->
val uiModeManager = getSystemService(UiModeManager::class.java)
uiModeManager.setApplicationNightMode(
when (darkThemeConfig) {
DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO

Loading…
Cancel
Save