From 302648afde4592e02aa090c49d4c4ca04179c2a2 Mon Sep 17 00:00:00 2001 From: shaominngqing Date: Tue, 3 Feb 2026 19:18:26 +0800 Subject: [PATCH 1/3] Fix splashscreen theme not updating after theme change Apply AppCompatDelegate.setDefaultNightMode() when the user changes the dark theme configuration to ensure the system-level night mode is synchronized. This makes the splash screen display with the correct theme on cold start. Changes: - SettingsViewModel: Apply night mode immediately when user changes theme - MainActivity: Apply saved theme config on startup Fixes #633 --- .../samples/apps/nowinandroid/MainActivity.kt | 20 +++++++++++++++++++ .../settings/impl/SettingsViewModel.kt | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt index ecc23d80e0..90f842d0e7 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt @@ -22,6 +22,7 @@ import androidx.activity.SystemBarStyle import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatDelegate import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -34,6 +35,7 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.metrics.performance.JankStats import androidx.tracing.trace import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading +import com.google.samples.apps.nowinandroid.MainActivityUiState.Success import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper import com.google.samples.apps.nowinandroid.core.analytics.LocalAnalyticsHelper import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository @@ -44,6 +46,7 @@ import com.google.samples.apps.nowinandroid.core.ui.LocalTimeZone import com.google.samples.apps.nowinandroid.ui.NiaApp import com.google.samples.apps.nowinandroid.ui.rememberNiaAppState import com.google.samples.apps.nowinandroid.util.isSystemInDarkTheme +import com.google.samples.apps.nowinandroid.util.toNightMode import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged @@ -79,6 +82,23 @@ class MainActivity : ComponentActivity() { val splashScreen = installSplashScreen() super.onCreate(savedInstanceState) + // Apply the saved dark theme config at startup to ensure the splash screen + // uses the correct theme. This addresses issue #633. + lifecycleScope.launch { + viewModel.uiState + .map { state -> + if (state is Success) { + state.userData.darkThemeConfig.toNightMode() + } else { + null + } + } + .first { it != null } // Only take the first non-null mode + .let { mode -> + AppCompatDelegate.setDefaultNightMode(mode) + } + } + // We keep this as a mutable state, so that we can track changes inside the composition. // This allows us to react to dark/light mode changes. var themeSettings by mutableStateOf( diff --git a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt index 274f916d13..ddb7df2bf6 100644 --- a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt +++ b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt @@ -16,6 +16,7 @@ package com.google.samples.apps.nowinandroid.feature.settings.impl +import androidx.appcompat.app.AppCompatDelegate import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository @@ -62,6 +63,11 @@ class SettingsViewModel @Inject constructor( fun updateDarkThemeConfig(darkThemeConfig: DarkThemeConfig) { viewModelScope.launch { userDataRepository.setDarkThemeConfig(darkThemeConfig) + + // Apply the night mode at the application level to ensure the splash screen + // uses the correct theme on cold start. This addresses issue #633. + // Reference: https://developer.android.com/develop/ui/views/theming/darktheme#change-themes + AppCompatDelegate.setDefaultNightMode(darkThemeConfig.toNightMode()) } } From 02fa916bb429112c6cea49ca4df8595348f8c5a2 Mon Sep 17 00:00:00 2001 From: shaominngqing Date: Tue, 3 Feb 2026 19:18:38 +0800 Subject: [PATCH 2/3] Address Gemini code review feedback - Use first() instead of collect() in MainActivity to avoid redundant calls when theme changes at runtime (since SettingsViewModel already handles that) - Extract DarkThemeConfig to night mode mapping into extension functions to avoid code duplication and improve maintainability Based on feedback from gemini-code-assist review --- .../nowinandroid/util/DarkThemeConfigExt.kt | 30 +++++++++++++++++++ .../settings/impl/DarkThemeConfigExt.kt | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt create mode 100644 feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt new file mode 100644 index 0000000000..670685791a --- /dev/null +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid.util + +import androidx.appcompat.app.AppCompatDelegate +import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig + +/** + * Converts [DarkThemeConfig] to the corresponding [AppCompatDelegate] night mode constant. + * This is used to set the application-level night mode for the splash screen and system UI. + */ +fun DarkThemeConfig.toNightMode(): Int = when (this) { + DarkThemeConfig.FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + DarkThemeConfig.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO + DarkThemeConfig.DARK -> AppCompatDelegate.MODE_NIGHT_YES +} diff --git a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt new file mode 100644 index 0000000000..c7ba042e43 --- /dev/null +++ b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid.feature.settings.impl + +import androidx.appcompat.app.AppCompatDelegate +import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig + +/** + * Converts [DarkThemeConfig] to the corresponding [AppCompatDelegate] night mode constant. + * This is used to set the application-level night mode for the splash screen and system UI. + */ +internal fun DarkThemeConfig.toNightMode(): Int = when (this) { + DarkThemeConfig.FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + DarkThemeConfig.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO + DarkThemeConfig.DARK -> AppCompatDelegate.MODE_NIGHT_YES +} From 12262491b8bf7ed8c8e34bd7f879f42d16fe7cd2 Mon Sep 17 00:00:00 2001 From: shaominngqing Date: Wed, 4 Feb 2026 12:06:45 +0800 Subject: [PATCH 3/3] Address Gemini code review feedback - Move toNightMode() extension to core:ui module to eliminate code duplication - Refactor MainActivity flow logic to use filterIsInstance() for better readability - Remove duplicate DarkThemeConfigExt.kt files from app and feature:settings modules - Update imports in MainActivity and SettingsViewModel --- .../samples/apps/nowinandroid/MainActivity.kt | 14 ++++----- .../core/ui}/DarkThemeConfigExt.kt | 7 ++--- .../settings/impl/DarkThemeConfigExt.kt | 30 ------------------- .../settings/impl/SettingsViewModel.kt | 1 + 4 files changed, 9 insertions(+), 43 deletions(-) rename {app/src/main/kotlin/com/google/samples/apps/nowinandroid/util => core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui}/DarkThemeConfigExt.kt (77%) delete mode 100644 feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt index 90f842d0e7..461298550a 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt @@ -43,13 +43,14 @@ import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.ui.LocalTimeZone +import com.google.samples.apps.nowinandroid.core.ui.toNightMode import com.google.samples.apps.nowinandroid.ui.NiaApp import com.google.samples.apps.nowinandroid.ui.rememberNiaAppState import com.google.samples.apps.nowinandroid.util.isSystemInDarkTheme -import com.google.samples.apps.nowinandroid.util.toNightMode import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -86,14 +87,9 @@ class MainActivity : ComponentActivity() { // uses the correct theme. This addresses issue #633. lifecycleScope.launch { viewModel.uiState - .map { state -> - if (state is Success) { - state.userData.darkThemeConfig.toNightMode() - } else { - null - } - } - .first { it != null } // Only take the first non-null mode + .filterIsInstance() + .map { it.userData.darkThemeConfig.toNightMode() } + .first() .let { mode -> AppCompatDelegate.setDefaultNightMode(mode) } diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DarkThemeConfigExt.kt similarity index 77% rename from app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt rename to core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DarkThemeConfigExt.kt index 670685791a..1d7aa97f0c 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt +++ b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DarkThemeConfigExt.kt @@ -1,5 +1,5 @@ /* - * Copyright 2022 The Android Open Source Project + * Copyright 2026 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,14 +14,13 @@ * limitations under the License. */ -package com.google.samples.apps.nowinandroid.util +package com.google.samples.apps.nowinandroid.core.ui import androidx.appcompat.app.AppCompatDelegate import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig /** - * Converts [DarkThemeConfig] to the corresponding [AppCompatDelegate] night mode constant. - * This is used to set the application-level night mode for the splash screen and system UI. + * Converts [DarkThemeConfig] to [AppCompatDelegate] night mode constant. */ fun DarkThemeConfig.toNightMode(): Int = when (this) { DarkThemeConfig.FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM diff --git a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt deleted file mode 100644 index c7ba042e43..0000000000 --- a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/DarkThemeConfigExt.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.samples.apps.nowinandroid.feature.settings.impl - -import androidx.appcompat.app.AppCompatDelegate -import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig - -/** - * Converts [DarkThemeConfig] to the corresponding [AppCompatDelegate] night mode constant. - * This is used to set the application-level night mode for the splash screen and system UI. - */ -internal fun DarkThemeConfig.toNightMode(): Int = when (this) { - DarkThemeConfig.FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - DarkThemeConfig.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO - DarkThemeConfig.DARK -> AppCompatDelegate.MODE_NIGHT_YES -} diff --git a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt index ddb7df2bf6..ff5f67c094 100644 --- a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt +++ b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt @@ -22,6 +22,7 @@ import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand +import com.google.samples.apps.nowinandroid.core.ui.toNightMode import com.google.samples.apps.nowinandroid.feature.settings.impl.SettingsUiState.Loading import com.google.samples.apps.nowinandroid.feature.settings.impl.SettingsUiState.Success import dagger.hilt.android.lifecycle.HiltViewModel