Conversation
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 android#633
- 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
Summary of ChangesHello @shaominngqing, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves an issue where the application's splash screen did not consistently reflect the user's chosen dark theme settings, particularly during cold starts. The changes ensure that the saved dark theme configuration is applied early in the application lifecycle, providing a seamless and consistent visual experience from the splash screen onwards. Additionally, the theme is updated dynamically when changed in the settings. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses an issue with the splash screen theme by ensuring the user's selected dark theme configuration is applied at application startup. This is achieved by observing the user data and setting the default night mode in MainActivity. The theme is also applied immediately when changed in the settings. The overall approach is sound. I've provided a suggestion to refactor a piece of Flow-handling logic for better readability and pointed out an instance of code duplication that should be resolved for better maintainability.
| 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 | ||
| } |
There was a problem hiding this comment.
This toNightMode() extension function is identical to the one added in app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt. This code duplication should be avoided to improve maintainability. Consider moving this function to a shared module that both the app and feature:settings modules can depend on, for example a core:ui or core:common module. This would make the function internal to that shared module but available to both the app and feature:settings modules.
| .map { state -> | ||
| if (state is Success) { | ||
| state.userData.darkThemeConfig.toNightMode() | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
| .first { it != null } // Only take the first non-null mode |
There was a problem hiding this comment.
This flow processing logic can be made more concise and idiomatic by using filterIsInstance to filter for the Success state, followed by map. This approach is more declarative and improves readability by removing the manual type check and null handling.
.filterIsInstance<Success>()
.map { it.userData.darkThemeConfig.toNightMode() }
.first()- 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
Summary
This PR fixes issue #633 where the splash screen does not reflect the user's chosen dark theme configuration during cold starts.
What I have done and why
Problem
When users change the dark theme setting (Light/Dark/Follow System) in the app settings, the splash screen continues to display with the old theme on the next app launch. This creates an inconsistent and jarring user experience.
Solution
This PR implements three key changes:
Apply theme configuration on startup: Modified
MainActivityto observe the user's dark theme preference and apply it immediately usingAppCompatDelegate.setDefaultNightMode()before the splash screen is displayed.Dynamic theme updates: Modified
SettingsViewModelto apply the theme configuration immediately when changed in settings, ensuring consistency throughout the app lifecycle.Code organization: Created a shared
toNightMode()extension function in thecore:uimodule to convertDarkThemeConfigenum values toAppCompatDelegatenight mode constants, eliminating code duplication and improving maintainability.Changes Made
Modified Files:
app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.ktfilterIsInstance<Success>()for better readability (per Gemini Code Assist feedback)feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.ktNew Files:
core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/DarkThemeConfigExt.ktCode Review Response
Addressed feedback from Gemini Code Assist:
toNightMode()extension tocore:uimodule (eliminated code duplication)filterIsInstance<Success>()(improved readability)Testing
Related Issues
Fixes #633