Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Myspoti's shuffle no longer cuts the song: a new order waits for the next song
kota-zofe9d7f762964
diff --git a/base/app/src/main/java/com/music/spotui/di/AppShuffle.kt b/base/app/src/main/java/com/music/spotui/di/AppShuffle.ktindex d19dd551..a376b37a 100644--- a/base/app/src/main/java/com/music/spotui/di/AppShuffle.kt+++ b/base/app/src/main/java/com/music/spotui/di/AppShuffle.kt@@ -55,8 +55,14 @@ object AppShuffle {private var startedAt = 0L/** Spotify's shuffle came back on once and was turned off again. */private var retried = false- /** Shuffle turned on or off while paused: the song goes on in its new order at the next play. */+ /**+ * Shuffle turned on or off: playing a list in a new order means playing it again, heard as a cut+ * or a jump back to the start (reported). So the song goes on untouched and the order changes+ * when the next song starts, or at the next play when it was paused.+ */private var due: (() -> Boolean)? = null+ /** The song [due] waits out; null: it waits for the next play. */+ private var dueAfter: String? = null/** Lists Spotify keeps on its own shuffle for this account, this session. */private val imposed = java.util.Collections.newSetFromMap(java.util.concurrent.ConcurrentHashMap<String, Boolean>())@@ -149,12 +155,23 @@ object AppShuffle {/** Shuffle changed while paused: what [due] holds plays at the next play (SongPlayer.play). True: done here. */fun resume(): Boolean {- val d = synchronized(this) { due.also { due = null } } ?: return false+ val d = synchronized(this) { dueAfter = null; due.also { due = null } } ?: return falsereturn d()}private fun whenPlaying(block: () -> Boolean) {- if (SongPlayer.isPlaying()) block() else synchronized(this) { due = block }+ val after = SongPlayer.nowPlaying.value.uri.takeIf { SongPlayer.isPlaying() }+ synchronized(this) { due = block; dueAfter = after }+ }++ /** The page reports [uri]: the song [due] waited out is over, the new order plays from this one. */+ private fun songMoved(uri: String) {+ val d = synchronized(this) {+ if (due == null || !ShuffleOrder.songOver(dueAfter, uri, TRACK)) return+ dueAfter = null+ due.also { due = null }+ } ?: return+ main.post { d() }}/**@@ -175,7 +192,7 @@ object AppShuffle {val picked = kept?.let { (o, a) -> ShuffleOrder.pick(o, a, first) }val dealt = picked ?: (ShuffleOrder.draw(uris, first) to 0)list = uri; order = dealt.first; at = dealt.second; plain = back- landed = same; startedAt = SystemClock.elapsedRealtime(); retried = false; due = null+ landed = same; startedAt = SystemClock.elapsedRealtime(); retried = false; due = null; dueAfter = nulldealt}val song = songs.getValue(dealt[from])@@ -195,6 +212,7 @@ object AppShuffle {* the list (the player shows shuffle on).*/fun onPage(contextUri: String, uri: String, shuffle: Boolean, trusted: Boolean): Boolean {+ songMoved(uri)var told: String? = nullvar restore = falseval mine = synchronized(this) {@@ -249,7 +267,7 @@ object AppShuffle {private fun clearLocked() {gen.incrementAndGet()- list = null; order = emptyList(); at = -1; plain = null; landed = false; due = null+ list = null; order = emptyList(); at = -1; plain = null; landed = false; due = null; dueAfter = null}/** Every song of the list [contextUri] (all its pages), null when it cannot be read in full. */diff --git a/base/app/src/main/java/com/music/spotui/di/ShuffleOrder.kt b/base/app/src/main/java/com/music/spotui/di/ShuffleOrder.ktindex 8078ed41..434eee91 100644--- a/base/app/src/main/java/com/music/spotui/di/ShuffleOrder.kt+++ b/base/app/src/main/java/com/music/spotui/di/ShuffleOrder.kt@@ -8,6 +8,10 @@ import kotlin.random.Random* the song playing in the order.*/internal object ShuffleOrder {+ /** [now] playing means [waitedOut] is over: another song, an ad in between does not count. */+ fun songOver(waitedOut: String?, now: String, songPrefix: String): Boolean =+ waitedOut != null && now != waitedOut && now.startsWith(songPrefix)+/** Fisher-Yates over [songs]; [first] (one of them) opens the order. */fun <T> draw(songs: List<T>, first: T?, random: Random = Random.Default): List<T> {val order = songs.toMutableList()diff --git a/base/app/src/test/java/com/music/spotui/di/ShuffleOrderTest.kt b/base/app/src/test/java/com/music/spotui/di/ShuffleOrderTest.ktindex af7048e4..2eeefa90 100644--- a/base/app/src/test/java/com/music/spotui/di/ShuffleOrderTest.kt+++ b/base/app/src/test/java/com/music/spotui/di/ShuffleOrderTest.kt@@ -1,6 +1,7 @@package com.music.spotui.diimport org.junit.Assert.assertEquals+import org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Test@@ -58,4 +59,12 @@ class ShuffleOrderTest {// Nothing changed: the same order.assertEquals(order to 2, ShuffleOrder.refresh(order, 2, order.reversed(), Random(1)))}++ @Test fun aNewOrderWaitsForTheNextSongNotForAnAd() {+ val song = "spotify:track:"+ assertFalse(ShuffleOrder.songOver(song + "a", song + "a", song))+ assertFalse(ShuffleOrder.songOver(song + "a", "spotify:ad:x", song))+ assertTrue(ShuffleOrder.songOver(song + "a", song + "b", song))+ assertFalse(ShuffleOrder.songOver(null, song + "b", song))+ }}