Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Take one song out of the queue, not several

Lelonioe53882c31b57
diff --git a/app/src/main/java/dev/lelonio/square/playback/PlayQueue.kt b/app/src/main/java/dev/lelonio/square/playback/PlayQueue.kt
index 02ab68f..8105dc4 100644
--- a/app/src/main/java/dev/lelonio/square/playback/PlayQueue.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/PlayQueue.kt
@@ -127,10 +127,25 @@ class PlayQueue {
fun add(index: Int, tracks: List<Track>) {
val at = index.coerceIn(0, _items.size)
+
+ // Added to both lists, so a shuffled queue keeps the order it has.
+ //
+ // As in [remove]: dropping the permutation here meant that queueing one
+ // song reshuffled everything after it. The new tracks go on the end of
+ // the pre-shuffle list and take their places in the permutation where
+ // the listener put them.
+ val order = originalIndices
+ if (order != null) {
+ val original = originalOrder.orEmpty()
+ originalOrder = original + tracks
+ originalIndices = order.toMutableList().apply {
+ addAll(at, tracks.indices.map { original.size + it })
+ }
+ }
+
_items.addAll(at, tracks)
if (at <= currentIndex) currentIndex += tracks.size
- // The saved order no longer describes this queue.
- clearShuffle()
+ if (originalIndices?.size != _items.size) clearShuffle()
}
/**
@@ -183,13 +198,32 @@ class PlayQueue {
val to = toIndex.coerceIn(from, _items.size)
if (from == to) return
+ // The shuffled order survives, minus what was taken out.
+ //
+ // Throwing it away and letting shuffle be applied again drew a *new*
+ // random order for everything still to come: taking one song out of a
+ // shuffled queue looked like several disappearing, because the rest had
+ // been dealt out afresh. What has to go is those tracks' places in the
+ // permutation, not the permutation.
+ val removed = originalIndices?.subList(from, to)?.toSet()
+ if (removed != null) {
+ val kept = originalIndices?.filterIndexed { at, _ -> at < from || at >= to }.orEmpty()
+ val original = originalOrder.orEmpty()
+ val survivors = original.indices.filterNot { it in removed }
+ // The original list loses the same tracks, and every index left has
+ // to point at where its track sits in the shorter list.
+ val moved = survivors.withIndex().associate { (to, from) -> from to to }
+ originalOrder = survivors.map(original::get)
+ originalIndices = kept.mapNotNull(moved::get)
+ }
+
_items.subList(from, to).clear()
currentIndex = when {
currentIndex >= to -> currentIndex - (to - from)
currentIndex >= from -> from
else -> currentIndex
}.coerceIn(0, maxOf(0, _items.lastIndex))
- clearShuffle()
+ if (originalIndices?.size != _items.size) clearShuffle()
}
fun move(fromIndex: Int, toIndex: Int, newIndex: Int) {
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt
index 4b08302..6b10ea0 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt
@@ -239,20 +239,43 @@ private fun RowScope.PanelTab(
}
/** One row of the upcoming-tracks list. */
-data class QueueEntry(val index: Int, val title: String, val artist: String, val isCurrent: Boolean)
+data class QueueEntry(
+ val index: Int,
+ /** What the row is, so a list that loses one can animate the rest. */
+ val uri: String,
+ val title: String,
+ val artist: String,
+ val isCurrent: Boolean,
+)
@Composable
-internal fun QueueList(queue: List<QueueEntry>, onPlay: (Int) -> Unit) {
+internal fun QueueList(
+ queue: List<QueueEntry>,
+ onPlay: (Int) -> Unit,
+ /** Takes a track out of the queue; absent for the one playing. */
+ onRemove: (Int) -> Unit,
+) {
if (queue.isEmpty()) {
EmptyPanel(stringResource(R.string.queue_empty))
return
}
LazyColumn(Modifier.padding(vertical = 8.dp)) {
- itemsIndexed(queue, key = { _, entry -> entry.index }) { _, entry ->
+ // Keyed on the track, not on where it sits.
+ //
+ // With the index as the key, taking one out renumbered every row below
+ // it — as far as the list is concerned each of those became a different
+ // item, so nothing could be animated and the queue jumped. Keyed on the
+ // track itself, the row that went is the only one that changes and the
+ // rest slide up into the gap.
+ itemsIndexed(
+ queue,
+ key = { at, entry -> "${entry.uri}-$at-${entry.title}" },
+ ) { _, entry ->
Row(
Modifier
.fillMaxWidth()
+ .animateItem()
.clickable { onPlay(entry.index) }
.padding(horizontal = 18.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerState.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerState.kt
index a571df9..82982f8 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerState.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerState.kt
@@ -272,6 +272,7 @@ fun rememberQueue(player: Player?): State<List<QueueEntry>> {
val metadata = player.getMediaItemAt(index).mediaMetadata
QueueEntry(
index = index,
+ uri = player.getMediaItemAt(index).mediaId,
title = metadata.title?.toString().orEmpty(),
artist = metadata.artist?.toString().orEmpty(),
isCurrent = index == current,