Skip to content

Native Android client for Spotify, built with Jetpack Compose

Pagination: advance by raw row count, not mapped rows

liliapkfb0e547d99f4
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt
index 23a6f3e8..5b90e245 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt
@@ -91,9 +91,11 @@ internal fun playlist(d: JsonElement?): Playlist? {
/** Search tab pages: `{items[], totalCount, pagingInfo{nextOffset}}`. */
internal fun <T> page(node: JsonElement?, offset: Int, map: (JsonElement) -> T?): Page<T> {
- val items = node.list("items").mapNotNull(map)
+ val rows = node.list("items")
+ val items = rows.mapNotNull(map)
val total = node.int("totalCount") ?: items.size
- val next = node.int("pagingInfo.nextOffset") ?: (offset + items.size).takeIf { items.isNotEmpty() && it < total }
+ // Raw row count, as in SpotifyLibrary: unmappable rows still occupy positions.
+ val next = node.int("pagingInfo.nextOffset") ?: (offset + rows.size).takeIf { rows.isNotEmpty() && it < total }
return Page(items, offset, total, next)
}
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 b0b4567e..35b02061 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
@@ -71,7 +71,8 @@ class SpotifyLibrary(private val bridge: Bridge, private val gql: Gql) : Library
"flatten" to false, "expandedFolders" to emptyList<String>(),
"folderUri" to folderUri, "includeFoldersWhenFlattening" to true,
)).entity("data.me.libraryV3", "libraryV3")
- val items = lib.list("items").mapNotNull { row ->
+ val rows = lib.list("items")
+ val items = rows.mapNotNull { row ->
val c = card(row.at("item")) ?: return@mapNotNull null
val d = row.at("item.data")
val kind = if (d.typename() == "Folder") MediaKind.FOLDER else c.kind
@@ -82,12 +83,15 @@ class SpotifyLibrary(private val bridge: Bridge, private val gql: Gql) : Library
)
}
val total = lib.int("totalCount") ?: items.size
- return Page(items, offset, total, (offset + items.size).takeIf { items.isNotEmpty() && it < total })
+ // Advance by the raw row count: rows that do not map (unknown kinds) still
+ // occupy positions, or the next page overlaps and rows repeat.
+ return Page(items, offset, total, (offset + rows.size).takeIf { rows.isNotEmpty() && it < total })
}
override suspend fun likedTracks(offset: Int, limit: Int): Page<SavedTrack> {
val page = gql.query("fetchLibraryTracks", vars("offset" to offset, "limit" to limit)).at("data.me.library.tracks")
- val items = page.list("items").mapNotNull { row ->
+ val rows = page.list("items")
+ val items = rows.mapNotNull { row ->
val data = row.at("track.data")
val withUri = if (data.str("uri") == null) row.str("track._uri")?.let { u ->
kotlinx.serialization.json.JsonObject((data as kotlinx.serialization.json.JsonObject) + ("uri" to JsonPrimitive(u)))
@@ -95,7 +99,7 @@ class SpotifyLibrary(private val bridge: Bridge, private val gql: Gql) : Library
track(withUri)?.let { SavedTrack(it, row.str("addedAt.isoString")) }
}
val total = page.int("totalCount") ?: items.size
- return Page(items, offset, total, (offset + items.size).takeIf { items.isNotEmpty() && it < total })
+ return Page(items, offset, total, (offset + rows.size).takeIf { rows.isNotEmpty() && it < total })
}
override suspend fun pin(uri: Uri) { gql.query("pinLibraryItem", vars("pinnableItemUri" to uri)); changed() }