Address Gemini code review feedback

- Move toNightMode() extension to core:ui module to eliminate code duplication
- Refactor MainActivity flow logic to use filterIsInstance<Success>() for better readability
- Remove duplicate DarkThemeConfigExt.kt files from app and feature:settings modules
- Update imports in MainActivity and SettingsViewModel
pull/2059/head
shaominngqing 3 weeks ago
parent 02fa916bb4
commit 12262491b8

@ -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<Success>()
.map { it.userData.darkThemeConfig.toNightMode() }
.first()
.let { mode ->
AppCompatDelegate.setDefaultNightMode(mode)
}

@ -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

@ -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
}

@ -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

Loading…
Cancel
Save