From a16f00a42ba6c0cd0f5704779f2dda0e58b8dce3 Mon Sep 17 00:00:00 2001 From: OmniaX-Dev Date: Mon, 6 Jul 2026 06:25:52 +0200 Subject: [PATCH] Fixed small bug in cached play offset --- .../pianotimeline/ui/RecordingListViewModel.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt b/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt index aa539e9..70bdffe 100644 --- a/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt +++ b/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt @@ -272,11 +272,15 @@ class RecordingListViewModel(app: Application) : AndroidViewModel(app) { } // Throttled persistence of the current track + offset: at most once per - // second, and only while a track is loaded. Saving live (every 200ms - // position tick) would hammer DataStore; 1s is plenty for resume. + // second, and only while a track is actively PLAYING. Saving while paused + // is both unnecessary (a paused position doesn't change) and harmful: on + // restore, preparePaused seeks asynchronously, so ExoPlayer briefly reports + // position 0 before the seek lands — saving during that window would clobber + // the good offset with 0. Gating on isPlaying closes that window. viewModelScope.launch { var lastSaveAt = 0L playbackState.collect { s -> + if (!s.isPlaying) return@collect val id = s.nowPlayingId ?: return@collect val now = System.currentTimeMillis() if (now - lastSaveAt >= 1000L) {