Skip to content

Native Android client for Spotify, built with Jetpack Compose

The playlist pickers ("Add to playlist", "Saved in", adding an album or a phone file) show the last list at once and read it again behind it, the library walked again for covers only when a playlist without one is new, a list that cannot be read now keeping the last one, and all of it forgotten at sign-out: a minute of "Loading playlists" each time a song was saved (#145)

kota-zod12600257031
diff --git a/base/app/src/main/java/com/music/spotui/data/api/EditablePlaylists.kt b/base/app/src/main/java/com/music/spotui/data/api/EditablePlaylists.kt
index ddec9e91..d0f88ae9 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/EditablePlaylists.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/EditablePlaylists.kt
@@ -25,6 +25,28 @@ object EditablePlaylists {
@Volatile private var idsAt = 0L
private const val IDS_TTL_MS = 5 * 60_000L
+ /**
+ * The pickers' last lists ([ALL] for [loadWithCovers], a folder's uri or [ROOT] for [level]):
+ * shown at once while they are read again. Reported: a minute of "Loading playlists" each
+ * time a song was saved to a playlist (#145).
+ */
+ private val shown = java.util.concurrent.ConcurrentHashMap<String, List<EditablePlaylist>>()
+ /** Playlist uri → the library's cover, kept so the library is walked again only for a new playlist. */
+ private val covers = java.util.concurrent.ConcurrentHashMap<String, String>()
+ private const val ALL = "all"
+ private const val ROOT = "root"
+
+ /** What [loadWithCovers] gave last, or null. */
+ fun cachedWithCovers(): List<EditablePlaylist>? = shown[ALL]
+
+ /** What [level] gave last for [folderUri], or null. */
+ fun cachedLevel(folderUri: String?): List<EditablePlaylist>? = shown[folderUri ?: ROOT]
+
+ /** Sign-out: nothing of the account's playlists is kept. */
+ fun clear() {
+ shown.clear(); covers.clear(); ids = null
+ }
+
suspend fun ids(): Set<String> {
val now = System.currentTimeMillis()
ids?.takeIf { now - idsAt < IDS_TTL_MS }?.let { return it }
@@ -43,10 +65,11 @@ object EditablePlaylists {
* mosaic of its albums); the library has it, so those are read from there, folders walked.
*/
suspend fun loadWithCovers(): List<EditablePlaylist> {
- val list = load()
- if (list.all { it.imageUrl != null }) return list
- val covers = libraryCovers()
+ // Not read (offline, the page reloading): the last list rather than none.
+ val list = load().ifEmpty { return shown[ALL].orEmpty() }
+ if (list.any { it.imageUrl == null && !covers.containsKey("spotify:playlist:${it.id}") }) covers.putAll(libraryCovers())
return list.map { if (it.imageUrl != null) it else it.copy(imageUrl = covers["spotify:playlist:${it.id}"]) }
+ .also { shown[ALL] = it }
}
/** Playlist uri → cover, as the library shows them, folders included. */
@@ -78,7 +101,7 @@ object EditablePlaylists {
*/
suspend fun level(folderUri: String?): List<EditablePlaylist> {
val api = MyspotiRuntime.api
- val editable = runCatching { api.playlists.editable() }.getOrNull()?.associateBy { it.uri } ?: return load()
+ val editable = runCatching { api.playlists.editable() }.getOrNull()?.associateBy { it.uri } ?: return shown[folderUri ?: ROOT] ?: load()
val items = ArrayList<app.myspoti.api.LibraryItem>()
runCatching {
var offset = 0
@@ -87,7 +110,7 @@ object EditablePlaylists {
items += page.items
offset = page.nextOffset ?: return@runCatching
}
- }.onFailure { if (items.isEmpty()) return if (folderUri == null) load() else emptyList() }
+ }.onFailure { if (items.isEmpty()) return shown[folderUri ?: ROOT] ?: if (folderUri == null) load() else emptyList() }
return items.mapNotNull { item ->
when (item.kind) {
MediaKind.FOLDER -> EditablePlaylist(item.uri, item.name, item.count ?: 0, null, folderUri = item.uri)
@@ -97,6 +120,6 @@ object EditablePlaylists {
}
else -> null
}
- }
+ }.also { shown[folderUri ?: ROOT] = it }
}
}
diff --git a/base/app/src/main/java/com/music/spotui/di/SessionActions.kt b/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
index 13939c77..9f73c74c 100644
--- a/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
+++ b/base/app/src/main/java/com/music/spotui/di/SessionActions.kt
@@ -44,6 +44,7 @@ object SessionActions {
SongPlayer.clearSession()
// The desktop token a Sonos login kept, and the way that logged one in.
com.music.spotui.connect.ConnectCode.clear(app)
+ com.music.spotui.data.api.EditablePlaylists.clear()
// The last track, likes, albums and followed artists of older builds are never read
// again: cleared too, so no ids of this account outlive it.
listOf("LibraryCache", "RecentItems", "PlaybackState", "LikedSongs",
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/AddCollectionToPlaylistSheet.kt b/base/app/src/main/java/com/music/spotui/ui/components/AddCollectionToPlaylistSheet.kt
index 061eb9cc..56d6940c 100644
--- a/base/app/src/main/java/com/music/spotui/ui/components/AddCollectionToPlaylistSheet.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/components/AddCollectionToPlaylistSheet.kt
@@ -56,7 +56,7 @@ fun AddCollectionToPlaylistSheet(sourceUri: String, title: String, onDismiss: ()
val context = LocalContext.current
val scope = rememberCoroutineScope()
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
- var playlists by remember { mutableStateOf<List<EditablePlaylist>?>(null) }
+ var playlists by remember { mutableStateOf(EditablePlaylists.cachedWithCovers()) }
var query by remember { mutableStateOf("") }
var reading by remember { mutableStateOf(false) }
var asking by remember { mutableStateOf<Pair<EditablePlaylist, MergePlan>?>(null) }
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/AddToPlaylistSheet.kt b/base/app/src/main/java/com/music/spotui/ui/components/AddToPlaylistSheet.kt
index 11c2606c..e2b1884e 100644
--- a/base/app/src/main/java/com/music/spotui/ui/components/AddToPlaylistSheet.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/components/AddToPlaylistSheet.kt
@@ -71,7 +71,8 @@ fun AddToPlaylistSheet(
excludePlaylistId: String? = null,
) {
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
- var playlists by remember { mutableStateOf<List<EditablePlaylist>?>(null) }
+ // The last list at once, read again behind it.
+ var playlists by remember { mutableStateOf(EditablePlaylists.cachedWithCovers()) }
var query by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
// Folders flattened: adding to a playlist does not need the folder tree.
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/SavedInSheet.kt b/base/app/src/main/java/com/music/spotui/ui/components/SavedInSheet.kt
index 636554d2..d1984fa7 100644
--- a/base/app/src/main/java/com/music/spotui/ui/components/SavedInSheet.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/components/SavedInSheet.kt
@@ -95,8 +95,9 @@ fun SavedInSheet(
val folder = folders.lastOrNull()
LaunchedEffect(folder?.first) {
- // Only playlists the user can change (theirs and collaborative ones), level by level.
- playlists = null
+ // Only playlists the user can change (theirs and collaborative ones), level by level: the
+ // last list of the level at once, read again behind it.
+ playlists = com.music.spotui.data.api.EditablePlaylists.cachedLevel(folder?.first)
playlists = com.music.spotui.data.api.EditablePlaylists.level(folder?.first)
}
androidx.activity.compose.BackHandler(enabled = folders.isNotEmpty()) { folders.removeAt(folders.lastIndex) }
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/LibraryExtras.kt b/base/app/src/main/java/com/music/spotui/ui/screens/LibraryExtras.kt
index 171f81dc..bbae5547 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/LibraryExtras.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/LibraryExtras.kt
@@ -519,7 +519,7 @@ fun LocalPlaylistScreen(navController: NavController, id: String) {
@Composable
fun AddLocalToPlaylistSheet(track: LocalTrack, onDismiss: () -> Unit) {
val context = LocalContext.current
- var spotifyLists by remember { mutableStateOf<List<com.music.spotui.data.api.EditablePlaylist>?>(null) }
+ var spotifyLists by remember { mutableStateOf(com.music.spotui.data.api.EditablePlaylists.cachedWithCovers()) }
var creating by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
spotifyLists = com.music.spotui.data.api.EditablePlaylists.loadWithCovers()