Skip to content

Native Android client for Spotify, built with Jetpack Compose

Library: a "By me" chip next to Playlists in both looks keeps the playlists the signed-in account owns (the owner comes with each playlist of the library list and is kept in the offline copy), folders and Liked Songs left out [skip ci]

kota-zo51e1b29bcc17
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 d0240135..9f491db8 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
@@ -94,6 +94,7 @@ class SpotifyLibrary(private val bridge: Bridge, private val gql: Gql) : Library
uri = if (kind == MediaKind.FOLDER) stableFolderUri(c.uri) else c.uri, kind = kind, name = c.title, subtitle = c.subtitle, images = c.images,
pinned = row.bool("pinned") ?: false, addedAt = row.str("addedAt.isoString"),
count = d.int("playlistCount")?.plus(d.int("folderCount") ?: 0) ?: d.int("count"),
+ ownerUri = d.str("ownerV2.data.uri"),
)
}
val total = lib.int("totalCount") ?: items.size
diff --git a/base/api/src/main/kotlin/app/myspoti/api/Media.kt b/base/api/src/main/kotlin/app/myspoti/api/Media.kt
index ef0ab7ce..991b1ba1 100644
--- a/base/api/src/main/kotlin/app/myspoti/api/Media.kt
+++ b/base/api/src/main/kotlin/app/myspoti/api/Media.kt
@@ -88,6 +88,8 @@ data class LibraryItem(
val addedAt: String?,
/** Items in a folder, tracks in Liked Songs. */
val count: Int? = null,
+ /** A playlist's owner (`spotify:user:…`), when the list says. */
+ val ownerUri: Uri? = null,
)
data class SavedTrack(val track: Track, val addedAt: String?)
diff --git a/base/app/src/main/java/com/music/spotui/data/api/Api.kt b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
index cb62d4c5..94448021 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/Api.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
@@ -1043,7 +1043,7 @@ class Api constructor(
)
MediaKind.PLAYLIST -> LibraryEntry(item.uri.substringAfterLast(':'), item.name,
context.getString(R.string.assisted_curation_search_playlist) + (item.subtitle?.let { " • $it" } ?: ""), cover(item.images), isPlaylist = true,
- uri = item.uri, pinned = item.pinned, addedAt = item.addedAt.orEmpty())
+ uri = item.uri, pinned = item.pinned, addedAt = item.addedAt.orEmpty(), ownerUri = item.ownerUri.orEmpty())
MediaKind.ALBUM -> LibraryEntry(item.uri.substringAfterLast(':'), item.name,
context.getString(R.string.album_type_album) + (item.subtitle?.let { " • $it" } ?: ""), cover(item.images), isPlaylist = false, artists = item.subtitle.orEmpty(),
uri = item.uri, pinned = item.pinned, addedAt = item.addedAt.orEmpty())
diff --git a/base/app/src/main/java/com/music/spotui/data/entity/LibraryEntry.kt b/base/app/src/main/java/com/music/spotui/data/entity/LibraryEntry.kt
index 11fce641..dfd334eb 100644
--- a/base/app/src/main/java/com/music/spotui/data/entity/LibraryEntry.kt
+++ b/base/app/src/main/java/com/music/spotui/data/entity/LibraryEntry.kt
@@ -23,6 +23,8 @@ data class LibraryEntry(
val pinned: Boolean = false,
/** When it was added to the library (ISO 8601), for "Recently added". */
val addedAt: String = "",
+ /** A playlist's owner (`spotify:user:…`), for the "By me" chip; empty when unknown. */
+ val ownerUri: String = "",
) {
val isFolder: Boolean get() = folderUri.isNotBlank()
}
diff --git a/base/app/src/main/java/com/music/spotui/data/local/LibraryChips.kt b/base/app/src/main/java/com/music/spotui/data/local/LibraryChips.kt
index e94353f8..782b184d 100644
--- a/base/app/src/main/java/com/music/spotui/data/local/LibraryChips.kt
+++ b/base/app/src/main/java/com/music/spotui/data/local/LibraryChips.kt
@@ -4,27 +4,31 @@ import com.music.spotui.data.entity.ArtistsModel
import com.music.spotui.data.entity.LibraryEntry
/**
- * The chips under "Your Library" (Playlists, Artists, Albums, Songs), like the official app's
- * filter row: one at a time, tapping the chosen one again shows everything.
+ * The chips under "Your Library" (Playlists, By me, Artists, Albums, Songs), like the official
+ * app's filter row: one at a time, tapping the chosen one again shows everything.
*/
-enum class LibraryChip { ALL, PLAYLISTS, ARTISTS, ALBUMS, SONGS }
+enum class LibraryChip { ALL, PLAYLISTS, BY_ME, ARTISTS, ALBUMS, SONGS }
object LibraryChips {
/**
* [songIds]: the library ids of the rows that hold songs rather than a playlist (Liked Songs,
* Local Files, Downloaded). Folders count as playlists, as they sit among them in Spotify.
- * Artists are a separate list, so only the Artists chip keeps them.
+ * Artists are a separate list, so only the Artists chip keeps them. [myId]: the signed-in
+ * account's Spotify user id; "By me" keeps the playlists it owns (a folder is not owned by
+ * anyone, so it is left out).
*/
fun apply(
chip: LibraryChip,
entries: List<LibraryEntry>,
artists: List<ArtistsModel>,
songIds: Set<String>,
+ myId: String? = null,
): Pair<List<LibraryEntry>, List<ArtistsModel>> = when (chip) {
LibraryChip.ALL -> entries to artists
LibraryChip.ARTISTS -> emptyList<LibraryEntry>() to artists
LibraryChip.SONGS -> entries.filter { it.spotifyId in songIds } to emptyList()
LibraryChip.PLAYLISTS -> entries.filter { it.spotifyId !in songIds && (it.isPlaylist || it.isFolder) } to emptyList()
+ LibraryChip.BY_ME -> entries.filter { myId != null && it.ownerUri == "spotify:user:$myId" } to emptyList()
LibraryChip.ALBUMS -> entries.filter { it.spotifyId !in songIds && !it.isPlaylist && !it.isFolder } to emptyList()
}
diff --git a/base/app/src/main/java/com/music/spotui/data/preferences/LibraryCachePref.kt b/base/app/src/main/java/com/music/spotui/data/preferences/LibraryCachePref.kt
index fa663e90..f54db3ec 100644
--- a/base/app/src/main/java/com/music/spotui/data/preferences/LibraryCachePref.kt
+++ b/base/app/src/main/java/com/music/spotui/data/preferences/LibraryCachePref.kt
@@ -20,6 +20,7 @@ fun cacheLibraryEntries(context: Context, entries: List<LibraryEntry>) {
put("artists", e.artists)
put("folderUri", e.folderUri)
put("childCount", e.childCount)
+ put("ownerUri", e.ownerUri)
})
}
}.toString()
@@ -42,6 +43,7 @@ fun getCachedLibraryEntries(context: Context): List<LibraryEntry> = runCatching
artists = o.optString("artists", ""),
folderUri = o.optString("folderUri", ""),
childCount = o.optInt("childCount", 0),
+ ownerUri = o.optString("ownerUri", ""),
)
}
}
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt
index d5206602..5301b1a1 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt
@@ -87,6 +87,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import app.myspoti.engine.R
import com.music.spotui.data.api.Api
+import com.music.spotui.data.api.ProfileCache
import com.music.spotui.data.api.Response
import com.music.spotui.data.entity.AccountModel
import com.music.spotui.data.entity.ArtistsModel
@@ -130,6 +131,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
/** The chips under the title, in the Classic order; their logic is the shared [LibraryChips]. */
private val LibraryChipLabels = listOf(
LibraryChip.PLAYLISTS to R.string.library_chip_playlists,
+ LibraryChip.BY_ME to R.string.library_chip_by_me,
LibraryChip.ARTISTS to R.string.library_chip_artists,
LibraryChip.ALBUMS to R.string.library_chip_albums,
LibraryChip.SONGS to R.string.library_chip_songs,
@@ -148,6 +150,7 @@ fun ImmersiveLibraryScreen(navController: NavController) {
val account by vm.account.collectAsState()
val followedArtists by vm.followedArtists.collectAsState()
val context = LocalContext.current
+ LaunchedEffect(Unit) { ProfileCache.ensure(context) }
// The first lists of the library load in the background, ready before a tap.
LaunchedEffect((entries as? Response.Success)?.data?.take(8)?.map { it.spotifyId }) {
@@ -187,8 +190,10 @@ fun ImmersiveLibraryScreen(navController: NavController) {
val loading = entries is Response.Loading
val (allEntries, pinned) = libraryEntries(entries)
// The kind and search filters only run again when their inputs change, not on every recomposition.
- val (shownEntries, shownArtists) = remember(kind, allEntries, followedArtists, searchQuery) {
- val (byKind, artists) = LibraryChips.apply(kind, allEntries, followedArtists, LIBRARY_SONG_IDS)
+ // Read through the name, which redraws the screen once the account is known.
+ val myId = ProfileCache.name?.let { ProfileCache.id }
+ val (shownEntries, shownArtists) = remember(kind, allEntries, followedArtists, searchQuery, myId) {
+ val (byKind, artists) = LibraryChips.apply(kind, allEntries, followedArtists, LIBRARY_SONG_IDS, myId)
if (searchQuery.isBlank()) byKind to artists
else byKind.filter {
it.name.contains(searchQuery, ignoreCase = true) || it.subtitle.contains(searchQuery, ignoreCase = true)
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt
index 75c1ab83..50f65ce6 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt
@@ -133,6 +133,7 @@ fun LibraryScreen(navController: NavController) {
val account by libraryViewModel.account.collectAsState()
var showAccount by remember { mutableStateOf(false) }
val context = LocalContext.current
+ androidx.compose.runtime.LaunchedEffect(Unit) { com.music.spotui.data.api.ProfileCache.ensure(context) }
// Spotify-style layout toggle: rows or a 3-column grid, persisted across runs.
var gridView by remember { mutableStateOf(isLibraryGridView(context)) }
var searchActive by androidx.compose.runtime.saveable.rememberSaveable { mutableStateOf(false) }
@@ -324,9 +325,12 @@ fun LibraryScreen(navController: NavController) {
LibraryOrder.version
LibraryOrder.arrange(list, LibraryOrder.sort, LibraryOrder.custom(LibraryOrder.ROOT), rootPinned)
}
+ // Read through the name, which redraws the screen once the account is known.
+ val myId = com.music.spotui.data.api.ProfileCache.name?.let { com.music.spotui.data.api.ProfileCache.id }
val (chipEntries, chipArtists) = com.music.spotui.data.local.LibraryChips.apply(
chip, allEntries, followedArtists,
setOf(Api.HomeCache.LIKED_SONGS_ID, Api.HomeCache.LOCAL_FILES_ID, Api.HomeCache.DOWNLOADS_ID),
+ myId,
)
val filteredEntries = if (searchQuery.isBlank()) chipEntries
else chipEntries.filter {
@@ -348,11 +352,12 @@ fun LibraryScreen(navController: NavController) {
}
}
-/** Playlists / Artists / Albums / Songs: one chip chosen at a time; the chosen one again clears it. */
+/** Playlists / By me / Artists / Albums / Songs: one chip chosen at a time; the chosen one again clears it. */
@Composable
private fun LibraryChipRow(chosen: com.music.spotui.data.local.LibraryChip, onTap: (com.music.spotui.data.local.LibraryChip) -> Unit) {
val chips = listOf(
com.music.spotui.data.local.LibraryChip.PLAYLISTS to R.string.library_chip_playlists,
+ com.music.spotui.data.local.LibraryChip.BY_ME to R.string.library_chip_by_me,
com.music.spotui.data.local.LibraryChip.ARTISTS to R.string.library_chip_artists,
com.music.spotui.data.local.LibraryChip.ALBUMS to R.string.library_chip_albums,
com.music.spotui.data.local.LibraryChip.SONGS to R.string.library_chip_songs,
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index 4abb0782..7bf09844 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -523,6 +523,7 @@
<string name="playlist_stop_collab_public">Stop collaborating on %1$s? Its collaborators are removed from the playlist. Invitation links already shared stay valid until they expire (up to 7 days) and cannot be cancelled.</string>
<string name="playlist_members_more">%1$s and %2$d others</string>
<string name="library_chip_playlists">Playlists</string>
+ <string name="library_chip_by_me">By me</string>
<string name="library_chip_artists">Artists</string>
<string name="library_chip_albums">Albums</string>
<string name="library_chip_songs">Songs</string>