Skip to content

Native Android client for Spotify, built with Jetpack Compose

Queue: autoplay songs under their own heading, no Spotify song crosses it

kota-zod8a7bc370012
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt
index 22025d36..04f6d8d7 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt
@@ -185,7 +185,7 @@ fun ImmersiveQueuePanel(modifier: Modifier = Modifier, contentPadding: PaddingVa
val f = from.key as? String ?: return@rememberReorderableLazyListState
val t = to.key as? String ?: return@rememberReorderableLazyListState
val row = dragRows[f] ?: return@rememberReorderableLazyListState
- QueueDragMath.swap(o, f, t, HEADING_NEXT, canLeaveQueue = row.item.provider == SongPlayer.LOCAL)?.let { dragOrder = it }
+ QueueDragMath.swap(o, f, t, HEADING_NEXT, canLeaveQueue = row.item.provider == SongPlayer.LOCAL, block = blockOf(dragRows))?.let { dragOrder = it }
}
val onDragStarted: (String) -> Unit = { k ->
dragStart = order; dragRows = byKey; dragOrder = order
@@ -197,7 +197,7 @@ fun ImmersiveQueuePanel(modifier: Modifier = Modifier, contentPadding: PaddingVa
val known = dragRows
dragOrder = null
val row = known[k] ?: return@stop
- val move = QueueDragMath.moveFor(start, after, k, HEADING_NEXT)
+ val move = QueueDragMath.moveFor(start, after, k, HEADING_NEXT, blockOf(known))
val call: () -> Unit = when (move) {
QueueDragMath.Move.None -> return@stop
is QueueDragMath.Move.IntoQueue -> {
@@ -286,8 +286,10 @@ fun ImmersiveQueuePanel(modifier: Modifier = Modifier, contentPadding: PaddingVa
}
}
// The queued rows, the context's heading and its rows: one list, so a row passing the
- // heading only changes places with it (and so changes part).
- items(order, key = { it }) { k ->
+ // heading only changes places with it (and so changes part). Autoplay's songs come under a
+ // heading of their own, a plain item: no Spotify song crosses it (QueueDragMath.swap).
+ val autoAt = autoplayStart(order, byKey)
+ val rowItem: @Composable LazyItemScope.(String) -> Unit = rowItem@{ k ->
if (k == HEADING_NEXT) {
// A place the dragged row swaps with (it then changes part), never dragged itself.
ReorderableItem(reorder, key = k, animateItemModifier = queueMotion()) {
@@ -296,14 +298,24 @@ fun ImmersiveQueuePanel(modifier: Modifier = Modifier, contentPadding: PaddingVa
modifier = Modifier.padding(top = 16.dp, bottom = 6.dp),
)
}
- return@items
+ return@rowItem
}
- val row = byKey[k] ?: return@items
+ val row = byKey[k] ?: return@rowItem
ReorderableItem(reorder, key = k, animateItemModifier = queueMotion()) { dragging ->
QueueEntry(row, removed, dragging,
handle = Modifier.draggableHandle(onDragStarted = { onDragStarted(k) }, onDragStopped = { onDragStopped(k) }))
}
}
+ items(if (autoAt < 0) order else order.subList(0, autoAt), key = { it }) { k -> rowItem(k) }
+ if (autoAt >= 0) {
+ item(key = "heading-autoplay") {
+ QueueHeading(
+ title = stringResource(R.string.queue_autoplay_heading),
+ modifier = Modifier.padding(top = 16.dp, bottom = 6.dp).then(queueMotion()),
+ )
+ }
+ items(order.subList(autoAt, order.size), key = { it }) { k -> rowItem(k) }
+ }
if (!hasCurrent && order.isEmpty()) {
item(key = "empty") {
Text(
@@ -327,6 +339,18 @@ private val CARRIED_ROW = Color(0xFF2C2C30)
private const val LIFT_SETTLE_MS = 450L
private const val HEADING_NEXT = "heading-next"
+/** Autoplay's songs (Spotify adds them after the context's) and the context's: blocks no Spotify song leaves. */
+private fun blockOf(rows: Map<String, QRow>): (String) -> Int = { k -> if (rows[k]?.item?.provider == AUTOPLAY) 1 else 0 }
+
+/** Where autoplay's songs start in [order]'s "next from" part, or -1. */
+private fun autoplayStart(order: List<String>, rows: Map<String, QRow>): Int {
+ val h = order.indexOf(HEADING_NEXT)
+ if (h < 0) return -1
+ return (h + 1 until order.size).firstOrNull { rows[order[it]]?.item?.provider == AUTOPLAY } ?: -1
+}
+
+private const val AUTOPLAY = "autoplay"
+
/** The panel's flat order of keys: the queued rows, the context's heading (when it has rows), its rows. */
private fun flatOrder(rows: List<QRow>): List<String> {
val next = rows.filter { !it.queued }.map { it.key }
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/QueueDragMath.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/QueueDragMath.kt
index 078315b8..ab89bc2a 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/QueueDragMath.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/QueueDragMath.kt
@@ -13,16 +13,27 @@ internal object QueueDragMath {
/**
* [order] with [from] moved to [to]'s place, or null when that move is not allowed: a song of
* Spotify's queue ([canLeaveQueue] false) cannot go back into "next from" (Spotify has no such
- * move; it would come back to the queue's end). A local song goes anywhere.
+ * move; it would come back to the queue's end). Nor can a Spotify song cross from the context's
+ * songs to autoplay's or back ([block] tells them apart): asked to, Spotify puts a context song
+ * at the context's end and an autoplay song first of autoplay's (seen on the test account,
+ * 04/11: "Brise du vent", dropped under an autoplay song, landed after the context's last, "K9").
+ * A local song goes anywhere.
*/
- fun swap(order: List<String>, from: String, to: String, heading: String, canLeaveQueue: Boolean): List<String>? {
+ fun swap(
+ order: List<String>, from: String, to: String, heading: String, canLeaveQueue: Boolean,
+ block: (String) -> Int = { 0 },
+ ): List<String>? {
val fi = order.indexOf(from); val ti = order.indexOf(to)
if (fi < 0 || ti < 0 || fi == ti || from == heading) return null
val r = order.toMutableList().apply { add(ti, removeAt(fi)) }
if (!canLeaveQueue && from in queuedPart(order, heading) && from !in queuedPart(r, heading)) return null
+ if (!canLeaveQueue && inOrder(nextPart(order, heading), block) && !inOrder(nextPart(r, heading), block)) return null
return r
}
+ /** The context's songs, then autoplay's: never mixed. */
+ private fun inOrder(part: List<String>, block: (String) -> Int) = part.zipWithNext().all { (a, b) -> block(a) <= block(b) }
+
/** What a drop asks of the engine. */
sealed interface Move {
data object None : Move
@@ -36,7 +47,7 @@ internal object QueueDragMath {
* The engine call for [dragged] going from [before] to [after]: named by the row it now
* follows (or, first of its part, the row it now precedes), as SongPlayer's moves take it.
*/
- fun moveFor(before: List<String>, after: List<String>, dragged: String, heading: String): Move {
+ fun moveFor(before: List<String>, after: List<String>, dragged: String, heading: String, block: (String) -> Int = { 0 }): Move {
if (before == after) return Move.None
val wasQueued = dragged in queuedPart(before, heading)
val q = queuedPart(after, heading)
@@ -47,6 +58,8 @@ internal object QueueDragMath {
val prev = part.getOrNull(j - 1); val next = part.getOrNull(j + 1)
return when {
nowQueued && !wasQueued -> if (prev != null) Move.IntoQueue(prev, true) else Move.IntoQueue(next, false)
+ // First of autoplay's songs: named by the one it precedes, not by the context's last.
+ !nowQueued && prev != null && next != null && block(prev) != block(dragged) && block(next) == block(dragged) -> Move.Beside(next, false)
prev != null -> Move.Beside(prev, true)
next != null -> Move.Beside(next, false)
else -> Move.None
diff --git a/base/app/src/main/res/values-de/myspoti_strings.xml b/base/app/src/main/res/values-de/myspoti_strings.xml
index 41404ff0..770a8388 100644
--- a/base/app/src/main/res/values-de/myspoti_strings.xml
+++ b/base/app/src/main/res/values-de/myspoti_strings.xml
@@ -782,4 +782,5 @@
<string name="settings_sources_detail">Welche Erweiterung zuerst abspielt und herunterlädt</string>
<string name="settings_sources_none">Noch keine Erweiterung spielt oder lädt Musik: Spotify spielt alles. Füge eine unter Erweiterungen hinzu.</string>
<string name="ext_settings_header">Einstellungen der Erweiterungen</string>
+ <string name="queue_autoplay_heading">Autoplay</string>
</resources>
diff --git a/base/app/src/main/res/values-el/myspoti_strings.xml b/base/app/src/main/res/values-el/myspoti_strings.xml
index 78e69adb..ab13d0ff 100644
--- a/base/app/src/main/res/values-el/myspoti_strings.xml
+++ b/base/app/src/main/res/values-el/myspoti_strings.xml
@@ -782,4 +782,5 @@
<string name="settings_sources_detail">Ποια επέκταση παίζει και κατεβάζει πρώτη</string>
<string name="settings_sources_none">Καμία επέκταση δεν παίζει ή κατεβάζει μουσική ακόμα: το Spotify παίζει τα πάντα. Πρόσθεσε μία στις Επεκτάσεις.</string>
<string name="ext_settings_header">Ρυθμίσεις επεκτάσεων</string>
+ <string name="queue_autoplay_heading">Αυτόματη αναπαραγωγή</string>
</resources>
diff --git a/base/app/src/main/res/values-es/myspoti_strings.xml b/base/app/src/main/res/values-es/myspoti_strings.xml
index cbd0b712..b37c760d 100644
--- a/base/app/src/main/res/values-es/myspoti_strings.xml
+++ b/base/app/src/main/res/values-es/myspoti_strings.xml
@@ -782,4 +782,5 @@
<string name="settings_sources_detail">Qué extensión reproduce y descarga primero</string>
<string name="settings_sources_none">Ninguna extensión reproduce ni descarga música todavía: Spotify lo reproduce todo. Añade una en Extensiones.</string>
<string name="ext_settings_header">Ajustes de las extensiones</string>
+ <string name="queue_autoplay_heading">Reproducción automática</string>
</resources>
diff --git a/base/app/src/main/res/values-fr/myspoti_strings.xml b/base/app/src/main/res/values-fr/myspoti_strings.xml
index 37299560..356bb985 100644
--- a/base/app/src/main/res/values-fr/myspoti_strings.xml
+++ b/base/app/src/main/res/values-fr/myspoti_strings.xml
@@ -787,4 +787,5 @@
<string name="settings_sources_detail">Quelle extension lit et télécharge en premier</string>
<string name="settings_sources_none">Aucune extension ne lit ni ne télécharge encore : Spotify lit tout. Ajoutes-en une dans Extensions.</string>
<string name="ext_settings_header">Réglages des extensions</string>
+ <string name="queue_autoplay_heading">Lecture automatique</string>
</resources>
diff --git a/base/app/src/main/res/values-it/myspoti_strings.xml b/base/app/src/main/res/values-it/myspoti_strings.xml
index ed73abd3..48b25a90 100644
--- a/base/app/src/main/res/values-it/myspoti_strings.xml
+++ b/base/app/src/main/res/values-it/myspoti_strings.xml
@@ -782,4 +782,5 @@
<string name="settings_sources_detail">Quale estensione riproduce e scarica per prima</string>
<string name="settings_sources_none">Nessuna estensione riproduce o scarica musica: Spotify riproduce tutto. Aggiungine una in Estensioni.</string>
<string name="ext_settings_header">Impostazioni delle estensioni</string>
+ <string name="queue_autoplay_heading">Riproduzione automatica</string>
</resources>
diff --git a/base/app/src/main/res/values-pl/myspoti_strings.xml b/base/app/src/main/res/values-pl/myspoti_strings.xml
index b3d5d0cd..956fb4e8 100644
--- a/base/app/src/main/res/values-pl/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pl/myspoti_strings.xml
@@ -797,4 +797,5 @@
<string name="settings_sources_detail">Które rozszerzenie odtwarza i pobiera jako pierwsze</string>
<string name="settings_sources_none">Żadne rozszerzenie nie odtwarza ani nie pobiera jeszcze muzyki: Spotify odtwarza wszystko. Dodaj je w Rozszerzeniach.</string>
<string name="ext_settings_header">Ustawienia rozszerzeń</string>
+ <string name="queue_autoplay_heading">Autoodtwarzanie</string>
</resources>
diff --git a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
index d10f48d4..fc4d2cf0 100644
--- a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
@@ -761,4 +761,5 @@
<string name="settings_sources_none">Nenhuma extensão toca ou baixa música ainda: o Spotify toca tudo. Adicione uma em Extensões.</string>
<string name="ext_settings_header">Configurações das extensões</string>
<string name="lyrics_romanize">Romanizar</string>
+ <string name="queue_autoplay_heading">Reprodução automática</string>
</resources>
diff --git a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
index fc447989..ce906f6e 100644
--- a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
@@ -728,4 +728,5 @@
<string name="settings_sources_detail">Que extensão reproduz e transfere primeiro</string>
<string name="settings_sources_none">Nenhuma extensão reproduz ou transfere música ainda: o Spotify reproduz tudo. Adiciona uma em Extensões.</string>
<string name="ext_settings_header">Definições das extensões</string>
+ <string name="queue_autoplay_heading">Reprodução automática</string>
</resources>
diff --git a/base/app/src/main/res/values-pt/myspoti_strings.xml b/base/app/src/main/res/values-pt/myspoti_strings.xml
index 459ca0d3..2e1786a1 100644
--- a/base/app/src/main/res/values-pt/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt/myspoti_strings.xml
@@ -782,4 +782,5 @@
<string name="settings_sources_detail">Qual extensão toca e baixa primeiro</string>
<string name="settings_sources_none">Nenhuma extensão toca ou baixa música ainda: o Spotify toca tudo. Adicione uma em Extensões.</string>
<string name="ext_settings_header">Configurações das extensões</string>
+ <string name="queue_autoplay_heading">Reprodução automática</string>
</resources>
diff --git a/base/app/src/main/res/values-ru/myspoti_strings.xml b/base/app/src/main/res/values-ru/myspoti_strings.xml
index afb99aa6..089ac9eb 100644
--- a/base/app/src/main/res/values-ru/myspoti_strings.xml
+++ b/base/app/src/main/res/values-ru/myspoti_strings.xml
@@ -797,4 +797,5 @@
<string name="settings_sources_detail">Какое расширение воспроизводит и скачивает первым</string>
<string name="settings_sources_none">Пока ни одно расширение не воспроизводит и не скачивает музыку: всё играет Spotify. Добавьте его в разделе «Расширения».</string>
<string name="ext_settings_header">Настройки расширений</string>
+ <string name="queue_autoplay_heading">Автовоспроизведение</string>
</resources>
diff --git a/base/app/src/main/res/values-zh/myspoti_strings.xml b/base/app/src/main/res/values-zh/myspoti_strings.xml
index 2d1d33c6..272cb356 100644
--- a/base/app/src/main/res/values-zh/myspoti_strings.xml
+++ b/base/app/src/main/res/values-zh/myspoti_strings.xml
@@ -782,4 +782,5 @@
<string name="settings_sources_detail">优先播放和下载的扩展</string>
<string name="settings_sources_none">还没有扩展播放或下载音乐:全部由 Spotify 播放。可在“扩展”中添加。</string>
<string name="ext_settings_header">扩展设置</string>
+ <string name="queue_autoplay_heading">自动播放</string>
</resources>
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index 36959564..98921864 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -789,4 +789,5 @@
<string name="settings_sources_detail">Which extension plays and downloads first</string>
<string name="settings_sources_none">No extension plays or downloads music yet: Spotify plays everything. Add one in Extensions.</string>
<string name="ext_settings_header">Extension settings</string>
+ <string name="queue_autoplay_heading">Autoplay</string>
</resources>
diff --git a/base/app/src/test/java/com/music/spotui/ui/immersive/lyrics/QueueDragMathTest.kt b/base/app/src/test/java/com/music/spotui/ui/immersive/lyrics/QueueDragMathTest.kt
index 3673606f..6bf53500 100644
--- a/base/app/src/test/java/com/music/spotui/ui/immersive/lyrics/QueueDragMathTest.kt
+++ b/base/app/src/test/java/com/music/spotui/ui/immersive/lyrics/QueueDragMathTest.kt
@@ -61,6 +61,47 @@ class QueueDragMathTest {
assertNull(QueueDragMath.swap(order, H, "n0", H, canLeaveQueue = true))
}
+ /**
+ * Spotify's reorderQueue within "next from", as seen on the test account (04/11): exact inside
+ * the context's songs (c..) and inside autoplay's (a..); asked across, a context song goes to the
+ * context's end, an autoplay song first of autoplay's.
+ */
+ private fun spotifyReorder(next: List<String>, moved: String, target: String, after: Boolean): List<String> {
+ val rest = next - moved
+ val auto = { x: String -> x.startsWith("a") }
+ val i = when {
+ auto(moved) == auto(target) -> rest.indexOf(target) + if (after) 1 else 0
+ auto(moved) -> rest.indexOfFirst(auto).let { if (it < 0) rest.size else it }
+ else -> rest.indexOfFirst(auto).let { if (it < 0) rest.size else it }
+ }
+ return rest.toMutableList().apply { add(i, moved) }
+ }
+
+ @Test fun everyDropOfASpotifySongEndsInThePanelsOrder() {
+ val block = { k: String -> if (k.startsWith("a")) 1 else 0 }
+ var checked = 0; var refused = 0
+ for (nc in 0..4) for (na in 0..4) {
+ val next = (0 until nc).map { "c$it" } + (0 until na).map { "a$it" }
+ if (next.isEmpty()) continue
+ val before = listOf(H) + next
+ for (dragged in next) for (to in next) {
+ val after = QueueDragMath.swap(before, dragged, to, H, canLeaveQueue = false, block = block)
+ if (after == null) { if (dragged != to) refused++; continue }
+ val move = QueueDragMath.moveFor(before, after, dragged, H, block)
+ assertTrue("drag $dragged to $to: $move", move is QueueDragMath.Move.Beside)
+ move as QueueDragMath.Move.Beside
+ assertEquals("drag $dragged to $to: $move", QueueDragMath.nextPart(after, H), spotifyReorder(next, dragged, move.target, move.after))
+ checked++
+ }
+ }
+ assertTrue(checked > 50 && refused > 10)
+ // The reported case: a context song under an autoplay one is not a place it can go.
+ assertNull(QueueDragMath.swap(listOf(H, "c0", "c1", "a0", "a1"), "c0", "a0", H, canLeaveQueue = false, block = block))
+ // An autoplay song moved first of autoplay's is named by the song it now precedes.
+ assertEquals(QueueDragMath.Move.Beside("a0", false),
+ QueueDragMath.moveFor(listOf(H, "c0", "a0", "a1"), listOf(H, "c0", "a1", "a0"), "a1", H, block))
+ }
+
@Test fun theEngineCallNamesTheRowBeforeElseTheRowAfter() {
val before = listOf("q0", "q1", H, "n0", "n1")
val m = QueueDragMath.Move.IntoQueue("q1", true)