From 02fa916bb429112c6cea49ca4df8595348f8c5a2 Mon Sep 17 00:00:00 2001 From: shaominngqing Date: Tue, 3 Feb 2026 19:18:38 +0800 Subject: [PATCH] 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 000000000..670685791 --- /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 000000000..c7ba042e4 --- /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 +}