Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Offer deleting a playlist only where it can happen, and ask first
Lelonio0f8afbfbc867
diff --git a/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt b/app/src/main/java/dev/lelonio/square/data/SpotifyApi.ktindex 88fb336..19702c7 100644--- a/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt+++ b/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt@@ -287,6 +287,29 @@ interface SpotifyApi {@Query("after") after: String? = null,): FollowedArtistsDto+ /**+ * Keeping a playlist, and keeping an album, which Spotify calls two+ * different things: a playlist is followed and an album is saved. Same+ * button on the page, two endpoints underneath.+ */+ @PUT("v1/playlists/{id}/followers")+ suspend fun followPlaylist(@Path("id") playlistId: String)++ @GET("v1/playlists/{id}/followers/contains")+ suspend fun playlistIsFollowed(+ @Path("id") playlistId: String,+ @Query("ids") userIds: String,+ ): List<Boolean>++ @PUT("v1/me/albums")+ suspend fun saveAlbums(@Query("ids") ids: String)++ @DELETE("v1/me/albums")+ suspend fun removeAlbums(@Query("ids") ids: String)++ @GET("v1/me/albums/contains")+ suspend fun albumsAreSaved(@Query("ids") ids: String): List<Boolean>+@GET("v1/search")suspend fun search(@Query("q") query: String,diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.ktindex c82222a..b5d8f71 100644--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt@@ -109,6 +109,23 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {val kind: DetailKind = DetailKind.PLAYLIST,/** What the source says the list is about; empty when it says nothing. */val description: String = "",+ /**+ * Whether this page is in the account's library — followed, for a+ * playlist, saved for an album.+ *+ * Null while unknown, and null forever for the pages where the question+ * does not apply: the button is absent rather than wrong.+ */+ val saved: Boolean? = null,+ /**+ * Whether this playlist is one the listener made.+ *+ * Null while unknown, and null for everything that is not a Spotify+ * playlist. It decides which of the actions in the menu are real:+ * Spotify has no way to delete somebody else's list, and offering it+ * meant offering a button that could only fail. Renaming is the same.+ */+ val mine: Boolean? = null,/** Populated for artists only. */val albums: List<SearchItem> = emptyList(),/** Their singles and EPs, kept apart from the albums above. */@@ -1128,6 +1145,9 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {}}+ /** The account's own Spotify id, once the profile has been read. */+ private var meId: String? = null+private fun loadProfile() = viewModelScope.launch {if (!container.webApi.isReady) return@launchrunCatching { container.api.me() }@@ -1138,6 +1158,10 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {?: ready.displayName,avatarUrl = profile.images.lastOrNull()?.url,)+ // Kept for the one question the playlist menu asks: is this+ // list mine. The id, not the display name — two accounts can+ // be called the same thing and only one of them owns it.+ meId = profile.id}.onFailure { android.util.Log.w(TAG, "profile unavailable: ${describe(it)}") }}@@ -1224,29 +1248,101 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {name = state.name.ifEmpty { current.name },artworkUrl = state.artworkUrl ?: current.artworkUrl,description = state.description.ifEmpty { current.description },+ saved = state.saved ?: current.saved,+ mine = state.mine ?: current.mine,)}}/**- * A playlist's own words about itself.+ * The two things one lookup answers: what the list says, and whose it is.*- * Spotify writes them in HTML, entities and anchors included, because the- * field is meant for a web page. What belongs under a title is the sentence,- * so the markup is taken back out.+ * One request rather than two. They are asked at the same moment and the+ * playlist endpoint returns both, so splitting them would double the+ * traffic for a page that is already waiting on its tracks.*/- private suspend fun descriptionOf(uri: String): String? {+ private suspend fun detailsOf(uri: String): Pair<String?, Boolean?>? {if (!container.webApi.isReady || !uri.startsWith("spotify:playlist:")) return null+ val dto = runCatching { container.api.playlist(uri.substringAfterLast(':')) }+ .onFailure { android.util.Log.w(TAG, "no details for $uri: ${describe(it)}") }+ .getOrNull() ?: return null+ val owner = dto.owner?.id+ // Null rather than false when either side is missing: "not yours" hides+ // the actions, and hiding them because a request came back thin is+ // worse than the button that was there before.+ val mine = if (owner == null || meId == null) null else owner == meId+ val text = dto.description+ ?.replace(Regex("<[^>]*>"), "")+ ?.replace("&", "&")+ ?.replace(""", "\"")+ ?.replace("'", "'")+ ?.replace("'", "'")+ ?.trim()+ ?.takeIf { it.isNotEmpty() }+ return text to mine+ }+++ /**+ * Whether a playlist is followed or an album saved.+ *+ * Two endpoints for one idea, because Spotify keeps the two words apart:+ * you follow somebody else's list and you save a record. Null when there is+ * no application to ask with, which leaves the button off the page rather+ * than showing a state nobody checked.+ */+ private suspend fun savedState(uri: String): Boolean? {+ if (!container.webApi.isReady) return null+ val id = uri.substringAfterLast(':')return runCatching {- container.api.playlist(uri.substringAfterLast(':')).description- ?.replace(Regex("<[^>]*>"), "")- ?.replace("&", "&")- ?.replace(""", "\"")- ?.replace("'", "'")- ?.replace("'", "'")- ?.trim()- ?.takeIf { it.isNotEmpty() }- }.getOrNull()+ when {+ uri.startsWith("spotify:album:") ->+ container.api.albumsAreSaved(id).firstOrNull()++ uri.startsWith("spotify:playlist:") ->+ container.api.playlistIsFollowed(id, container.api.me().id).firstOrNull()++ else -> null+ }+ }+ .onFailure { android.util.Log.w(TAG, "cannot tell if kept: ${describe(it)}") }+ .getOrNull()+ }++ /**+ * Keeps the open page, or lets it go.+ *+ * The button moves first and is put back if Spotify refuses, for the same+ * reason the follow button does: this is a round trip, and a control that+ * waits for one before acknowledging a tap feels broken even when it works.+ */+ fun toggleSaved() = viewModelScope.launch {+ val page = _playlist.value+ val uri = page.uri ?: return@launch+ val was = page.saved ?: return@launch+ val id = uri.substringAfterLast(':')++ _playlist.value = _playlist.value.copy(saved = !was)+ runCatching {+ when {+ uri.startsWith("spotify:album:") ->+ if (was) container.api.removeAlbums(id) else container.api.saveAlbums(id)++ else ->+ if (was) container.api.unfollowPlaylist(id) else container.api.followPlaylist(id)+ }+ }+ .onSuccess {+ // The library is what this button is about, so it is the library+ // that has to show the answer.+ refresh()+ }+ .onFailure {+ android.util.Log.e(TAG, "keeping failed: ${chain(it)}", it)+ if (_playlist.value.uri == uri) {+ _playlist.value = _playlist.value.copy(saved = was)+ }+ }}/** A link's own title, from whichever side of Spotify will say. */@@ -1444,11 +1540,25 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {// Asked for beside the picture and written the same way, through// `base`, so a later publish cannot take it back off the screen.if (base.description.isEmpty() && kind == DetailKind.PLAYLIST) launch {- val text = descriptionOf(playlist.uri) ?: return@launch- base = base.copy(description = text)+ val details = detailsOf(playlist.uri) ?: return@launch+ val text = details.first+ val mine = details.second+ base = base.copy(description = text.orEmpty(), mine = mine)+ if (_playlist.value.uri == playlist.uri) {+ _playlist.value = _playlist.value.copy(+ description = text.orEmpty().ifEmpty { _playlist.value.description },+ mine = mine,+ )+ }+ }++ // Whether it is already kept, for the button that keeps it.+ if (kind != DetailKind.ARTIST) launch {+ val kept = savedState(playlist.uri) ?: return@launchif (_playlist.value.uri == playlist.uri) {- _playlist.value = _playlist.value.copy(description = text)+ _playlist.value = _playlist.value.copy(saved = kept)}+ base = base.copy(saved = kept)}if (base.artworkUrl == null) launch {diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.ktindex 7a68e6a..bc6b272 100644--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt@@ -347,6 +347,12 @@ fun SquareApp(var trackMenu by remember { mutableStateOf<TrackMenuRequest?>(null) }// The playlist a long press opened the actions for.var playlistMenu by remember { mutableStateOf<CatalogPlaylist?>(null) }+ // What the menu may offer for the playlist it is open on. Defaults say yes,+ // because the library's own rows are the account's own library: only the+ // detail page, which can be a playlist belonging to anyone, ever says no.+ var playlistMenuMine by remember { mutableStateOf(true) }+ var playlistMenuSaved by remember { mutableStateOf(true) }+ var deleting by remember { mutableStateOf<CatalogPlaylist?>(null) }// A song somebody sent, waiting to be looked at; see SharedTrackCard.var sharedTrack by remember { mutableStateOf<CatalogTrack?>(null) }// Whether the friend list is open; the faces in the header open it.@@ -976,6 +982,11 @@ fun SquareApp(},onMenu = {val uri = playlist.uri ?: return@PlaylistScreen+ // What the menu is allowed to offer, from+ // the page that knows: whose list this is,+ // and whether it is in the library at all.+ playlistMenuMine = playlist.mine != false+ playlistMenuSaved = playlist.saved != falseplaylistMenu = CatalogPlaylist(uri = uri,name = playlist.name,@@ -1489,7 +1500,10 @@ fun SquareApp(// First action: this is the one that costs nothing and// can be undone by pressing it again.val isPinned = shownPlaylist.uri in pinnedPlaylists- TrackSheetAction(+ // Pinning is about where a row sits in the library, so+ // it is offered for what is in the library. A playlist+ // you are only looking at has no row to move.+ if (playlistMenuSaved) TrackSheetAction(stringResource(if (isPinned) R.string.unpin else R.string.pin),PhosphorIcons.Regular.PushPin,) {@@ -1498,8 +1512,13 @@ fun SquareApp(}// Liked Songs is Spotify's own list: it can be pinned// like any other row, but there is no name to change- // and no way to delete it.- val editable = !shownPlaylist.uri.endsWith(":collection")+ // and no way to delete it. Nor is anything editable on+ // somebody else's playlist — Spotify has no request for+ // "delete the list an editor made", and the app offering+ // one meant offering a button whose only outcome was an+ // error.+ val editable = !shownPlaylist.uri.endsWith(":collection") &&+ playlistMenuMineif (editable) TrackSheetAction(stringResource(R.string.rename),PhosphorIcons.Regular.PencilSimple,@@ -1526,12 +1545,26 @@ fun SquareApp(PhosphorIcons.Regular.Trash,destructive = true,) {- viewModel.deletePlaylist(shownPlaylist.uri)+ // Asked rather than done: see ConfirmDialog.+ deleting = shownPlaylistplaylistMenu = null}}}+ deleting?.let { target ->+ dev.lelonio.square.ui.components.ConfirmDialog(+ title = stringResource(R.string.delete_playlist_title),+ message = stringResource(R.string.delete_playlist_message, target.name),+ confirmLabel = stringResource(R.string.delete),+ onConfirm = {+ viewModel.deletePlaylist(target.uri)+ deleting = null+ },+ onDismiss = { deleting = null },+ )+ }+naming?.let { request ->dev.lelonio.square.ui.components.NameDialog(title = stringResource(diff --git a/app/src/main/java/dev/lelonio/square/ui/components/ConfirmDialog.kt b/app/src/main/java/dev/lelonio/square/ui/components/ConfirmDialog.ktnew file mode 100644index 0000000..214f1a3--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/ui/components/ConfirmDialog.kt@@ -0,0 +1,86 @@+package dev.lelonio.square.ui.components++import androidx.compose.foundation.background+import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.Text+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import androidx.compose.ui.window.Dialog+import dev.lelonio.square.R+import dev.lelonio.square.ui.theme.Ink+import dev.lelonio.square.ui.theme.InkDim++/**+ * Asks before something that cannot be taken back.+ *+ * Deleting a playlist is one tap in a menu and Spotify keeps no copy: the list+ * is gone from every client the account has, and there is nowhere in this app or+ * in theirs to get it back. That is the whole argument for the extra tap, and+ * why the destructive answer is the one that has to be reached for rather than+ * the one under the thumb.+ */+@Composable+fun ConfirmDialog(+ title: String,+ message: String,+ confirmLabel: String,+ onConfirm: () -> Unit,+ onDismiss: () -> Unit,+) {+ val shape = RoundedCornerShape(24.dp)++ Dialog(onDismissRequest = onDismiss) {+ Column(+ Modifier+ .fillMaxWidth()+ .clip(shape)+ .background(DialogFill)+ .padding(22.dp),+ ) {+ Text(title, style = MaterialTheme.typography.titleMedium, color = Ink)+ Text(+ message,+ style = MaterialTheme.typography.bodyMedium,+ color = InkDim,+ modifier = Modifier.padding(top = 10.dp),+ )++ Row(+ Modifier+ .padding(top = 20.dp)+ .fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End),+ ) {+ DialogAction(stringResource(R.string.cancel), InkDim, onDismiss)+ DialogAction(confirmLabel, MaterialTheme.colorScheme.error, onConfirm)+ }+ }+ }+}++@Composable+private fun DialogAction(label: String, color: Color, onClick: () -> Unit) {+ Text(+ label,+ style = MaterialTheme.typography.bodyLarge,+ color = color,+ modifier = Modifier+ .clip(RoundedCornerShape(50))+ .clickable(onClick = onClick)+ .padding(horizontal = 16.dp, vertical = 8.dp),+ )+}++private val DialogFill = Color(0xFF1B1B1B)