Bind settings persistence to lifecycle#228
Merged
Merged
Conversation
Replace dispose-triggered Global scope writes with lifecycle observer callbacks and ViewModel-scoped persistence so settings are saved from onStop without escaping the screen lifecycle. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Intended to use non-viewModel scope as we need the action to be called on disposed.
There was a problem hiding this comment.
Pull request overview
This PR aims to ensure settings persistence happens from lifecycle callbacks (e.g., onStop) rather than Compose onDispose, so persistence is better aligned with screen lifecycle and avoids late/disposed-triggered writes.
Changes:
- Convert settings ViewModels to implement
DefaultLifecycleObserverand persist/clear ononStop. - Update Settings Compose screens to register/unregister their ViewModel as a lifecycle observer via
LocalLifecycleOwner. - Adjust “reset” behavior to rely on the
onStoppersistence path rather than immediately clearing in aGlobalcoroutine.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| ui/settings/src/main/kotlin/com/github/kr328/clash/settings/vm/OverrideSettingsViewModel.kt | Switch persistence trigger to DefaultLifecycleObserver.onStop (currently still uses Global.launch). |
| ui/settings/src/main/kotlin/com/github/kr328/clash/settings/vm/MetaFeatureSettingsViewModel.kt | Same onStop-based persistence/clear behavior for meta feature overrides (currently still uses Global.launch). |
| ui/settings/src/main/kotlin/com/github/kr328/clash/settings/vm/AccessControlViewModel.kt | Move selection persistence + optional service restart to onStop (currently still uses Global.launch). |
| ui/settings/src/main/kotlin/com/github/kr328/clash/settings/ui/OverrideSettingsScreen.kt | Register/unregister the ViewModel as a lifecycle observer using LocalLifecycleOwner. |
| ui/settings/src/main/kotlin/com/github/kr328/clash/settings/ui/MetaFeatureSettingsScreen.kt | Register/unregister the ViewModel as a lifecycle observer using LocalLifecycleOwner. |
| ui/settings/src/main/kotlin/com/github/kr328/clash/settings/ui/AccessControlScreen.kt | Register/unregister the ViewModel as a lifecycle observer using LocalLifecycleOwner. |
Comments suppressed due to low confidence (1)
ui/settings/src/main/kotlin/com/github/kr328/clash/settings/vm/AccessControlViewModel.kt:70
onStop()performs persistence + service restart logic insideGlobal.launch, which is not tied to the ViewModel/screen lifecycle and may continue running after the screen is gone. Consider moving this work toviewModelScope.launch(Dispatchers.IO)(and update/remove the stale comment about needing a non-ViewModel scope for disposal) so it matches the lifecycle-bound persistence approach described in the PR.
override fun onStop(owner: LifecycleOwner) {
// Intended to use non-viewModel scope as we need the action to be called on disposed.
Global.launch {
val selected = uiState.value.selected
val persistedSelection = serviceStore.accessControlPackages
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+33
to
+37
| override fun onStop(owner: LifecycleOwner) { | ||
| // Intended to use non-viewModel scope as we need the action to be called on disposed. | ||
| Global.launch { | ||
| if (skipPersist) return@launch | ||
| withClash { patchOverride(Clash.OverrideSlot.Persist, configuration.value) } | ||
| withClash { | ||
| if (skipPersist) clearOverride(Clash.OverrideSlot.Persist) |
Comment on lines
+42
to
+46
| override fun onStop(owner: LifecycleOwner) { | ||
| // Intended to use non-viewModel scope as we need the action to be called on disposed. | ||
| Global.launch { | ||
| if (skipPersist) return@launch | ||
| withClash { patchOverride(Clash.OverrideSlot.Persist, configuration.value) } | ||
| withClash { | ||
| if (skipPersist) clearOverride(Clash.OverrideSlot.Persist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace dispose-triggered Global scope writes with lifecycle observer callbacks and ViewModel-scoped persistence so settings are saved from onStop without escaping the screen lifecycle.
Refs #226.