Skip to content

Native Android client for Spotify, built with Jetpack Compose

The queue survives an ad got past by reloading the player: the songs the user queued are taken before the reload and added again to the new page's queue in their order, but for the one played on and those it lists already, instead of being lost with the old page (#144)

kota-zo087961a58672
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt
index f6a6a279..37770234 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/AdRecoveryRules.kt
@@ -1,5 +1,7 @@
package app.myspoti.spotify
+import app.myspoti.api.Uri
+
/**
* How [SpotifyPlayer] gets past an ad and what plays on a new page, without the page.
*
@@ -72,4 +74,15 @@ object AdRecoveryRules {
/** After our own reload: whether the player plays a song there itself (else the mirror does). */
fun playsAfterOwnReload(mirrored: Boolean): Boolean = !mirrored
+
+ /**
+ * The songs of the user's queue [queued] (before our reload) to add again to the new page's:
+ * all in their order, but for the one now [playing] (played on from the queue) and those the
+ * page lists already ([has]). Reported: the queue was gone after an ad got past by a reload (#144).
+ */
+ fun requeued(queued: List<Uri>, playing: Uri?, has: Set<Uri>): List<Uri> {
+ // Played on from the queue: that song and the ones before it in the queue are done.
+ val from = queued.indexOf(playing).let { if (it >= 0) it + 1 else 0 }
+ return queued.drop(from).filter { it !in has }
+ }
}
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
index a839bcab..a725421d 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
@@ -336,6 +336,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
// (the new page's is a new station, see RepeatRules.stationOrder).
val nextUp = s?.takeIf { RepeatRules.isStation(it.contextUri) && !it.shuffle && lastOrder?.first != it.contextUri }
?.let { _queue.value?.nextUp }
+ // The songs the user queued: the new page has no queue, they are added to it again (#144).
+ val queued = _queue.value?.queued.orEmpty().filter { it.kind == ItemKind.TRACK || it.kind == ItemKind.EPISODE }
ownReload = true
try {
runCatching {
@@ -358,6 +360,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
}
if (next) playOnNewPage { playOn(s, nextUp) } else { awaitDeviceJoined(); resumeAfterReload(s, nextUp) }
}
+ if (queued.isNotEmpty() && AdRecoveryRules.playsAfterOwnReload(mirroring)) runCatching { requeue(queued) }
+ .onFailure { bridge.logger?.invoke("page back after a reload: the queue was not added again (${it.message})") }
} finally {
ownReload = false
playingAtLoss = null
@@ -454,6 +458,21 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
/** Between our reload's start and its new page's "ready": a pick then is held ([playAsked]). */
@Volatile private var reloadUnderWay = false
+ /**
+ * [queued] (the user's queue before a reload of ours) added again to the new page's queue, but
+ * for the song now playing (played on from the first of them) and those the page has already.
+ */
+ private suspend fun requeue(queued: List<NowPlaying>) {
+ // The queue the page reports after the song it plays on.
+ delay(REQUEUE_WAIT_MS)
+ val playing = _state.value?.item?.uri
+ val has = _queue.value?.queued.orEmpty().map { it.uri }.toSet()
+ val missing = AdRecoveryRules.requeued(queued.map { it.uri }, playing, has)
+ if (missing.isEmpty()) return
+ bridge.logger?.invoke("page back after a reload: ${missing.size} queued songs added to the queue again")
+ addToQueue(missing)
+ }
+
/**
* A new page after [lost] was playing: the page restores the session paused, or not at
* all, and takes commands only once its device is registered again. The song goes on from
@@ -2274,6 +2293,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
const val AD_MUTE_MAX_MS = 75_000L
/** A reloaded page reports its restored state within that long. */
const val RESTORE_WAIT_MS = 6_000L
+ /** After a play on a new page, how long its queue is given to be reported before the user's is added again. */
+ const val REQUEUE_WAIT_MS = 2_000L
/** How long a play on a new page waits for its device to join Spotify Connect (see [awaitDeviceJoined]). */
const val JOIN_WAIT_MS = 5_000L
/** How long after a play on a new page its DEVICE_NOT_FOUND is waited for (seen 0.5 s after). */
diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt
index f5c4f17a..ff124eba 100644
--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt
+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/AdRecoveryRulesTest.kt
@@ -40,4 +40,15 @@ class AdRecoveryRulesTest {
assertEquals(PageBack.INTENT, AdRecoveryRules.pageBack(ownReload = false, mirrored = false, pickHeld = false, lost = false, intent = true))
assertEquals(PageBack.NOTHING, AdRecoveryRules.pageBack(ownReload = false, mirrored = false, pickHeld = false, lost = false, intent = false))
}
+
+ @Test fun theUsersQueueIsAddedAgainAfterOurReload() {
+ val q = listOf("t:a", "t:b", "t:c")
+ // Played on from the queue's first song: the others come again, in their order.
+ assertEquals(listOf("t:b", "t:c"), AdRecoveryRules.requeued(q, playing = "t:a", has = emptySet()))
+ // The song before the ad played on (not from the queue): all of it.
+ assertEquals(q, AdRecoveryRules.requeued(q, playing = "t:x", has = emptySet()))
+ // What the new page lists already is not added twice.
+ assertEquals(listOf("t:c"), AdRecoveryRules.requeued(q, playing = "t:a", has = setOf("t:b")))
+ assertEquals(emptyList<String>(), AdRecoveryRules.requeued(q, playing = "t:c", has = emptySet()))
+ }
}