Introduce a Connecting state so the UI reflects user intent immediately, centralize navigation in MainActivity via state transitions, and replace the pauseRequested volatile flag with controller state as single source of truth. Made-with: Cursor
2.5 KiB
2.5 KiB
Player State Machine Refactor
Task
Refactored the player state management to eliminate race conditions during rapid station tapping, stop/play transitions, and the NowPlayingScreen bounce-back bug.
Changes Made
PlaybackState.kt
- Added
Connectingstate variant to represent the period between user tap and service processing the intent.
RadioController.kt
play()now sets state toConnectingsynchronously before sending the intent, eliminating the async gap.stop()now sets state toIdlesynchronously before sending the intent.pause()now sets state toPausedsynchronously before sending the intent.- Added redundancy guard: double-tapping a station that's already
Connectingis a no-op.
RadioPlaybackService.kt
- Removed the
pauseRequestedvolatile flag entirely. - The
handlePlay()finally block now checkscontroller.state.value(the single source of truth) instead of the volatile flag to determine cleanup behavior. - Renamed
cleanup()tocleanupResources()since it no longer setsIdlestate (that's now the controller's responsibility). handlePause()andhandleStop()no longer setpauseRequested.
MainActivity.kt
- Centralized navigation logic via a
LaunchedEffectthat observesRadioController.state. - Navigates to NowPlaying on
Idle -> Activetransitions. - Navigates back to StationList on
Active -> Idletransitions while on NowPlaying. - Station switching (
Playing(A) -> Connecting(B)) stays on NowPlaying.
NowPlayingScreen.kt
- Removed the
LaunchedEffect(playbackState)that calledonBack()onIdle(was the source of the bounce-back bug). - Added
Connectingto thewhenbranches, showing a loading spinner overlay. - Disabled pause/skip-ahead buttons while connecting.
NowPlayingViewModel.kt
- Added
Connectinghandling in session timer (usessessionStartedAt), connection timer (shows 0), and artwork resolver (uses station default artwork).
StationListScreen.kt
- Removed
onNavigateToNowPlaying()fromonPlaycallbacks (navigation is now centralized in MainActivity). - Added
Connectingto mini player visibility and "now playing" station highlight.
MiniPlayer.kt
- Added
Connectingto the destructuringwhenblock. - Shows "Connecting..." subtitle for the
Connectingstate.
Follow-up Items
- The
stayConnectedandretryImmediatelyOnNetworkvolatile flags in RadioPlaybackService could be further consolidated into the state machine in a future pass.