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.

Load whole playlists, a batch at a time

Lelonio190259a72bea
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index d976e7f..35820b6 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -107,7 +107,8 @@
"Bash(git commit -q -m 'Add a settings screen behind the account picture *)",
"Bash(git commit -q -m 'Credit the author in the settings *)",
"Bash(git commit -q -m 'Swap the player'\\\\''s heart for add-to-playlist *)",
- "Bash(git commit -q -m 'Re-read a playlist when it is reopened *)"
+ "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 *)"
]
}
}
diff --git a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
index 6f3a392..7dae8ba 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 7303c85..aea09ae 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
@@ -80,6 +80,14 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
val artworkUrl: String? = null,
val tracks: List<CatalogTrack> = emptyList(),
val loading: Boolean = false,
+ /**
+ * More of the list is still being resolved.
+ *
+ * Separate from [loading]: the first batch replaces the screen with a
+ * spinner, the rest arrive under a list the user is already reading and
+ * must not.
+ */
+ val loadingMore: Boolean = false,
val error: String? = null,
val kind: DetailKind = DetailKind.PLAYLIST,
/** Populated for artists only. */
@@ -586,9 +594,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
openPlaylist(CatalogPlaylist(uri = uri, name = name, artworkUrl = artworkUrl))
fun openPlaylist(playlist: CatalogPlaylist) {
- // Recorded before the early return: reopening what is already loaded
- // still counts as opening it, and that is exactly the playlist the home
- // page should keep at the front.
+ // 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
@@ -612,11 +619,13 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
_playlist.value = base.copy(tracks = cached, loading = cached.isEmpty())
playlistJob = viewModelScope.launch {
runCatching {
- if (kind == DetailKind.ARTIST) loadArtist(playlist.uri) else loadContext(playlist.uri)
- }
- .onSuccess { loaded ->
- _playlist.value = base.copy(tracks = loaded.first, albums = loaded.second)
+ if (kind == DetailKind.ARTIST) {
+ val (tracks, albums) = loadArtist(playlist.uri)
+ _playlist.value = base.copy(tracks = tracks, albums = albums)
+ } else {
+ loadContextInto(base, playlist.uri)
}
+ }
.onFailure {
android.util.Log.e(TAG, "detail load failed: ${chain(it)}", it)
// Only when there is nothing to show. A refresh that fails
@@ -627,9 +636,35 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
}
}
- /** Playlists and albums are both contexts the access point can resolve. */
- private suspend fun loadContext(uri: String): Pair<List<CatalogTrack>, List<SearchItem>> =
- Catalog.tracks(Catalog.contextTrackUris(uri).take(PLAYLIST_LIMIT)) to emptyList()
+ /**
+ * Resolves a whole playlist or album, publishing it as it arrives.
+ *
+ * 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.
+ *
+ * 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.
+ */
+ private suspend fun loadContextInto(base: PlaylistState, uri: String) {
+ val uris = Catalog.contextTrackUris(uri)
+ if (uris.isEmpty()) {
+ _playlist.value = base
+ return
+ }
+
+ val loaded = mutableListOf<CatalogTrack>()
+ uris.chunked(METADATA_BATCH).forEach { batch ->
+ loaded += Catalog.tracks(batch)
+ _playlist.value = base.copy(
+ tracks = loaded.toList(),
+ loadingMore = loaded.size < uris.size,
+ )
+ }
+ }
/**
* Top tracks and albums for an artist.
@@ -703,7 +738,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
const val POLL_INTERVAL_MS = 250L
/** Each track is its own access-point round trip. */
- const val PLAYLIST_LIMIT = 100
+ /** Tracks resolved per access-point round trip; see loadContextInto. */
+ const val METADATA_BATCH = 100
const val SEARCH_DEBOUNCE_MS = 350L
diff --git a/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt b/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
index 2eba7e9..bbb30af 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
@@ -238,6 +238,34 @@ fun PlaylistScreen(
}
}
}
+
+ // A long playlist arrives a batch at a time and this is the tail of
+ // it still coming. Deliberately quiet: the list above is already
+ // readable and playable, and a spinner in the middle of the screen
+ // would say otherwise.
+ if (state.loadingMore) {
+ item(contentType = "more") {
+ Row(
+ Modifier
+ .fillMaxWidth()
+ .padding(vertical = 18.dp),
+ horizontalArrangement = Arrangement.Center,
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ CircularProgressIndicator(
+ strokeWidth = 2.dp,
+ modifier = Modifier.size(16.dp),
+ color = MaterialTheme.colorScheme.onSurfaceVariant,
+ )
+ Text(
+ "Altri brani in arrivo",
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant,
+ modifier = Modifier.padding(start = 10.dp),
+ )
+ }
+ }
+ }
}
// Floating rather than a top bar: the list scrolls under it, so the