Skip to content

Resolve start/stop toggle state race conditions on HomeScreen - #320

Merged
Goooler merged 4 commits into
trunkfrom
fix-state-switching
Jun 7, 2026
Merged

Goooler merged 4 commits into
trunkfrom
fix-state-switching

Conversation

@Goooler

@Goooler Goooler commented Jun 7, 2026

Copy link
Copy Markdown
Owner

This PR resolves the issue where the start/stop status card toggling on the HomeScreen doesn't always take effect or can be clicked repeatedly, leading to race conditions in the background service.

Changes:

  1. Introduced isTransitioning in HomeViewModel's UiState to disable toggle button clicking and display a Loading state (with TabbyIcons.BaselineSync and CommonR.string.loading) when start/stop is in progress.
  2. Refactored the control flow logic using Kotlin's idiomatic when expression syntax.
  3. Added a 10s timeout safety net to auto-reset transition state if the service fails to start or get destroyed.
  4. Handled VPN permission request cancellations to reset the transition state immediately.
  5. Added unit tests verifying isTransitioning flow.

@Goooler Goooler changed the title fix: resolve start/stop toggle state race conditions on HomeScreen Resolve start/stop toggle state race conditions on HomeScreen Jun 7, 2026
@Goooler
Goooler requested a review from Copilot June 7, 2026 08:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to eliminate race conditions from repeated start/stop toggles on the HomeScreen by introducing an explicit “transition in progress” UI/VM state and guarding the toggle action while service state changes are pending.

Changes:

  • Added UiState.isTransitioning to HomeViewModel, with lifecycle-managed reset and a 10s timeout fallback.
  • Updated HomeScreen status card to display a loading state (icon/text) and suppress toggling during transitions; reset transition state when VPN permission is cancelled.
  • Added a unit test to validate isTransitioning transitions across start/stop.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
ui/home/src/main/kotlin/com/github/kr328/clash/home/vm/HomeViewModel.kt Introduces isTransitioning, transition timeout handling, and permission-denial reset to prevent repeated toggles during service state changes.
ui/home/src/main/kotlin/com/github/kr328/clash/home/ui/HomeScreen.kt Renders a loading state on the status card during transitions and handles VPN permission cancellation by resetting transition state.
ui/home/src/test/kotlin/com/github/kr328/clash/home/vm/HomeViewModelTest.kt Adds a test verifying isTransitioning flips on toggle and resets on clashRunning changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ui/home/src/main/kotlin/com/github/kr328/clash/home/ui/HomeScreen.kt Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@Goooler
Goooler merged commit 674eb4a into trunk Jun 7, 2026
4 checks passed
@Goooler
Goooler deleted the fix-state-switching branch June 7, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants