From 4581532928fa038c3ad63f477a2b64e1a1576942 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Fri, 2 Jun 2023 13:34:00 -0600 Subject: [PATCH] queue: shift back if removing playing end Shift the queue index backwards if the current song at the end of a queue is removed. Without this, the index becomes OOB and makes the queue be interpreted as entirely empty when it actually isn't, which is compounded by a remove-1 update intruction leading to a RecyclerView inconsistency crash. --- .../main/java/org/oxycblt/auxio/playback/queue/Queue.kt | 9 +++++++-- .../oxycblt/auxio/playback/state/PlaybackStateManager.kt | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/queue/Queue.kt b/app/src/main/java/org/oxycblt/auxio/playback/queue/Queue.kt index 9d9352ef4..c5ebf9904 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/queue/Queue.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/queue/Queue.kt @@ -107,7 +107,7 @@ interface Queue { } } -class EditableQueue : Queue { +class MutableQueue : Queue { @Volatile private var heap = mutableListOf() @Volatile private var orderedMapping = mutableListOf() @Volatile private var shuffledMapping = mutableListOf() @@ -302,6 +302,7 @@ class EditableQueue : Queue { * @return A [Queue.Change] instance that reflects the changes made. */ fun remove(at: Int): Queue.Change { + val lastIndex = orderedMapping.lastIndex if (shuffledMapping.isNotEmpty()) { // Remove the specified index in the shuffled mapping and the analogous song in the // ordered mapping. @@ -321,12 +322,16 @@ class EditableQueue : Queue { // We just removed the currently playing song. index == at -> { logD("Removed current song") + if (lastIndex == index) { + logD("Current song at end of queue, shift back") + --index + } Queue.Change.Type.SONG } // Index was ahead of removed song, shift back to preserve consistency. index > at -> { logD("Removed before current song, shift back") - index -= 1 + --index Queue.Change.Type.INDEX } // Nothing to do diff --git a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt index 4f2dbb5c0..388087653 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt @@ -22,7 +22,7 @@ import javax.inject.Inject import org.oxycblt.auxio.BuildConfig import org.oxycblt.auxio.music.MusicParent import org.oxycblt.auxio.music.Song -import org.oxycblt.auxio.playback.queue.EditableQueue +import org.oxycblt.auxio.playback.queue.MutableQueue import org.oxycblt.auxio.playback.queue.Queue import org.oxycblt.auxio.util.logD import org.oxycblt.auxio.util.logW @@ -305,7 +305,7 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { @Volatile private var pendingAction: InternalPlayer.Action? = null @Volatile private var isInitialized = false - override val queue = EditableQueue() + override val queue = MutableQueue() @Volatile override var parent: MusicParent? = null private set