Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Remember what is in a playlist across a restart

Leloniod2d5b1a1f66c
diff --git a/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt b/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt
index f1d41d8..8c06d28 100644
--- a/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt
+++ b/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt
@@ -30,6 +30,16 @@ class ContextCacheStore(context: Context) {
/** Spotify's stamp for the version this was read from; null for albums. */
val snapshotId: String? = null,
val savedAt: Long = 0,
+ /**
+ * What this is a copy of.
+ *
+ * The file is named after a hash of it, which cannot be read back, and
+ * something has to be able to tell a playlist from an album without
+ * being told which URI to look for; see [tracksInPlaylists]. Empty for
+ * anything written before this was carried, which simply means it is
+ * skipped there until it is read again.
+ */
+ val uri: String = "",
)
private val dir = File(context.applicationContext.cacheDir, DIR_NAME)
@@ -40,21 +50,62 @@ class ContextCacheStore(context: Context) {
if (!file.exists()) return@withContext null
// A half-written or outdated file is not worth a crash: dropping it
// costs one reload, and the next save repairs it.
- runCatching { json.decodeFromString(Entry.serializer(), file.readText()) }
+ val entry = runCatching { json.decodeFromString(Entry.serializer(), file.readText()) }
.onFailure { file.delete() }
.getOrNull()
?.takeIf { it.savedAt > System.currentTimeMillis() - MAX_AGE_MS }
+ ?: return@withContext null
+
+ // Written before the entry carried what it is a copy of. Filled in
+ // here rather than left for the next full read: a playlist whose
+ // stored version is still current is never written again, so it would
+ // stay nameless for as long as it stays unchanged, and
+ // [tracksInPlaylists] would go on skipping it.
+ if (entry.uri.isEmpty()) {
+ val named = entry.copy(uri = uri)
+ runCatching { file.writeText(json.encodeToString(Entry.serializer(), named)) }
+ return@withContext named
+ }
+ entry
}
suspend fun write(uri: String, tracks: List<CatalogTrack>, snapshotId: String?) =
withContext(Dispatchers.IO) {
if (tracks.isEmpty()) return@withContext
dir.mkdirs()
- val entry = Entry(tracks, snapshotId, System.currentTimeMillis())
+ val entry = Entry(tracks, snapshotId, System.currentTimeMillis(), uri)
runCatching { fileFor(uri).writeText(json.encodeToString(Entry.serializer(), entry)) }
trim()
}
+ /**
+ * Every track in every playlist this device has read and still holds.
+ *
+ * For the tick in the player, which says a track is already in one of the
+ * listener's playlists. Spotify has no endpoint for that question — see
+ * MainViewModel.inPlaylists — so the answer can only be what the app has
+ * seen, and until now "seen" meant since the app was last started: reopening
+ * it turned every tick back into a plus, on a queue it had just restored
+ * from the same disk this reads.
+ *
+ * Albums are left out on purpose. Being on a record is not being in a list
+ * the listener made.
+ */
+ suspend fun tracksInPlaylists(): Set<String> = withContext(Dispatchers.IO) {
+ val files = dir.listFiles().orEmpty()
+ val cutoff = System.currentTimeMillis() - MAX_AGE_MS
+ buildSet {
+ for (file in files) {
+ val entry = runCatching {
+ json.decodeFromString(Entry.serializer(), file.readText())
+ }.getOrNull() ?: continue
+ if (entry.savedAt <= cutoff) continue
+ if (!entry.uri.startsWith("spotify:playlist:")) continue
+ entry.tracks.forEach { add(it.uri) }
+ }
+ }
+ }
+
suspend fun remove(uri: String) = withContext(Dispatchers.IO) {
fileFor(uri).delete()
Unit
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
index 3621e6b..e5e0b37 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -1467,8 +1467,9 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
// a heart rather than a tick.
if (contextUri.endsWith(":collection")) {
_liked.value = _liked.value + tracks.map { it.uri }
- // Just read, so nothing missing from it is saved.
- likedSeeded = true
+ // Just read, so nothing missing from it is saved. The seeding
+ // below is left to run: it is the tick's half that this says
+ // nothing about.
likedListTrusted = true
return
}
@@ -1498,8 +1499,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
private val likedPending = linkedSetOf<String>()
private var likedBatch: kotlinx.coroutines.Job? = null
- /** Whether the copy of Liked Songs on disk has been looked at yet. */
- private var likedSeeded = false
+ /** Whether what is on disk has been looked at yet, for both marks. */
+ private var membershipSeeded = false
/**
* Whether that copy is recent enough to answer for itself.
@@ -1513,7 +1514,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
fun checkLiked(uri: String?) {
if (uri == null || !uri.startsWith("spotify:track:")) return
viewModelScope.launch {
- seedLiked()
+ seedMembership()
if (uri in _liked.value || likedListTrusted) return@launch
if (!likedAsked.add(uri)) return@launch
@@ -1530,19 +1531,37 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
}
}
- /** Fills the set from the copy of Liked Songs already on disk, at no cost. */
- private suspend fun seedLiked() {
- if (likedSeeded) return
- likedSeeded = true
+ /**
+ * Fills both marks from what is already on disk, at no cost in requests.
+ *
+ * Both used to start every run empty. The heart then asked Spotify about
+ * each track as it played, and the tick — which nobody can be asked about —
+ * simply went missing: a song from one of the listener's own playlists
+ * showed a plus until that playlist happened to be opened again, even
+ * though the queue it was playing from had just been restored from this
+ * same disk.
+ */
+ private suspend fun seedMembership() {
+ if (membershipSeeded) return
+ membershipSeeded = true
- val uri = runCatching { withContext(Dispatchers.IO) { NativeBridge.collectionUri() } }
+ val collection = runCatching { withContext(Dispatchers.IO) { NativeBridge.collectionUri() } }
.getOrNull()
.orEmpty()
- if (uri.isEmpty()) return
+ val liked = collection.takeIf { it.isNotEmpty() }
+ ?.let { contextCache[it] ?: container.contextCache.read(it) }
+ if (liked != null) {
+ _liked.value = _liked.value + liked.tracks.map { it.uri }
+ // Never downwards: this run may already have read the list itself,
+ // which is fresher than anything on disk.
+ likedListTrusted = likedListTrusted ||
+ System.currentTimeMillis() - liked.savedAt < LIKED_TRUSTED_MS
+ }
- val entry = contextCache[uri] ?: container.contextCache.read(uri) ?: return
- _liked.value = _liked.value + entry.tracks.map { it.uri }
- likedListTrusted = System.currentTimeMillis() - entry.savedAt < LIKED_TRUSTED_MS
+ val inPlaylists = runCatching { container.contextCache.tracksInPlaylists() }
+ .onFailure { android.util.Log.i(TAG, "no playlists remembered: ${describe(it)}") }
+ .getOrDefault(emptySet())
+ if (inPlaylists.isNotEmpty()) _inPlaylists.value = _inPlaylists.value + inPlaylists
}
/**