Skip to content

Native Android client for Spotify, built with Jetpack Compose

Liked checks by batches of 50 (partial page answers were cached as not liked); the artist's liked page is liked by definition

kota-zo3ec1fafb91e3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3b9e25f..3ae0ee68 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@ Each release on GitHub shows its section below. The app shows it too when it off
- **Myspoti's buttons play on this phone again** when another device (the Spotify app on the same account, for instance) took playback without being picked in Myspoti: the music comes back here first, instead of driving that device (where skips, previous and volume were refused).
- **Faster artist pages and playlists**: an artist seen in the last minutes opens at once (and its releases page too), the discography and a playlist's first songs are asked for together with the page, and your liked songs load once per session instead of on every artist page.
- **Your liked songs by an artist** come first on its page, above the popular tracks, and open their own page (as in Spotify), where play and shuffle play only them.
+- **Hearts right on long lists**: liked songs of a long playlist (or of an artist's liked songs) no longer show without their heart; the check goes by batches of 50, and an incomplete answer is never kept as "not liked".
- **New languages**: German, Spanish, Portuguese and Greek, on every screen.
- **Music no longer stops for a minute**: a Web Player page that stops answering is reloaded at once, and playback goes on.
- **No more silent songs**: the sound comes back by itself if a skip or a muted ad left it at zero; after a skip it comes back as soon as the next song is there.
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt
index afba5b1c..00aaefaf 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt
@@ -40,13 +40,21 @@ class SpotifyLibrary(private val bridge: Bridge, private val gql: Gql) : Library
* playlist: playlists are asked to the rootlist (the followed / own playlists), the rest to
* the library, and the answers come back in the order of [uris].
*/
+ private companion object {
+ /** Items per library.contains call. */
+ const val CONTAINS_BATCH = 50
+ }
+
override suspend fun contains(uris: List<Uri>): List<Boolean> {
if (uris.isEmpty()) return emptyList()
val (lists, others) = uris.withIndex().partition { it.value.contains(":playlist:") }
val out = BooleanArray(uris.size)
- if (others.isNotEmpty()) {
- val flags = bridge.call("library.contains", uris(others.map { it.value }) as kotlinx.serialization.json.JsonObject) as JsonArray
- others.forEachIndexed { i, v -> out[v.index] = flags.getOrNull(i)?.jsonPrimitive?.boolean == true }
+ // In batches: the page answers a long list only in part, and a missing answer read as
+ // "not liked" showed liked songs without their heart (and was kept as such).
+ for (batch in others.chunked(CONTAINS_BATCH)) {
+ val flags = bridge.call("library.contains", uris(batch.map { it.value }) as kotlinx.serialization.json.JsonObject) as JsonArray
+ if (flags.size != batch.size) throw IllegalStateException("library.contains: ${flags.size} answers for ${batch.size} items")
+ batch.forEachIndexed { i, v -> out[v.index] = flags[i].jsonPrimitive.boolean }
}
if (lists.isNotEmpty()) {
val arg = JsonArray(lists.map { JsonPrimitive(it.value) })
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
index 34968b58..164d0f4a 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
@@ -139,7 +139,7 @@ class ContextViewModel(
recent[key] = first
// The hearts are known before the page opens.
val p = first.data
- if (p.type != "liked") SongPlayer.libraryContains(
+ if (p.type != "liked" && p.type != "liked-artist") SongPlayer.libraryContains(
p.tracks.mapNotNull { s -> s.spotifyTrackId.takeIf { it.isNotBlank() }?.let { "spotify:track:$it" } } + p.uri)
}
} catch (t: kotlinx.coroutines.CancellationException) {
@@ -221,7 +221,7 @@ class ContextViewModel(
}
private fun refreshSaved(p: ContextPageModel) = viewModelScope.launch {
- if (p.type == "liked") {
+ if (p.type == "liked" || p.type == "liked-artist") {
// Everything here is liked by definition.
savedUris.value = p.tracks.map { "spotify:track:${it.spotifyTrackId}" }.toSet()
return@launch
@@ -357,7 +357,7 @@ private fun ContextContent(navController: NavController, vm: ContextViewModel, p
val findable = p.type == "liked" || p.type == "playlist"
val all by remember(p) {
derivedStateOf {
- if (p.type == "liked") p.tracks.filter { SongPlayer.library["spotify:track:${it.spotifyTrackId}"] != false } else p.tracks
+ if (p.type == "liked" || p.type == "liked-artist") p.tracks.filter { SongPlayer.library["spotify:track:${it.spotifyTrackId}"] != false } else p.tracks
}
}
val rows by remember(p, find, sort) {