Expose UiStore states as Flow#177
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the UI theming pipeline to react to UiStore preference changes via a Flow, so theme changes (notably dark mode) can propagate without explicitly recreating the current Activity.
Changes:
- Add
UiStore.valueState: StateFlow<ValueState>driven bySharedPreferenceschange callbacks. - Update
MainActivityto collectuiStore.valueStateand feeddarkModeintoMihomoTheme. - Remove the manual
Activity.recreate()workaround on dark mode changes inAppSettingsScreen, and simplifyMihomoThemeto acceptDarkModedirectly.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| app/src/main/kotlin/com/github/kr328/clash/ui/theme/Theme.kt | Removes internal UiStore creation; theme now depends on a passed-in DarkMode. |
| app/src/main/kotlin/com/github/kr328/clash/store/UiStore.kt | Introduces valueState as a StateFlow reflecting current preference values. |
| app/src/main/kotlin/com/github/kr328/clash/settings/ui/AppSettingsScreen.kt | Removes Activity.recreate() on dark mode changes; delegates directly to the ViewModel updater. |
| app/src/main/kotlin/com/github/kr328/clash/MainActivity.kt | Collects uiStore.valueState and wires darkMode into MihomoTheme. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| private val _valueState by unsafeLazy { | ||
| val flow = MutableStateFlow(readValueState()) | ||
| preferences.registerOnSharedPreferenceChangeListener { _, _ -> flow.value = readValueState() } | ||
| flow |
There was a problem hiding this comment.
valueState registers a SharedPreferences change listener via a lambda, but the listener instance isn’t retained and therefore cannot be unregistered. Any UiStore instance whose valueState is accessed will be kept alive by SharedPreferences, and if multiple UiStore instances are created over time (e.g., MainActivity recreation), listeners will accumulate and all of them will recompute readValueState() on every preference change. Consider storing the listener in a property and providing a way to unregister it (lifecycle-aware close()), or exposing valueState via a callbackFlow { ... awaitClose { unregister } } / shareIn so it’s automatically cleaned up when no longer collected.
No description provided.