Skip to content

Native Android client for Spotify, built with Jetpack Compose

Clearing the search of a playlist or Liked Songs in the Immersive look no longer closes the app: the rows were drawn with the keys kept from the shorter filtered list, so the list that grew back on the "×" (or on a deleted letter) ran out of keys; the rows and their keys now come out of one derived state, as in the Classic look [skip ci]

kota-zo76cbe8f698d6
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/FindInList.kt b/base/app/src/main/java/com/music/spotui/ui/components/FindInList.kt
index 9afa5c27..d4390a02 100644
--- a/base/app/src/main/java/com/music/spotui/ui/components/FindInList.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/components/FindInList.kt
@@ -66,6 +66,22 @@ fun matchesFind(query: String, vararg fields: String): Boolean {
return words.all { w -> fields.any { fold(it).contains(w) } }
}
+/**
+ * [rows] with one key each, unique and stable for that 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). Returned together with the rows: a list that re-reads its rows outside composition (a
+ * search letter, the "×") must never pair them with keys kept from the list before.
+ */
+fun <T> withRowKeys(rows: List<T>, id: (T) -> String): Pair<List<T>, List<String>> {
+ val seen = HashMap<String, Int>()
+ return rows to rows.map { s ->
+ val key = id(s)
+ val n = (seen[key] ?: 0) + 1
+ seen[key] = n
+ if (n == 1) key else "$key#$n"
+ }
+}
+
/**
* The "×" at the end of a search field, shown by the caller only while there is text. A tap clears
* the field through [onClear] (which resets the results), then puts the cursor back in the field
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveContextScreen.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveContextScreen.kt
index 33922cc0..7dcbf7b4 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveContextScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveContextScreen.kt
@@ -105,6 +105,7 @@ import com.music.spotui.di.SongPlayer
import com.music.spotui.ui.components.DownloadAsk
import com.music.spotui.ui.components.FastScrollbar
import com.music.spotui.ui.components.matchesFind
+import com.music.spotui.ui.components.withRowKeys
import com.music.spotui.ui.immersive.Immersive
import com.music.spotui.ui.immersive.components.ImmersiveButton
import com.music.spotui.ui.immersive.components.ImmersiveCircleButton
@@ -372,23 +373,16 @@ private fun ImmersiveContextContent(navController: NavController, vm: ContextVie
if (isLiked) p.tracks.filter { SongPlayer.library["spotify:track:${it.spotifyTrackId}"] != false } else p.tracks
}
}
- val rows by remember(p, find, sort) {
+ // The rows and their keys in one derived state: the list re-reads its rows outside composition
+ // (a search letter, the "×" of the search), and keys remembered apart from them were one list
+ // behind (a list that grows on the "×" ran out of keys: a crash).
+ val shown by remember(p, find, sort) {
derivedStateOf {
val found = if (findable) all.filter { 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"
+ withRowKeys(if (findable) sort.apply(found) else found) { it.id.toString() }
}
}
+ val rows = shown.first
// What the Play pill does: pause / resume this list, else start it (smart shuffle if it is on).
fun onPlay() = when {
@@ -497,7 +491,7 @@ private fun ImmersiveContextContent(navController: NavController, vm: ContextVie
ImmersiveMessageState(stringResource(R.string.assisted_curation_search_empty_view_no_result_title, find.trim()))
}
val numbered = p.type == "album"
- itemsIndexed(rows, key = { i, _ -> keys[i] }) { index, song ->
+ itemsIndexed(shown.first, key = { i, _ -> shown.second[i] }) { index, song ->
Column {
ImmersiveSwipeToQueue(song) {
ImmersiveSongRow(
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.kt
index f2616669..20bdbd66 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
@@ -680,14 +680,7 @@ private fun ContextContent(navController: NavController, vm: ContextViewModel, p
val shown 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
- val list = if (findable) sort.apply(found) else found
- val seen = HashMap<String, Int>()
- list to list.map { s ->
- val id = s.id.toString()
- val n = (seen[id] ?: 0) + 1
- seen[id] = n
- if (n == 1) id else "$id#$n"
- }
+ com.music.spotui.ui.components.withRowKeys(if (findable) sort.apply(found) else found) { it.id.toString() }
}
}
val rows = shown.first
diff --git a/base/app/src/test/java/com/music/spotui/ui/FindInListTest.kt b/base/app/src/test/java/com/music/spotui/ui/FindInListTest.kt
index f4f5e913..bba51df8 100644
--- a/base/app/src/test/java/com/music/spotui/ui/FindInListTest.kt
+++ b/base/app/src/test/java/com/music/spotui/ui/FindInListTest.kt
@@ -1,6 +1,8 @@
package com.music.spotui.ui
import com.music.spotui.ui.components.matchesFind
+import com.music.spotui.ui.components.withRowKeys
+import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -11,4 +13,10 @@ class FindInListTest {
@Test fun everyWordInAnyField() = assertTrue(matchesFind("beyonce hal", "Halo", "Beyoncé", "I Am... Sasha Fierce"))
@Test fun missingWordFails() = assertFalse(matchesFind("beyonce crazy", "Halo", "Beyoncé"))
@Test fun matchesPartOfAWord() = assertTrue(matchesFind("sava", "J'SAVAIS", "Ikaz Boi"))
+
+ @Test fun rowKeysAreUniqueAndTravelWithTheirRows() {
+ val (rows, keys) = withRowKeys(listOf("a", "b", "a", "a")) { it }
+ assertEquals(listOf("a", "b", "a", "a"), rows)
+ assertEquals(listOf("a", "b", "a#2", "a#3"), keys)
+ }
}