Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Tick the tracks that are already in a playlist

Lelonio373c762cdd8b
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
index aedd3ba..167082b 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -646,6 +646,10 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
.onSuccess {
_addToPlaylist.value =
_addToPlaylist.value.copy(busy = null, done = playlist.name)
+ // Known at once, rather than when that playlist is next
+ // read: the tick is about the track, and the track is in a
+ // playlist from this moment.
+ _inPlaylists.value = _inPlaylists.value + trackUri
// The detail screen holds a list resolved before this track
// was in it; if that is the playlist just written to, read
// it again.
@@ -1096,6 +1100,26 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
* replaces it only when it has the whole thing, so reopening never takes a
* finished list away and rebuilds it in front of the user.
*/
+ private val _inPlaylists = MutableStateFlow<Set<String>>(emptySet())
+
+ /**
+ * Tracks known to be in one of the account's playlists.
+ *
+ * Known, not proven. Spotify has no endpoint for "which of my playlists
+ * hold this track", and asking would mean reading every playlist on the
+ * account to answer a question about one song. This is filled from the
+ * playlists the app has already read, which is what it has been looking at,
+ * plus whatever is added from inside the app. So a tick means yes; the
+ * absence of one means "not that I have seen".
+ */
+ val inPlaylists: StateFlow<Set<String>> = _inPlaylists.asStateFlow()
+
+ /** Notes the tracks of a playlist that has just been read. */
+ private fun rememberMembership(contextUri: String, tracks: List<CatalogTrack>) {
+ if (!contextUri.startsWith("spotify:playlist:") || tracks.isEmpty()) return
+ _inPlaylists.value = _inPlaylists.value + tracks.map { it.uri }
+ }
+
private val contextCache =
object : LinkedHashMap<String, ContextCacheStore.Entry>(0, 0.75f, true) {
override fun removeEldestEntry(eldest: Map.Entry<String, ContextCacheStore.Entry>) =
@@ -1128,6 +1152,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
// list already known appears at once rather than filling in.
val entry = remembered ?: container.contextCache.read(playlist.uri)?.also {
contextCache[playlist.uri] = it
+ rememberMembership(playlist.uri, it.tracks)
_playlist.value = base.copy(tracks = it.tracks, loading = false)
}
val cached = entry?.tracks.orEmpty()
@@ -1315,6 +1340,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
// entry still saves — it just gets refreshed silently every time.
val snapshotId = snapshotOf(uri)
contextCache[uri] = ContextCacheStore.Entry(tracks, snapshotId)
+ rememberMembership(uri, tracks)
container.contextCache.write(uri, tracks, snapshotId)
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index 54a1183..1a4e509 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -232,6 +232,7 @@ fun SquareApp(
*/
val elsewhereActive by dev.lelonio.square.data.RemoteConnect.elsewhereActive
.collectAsStateWithLifecycle()
+ val inPlaylists by viewModel.inPlaylists.collectAsStateWithLifecycle()
val remote = elsewhere?.takeIf { elsewhereActive }
val remoteLabel = stringResource(R.string.playing_on, remote?.deviceName.orEmpty())
val playback = remote?.asPlaybackState(remoteLabel) ?: local
@@ -960,6 +961,8 @@ fun SquareApp(
canvas = canvas,
devices = devices,
onAnotherDevice = remote != null,
+ alreadySaved = playback.mediaId != null &&
+ playback.mediaId in inPlaylists,
onOpenDevices = viewModel::openDevices,
connectAvailable =
backend == dev.lelonio.square.backend.BackendId.SPOTIFY,
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index 0efadf2..b3942f9 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -88,6 +88,7 @@ import com.adamglin.phosphoricons.fill.SkipBack
import com.adamglin.phosphoricons.fill.SkipForward
import com.adamglin.phosphoricons.regular.CaretDown
import com.adamglin.phosphoricons.regular.Devices
+import com.adamglin.phosphoricons.regular.Check
import com.adamglin.phosphoricons.regular.Plus
import com.adamglin.phosphoricons.regular.Queue
import com.adamglin.phosphoricons.regular.YoutubeLogo
@@ -176,6 +177,14 @@ fun PlayerScreen(
* of one thing.
*/
onAddToPlaylist: () -> Unit,
+ /**
+ * Whether this track is already in one of the account's playlists.
+ *
+ * Only ever true when it is known to be; see MainViewModel.inPlaylists. A
+ * button that says "add" over a track that is already there is a small lie,
+ * and the answer is cheap for anything the app has already read.
+ */
+ alreadySaved: Boolean = false,
/** The playlist picker's state, shown in the panel rather than as a sheet. */
addToPlaylist: MainViewModel.AddToPlaylistState,
onPickPlaylist: (dev.lelonio.square.data.CatalogPlaylist) -> Unit,
@@ -632,9 +641,21 @@ fun PlayerScreen(
},
) {
Icon(
- PhosphorIcons.Regular.Plus,
- contentDescription = stringResource(R.string.add_to_playlist),
- tint = panelTint(panel == PlayerPanel.ADD_TO_PLAYLIST),
+ if (alreadySaved) {
+ PhosphorIcons.Regular.Check
+ } else {
+ PhosphorIcons.Regular.Plus
+ },
+ contentDescription = stringResource(
+ if (alreadySaved) R.string.in_a_playlist
+ else R.string.add_to_playlist,
+ ),
+ tint = when {
+ panel == PlayerPanel.ADD_TO_PLAYLIST ->
+ panelTint(true)
+ alreadySaved -> SavedInk
+ else -> panelTint(false)
+ },
modifier = Modifier.size(20.dp),
)
}
@@ -1215,6 +1236,9 @@ private fun panelTint(active: Boolean) =
/** Spotify's own green, which already means "playing over there". */
private val ConnectedInk = androidx.compose.ui.graphics.Color(0xFF1ED760)
+/** The same green for "this one is already in a playlist of yours". */
+private val SavedInk = ConnectedInk
+
@Composable
private fun ToggleIcon(
icon: androidx.compose.ui.graphics.vector.ImageVector,
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 8ac1a2b..82bc900 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -224,4 +224,5 @@
<string name="page_about_summary">Version, Berechtigungen, Lizenzen und Entwickler</string>
<string name="playing_on">Wiedergabe auf %1$s</string>
<string name="switching">Wechsel…</string>
+ <string name="in_a_playlist">Schon in einer Playlist</string>
</resources>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index 192de5a..00711dd 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -224,4 +224,5 @@
<string name="page_about_summary">Versión, permisos, licencias y desarrollador</string>
<string name="playing_on">Reproduciendo en %1$s</string>
<string name="switching">Cambiando…</string>
+ <string name="in_a_playlist">Ya está en una lista</string>
</resources>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 4041e88..e25acd7 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -224,4 +224,5 @@
<string name="page_about_summary">Version, autorisations, licences et développeur</string>
<string name="playing_on">Lecture sur %1$s</string>
<string name="switching">Transfert…</string>
+ <string name="in_a_playlist">Déjà dans une playlist</string>
</resources>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 0fb744c..70091e2 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -225,4 +225,5 @@
<string name="page_about_summary">Versione, permessi, licenze e sviluppatore</string>
<string name="playing_on">In riprod. su %1$s</string>
<string name="switching">Passaggio in corso…</string>
+ <string name="in_a_playlist">Già in una playlist</string>
</resources>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index 459ce25..a9b9633 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -224,4 +224,5 @@
<string name="page_about_summary">Versão, permissões, licenças e desenvolvedor</string>
<string name="playing_on">Reproduzindo em %1$s</string>
<string name="switching">Trocando…</string>
+ <string name="in_a_playlist">Já está numa playlist</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index f0bc6f8..00d0624 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -245,4 +245,5 @@
<string name="page_about_summary">Version, permissions, licences and the developer</string>
<string name="playing_on">Playing on %1$s</string>
<string name="switching">Switching…</string>
+ <string name="in_a_playlist">Already in a playlist</string>
</resources>