Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Repeat after a play taken back from another device and at a cold start: a play of ours newer than the watcher's last read of the element plays here, so the page's first report (its own repeat off) is set again instead of taken for the other device's choice, and a report before the element was ever read sets nothing until that read says this phone is the one playing
kota-zo982f91f67e13
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/RepeatRules.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/RepeatRules.ktindex a879ca13..a3d7202d 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/RepeatRules.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/RepeatRules.kt@@ -26,22 +26,25 @@ object RepeatRules {THEIRS,/** Left as it is: the page only mirrors, its repeat is not the user's. */IGNORED,+ /** Would be set again, but where the page plays is not read yet: kept, judged at that read. */+ WAIT,}/*** [held]: our own set is not answered yet. [afterOwnPlay]: the state is the first of a play or* a page of ours (it starts on its own options). [atLoop]: the song (or the list) is coming* round again, see [atLoop]. [elsewhere]: another device plays, and nothing is ever set on it- * from here. [mirrored]: the page only mirrors.+ * from here; null while not known (the element not read yet since the app started), when+ * nothing is set either. [mirrored]: the page only mirrors.*/- fun onReport(wanted: Repeat, reported: Repeat, held: Boolean, afterOwnPlay: Boolean, atLoop: Boolean, elsewhere: Boolean, mirrored: Boolean): Report = when {+ fun onReport(wanted: Repeat, reported: Repeat, held: Boolean, afterOwnPlay: Boolean, atLoop: Boolean, elsewhere: Boolean?, mirrored: Boolean): Report = when {reported == wanted || held -> Report.KEPT- elsewhere -> Report.THEIRS- afterOwnPlay -> Report.SET_AGAIN+ elsewhere == true -> Report.THEIRS+ afterOwnPlay -> if (elsewhere == null) Report.WAIT else Report.SET_AGAINmirrored -> Report.IGNORED// Less repeat away from the loop is a change from another device (or the page's own word// after a skip): theirs. At the loop it is the page forgetting it.- atLoop && reported < wanted -> Report.SET_AGAIN+ atLoop && reported < wanted -> if (elsewhere == null) Report.WAIT else Report.SET_AGAINelse -> Report.THEIRS}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.ktindex 295c4a86..a839bcab 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@@ -505,6 +505,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop/** The media element as the watcher last read it. */@Volatile private var lastMedia: Media? = null+ /** When the watcher read [lastMedia] (its tick's start). */+ @Volatile private var lastMediaAt = 0L/*** Another device of the account is Spotify Connect's active one: this page's element stands@@ -652,7 +654,9 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScoppageSuspect = falseval media = read.getOrNull()lastMedia = media+ lastMediaAt = now_playsElsewhere.value = remoteActive+ if (media != null) repeatOnRead()val s = _state.valuepreloadNextSong(s, media)tickCrossfade(s, media, now)@@ -2070,12 +2074,14 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop* with the chosen repeat while the page is being told it.*/private fun followRepeat(s: PlayerState): PlayerState {+ repeatUnread = nullval want = wantedRepeat ?: return sif (s.item == null || s.repeat == want) return sval now = System.currentTimeMillis()val afterPlay = now < repeatRestoreUntil+ val elsewhere = elsewhere()val how = RepeatRules.onReport(want, s.repeat, held = now < repeatHeldUntil, afterOwnPlay = afterPlay,- atLoop = RepeatRules.atLoop(want, lastTrackState, s, now), elsewhere = remoteActive, mirrored = mirroring)+ atLoop = RepeatRules.atLoop(want, lastTrackState, s, now), elsewhere = elsewhere, mirrored = mirroring)when (how) {RepeatRules.Report.SET_AGAIN -> {repeatRestoreUntil = 0L@@ -2086,16 +2092,49 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop}RepeatRules.Report.THEIRS -> {wantedRepeat = s.repeat- bridge.logger?.invoke("repeat is ${s.repeat} from the page${if (remoteActive) " (another device plays)" else ""}: $want no longer kept")+ bridge.logger?.invoke("repeat is ${s.repeat} from the page${if (elsewhere == true) " (another device plays)" else ""}: $want no longer kept")}+ RepeatRules.Report.WAIT -> repeatUnread = s.repeatRepeatRules.Report.KEPT, RepeatRules.Report.IGNORED -> {}}- return if (how == RepeatRules.Report.KEPT || how == RepeatRules.Report.SET_AGAIN) s.copy(repeat = want) else s+ return if (how == RepeatRules.Report.KEPT || how == RepeatRules.Report.SET_AGAIN || how == RepeatRules.Report.WAIT) s.copy(repeat = want) else s+ }++ /**+ * Another device plays, for the repeat: no for a play of ours since the watcher's last read,+ * which plays here and is newer than the element it read (seen in review: the first report of a+ * play taken back from the desktop, repeat off, was taken for the desktop's); null while the+ * element was never read (a cold start).+ */+ private fun elsewhere(): Boolean? = when {+ lastPlay?.let { it.third >= lastMediaAt } == true -> false+ lastMedia == null -> null+ else -> remoteActive+ }++ /** The repeat a page reported while [elsewhere] was not known ([RepeatRules.Report.WAIT]), judged at the watcher's read. */+ @Volatile private var repeatUnread: Repeat? = null++ /**+ * [repeatUnread] once the element is read: this page playing is told the choice again. Not+ * while another device plays (a page with no active device reads the same): nothing is sent,+ * and the choice is kept for a play here, that device's next report judged as any other.+ */+ private fun repeatOnRead() {+ val reported = repeatUnread ?: return+ repeatUnread = null+ val want = wantedRepeat ?: return+ if (elsewhere() != false) return+ repeatRestoreUntil = 0L+ repeatHeldUntil = System.currentTimeMillis() + RepeatRules.REPEAT_HOLD_MS+ bridge.logger?.invoke("repeat is $reported before the element was read, $want was chosen: set again")+ scope.launch { runCatching { setRepeat(want) } }}/*** The repeat mode chosen in an earlier run of the app: nothing is sent now. The page starts- * with repeat off, so the mode is set again on the first song it reports, as after a play.+ * with repeat off, so the mode is set again on the first song it reports once this phone is+ * known to be the one playing, as after a play.*/fun restoreRepeat(mode: Repeat) {if (wantedRepeat != null) return@@ -2105,6 +2144,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScopoverride suspend fun setRepeat(mode: Repeat) {wantedRepeat = mode+ repeatUnread = nullrepeatHeldUntil = System.currentTimeMillis() + RepeatRules.REPEAT_HOLD_MSbridge.call("player.setRepeat", buildJsonObject { put("mode", mode.ordinal) })}diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.ktindex f99c886e..648609a2 100644--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt@@ -150,7 +150,9 @@ class PlaybackFollowUpTest {player.restoreRepeat(Repeat.CONTEXT)host.post("""{"type":"ready"}""")host.post(state(track("A"), repeat = 0))- waitFor("repeat set again") { called("player.setRepeat") == 1 }+ // Sent once the watcher read the element (this phone the one playing, not another device):+ // at its first tick, up to WATCH_IDLE_TICK_MS after it started.+ waitFor("repeat set again", ms = 10_000) { called("player.setRepeat") == 1 }assertEquals("{\"mode\":1}", host.calls.last { it.first == "player.setRepeat" }.second)}diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatOneTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatOneTest.ktindex a01d9260..da4e3089 100644--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatOneTest.kt+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatOneTest.kt@@ -150,4 +150,40 @@ class RepeatOneTest {assertEquals(0, called("player.setRepeat"), "nothing sent to the other device: ${host.calls.map { it.first }}")assertEquals(Repeat.OFF, player.state.value?.repeat)}++ @Test fun `a play here taking the music back from another device is given the repeat chosen`() = runBlocking {+ host.mediaActive = false+ host.mediaPaused = true+ host.post("""{"type":"ready"}""")+ host.post(state(track("A"), repeat = 0))+ waitFor("another device seen", ms = 10_000) { player.remoteActive }+ player.restoreRepeat(Repeat.TRACK)+ // The play takes the music here; the page's first report (its own options) comes before+ // the watcher reads the element again, which still says another device.+ player.play("spotify:playlist:C", trackUri = "spotify:track:B")+ host.post(state(track("B"), repeat = 0))+ host.mediaActive = true+ host.mediaPaused = false+ waitFor("repeat set again") { called("player.setRepeat") > 0 }+ assertEquals(Repeat.TRACK, player.state.value?.repeat)+ }++ @Test fun `a report before the element is read gives another device playing no repeat`() = runBlocking {+ host.mediaActive = false+ host.mediaPaused = true+ player.restoreRepeat(Repeat.TRACK)+ host.post("""{"type":"ready"}""")+ host.post(state(track("A"), repeat = 0))+ waitFor("another device seen", ms = 10_000) { player.remoteActive }+ delay(400)+ assertEquals(0, called("player.setRepeat"), "nothing sent to the other device: ${host.calls.map { it.first }}")+ }++ @Test fun `a report before the element is read is given the repeat once this page is seen playing`() = runBlocking {+ player.restoreRepeat(Repeat.TRACK)+ host.post("""{"type":"ready"}""")+ host.post(state(track("A"), repeat = 0))+ waitFor("repeat set again", ms = 10_000) { called("player.setRepeat") > 0 }+ assertEquals(1, called("player.setRepeat"))+ }}diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatRulesTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatRulesTest.ktindex a159007b..f631b72a 100644--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatRulesTest.kt+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/RepeatRulesTest.kt@@ -21,7 +21,7 @@ class RepeatRulesTest {speed = 1.0, shuffle = false, repeat = Repeat.OFF, restrictions = PlayerRestrictions())private fun report(wanted: Repeat, reported: Repeat, held: Boolean = false, afterOwnPlay: Boolean = false, atLoop: Boolean = false,- elsewhere: Boolean = false, mirrored: Boolean = false) =+ elsewhere: Boolean? = false, mirrored: Boolean = false) =RepeatRules.onReport(wanted, reported, held, afterOwnPlay, atLoop, elsewhere, mirrored)@Test fun `less repeat at the loop is the page forgetting it, set again`() {@@ -44,6 +44,12 @@ class RepeatRulesTest {assertEquals(Report.THEIRS, report(Repeat.TRACK, Repeat.OFF, afterOwnPlay = true, elsewhere = true))}+ @Test fun `where the page plays not read yet, nothing is set and the choice is kept`() {+ assertEquals(Report.WAIT, report(Repeat.TRACK, Repeat.OFF, afterOwnPlay = true, elsewhere = null))+ assertEquals(Report.WAIT, report(Repeat.TRACK, Repeat.OFF, atLoop = true, elsewhere = null))+ assertEquals(Report.THEIRS, report(Repeat.TRACK, Repeat.OFF, elsewhere = null))+ }+@Test fun `a set of ours not answered yet, the same repeat or a mirror leave the choice alone`() {assertEquals(Report.KEPT, report(Repeat.TRACK, Repeat.OFF, held = true))assertEquals(Report.KEPT, report(Repeat.TRACK, Repeat.OFF, held = true, elsewhere = true))