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.

The tick is back in the player, and takes a song out of its playlist

Leloniof0ac283b88bf
diff --git a/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt b/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt
index 8c06d28..cd01cf1 100644
--- a/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt
+++ b/app/src/main/java/dev/lelonio/square/data/ContextCacheStore.kt
@@ -106,6 +106,26 @@ class ContextCacheStore(context: Context) {
}
}
+ /**
+ * The playlists this device holds that have [trackUri] in them.
+ *
+ * For the ticks in the playlist picker, which say where the track already
+ * is so that picking one of those takes it out; the same caveat as
+ * [tracksInPlaylists] about what the app has seen.
+ */
+ suspend fun playlistsWith(trackUri: String): Set<String> = withContext(Dispatchers.IO) {
+ val cutoff = System.currentTimeMillis() - MAX_AGE_MS
+ dir.listFiles().orEmpty().mapNotNullTo(mutableSetOf()) { file ->
+ val entry = runCatching {
+ json.decodeFromString(Entry.serializer(), file.readText())
+ }.getOrNull() ?: return@mapNotNullTo null
+ entry.uri.takeIf {
+ entry.savedAt > cutoff && it.startsWith("spotify:playlist:") &&
+ entry.tracks.any { track -> track.uri == trackUri }
+ }
+ }
+ }
+
suspend fun remove(uri: String) = withContext(Dispatchers.IO) {
fileFor(uri).delete()
Unit
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 1183fa7..0854467 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -1058,6 +1058,14 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
* be undone: picking it again takes the track back out.
*/
val liked: Boolean = false,
+ /**
+ * The listener's playlists the track is known to be in already.
+ *
+ * Ticked in the list, and picking one takes the track out: the same as
+ * Liked Songs, and the only way left to take a song out of the list it
+ * is playing from without going to that list.
+ */
+ val containing: Set<String> = emptySet(),
val error: String? = null,
)
@@ -1068,7 +1076,13 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
* @param asSheet whether to show the modal. False from the player, which
* shows the same picker in its own panel and would otherwise get both.
*/
- fun openAddToPlaylist(trackUri: String?, trackTitle: String, asSheet: Boolean = true) {
+ fun openAddToPlaylist(
+ trackUri: String?,
+ trackTitle: String,
+ asSheet: Boolean = true,
+ /** What the track is playing from, when it is; see [AddToPlaylistState.containing]. */
+ contextUri: String? = null,
+ ) {
// The other source writes through its own account, to the lists it
// says can be written to; they are asked for as the sheet opens.
val backend = container.activeBackend
@@ -1104,6 +1118,18 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
else -> null
},
)
+ if (trackUri != null) viewModelScope.launch {
+ val own = _addToPlaylist.value.playlists.map { it.uri }.toSet()
+ val cached = runCatching { container.contextCache.playlistsWith(trackUri) }.getOrDefault(emptySet())
+ // The list it is playing from counts even when it has not been
+ // read into the cache, as long as it is one of the listener's.
+ val playing = contextUri?.takeIf { it.startsWith("spotify:playlist:") }
+ val open = _playlist.value.uri?.takeIf { uri -> _playlist.value.tracks.any { it.uri == trackUri } }
+ val containing = (cached + listOfNotNull(playing, open)).filterTo(mutableSetOf()) { it in own }
+ if (_addToPlaylist.value.trackUri == trackUri) {
+ _addToPlaylist.value = _addToPlaylist.value.copy(containing = containing)
+ }
+ }
}
fun closeAddToPlaylist() {
@@ -1167,6 +1193,41 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
return
}
+ // Already in it: the tick in that row is taken back, as the heart is.
+ if (playlist.uri in current.containing) {
+ _addToPlaylist.value =
+ current.copy(busy = playlist.uri, done = null, removed = null, error = null)
+ viewModelScope.launch {
+ runCatching {
+ container.api.removeFromPlaylist(id, RemoveTracksRequestDto(listOf(TrackUriDto(trackUri))))
+ }
+ .onSuccess {
+ val left = _addToPlaylist.value.containing - playlist.uri
+ _addToPlaylist.value = _addToPlaylist.value.copy(
+ busy = null,
+ removed = playlist.name,
+ containing = left,
+ )
+ if (left.isEmpty()) _inPlaylists.value = _inPlaylists.value - trackUri
+ _takenOut.value = _takenOut.value + "${playlist.uri}|$trackUri"
+ invalidateContext(playlist.uri)
+ if (_playlist.value.uri == playlist.uri) {
+ _playlist.value = _playlist.value.copy(
+ tracks = _playlist.value.tracks.filterNot { it.uri == trackUri },
+ )
+ }
+ }
+ .onFailure {
+ android.util.Log.e(TAG, "remove from playlist failed: ${chain(it)}", it)
+ _addToPlaylist.value = _addToPlaylist.value.copy(
+ busy = null,
+ error = string(R.string.remove_failed, playlist.name),
+ )
+ }
+ }
+ return
+ }
+
_addToPlaylist.value =
current.copy(busy = playlist.uri, done = null, removed = null, error = null)
viewModelScope.launch {
@@ -1178,7 +1239,9 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
busy = null,
done = playlist.name,
removed = null,
+ containing = _addToPlaylist.value.containing + playlist.uri,
)
+ _takenOut.value = _takenOut.value - "${playlist.uri}|$trackUri"
// 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.
@@ -2751,6 +2814,17 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
*/
val inPlaylists: StateFlow<Set<String>> = _inPlaylists.asStateFlow()
+ /**
+ * Tracks taken out of a playlist in this session, as "playlist|track".
+ *
+ * The player ticks a song that is playing from one of the listener's own
+ * playlists, and it goes on playing from it after being taken out: without
+ * this the tick stayed on a song that was no longer in the list. Put back,
+ * it leaves here again.
+ */
+ private val _takenOut = MutableStateFlow<Set<String>>(emptySet())
+ val takenOut: StateFlow<Set<String>> = _takenOut.asStateFlow()
+
/** Notes the tracks of a playlist that has just been read. */
private fun rememberMembership(contextUri: String, tracks: List<CatalogTrack>) {
if (tracks.isEmpty()) return
@@ -3838,6 +3912,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
return
}
if (!uri.startsWith("spotify:playlist:")) return
+ _takenOut.value = _takenOut.value + "$uri|${track.uri}"
val before = _playlist.value.tracks
_playlist.value = _playlist.value.copy(tracks = before.filterNot { it.uri == track.uri })
@@ -3851,6 +3926,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
)
}.onFailure {
android.util.Log.e(TAG, "remove from playlist failed: ${chain(it)}", it)
+ _takenOut.value = _takenOut.value - "$uri|${track.uri}"
// Only if the screen is still showing the same playlist.
if (_playlist.value.uri == uri) {
_playlist.value = _playlist.value.copy(tracks = before)
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 82a7c74..122d15c 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -421,6 +421,7 @@ fun SquareApp(
) {
val state by viewModel.state.collectAsStateWithLifecycle()
val playlist by viewModel.playlist.collectAsStateWithLifecycle()
+ val takenOut by viewModel.takenOut.collectAsStateWithLifecycle()
val context = LocalContext.current
// The session as it was left, so the player has something to draw before
@@ -2939,8 +2940,18 @@ fun SquareApp(
coverPending = nowPlayingArtPending,
devices = devices,
onAnotherDevice = remote != null,
+ // Or playing from one of the listener's own
+ // playlists, which it is in whether or not
+ // that list has been read into the cache.
alreadySaved = playback.mediaId != null &&
- playback.mediaId in inPlaylists,
+ (
+ playback.mediaId in inPlaylists ||
+ (
+ (state as? MainViewModel.UiState.Ready)?.playlists
+ ?.any { it.uri == playback.contextUri } == true &&
+ "${playback.contextUri}|${playback.mediaId}" !in takenOut
+ )
+ ),
inLikedSongs = playback.mediaId != null &&
playback.mediaId in likedTracks,
// Spotify's own radio, and only where Spotify
@@ -3014,6 +3025,7 @@ fun SquareApp(
playback.mediaId,
playback.title,
asSheet = false,
+ contextUri = playback.contextUri,
)
},
// The heart is Spotify's library. On the other
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/AddToPlaylistSheet.kt b/app/src/main/java/dev/lelonio/square/ui/player/AddToPlaylistSheet.kt
index 11e9e98..1059aa9 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/AddToPlaylistSheet.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/AddToPlaylistSheet.kt
@@ -164,7 +164,7 @@ fun AddToPlaylistSheet(
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(2.dp),
) {
- state.playlists.forEach { playlist ->
+ state.playlists.ticksFirst(state).forEach { playlist ->
PlaylistRow(
playlist = playlist,
busy = state.busy == playlist.uri,
@@ -173,6 +173,7 @@ fun AddToPlaylistSheet(
// the track already is, and the one a second
// press takes it out of.
liked = state.liked && playlist.uri.endsWith(":collection"),
+ inIt = playlist.uri in state.containing,
enabled = state.trackUri != null && state.busy == null,
onClick = { onSelect(playlist) },
)
@@ -250,7 +251,7 @@ fun PlaylistPicker(
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(2.dp),
) {
- state.playlists.forEach { playlist ->
+ state.playlists.ticksFirst(state).forEach { playlist ->
PlaylistRow(
playlist = playlist,
busy = state.busy == playlist.uri,
@@ -259,6 +260,7 @@ fun PlaylistPicker(
// the track already is, and the one a second
// press takes it out of.
liked = state.liked && playlist.uri.endsWith(":collection"),
+ inIt = playlist.uri in state.containing,
enabled = state.trackUri != null && state.busy == null,
onClick = { onSelect(playlist) },
)
@@ -275,6 +277,8 @@ private fun PlaylistRow(
added: Boolean,
/** In Liked Songs already, so this row removes rather than adds. */
liked: Boolean = false,
+ /** In this playlist already, so this row removes rather than adds. */
+ inIt: Boolean = false,
enabled: Boolean,
onClick: () -> Unit,
) {
@@ -316,6 +320,13 @@ private fun PlaylistRow(
modifier = Modifier.size(20.dp),
)
+ inIt -> Icon(
+ PhosphorIcons.Regular.Check,
+ contentDescription = stringResource(R.string.remove_from_playlist),
+ tint = MaterialTheme.colorScheme.primary,
+ modifier = Modifier.size(20.dp),
+ )
+
added -> Icon(
PhosphorIcons.Regular.Check,
contentDescription = null,
@@ -325,3 +336,14 @@ private fun PlaylistRow(
}
}
}
+
+/**
+ * The lists the track is already in, at the top: they are the ones this panel
+ * is opened to find when the song is ticked, and further down a long list of
+ * playlists they were a scroll away. Otherwise in the order they came, which
+ * is the library's.
+ */
+private fun List<CatalogPlaylist>.ticksFirst(state: MainViewModel.AddToPlaylistState) =
+ sortedByDescending { playlist ->
+ playlist.uri in state.containing || (state.liked && playlist.uri.endsWith(":collection"))
+ }
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 367e547..6f06d9f 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
@@ -123,6 +123,7 @@ import com.adamglin.phosphoricons.fill.SkipForward
import com.adamglin.phosphoricons.regular.CaretDown
import com.adamglin.phosphoricons.regular.Devices
import com.adamglin.phosphoricons.regular.Heart
+import com.adamglin.phosphoricons.regular.Check
import com.adamglin.phosphoricons.regular.Plus
import com.adamglin.phosphoricons.regular.Queue
import com.adamglin.phosphoricons.regular.Broadcast
@@ -1244,7 +1245,10 @@ fun PlayerScreen(
backdrop = glassBackdrop,
size = 40.dp,
onClick = {
- if (onToggleLike != null) {
+ // A tick opens the lists, where it can
+ // be taken back; a heart, filled or
+ // not, is the like itself.
+ if (onToggleLike != null && (inLikedSongs || !alreadySaved)) {
onToggleLike()
} else {
panel = if (panel == PlayerPanel.ADD_TO_PLAYLIST) {
@@ -1266,18 +1270,25 @@ fun PlayerScreen(
) {
// A heart where a tap likes the song, a plus
// where the tap opens the lists instead: the
- // picture says what pressing it does.
+ // picture says what pressing it does. And a
+ // tick where the song is in one of the
+ // listener's playlists and not liked: the
+ // heart had taken its place, which left no
+ // way to see that, or to take the song back
+ // out of the list it was playing from.
val hearts = onToggleLike != null
Icon(
when {
+ hearts && inLikedSongs -> PhosphorIcons.Fill.Heart
+ alreadySaved -> PhosphorIcons.Regular.Check
!hearts -> PhosphorIcons.Regular.Plus
- inLikedSongs -> PhosphorIcons.Fill.Heart
else -> PhosphorIcons.Regular.Heart
},
contentDescription = stringResource(
when {
+ hearts && inLikedSongs -> R.string.remove_from_liked
+ alreadySaved -> R.string.in_a_playlist
!hearts -> R.string.add_to_playlist
- inLikedSongs -> R.string.remove_from_liked
else -> R.string.liked_songs
},
),
@@ -1285,7 +1296,7 @@ fun PlayerScreen(
panel == PlayerPanel.ADD_TO_PLAYLIST ->
panelTint(true)
hearts && inLikedSongs -> SavedInk
- !hearts && alreadySaved -> SavedInk
+ alreadySaved -> SavedInk
else -> panelTint(false)
},
modifier = Modifier.size(20.dp),