Skip to content

Native Android client for Spotify, built with Jetpack Compose

The + of a row is checked by the player's own-playlist rule only for the song that plays, not for every other song on screen (search rows)

kota-zo5e5d4048d106
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/TrackSaved.kt b/base/app/src/main/java/com/music/spotui/ui/components/TrackSaved.kt
new file mode 100644
index 00000000..2bfe329a
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/ui/components/TrackSaved.kt
@@ -0,0 +1,18 @@
+package com.music.spotui.ui.components
+
+/**
+ * Whether a track's + shows as saved (the green check), as in the official app: in Liked Songs,
+ * or one of the rows of an own playlist that the player is playing from.
+ *
+ * The playlist rule describes the song the player is on: the context belongs to that song alone.
+ * Read for any other button (a row of a search result, say) it checked every one of them while an
+ * own playlist played. A song Smart Shuffle mixed in is not a row of the playlist, and a queued
+ * song plays with the list still as its context ([fromList] false).
+ */
+internal fun savedSomewhere(
+ liked: Boolean,
+ isRecommendation: Boolean,
+ isPlayingTrack: Boolean,
+ playingFromOwnList: Boolean,
+ fromList: Boolean,
+): Boolean = liked || (!isRecommendation && isPlayingTrack && playingFromOwnList && fromList)
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt
index 6583591b..f865b7a6 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/PlayerScreen.kt
@@ -1470,7 +1470,14 @@ fun AddButton(song: () -> SongsModel, liked: Boolean, size: androidx.compose.ui.
}
// A queued song plays with the list still as its context: only the list's own rows count.
val fromList = SongPlayer.nowPlaying.value.provider.let { it.isBlank() || it == "context" }
- val saved = liked || (inOwnList && fromList && !SmartShuffle.isRecommendation(current.spotifyTrackId))
+ val saved = com.music.spotui.ui.components.savedSomewhere(
+ liked = liked,
+ isRecommendation = SmartShuffle.isRecommendation(current.spotifyTrackId),
+ // The list is the player's: it says nothing of a row of a search result or an album.
+ isPlayingTrack = current.spotifyTrackId.isNotBlank() && current.spotifyTrackId == SongPlayer.currentTrackId,
+ playingFromOwnList = inOwnList,
+ fromList = fromList,
+ )
if (SmartShuffle.isRecommendation(current.spotifyTrackId)) {
Icon(
Icons.Outlined.AddCircleOutline,
diff --git a/base/app/src/test/java/com/music/spotui/ui/components/TrackSavedTest.kt b/base/app/src/test/java/com/music/spotui/ui/components/TrackSavedTest.kt
new file mode 100644
index 00000000..dde7a7e3
--- /dev/null
+++ b/base/app/src/test/java/com/music/spotui/ui/components/TrackSavedTest.kt
@@ -0,0 +1,20 @@
+package com.music.spotui.ui.components
+
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class TrackSavedTest {
+ @Test fun likedIsAlwaysSaved() = assertTrue(savedSomewhere(true, true, false, false, false))
+
+ @Test fun theSongPlayingFromAnOwnListIsSaved() = assertTrue(savedSomewhere(false, false, true, true, true))
+
+ // The search rows: another song's + was checked because the player's list was an own playlist.
+ @Test fun anotherSongIsNotSavedByTheListThatPlays() = assertFalse(savedSomewhere(false, false, false, true, true))
+
+ @Test fun aQueuedSongIsNotARowOfTheList() = assertFalse(savedSomewhere(false, false, true, true, false))
+
+ @Test fun aSmartShuffleSuggestionIsNotARowOfTheList() = assertFalse(savedSomewhere(false, true, true, true, true))
+
+ @Test fun aListThatIsNotOwnedSavesNothing() = assertFalse(savedSomewhere(false, false, true, false, true))
+}