Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
After a relaunch, resuming plays the song in its playlist or album again, with its queue, not the song alone (extensions and downloads included)
kota-zo90eef3a95842
diff --git a/base/app/src/main/java/com/music/spotui/data/preferences/LastSession.kt b/base/app/src/main/java/com/music/spotui/data/preferences/LastSession.ktindex 30472dce..acc083ae 100644--- a/base/app/src/main/java/com/music/spotui/data/preferences/LastSession.kt+++ b/base/app/src/main/java/com/music/spotui/data/preferences/LastSession.kt@@ -4,7 +4,7 @@ import com.music.spotui.data.entity.SongsModelimport kotlinx.serialization.builtins.ListSerializerimport kotlinx.serialization.json.Json-// The last session's queue and the rule that decides who wins at launch, kept pure so they+// The last session's queue (and the list it was played in, kept beside it) and the rule that decides who wins at launch, kept pure so they// are tested without Android. The song itself lives in SongPlayer's "myspoti_last_track".object LastSession {@@ -25,6 +25,17 @@ object LastSession {return runCatching { json.decodeFromString(ListSerializer(SongsModel.serializer()), raw) }.getOrDefault(emptyList())}+ /**+ * The saved queue of the song [trackId]: [songs] from that song on, once each. The page's queue+ * can still list the song before it, and a queue read just after a skip belongs to the song+ * left: that one holds no [trackId] and is not saved with it. Empty when it does not hold it.+ */+ fun queueFor(trackId: String, songs: List<SongsModel>): List<SongsModel> {+ val at = songs.indexOfFirst { it.spotifyTrackId == trackId }+ if (trackId.isBlank() || at < 0) return emptyList()+ return songs.drop(at).distinctBy { it.spotifyTrackId.ifBlank { it.url } }+ }+/*** Spotify's state at launch is older than the session saved from the local player: it still* shows the song played before and must not replace it. A state stamped after the savediff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.ktindex 11d7cbe3..d24e009e 100644--- a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt@@ -411,7 +411,13 @@ object SongPlayer {positionMs = p.getLong("position", 0L)positionAt = SystemClock.elapsedRealtime()playing = false- _nowPlaying.value = NowPlaying(uri = uri)+ // The list it was played in, with its name and queue: a resume through the extensions plays+ // the song in it (alone, the list was lost at every launch).+ val contextUri = p.getString("contextUri", "").orEmpty()+ val contextName = p.getString("contextName", "").orEmpty()+ registerContextName(contextUri, contextName)+ currentContextUri = contextUri+ _nowPlaying.value = NowPlaying(uri = uri, contextUri = contextUri, contextName = contextName)state.updateSongState(coverUri = p.getString("cover", "").orEmpty(),title = title,@@ -422,29 +428,32 @@ object SongPlayer {album = p.getString("album", "").orEmpty(),)state.likeState.value = p.getBoolean("liked", false)- if (restoredLocal) {- val rest = com.music.spotui.data.preferences.LastSession.decodeQueue(p.getString("queue", null))- if (rest.isNotEmpty()) {- _queue.value = EngineQueue(- current = rest.first(),- nextUp = rest.drop(1).map { QueueItem(it, SPOTIFY_TRACK_PREFIX + it.spotifyTrackId, "", "") },- )- }+ // Spotify's own queue too: the page's replaces it once it reports.+ val rest = com.music.spotui.data.preferences.LastSession.decodeQueue(p.getString("queue", null))+ if (rest.isNotEmpty()) {+ _queue.value = EngineQueue(+ current = rest.first(),+ nextUp = rest.drop(1).map { QueueItem(it, SPOTIFY_TRACK_PREFIX + it.spotifyTrackId, "", "") },+ )}}/** Saved on track change and on play/pause (not on every state). */private fun saveLastTrack(- song: SongsModel, uri: String, liked: Boolean,+ song: SongsModel, uri: String, liked: Boolean, contextUri: String, contextName: String,source: String = com.music.spotui.data.preferences.LastSession.SOURCE_SPOTIFY, queue: List<SongsModel>? = null,) {- val key = "$uri|$playing|$liked|$source"+ val key = "$uri|$playing|$liked|$source|$contextUri"if (uri.isBlank() || key == savedLast) returnsavedLast = keysavedSource = source; savedAtMs = System.currentTimeMillis(); restoredLocal = false- lastTrackPrefs().edit()+ val p = lastTrackPrefs()+ // No queue known yet for this song: the one saved with it stays (a pause, a like), another song's goes.+ val kept = if (queue.isNullOrEmpty() && p.getString("uri", "") == uri) p.getString("queue", "").orEmpty() else null+ p.edit().putString("source", source).putLong("savedAt", savedAtMs)- .putString("queue", queue?.let(com.music.spotui.data.preferences.LastSession::encodeQueue).orEmpty())+ .putString("queue", kept ?: queue?.let(com.music.spotui.data.preferences.LastSession::encodeQueue).orEmpty())+ .putString("contextUri", contextUri).putString("contextName", contextName).putString("uri", uri).putString("title", song.title).putString("singer", song.singer).putString("album", song.album).putString("cover", song.coverUri).putLong("duration", durationMs).putLong("position", getCurrentPosition())@@ -733,7 +742,7 @@ object SongPlayer {album = song.album,)pushNotification(song, liked)- saveLastTrack(song, uri, liked)+ saveLastTrack(song, uri, liked, s.contextUri.orEmpty(), s.contextName.orEmpty(), queue = spotifyQueueOf(id))trackListen(uri, song, com.music.spotui.data.local.ListenHistory.Source.SPOTIFY, playing)// A song never asked about (the one restored at launch is not a change): its state is asked// once, not read as "not liked" until the next song. A failed ask is not repeated by every state.@@ -850,6 +859,24 @@ object SongPlayer {queued = items(q.queued),nextUp = items(q.nextUp),)+ saveSpotifyQueue()+ }++ /** The page's queue from the song [id] on, for the saved session (see [saveLastTrack]). */+ private fun spotifyQueueOf(id: String): List<SongsModel> {+ val q = _queue.value+ if (LocalPlayer.active.value || id.isBlank()) return emptyList()+ return com.music.spotui.data.preferences.LastSession.queueFor(id, listOfNotNull(q.current) + (q.queued + q.nextUp).map { it.song })+ }++ /** The queue arrives after the song's state: it is saved with the song once it is there. */+ private fun saveSpotifyQueue() {+ val uri = _nowPlaying.value.uri+ val queue = spotifyQueueOf(trackIdOf(uri)).ifEmpty { return }+ val p = lastTrackPrefs()+ if (savedSource != com.music.spotui.data.preferences.LastSession.SOURCE_SPOTIFY || p.getString("uri", "") != uri) return+ val encoded = com.music.spotui.data.preferences.LastSession.encodeQueue(queue)+ if (p.getString("queue", "") != encoded) p.edit().putString("queue", encoded).apply()}/**@@ -2854,7 +2881,8 @@ object SongPlayer {// An extension's stream or a download (it has a Spotify id): kept to come back to at launch.if (likeUri != null) {val rest = LocalPlayer.upcoming().map { localSong(it.second) }.filter { it.spotifyTrackId.isNotBlank() }- saveLastTrack(song.copy(coverUri = cover), likeUri, liked, com.music.spotui.data.preferences.LastSession.SOURCE_LOCAL, listOf(song) + rest)+ saveLastTrack(song.copy(coverUri = cover), likeUri, liked, LocalPlayer.contextUri, LocalPlayer.contextName,+ com.music.spotui.data.preferences.LastSession.SOURCE_LOCAL, listOf(song) + rest)}}}@@ -2872,7 +2900,9 @@ object SongPlayer {val downloads = store.list().associateBy { it.trackId }if (songs.firstOrNull()?.spotifyTrackId?.let(downloads::containsKey) != true) return falseval list = songs.mapNotNull { downloads[it.spotifyTrackId] }.map(store::toLocalTrack)- LocalPlayer.play(list, 0, startMs = getCurrentPosition(), context = null)+ // In the list it was played in, as the extensions' resume does.+ val np = _nowPlaying.value+ LocalPlayer.play(list, 0, startMs = getCurrentPosition(), context = np.contextName.ifBlank { null }, contextUri = np.contextUri)return true}diff --git a/base/app/src/test/java/com/music/spotui/di/LastSessionTest.kt b/base/app/src/test/java/com/music/spotui/di/LastSessionTest.ktindex 79681a09..dd95478d 100644--- a/base/app/src/test/java/com/music/spotui/di/LastSessionTest.kt+++ b/base/app/src/test/java/com/music/spotui/di/LastSessionTest.kt@@ -21,6 +21,15 @@ class LastSessionTest {assertTrue(LastSession.decodeQueue(null).isEmpty())}+ @Test fun theSavedQueueStartsAtItsSongAndHoldsOnlyItsOwn() {+ val page = listOf(song(1), song(2), song(3), song(2))+ // The page's queue still lists the song before; once each, from the song on.+ assertEquals(listOf(song(2), song(3)), LastSession.queueFor("id2", page))+ // A queue read just after a skip belongs to the song left: not saved with the new one.+ assertTrue(LastSession.queueFor("id9", page).isEmpty())+ assertTrue(LastSession.queueFor("", page).isEmpty())+ }+@Test fun olderPausedEngineStateDoesNotReplaceLocalSession() {assertTrue(LastSession.engineStateIsStale(LastSession.SOURCE_LOCAL, 2000, 1000, true))assertFalse(LastSession.engineStateIsStale(LastSession.SOURCE_LOCAL, 2000, 3000, true))