Skip to content

Native Android client for Spotify, built with Jetpack Compose

A Spotify session lost without a sign-out (an anonymous token, Spotify's sign-in page back) forgets whose account it was: the profile is read again as soon as the next session is ready, and the likes kept for later wait for that account's id before anything is sent, so another account signing in in the same run never receives the first one's likes, nor gets its downloads (claimed under the new account's id, the first one's put aside); what the sign-out does is unchanged and nothing kept by account is removed [skip ci]

kota-zo5be5ebcec189
diff --git a/base/app/src/main/java/app/myspoti/MainActivity.kt b/base/app/src/main/java/app/myspoti/MainActivity.kt
index 319194c7..4e05701f 100644
--- a/base/app/src/main/java/app/myspoti/MainActivity.kt
+++ b/base/app/src/main/java/app/myspoti/MainActivity.kt
@@ -354,6 +354,9 @@ class MainActivity : AppCompatActivity() {
finish()
return
}
+ // Whose session this is, read now when not known (a session lost before may have been
+ // another account's): the likes kept for later and the downloads go by it.
+ com.music.spotui.data.api.ProfileCache.ensure(applicationContext)
// The player reports ready on every page load: only a change is a session being established.
if (!signedIn || showingLogin || connecting) {
Logbook.log("session established: player shown")
@@ -481,7 +484,11 @@ class MainActivity : AppCompatActivity() {
lifecycleScope.launch {
combine(api.ready, MyspotiRuntime.loginRequired) { ready, login -> ready to login }.collect { (ready, login) ->
when {
- login && !showingLogin && signedIn -> showLogin()
+ login && !showingLogin && signedIn -> {
+ // The session expired: whoever signs in on that page may be another account.
+ com.music.spotui.di.SessionActions.sessionLost()
+ showLogin()
+ }
ready && !login -> {
val user = runCatching { MyspotiRuntime.isSignedIn() }.getOrDefault(false)
if (user) {
@@ -495,6 +502,7 @@ class MainActivity : AppCompatActivity() {
prefs.edit().putBoolean("LoggedIn", false).apply()
signedIn = false
connecting = false
+ com.music.spotui.di.SessionActions.sessionLost()
showLogin()
}
}
diff --git a/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt b/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt
index dca01e94..2789af1c 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/ProfileCache.kt
@@ -23,6 +23,11 @@ object ProfileCache {
private val fetching = AtomicBoolean(false)
private val generation = java.util.concurrent.atomic.AtomicLong()
+ /**
+ * The account is no longer known: at sign-out, and when the session is lost without one
+ * (SessionActions.sessionLost), when another account may sign in next. A profile still being
+ * read is dropped, and read again for the session that follows.
+ */
fun clearSession() {
generation.incrementAndGet()
name = null
@@ -33,20 +38,39 @@ object ProfileCache {
fun ensure(context: Context) {
if (name != null) return
if (!fetching.compareAndSet(false, true)) return
- val requestGeneration = generation.get()
MyspotiRuntime.scope.launch {
try {
- MyspotiRuntime.api.ready.first { it }
- val me = runCatching { MyspotiRuntime.api.users.me() }.getOrNull() ?: return@launch
- if (generation.get() != requestGeneration) return@launch
- name = me.name
- imageUrl = me.images.best(160)?.url
- id = me.username.takeIf { it.isNotBlank() }
- // The downloads shown are this account's own (see OfflineAccounts).
- id?.let { com.music.spotui.data.offline.OfflineAccounts.claim(context.applicationContext, it) }
+ while (true) {
+ val requestGeneration = generation.get()
+ MyspotiRuntime.api.ready.first { it }
+ val me = runCatching { MyspotiRuntime.api.users.me() }.getOrNull()
+ // The session was lost meanwhile: this profile may be the account before, the next one's is read.
+ if (generation.get() != requestGeneration) continue
+ // No account (an anonymous token): nothing is kept, the next session's is read.
+ if (me == null || me.username.isBlank()) return@launch
+ name = me.name
+ imageUrl = me.images.best(160)?.url
+ id = me.username
+ // The downloads shown are this account's own (see OfflineAccounts).
+ id?.let { com.music.spotui.data.offline.OfflineAccounts.claim(context.applicationContext, it) }
+ return@launch
+ }
} finally {
fetching.set(false)
}
}
}
+
+ /**
+ * The signed-in account's id, read first when it is not known yet ([ensure]): waited for at most
+ * [timeoutMs] (offline, Spotify not answering), null then.
+ */
+ suspend fun awaitId(context: Context, timeoutMs: Long): String? {
+ id?.let { return it }
+ ensure(context)
+ return kotlinx.coroutines.withTimeoutOrNull(timeoutMs) {
+ while (id == null) kotlinx.coroutines.delay(100)
+ id
+ }
+ }
}
diff --git a/base/app/src/main/java/com/music/spotui/di/SessionActions.kt b/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
index 5fcc56aa..48cc6786 100644
--- a/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
+++ b/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
@@ -52,6 +52,20 @@ object SessionActions {
MyspotiRuntime.host.logout()
}
+ /**
+ * The Spotify session was lost without a sign-out (an anonymous token, Spotify's sign-in page
+ * back): whose account comes next is not known, maybe another one. What follows the account
+ * forgets it as a sign-out does (the profile, so ProfileCache.id is read again as soon as the
+ * next session is ready; the likes kept for later stay their account's, sent to it only), and
+ * nothing kept by account is removed: the downloads stay in place for the account read next
+ * (OfflineAccounts.claim puts another account's aside then). Nothing else of a sign-out is done.
+ */
+ fun sessionLost() {
+ Logbook.log("session lost without a sign-out: the account is read again at the next session")
+ com.music.spotui.data.api.ProfileCache.clearSession()
+ com.music.spotui.data.LibraryOutbox.signedOut()
+ }
+
/** The new account is signed in. */
fun signedIn() { _signedOut.value = false }
}
diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
index b6f2f22f..6fd5b4d3 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
@@ -2225,17 +2225,25 @@ object SongPlayer {
fun startLibraryOutbox(context: Context) {
com.music.spotui.data.LibraryOutbox.init(context, scope,
sender = { uri, on -> libraryLock.withLock { writeLibrary(uri, on) }; if (uri.startsWith(SPOTIFY_TRACK_PREFIX)) likedChanges.value++ },
- // The engine is started after the app's graph: wait for it, then for Spotify to answer.
- ready = { awaitEngine(); api.ready.first { it } },
+ // The engine is started after the app's graph: wait for it, then for Spotify to answer,
+ // then for the session's account (unknown after a session lost: it may be another one).
+ ready = { awaitEngine(); api.ready.first { it }; com.music.spotui.data.api.ProfileCache.awaitId(context, ACCOUNT_WAIT_MS) },
account = { com.music.spotui.data.api.ProfileCache.id },
onAvailable = { scope.launch { kotlinx.coroutines.delay(3_000); com.music.spotui.data.LibraryOutbox.request() } },
)
- // The Web Player ready again (a reload, a new sign-in): what waits is sent (nothing is asked when nothing waits).
- scope.launch { awaitEngine(); api.ready.collect { if (it) com.music.spotui.data.LibraryOutbox.request() } }
+ // The Web Player ready again (a reload, a new sign-in): what waits is sent (nothing is asked when nothing waits),
+ // once the session's account is read: after a session lost, what waits for it is told from what is not its own.
+ scope.launch {
+ awaitEngine()
+ api.ready.collect { if (it) { com.music.spotui.data.api.ProfileCache.awaitId(context, ACCOUNT_WAIT_MS); com.music.spotui.data.LibraryOutbox.request() } }
+ }
}
private suspend fun awaitEngine() { while (!MyspotiRuntime.isStarted) kotlinx.coroutines.delay(1_000) }
+ /** The library changes kept for later wait that long for the account's id (see ProfileCache.awaitId). */
+ private const val ACCOUNT_WAIT_MS = 10_000L
+
// ── Music video: Spotify's own clips are not played by this engine; the YouTube
// clips (MusicVideoController) are. These keep the player screen's contract. ──
data class VideoState(val available: Boolean = false, val on: Boolean = false, val ready: Boolean = false, val aspect: Float = 0f, val placed: Boolean = false)
diff --git a/base/app/src/test/java/com/music/spotui/data/LibraryOutboxTest.kt b/base/app/src/test/java/com/music/spotui/data/LibraryOutboxTest.kt
index d189d376..3b7d1a44 100644
--- a/base/app/src/test/java/com/music/spotui/data/LibraryOutboxTest.kt
+++ b/base/app/src/test/java/com/music/spotui/data/LibraryOutboxTest.kt
@@ -148,6 +148,29 @@ class LibraryOutboxTest {
assertEquals(2, rig.sent.size)
}
+ @Test fun aSessionLostWithoutSignOutSendsNothingOfTheAccountBeforeToTheNextOne() = test { rig ->
+ // Offline: A likes, nothing goes. A's session is lost (no sign-out): its id is no longer known.
+ rig.fail = { java.io.IOException("offline") }
+ rig.sync.put("spotify:track:a-like", true)
+ settle()
+ rig.sync.signedOut()
+ rig.account = null
+ rig.fail = { null }
+ // B's session is ready before its id is read: nothing of A's goes to it.
+ rig.sync.request()
+ settle()
+ assertEquals(emptyList<Any>(), rig.sent)
+ // B's id read: still nothing of A's, B's own like goes to B.
+ rig.account = "b"
+ rig.sync.request()
+ settle()
+ assertEquals(emptyList<Any>(), rig.sent)
+ rig.sync.put("spotify:track:b-like", true)
+ settle()
+ assertEquals(listOf(Triple("b", "spotify:track:b-like", true)), rig.sent)
+ assertEquals(1, LibraryOutbox.Book(rig.saved.last()).keptForOthers)
+ }
+
@Test fun roundsAskedTogetherSendEachChangeOnce() = test { rig ->
rig.hold = CompletableDeferred()
rig.sync.put("spotify:track:a", true)