Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Lists: no sorting at every recomposition, stable row keys
kota-zoe824eb0e9c7f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9b220adf..880b0f3a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,7 @@ Each release on GitHub shows its section below. The app shows it too when it off## Unreleased+- **Long playlists open and scroll faster**: the list is filtered and sorted again only when the search, the sort or a like changes (not at every recomposition), and rows keep their identity across sorts and filters.- **Notification and lock screen no longer churn**: only a real change (song, play/pause, buffering, heart, shuffle) reaches the media session and the notification; the position is extrapolated by the system in between.- **Skips a little faster and less work while music plays**: a fade is one call to the page instead of three (the ramp runs in the page while the command is on its way), the volume check comes with the media read instead of polling every 6 s, and the watcher looks every 4 s instead of 2 s when nothing plays.- **Hearts (liked states) are asked once**: a page and its prefetch share the same request, and what Spotify confirmed less than a minute ago is not asked again (opening a playlist asked the whole list twice, then again for each page of tracks).diff --git a/base/app/src/main/java/com/music/spotui/App.kt b/base/app/src/main/java/com/music/spotui/App.ktindex 55e53073..c90b3d37 100644--- a/base/app/src/main/java/com/music/spotui/App.kt+++ b/base/app/src/main/java/com/music/spotui/App.kt@@ -18,6 +18,7 @@ import androidx.annotation.RequiresApiimport androidx.compose.foundation.layout.navigationBarsPaddingimport androidx.compose.material3.Scaffoldimport androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.getValueimport kotlinx.coroutines.launch@@ -40,19 +41,13 @@ fun App() {val navController = rememberNavController()val navBackStackEntry by navController.currentBackStackEntryAsState()- // Control TopBar and BottomBar- when (navBackStackEntry?.destination?.route) {- Routes.Home.route -> {- bottomBarState.value = true- }- Routes.Search.route -> {- bottomBarState.value = true- }- Routes.Library.route -> {- bottomBarState.value = true- }- Routes.Album.route -> {- bottomBarState.value = false+ // Control TopBar and BottomBar: on a route change (writing state during composition+ // forces an extra recomposition pass).+ val route = navBackStackEntry?.destination?.route+ LaunchedEffect(route) {+ when (route) {+ Routes.Home.route, Routes.Search.route, Routes.Library.route -> bottomBarState.value = true+ Routes.Album.route -> bottomBarState.value = false}}diff --git a/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt b/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.ktindex 901cf484..5a41ac53 100644--- a/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt+++ b/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt@@ -55,8 +55,6 @@ fun MyNavHost(val playerViewModel : PlayerViewModel = viewModel()val playerState by playerViewModel.currentSongTitle- Log.d("player", playerState.toString())-// val context = LocalContext.current// var player : ExoPlayer? = null// player = ExoPlayer.Builder(context).build()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.ktindex acacdee2..cf955b87 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@@ -39,6 +39,7 @@ import androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.collectAsState+import androidx.compose.runtime.derivedStateOfimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.remember@@ -347,6 +348,33 @@ private fun ContextContent(navController: NavController, vm: ContextViewModel, p)}+ // Liked Songs: a track unliked anywhere leaves the list right away. Derived state:+ // recomputed when the page, the search, the sort or a like changes, while a recomposition+ // of the screen with nothing new in them re-sorts nothing (a 900-track list is real work).+ 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+ }+ }+ val rows by remember(p, find, sort) {+ derivedStateOf {+ val found = if (findable) all.filter { com.music.spotui.ui.components.matchesFind(find, it.title, it.singer, it.album) } else all+ if (findable) sort.apply(found) else found+ }+ }+ // Unique and stable for a given list: a playlist can hold the same song twice (the id alone+ // is not unique) and the index changes at every sort (which would lose every item).+ val keys = remember(rows) {+ val seen = HashMap<String, Int>()+ rows.map { s ->+ val id = s.id.toString()+ val n = (seen[id] ?: 0) + 1+ seen[id] = n+ if (n == 1) id else "$id#$n"+ }+ }+val listState = androidx.compose.foundation.lazy.rememberLazyListState()Box(Modifier.fillMaxSize()) {LazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {@@ -530,10 +558,6 @@ private fun ContextContent(navController: NavController, vm: ContextViewModel, p}}// Liked Songs: a track unliked anywhere leaves the list right away.- val all = if (p.type == "liked") p.tracks.filter { SongPlayer.library["spotify:track:${it.spotifyTrackId}"] != false } else p.tracks- val findable = p.type == "liked" || p.type == "playlist"- val found = if (findable) all.filter { com.music.spotui.ui.components.matchesFind(find, it.title, it.singer, it.album) } else all- val rows = if (findable) sort.apply(found) else foundif (findable && all.isNotEmpty()) item(key = "find") {Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(start = 16.dp, end = 8.dp, top = 8.dp, bottom = 8.dp)) {com.music.spotui.ui.components.FindInListField(@@ -547,7 +571,7 @@ private fun ContextContent(navController: NavController, vm: ContextViewModel, pif (rows.isEmpty() && find.isNotBlank()) item(key = "find-empty") {com.music.spotui.ui.components.FindInListEmpty(stringResource(R.string.assisted_curation_search_empty_view_no_result_title, find.trim()))}- itemsIndexed(rows, key = { i, s -> "$i:${s.id}" }) { _, song ->+ itemsIndexed(rows, key = { i, _ -> keys[i] }) { _, song ->com.music.spotui.ui.components.SwipeToQueue(song) { TrackRow(song = song,showCover = p.type != "album",@@ -722,6 +746,11 @@ object SmartShuffle {}}+/** Text order for sorting (accents ignored): one collator per thread, none is thread-safe. */+private val textOrder = ThreadLocal.withInitial {+ java.text.Collator.getInstance().apply { strength = java.text.Collator.PRIMARY }+}+enum class TrackSort(val label: Int) {CUSTOM(R.string.your_library_sort_order_custom),TITLE(R.string.track_sort_title),@@ -731,7 +760,8 @@ enum class TrackSort(val label: Int) {ADDED(R.string.track_sort_added);internal fun natural(songs: List<SongsModel>): List<SongsModel> {- val text = java.text.Collator.getInstance().apply { strength = java.text.Collator.PRIMARY }+ // One collator per thread: sorting runs at every sort change, not at every recomposition.+ val text = textOrder.get()return when (this) {CUSTOM -> songsTITLE -> songs.sortedWith { a, b -> text.compare(a.title, b.title) }