Skip to content

Native Android client for Spotify, built with Jetpack Compose

The library's sort orders the followed artists too in both looks (by name for Alphabetical and Creator, Spotify's order of the latest followed first otherwise): the Artists chip ignored it (reported on Discord)

kota-zo7b7a9c19dd06
diff --git a/base/app/src/main/java/com/music/spotui/data/local/LibraryOrder.kt b/base/app/src/main/java/com/music/spotui/data/local/LibraryOrder.kt
index 986169d5..581442a9 100644
--- a/base/app/src/main/java/com/music/spotui/data/local/LibraryOrder.kt
+++ b/base/app/src/main/java/com/music/spotui/data/local/LibraryOrder.kt
@@ -4,6 +4,7 @@ import android.content.Context
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
+import com.music.spotui.data.entity.ArtistsModel
import com.music.spotui.data.entity.LibraryEntry
import java.text.Collator
@@ -97,4 +98,17 @@ object LibraryOrder {
// Items pinned on Spotify stay at the top, in the chosen order.
return head + sorted.sortedBy { !it.pinned }
}
+
+ /**
+ * The followed artists in the chosen order: by name for "Alphabetical" and "Creator" (an
+ * artist is their own creator), else Spotify's own order, the most recently followed first
+ * (no date to sort by). Reported: the sort did nothing on the Artists chip.
+ */
+ fun arrangeArtists(artists: List<ArtistsModel>, sort: Sort): List<ArtistsModel> = when (sort) {
+ Sort.ALPHABETICAL, Sort.CREATOR -> {
+ val collator = Collator.getInstance().apply { strength = Collator.PRIMARY }
+ artists.sortedWith { a, b -> collator.compare(a.name, b.name) }
+ }
+ else -> artists
+ }
}
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 6503af38..75a265cb 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
@@ -197,9 +197,10 @@ fun ImmersiveLibraryScreen(navController: NavController) {
// The kind and search filters only run again when their inputs change, not on every recomposition.
// 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, followedShows, searchQuery, myId) {
+ val artistSort = LibraryOrder.sort
+ val (shownEntries, shownArtists) = remember(kind, allEntries, followedArtists, followedShows, searchQuery, myId, artistSort) {
val (byKind, artists) = LibraryChips.apply(
- kind, allEntries, followedArtists, LIBRARY_SONG_IDS, myId, followedShows, setOf(Api.HomeCache.EPISODES_ID),
+ kind, allEntries, LibraryOrder.arrangeArtists(followedArtists, artistSort), LIBRARY_SONG_IDS, myId, followedShows, setOf(Api.HomeCache.EPISODES_ID),
)
if (searchQuery.isBlank()) byKind to artists
else byKind.filter {
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 128719dc..b16aeba4 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
@@ -348,8 +348,9 @@ fun LibraryScreen(navController: NavController) {
it.name.contains(searchQuery, ignoreCase = true) ||
it.subtitle.contains(searchQuery, ignoreCase = true)
}
- val filteredArtists = if (searchQuery.isBlank()) chipArtists
- else chipArtists.filter { it.name.contains(searchQuery, ignoreCase = true) }
+ val filteredArtists = LibraryOrder.arrangeArtists(chipArtists, LibraryOrder.sort).let { sorted ->
+ if (searchQuery.isBlank()) sorted else sorted.filter { it.name.contains(searchQuery, ignoreCase = true) }
+ }
// Moving rows only makes sense on the whole list.
val movable = searchQuery.isBlank() && chip == com.music.spotui.data.local.LibraryChip.ALL
// The Podcasts chip waits for the shows, and says when none is followed or they could not be read (#136).
diff --git a/base/app/src/test/java/com/music/spotui/data/LibraryOrderTest.kt b/base/app/src/test/java/com/music/spotui/data/LibraryOrderTest.kt
index fc7420be..408110f7 100644
--- a/base/app/src/test/java/com/music/spotui/data/LibraryOrderTest.kt
+++ b/base/app/src/test/java/com/music/spotui/data/LibraryOrderTest.kt
@@ -1,5 +1,6 @@
package com.music.spotui.data
+import com.music.spotui.data.entity.ArtistsModel
import com.music.spotui.data.entity.LibraryEntry
import com.music.spotui.data.local.LibraryOrder
import com.music.spotui.data.local.LibraryOrder.Sort
@@ -38,4 +39,14 @@ class LibraryOrderTest {
assertEquals(listOf("liked", "spotify:user:x:folder:1", "e", "a", "b"), ids(LibraryOrder.arrange(all, Sort.CREATOR, emptyList(), 1)))
@Test fun folderKeyIsItsUri() = assertEquals("spotify:user:x:folder:1", LibraryOrder.key(f))
+
+ @Test
+ fun followedArtistsFollowTheSortByName() {
+ val artists = listOf(ArtistsModel("Zaz", ""), ArtistsModel("ABBA", ""), ArtistsModel("éric", ""))
+ assertEquals(listOf("ABBA", "éric", "Zaz"), LibraryOrder.arrangeArtists(artists, LibraryOrder.Sort.ALPHABETICAL).map { it.name })
+ assertEquals(listOf("ABBA", "éric", "Zaz"), LibraryOrder.arrangeArtists(artists, LibraryOrder.Sort.CREATOR).map { it.name })
+ // Spotify's order (the latest followed first) for the other sorts.
+ assertEquals(artists, LibraryOrder.arrangeArtists(artists, LibraryOrder.Sort.RECENTS))
+ assertEquals(artists, LibraryOrder.arrangeArtists(artists, LibraryOrder.Sort.CUSTOM))
+ }
}