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
Make the notification's heart work on YouTube Music, starting from the account's likes
Lelonio2c36b991a30c
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.ktindex ebe9858..705d1d9 100644--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt@@ -811,6 +811,9 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {/** YouTube Music's own id for the account's liked songs. */private const val LIKED_MUSIC_ID = "LM"+ /** The account's liked songs as a playlist address; what a heart adds to. */+ const val LIKED_MUSIC_URI = "${PLAYLIST_PREFIX}LM"+/** How many moods, and how many genres, the Radio tab reads. */private const val RADIO_PER_GROUP = 5diff --git a/app/src/main/java/dev/lelonio/square/data/LikedStore.kt b/app/src/main/java/dev/lelonio/square/data/LikedStore.ktindex 95a9dab..8da1b18 100644--- a/app/src/main/java/dev/lelonio/square/data/LikedStore.kt+++ b/app/src/main/java/dev/lelonio/square/data/LikedStore.kt@@ -40,6 +40,19 @@ class LikedStore(context: Context) {}}+ /**+ * The whole set for one source, as that source has it now: the songs under+ * [prefix] become exactly [uris], and the other source's are left alone.+ * Unlike [seed] this also takes back a heart removed somewhere else.+ */+ fun replaceSource(prefix: String, uris: Collection<String>) {+ val updated = _liked.value.filterNot { it.startsWith(prefix) }.toSet() + uris.filter { it.startsWith(prefix) }+ if (updated != _liked.value) {+ _liked.value = updated+ save(updated)+ }+ }+fun add(uri: String) {if (uri in _liked.value) returnval updated = _liked.value + uridiff --git a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.ktindex e49e610..51531f8 100644--- a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt+++ b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt@@ -169,22 +169,31 @@ class MediaBrowseTree(val currentUri = player.currentMediaItem?.mediaIdval liked = isLiked ?: app.likedStore.isLiked(currentUri)- return ImmutableList.of(- shuffle,- CommandButton.Builder()- .setIconResId(if (liked) R.drawable.ic_heart_filled else R.drawable.ic_heart_outline)- .setSessionCommand(SessionCommand(CMD_LIKE, Bundle.EMPTY))- .setDisplayName(- strings.getString(if (liked) R.string.remove_from_liked else R.string.liked_songs),- )- .build(),- CommandButton.Builder(CommandButton.ICON_RADIO)- .setSessionCommand(SessionCommand(CMD_RADIO, Bundle.EMPTY))- .setDisplayName(strings.getString(R.string.radio))- .build(),- )+ val heart = CommandButton.Builder()+ .setIconResId(if (liked) R.drawable.ic_heart_filled else R.drawable.ic_heart_outline)+ .setSessionCommand(SessionCommand(CMD_LIKE, Bundle.EMPTY))+ .setDisplayName(+ strings.getString(if (liked) R.string.remove_from_liked else R.string.liked_songs),+ )+ .build()+ val radio = CommandButton.Builder(CommandButton.ICON_RADIO)+ .setSessionCommand(SessionCommand(CMD_RADIO, Bundle.EMPTY))+ .setDisplayName(strings.getString(R.string.radio))+ .build()+ // The heart only where it can do something: a button that did nothing+ // was what a YouTube Music listener with no account found here.+ return if (likeable(currentUri)) ImmutableList.of(shuffle, heart, radio) else ImmutableList.of(shuffle, radio)}+ /**+ * Whether the song can be liked from here: any Spotify track, and a YouTube+ * Music one when an account is signed in to keep it in.+ */+ private fun likeable(uri: String?): Boolean = uri != null && (+ uri.startsWith("spotify:track:") ||+ (uri.startsWith(dev.lelonio.square.backend.youtube.YouTubeBackend.TRACK_PREFIX) && app.youtubeAccount.isSignedIn)+ )+override fun onConnect(session: MediaSession,controller: MediaSession.ControllerInfo,@@ -268,7 +277,8 @@ class MediaBrowseTree(private fun toggleLike(session: MediaSession) {val player = session.playerval uri = player.currentMediaItem?.mediaId ?: return- if (!uri.startsWith("spotify:track:")) return+ if (!likeable(uri)) return+ val youtube = uri.startsWith(dev.lelonio.square.backend.youtube.YouTubeBackend.TRACK_PREFIX)val nowLiked = app.likedStore.toggle(uri)val id = uri.substringAfterLast(':')@@ -287,7 +297,13 @@ class MediaBrowseTree(scope.launch {val callResult = runCatching {- if (nowLiked) {+ // YouTube Music keeps liked songs as a list of its own, and+ // liking is adding to it, as the app's own plus does.+ if (youtube) {+ val liked = dev.lelonio.square.backend.youtube.YouTubeBackend.LIKED_MUSIC_URI+ if (nowLiked) app.youtubeBackend.addToPlaylist(liked, uri)+ else app.youtubeBackend.removeFromPlaylist(liked, uri)+ } else if (nowLiked) {app.api.saveToLibrary("spotify:track:$id", dev.lelonio.square.data.SpotifyApi.EMPTY_BODY)} else {app.api.removeFromLibrary("spotify:track:$id")diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.ktindex 4d235a1..3751ff9 100644--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt@@ -2058,6 +2058,17 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {now = now,)store.importRemote(plays, dev.lelonio.square.data.ListeningStore.YOUTUBE_OWNER)+ // And which songs already have a heart: the account's liked+ // list, so the heart in the notification starts right.+ runCatching {+ val liked = container.youtubeBackend.tracksOf(+ dev.lelonio.square.backend.youtube.YouTubeBackend.LIKED_MUSIC_URI,+ )+ container.likedStore.replaceSource(+ dev.lelonio.square.backend.youtube.YouTubeBackend.TRACK_PREFIX,+ liked.map { it.uri },+ )+ }.onFailure { android.util.Log.w(TAG, "youtube likes unavailable: ${describe(it)}") }store.keepYoutubeReading(sections.flatMap { it.second }.map { it.uri }, now)android.util.Log.i(TAG, "youtube history: ${sections.map { it.first }}, ${plays.size} plays filed")}.onSuccess { lastListeningSync = System.currentTimeMillis() }@@ -2588,6 +2599,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {/** Playlists in the order this device last opened them; see [PlaylistOrderStore]. */val playlistOrder: StateFlow<List<String>> get() = container.playlistOrder.order+/** Pinned playlists, in the order they were pinned. */val pinnedPlaylists: StateFlow<List<String>> get() = container.pinnedPlaylists.pinned