fix: avoid duplicate playback events at the end of the timeline
CI / play (push) Successful in 3m38s
CI / build (push) Successful in 3m55s

Also prefer the freshest mood detail during offline sync and reset the skip
suppression after a previous that only restarted the track.
This commit is contained in:
TapTap committed 2026-11-19 16:41:41 +00:00
1 parent 1e3dac4e47
commit 8cf77f156d
2 files changed
+21 -7

No files matched your search

@@ -1161,7 +1161,11 @@ class AppStore(
_state.update { _state.update {
it.copy( it.copy(
downloads = storage.listDownloads().keys, downloads = storage.listDownloads().keys,
message = "Download removed", message = if (settings.offlineMoods.isEmpty()) {
"Download removed"
} else {
"Download removed; offline moods restore it on the next sync"
},
messageIsError = false, messageIsError = false,
messageId = it.messageId + 1, messageId = it.messageId + 1,
) )
@@ -1258,7 +1262,7 @@ class AppStore(
private fun syncOfflineMoods() { private fun syncOfflineMoods() {
if (settings.offlineMoods.isEmpty()) return if (settings.offlineMoods.isEmpty()) return
if (!isUnmeteredNetwork()) return if (!isUnmeteredNetwork()) return
val details = (cache.load().moodDetails + listOfNotNull(_state.value.selectedMood)) val details = (listOfNotNull(_state.value.selectedMood) + cache.load().moodDetails)
.distinctBy { it.mood.id } .distinctBy { it.mood.id }
val songs = details val songs = details
.filter { it.mood.id in settings.offlineMoods } .filter { it.mood.id in settings.offlineMoods }
@@ -1380,6 +1384,10 @@ class AppStore(
suppressCompletion = false suppressCompletion = false
trackIndex = index trackIndex = index
report("started", timelineSongs.getOrNull(index)?.id, 0) report("started", timelineSongs.getOrNull(index)?.id, 0)
} else {
// A previous that restarted the track must not suppress the next
// natural completion event.
suppressCompletion = false
} }
applyTimelineState() applyTimelineState()
_state.update { _state.update {
@@ -1503,16 +1511,18 @@ class AppStore(
/** Returns false when playback should stop at the end of the timeline. */ /** Returns false when playback should stop at the end of the timeline. */
private suspend fun advanceAtEnd(): Boolean { private suspend fun advanceAtEnd(): Boolean {
if (fetching) {
delay(500)
return true
}
// A prefetch may already have appended the next batch while ExoPlayer // A prefetch may already have appended the next batch while ExoPlayer
// was finishing. Play it instead of requesting duplicates. // was finishing. Play it instead of requesting duplicates.
if (trackIndex + 1 < timelineSongs.size) { if (trackIndex + 1 < timelineSongs.size) {
report("completed", timelineSongs.getOrNull(trackIndex)?.id, _state.value.positionSeconds.toInt())
suppressCompletion = true
player.playIndex(trackIndex + 1) player.playIndex(trackIndex + 1)
return true return true
} }
if (fetching) {
delay(500)
return true
}
if (_state.value.sleepAtTrackEnd) { if (_state.value.sleepAtTrackEnd) {
sleepJob?.cancel() sleepJob?.cancel()
sleepJob = null sleepJob = null
@@ -1530,6 +1540,7 @@ class AppStore(
val session = _state.value.session val session = _state.value.session
val current = timelineSongs.getOrNull(trackIndex) val current = timelineSongs.getOrNull(trackIndex)
if (session == null) { if (session == null) {
report("completed", timelineSongs.getOrNull(trackIndex)?.id, _state.value.positionSeconds.toInt())
_state.update { it.copy(playing = false) } _state.update { it.copy(playing = false) }
return false return false
} }
@@ -1539,6 +1550,7 @@ class AppStore(
return false return false
} }
if (more.isEmpty()) { if (more.isEmpty()) {
report("completed", timelineSongs.getOrNull(trackIndex)?.id, _state.value.positionSeconds.toInt())
_state.update { it.copy(playing = false) } _state.update { it.copy(playing = false) }
return false return false
} }
@@ -1546,6 +1558,8 @@ class AppStore(
val from = timelineSongs.size val from = timelineSongs.size
timelineSongs = timelineSongs + more timelineSongs = timelineSongs + more
player.appendToQueue(buildTracks(more)) player.appendToQueue(buildTracks(more))
report("completed", timelineSongs.getOrNull(trackIndex)?.id, _state.value.positionSeconds.toInt())
suppressCompletion = true
applyTimelineState() applyTimelineState()
_state.update { it.copy(playing = true) } _state.update { it.copy(playing = true) }
player.playIndex(from) player.playIndex(from)
@@ -142,7 +142,7 @@ fun PlayerBar(state: AppState, store: AppStore) {
Icon( Icon(
Icons.Rounded.Bedtime, Icons.Rounded.Bedtime,
contentDescription = "Sleep timer", contentDescription = "Sleep timer",
tint = if (sleepRemaining != null) { tint = if (sleepRemaining != null || state.sleepAtTrackEnd) {
MaterialTheme.colorScheme.primary MaterialTheme.colorScheme.primary
} else { } else {
MaterialTheme.colorScheme.onSurfaceVariant MaterialTheme.colorScheme.onSurfaceVariant