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.

Read playlists through the Web API, and keep them

Lelonio03ac4bea7470
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 65b47c5..774f5d4 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -110,7 +110,8 @@
"Bash(git commit -q -m 'Re-read a playlist when it is reopened *)",
"Bash(git commit -q -m 'Load whole playlists, a batch at a time *)",
"Bash(PATH=/Users/emanuele/.cargo/bin:__TRACKED_VAR__ cargo check --quiet)",
- "Bash(git commit -q -m 'Cap concurrent track lookups and retry the dropped ones *)"
+ "Bash(git commit -q -m 'Cap concurrent track lookups and retry the dropped ones *)",
+ "Bash(git commit -q -m 'Read playlists through the Web API, and keep them *)"
]
}
}
diff --git a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
index 82fbefe..92db97c 100644
--- a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
+++ b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
@@ -2,27 +2,27 @@ C/C++ Structured Logl
j
h/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  ����3  ����3i
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ����3i
g
-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json  ����3� ����3n
+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json  �����3� ����3n
l
-j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json  ����3� �����3[
+j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json  �����3� �����3[
Y
-W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja  ����3�� ����3_
+W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja  �����3�� ����3_
]
-[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt  ����3d
+[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt  �����3d
b
-`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt  ����3 @ ����3e
+`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt  �����3 @ ����3e
c
-a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json  ����3�� ����3i
+a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json  �����3�� ����3i
g
-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin  ����3 �" ����3o
+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin  �����3 �" ����3o
m
-k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt  ����3
+k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt  �����3
� ����3b
`
-^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json  ����3  ( ����3g
+^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json  �����3  ( ����3g
e
-c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt  ����3  b ����3D
+c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt  �����3  b ����3D
B
-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  ����3 � �Ȅ��3
\ No newline at end of file
+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
diff --git a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
index 234cd51..b223c0e 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
@@ -318,6 +318,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
// The detail screen holds a list resolved before this track
// was in it; if that is the playlist just written to, read
// it again.
+ invalidateContext(playlist.uri)
if (_playlist.value.uri == playlist.uri) openPlaylist(playlist)
}
.onFailure {
@@ -593,21 +594,25 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
fun openContext(uri: String, name: String, artworkUrl: String? = null) =
openPlaylist(CatalogPlaylist(uri = uri, name = name, artworkUrl = artworkUrl))
+ /**
+ * Track lists already resolved, keyed by context URI.
+ *
+ * Reopening a playlist used to re-resolve it from scratch, which on a
+ * thousand-track playlist is a minute of watching rows appear. The list is
+ * held for the session and shown immediately; a refresh runs behind it and
+ * replaces it only when it has the whole thing, so reopening never takes a
+ * finished list away and rebuilds it in front of the user.
+ */
+ private val contextCache = object : LinkedHashMap<String, List<CatalogTrack>>(0, 0.75f, true) {
+ override fun removeEldestEntry(eldest: Map.Entry<String, List<CatalogTrack>>) =
+ size > CONTEXT_CACHE_SIZE
+ }
+
fun openPlaylist(playlist: CatalogPlaylist) {
// Reopening one counts as opening it, and that is exactly the playlist
// the home page should keep at the front.
container.playlistOrder.record(playlist.uri)
- // Reopening what is already loaded re-reads it rather than showing the
- // copy in memory. It used to return here, which is how a track added
- // from the player could be genuinely in the playlist on Spotify and
- // missing from this screen: the list had been resolved before the track
- // existed and nothing ever asked again.
- //
- // The tracks already on screen stay up while the reload runs, so this
- // costs a round trip and shows no spinner.
- val cached = _playlist.value.takeIf { it.uri == playlist.uri }?.tracks.orEmpty()
-
val kind = kindOf(playlist.uri)
playlistJob?.cancel()
val base = PlaylistState(
@@ -616,14 +621,17 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
artworkUrl = playlist.artworkUrl,
kind = kind,
)
+
+ val cached = contextCache[playlist.uri].orEmpty()
_playlist.value = base.copy(tracks = cached, loading = cached.isEmpty())
+
playlistJob = viewModelScope.launch {
runCatching {
if (kind == DetailKind.ARTIST) {
val (tracks, albums) = loadArtist(playlist.uri)
_playlist.value = base.copy(tracks = tracks, albums = albums)
} else {
- loadContextInto(base, playlist.uri)
+ loadContextInto(base, playlist.uri, showProgress = cached.isEmpty())
}
}
.onFailure {
@@ -636,39 +644,89 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
}
}
+ /** Drops a context from the cache so the next open re-reads it. */
+ private fun invalidateContext(uri: String) {
+ contextCache.remove(uri)
+ }
+
/**
- * Resolves a whole playlist or album, publishing it as it arrives.
+ * Resolves a whole playlist or album.
*
- * The list used to stop at the first hundred tracks, which is not a length
- * anyone's playlists respect, and a track added from the player landed past
- * the cut and looked like it had not been added at all.
+ * Two sources, and which one is used matters more than anything else on this
+ * screen. The Web API returns a hundred *complete* tracks per request, so a
+ * twelve-hundred-track playlist is a dozen requests. The access point
+ * answers with URIs and then charges one round trip per track to turn each
+ * into a name — the same playlist is twelve hundred lookups, minutes of
+ * waiting, and enough of them get dropped that the total came out different
+ * on every open. So the Web API is the path whenever the user has connected
+ * their application, and the access point is the fallback for when they have
+ * not.
*
- * In batches rather than in one call because each track is its own
- * access-point lookup: the native side runs a batch concurrently, so asking
- * for two thousand at once would open two thousand requests and get itself
- * throttled. A batch at a time keeps that bounded and has the useful side
- * effect that the screen fills from the top while the rest is still coming.
+ * @param showProgress publish each page as it arrives. Off when there is a
+ * cached list on screen: a finished list must not be replaced by a
+ * growing one.
*/
- private suspend fun loadContextInto(base: PlaylistState, uri: String) {
- val uris = Catalog.contextTrackUris(uri)
- if (uris.isEmpty()) {
- _playlist.value = base
- return
+ private suspend fun loadContextInto(base: PlaylistState, uri: String, showProgress: Boolean) {
+ val viaWebApi = runCatching { webApiTracks(base, uri, showProgress) }
+ .onFailure { android.util.Log.w(TAG, "web api playlist read failed: ${describe(it)}") }
+ .getOrNull()
+
+ val tracks = viaWebApi ?: accessPointTracks(base, uri, showProgress)
+
+ contextCache[uri] = tracks
+ _playlist.value = base.copy(tracks = tracks, loadingMore = false)
+ }
+
+ /** Null when this is not something the Web API can page through. */
+ private suspend fun webApiTracks(
+ base: PlaylistState,
+ uri: String,
+ showProgress: Boolean,
+ ): List<CatalogTrack>? {
+ if (!container.webApi.isReady || !uri.startsWith("spotify:playlist:")) return null
+ val id = uri.substringAfterLast(':')
+
+ val loaded = mutableListOf<CatalogTrack>()
+ var offset = 0
+ while (true) {
+ val page = container.api.playlistTracks(id, limit = WEB_API_PAGE, offset = offset)
+ // Episodes and delisted tracks come back as a null track.
+ loaded += page.items.mapNotNull { it.track?.toCatalogTrack() }
+ offset += page.items.size
+ if (showProgress) {
+ _playlist.value = base.copy(
+ tracks = loaded.toList(),
+ loadingMore = offset < page.total,
+ )
+ }
+ if (page.items.isEmpty() || offset >= page.total) return loaded
}
+ }
+
+ private suspend fun accessPointTracks(
+ base: PlaylistState,
+ uri: String,
+ showProgress: Boolean,
+ ): List<CatalogTrack> {
+ val uris = Catalog.contextTrackUris(uri)
+ if (uris.isEmpty()) return emptyList()
val batches = uris.chunked(METADATA_BATCH)
val loaded = mutableListOf<CatalogTrack>()
batches.forEachIndexed { index, batch ->
loaded += Catalog.tracks(batch)
- _playlist.value = base.copy(
- tracks = loaded.toList(),
- // Counted in batches, not in tracks. Comparing the two totals
- // left "more coming" on forever whenever anything was dropped —
- // a delisted or region-locked track resolves to nothing, so the
- // list is legitimately shorter than the URIs asked for.
- loadingMore = index < batches.lastIndex,
- )
+ if (showProgress) {
+ _playlist.value = base.copy(
+ tracks = loaded.toList(),
+ // Counted in batches, not in tracks: a delisted track
+ // resolves to nothing, so the list is legitimately shorter
+ // than the URIs asked for and comparing the two totals left
+ // "more coming" on forever.
+ loadingMore = index < batches.lastIndex,
+ )
+ }
}
+ return loaded
}
/**
@@ -746,6 +804,12 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
/** Tracks resolved per access-point round trip; see loadContextInto. */
const val METADATA_BATCH = 100
+ /** The Web API's own maximum page size for playlist tracks. */
+ const val WEB_API_PAGE = 100
+
+ /** How many track lists to keep resolved; see contextCache. */
+ const val CONTEXT_CACHE_SIZE = 8
+
const val SEARCH_DEBOUNCE_MS = 350L
/** Spotify acknowledges a transfer before it has taken effect. */