From d2918aeeb0ab5e84bb9e2219bab2b21643fa7abe Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 10:19:26 -0500 Subject: [PATCH] refactor(android): Home and the Artist page shuffle-play an artist through one function (DRY A5) Home's artist tile and the Artist page's Play each fetched the artist's tracks, shuffled and queued them, with their own copy of the error handling and different words for an empty artist ("This artist has no tracks to play." vs "No tracks to play for this artist."). Both call library/data/LibraryPlayback.kt's shufflePlayArtist now, beside playAlbumFromStart for Home's album tiles, the way PlaylistPlayback serves the playlist tiles. Co-Authored-By: Claude Opus 5.5 --- .../minstrel/home/ui/HomeScreen.kt | 36 ++---------- .../minstrel/library/data/LibraryPlayback.kt | 58 +++++++++++++++++++ .../library/ui/ArtistDetailViewModel.kt | 20 +------ 3 files changed, 66 insertions(+), 48 deletions(-) create mode 100644 android/app/src/main/java/com/fabledsword/minstrel/library/data/LibraryPlayback.kt diff --git a/android/app/src/main/java/com/fabledsword/minstrel/home/ui/HomeScreen.kt b/android/app/src/main/java/com/fabledsword/minstrel/home/ui/HomeScreen.kt index bdeb4228..68af5ea4 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/home/ui/HomeScreen.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/home/ui/HomeScreen.kt @@ -64,6 +64,8 @@ import com.fabledsword.minstrel.connectivity.ServerHealth import com.fabledsword.minstrel.connectivity.recoveries import com.fabledsword.minstrel.home.data.HomeRepository import com.fabledsword.minstrel.library.data.LibraryRepository +import com.fabledsword.minstrel.library.data.playAlbumFromStart +import com.fabledsword.minstrel.library.data.shufflePlayArtist import com.fabledsword.minstrel.library.widgets.AlbumCard import com.fabledsword.minstrel.library.widgets.ArtistCard import com.fabledsword.minstrel.models.AlbumRef @@ -272,44 +274,18 @@ class HomeViewModel @Inject constructor( */ suspend fun playAlbum(albumId: String) { viewModelScope.launch { - try { - val detail = libraryRepository.refreshAlbumDetail(albumId) - if (detail.tracks.isEmpty()) { - snackbarMessages.trySend("This album has no tracks to play.") - } else { - player.setQueue(detail.tracks, initialIndex = 0, source = "album:$albumId") - } - } catch ( - @Suppress("TooGenericExceptionCaught") e: Throwable, - ) { - snackbarMessages.trySend( - "Couldn't start playback: ${ErrorCopy.fromThrowable(e)}", - ) - } + playAlbumFromStart(albumId, libraryRepository, player) { snackbarMessages.trySend(it) } }.join() } /** * Tap the artist play-button overlay: fetch every track across the - * artist's albums, Fisher-Yates shuffle, play from index 0. Empty + - * error semantics follow [playAlbum]. + * artist's albums, shuffle, play from index 0 (shufflePlayArtist, as + * the Artist page's Play does). */ suspend fun playArtistShuffled(artistId: String) { viewModelScope.launch { - try { - val tracks = libraryRepository.fetchArtistTracks(artistId).shuffled() - if (tracks.isEmpty()) { - snackbarMessages.trySend("This artist has no tracks to play.") - } else { - player.setQueue(tracks, initialIndex = 0, source = "artist:$artistId") - } - } catch ( - @Suppress("TooGenericExceptionCaught") e: Throwable, - ) { - snackbarMessages.trySend( - "Couldn't start playback: ${ErrorCopy.fromThrowable(e)}", - ) - } + shufflePlayArtist(artistId, libraryRepository, player) { snackbarMessages.trySend(it) } }.join() } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/library/data/LibraryPlayback.kt b/android/app/src/main/java/com/fabledsword/minstrel/library/data/LibraryPlayback.kt new file mode 100644 index 00000000..54af4893 --- /dev/null +++ b/android/app/src/main/java/com/fabledsword/minstrel/library/data/LibraryPlayback.kt @@ -0,0 +1,58 @@ +package com.fabledsword.minstrel.library.data + +import com.fabledsword.minstrel.api.ErrorCopy +import com.fabledsword.minstrel.player.PlayerController +import kotlinx.coroutines.CancellationException + +/** + * Fetch an artist's tracks across all their albums and play them shuffled. + * Pressing Play on an artist means "shuffle this artist", not "play album + * by album in tracklist order". Home's artist tiles and the Artist page's + * Play button both start here, so they behave and word their messages + * the same. + * + * An empty artist or a failed fetch reaches [onMessage] for the caller to + * show as a snackbar. + */ +suspend fun shufflePlayArtist( + artistId: String, + repository: LibraryRepository, + player: PlayerController, + onMessage: (String) -> Unit, +) = playOrSay(onMessage, empty = "This artist has no tracks to play.") { + val tracks = repository.fetchArtistTracks(artistId).shuffled() + if (tracks.isNotEmpty()) player.setQueue(tracks, initialIndex = 0, source = "artist:$artistId") + tracks.isNotEmpty() +} + +/** + * Fetch an album and play it from its first track, in tracklist order. + * For an album tile's play button, where the album isn't loaded yet. + */ +suspend fun playAlbumFromStart( + albumId: String, + repository: LibraryRepository, + player: PlayerController, + onMessage: (String) -> Unit, +) = playOrSay(onMessage, empty = "This album has no tracks to play.") { + val tracks = repository.refreshAlbumDetail(albumId).tracks + if (tracks.isNotEmpty()) player.setQueue(tracks, initialIndex = 0, source = "album:$albumId") + tracks.isNotEmpty() +} + +/** Runs [start]; says [empty] when it found nothing to play, and why when it failed. */ +private suspend fun playOrSay( + onMessage: (String) -> Unit, + empty: String, + start: suspend () -> Boolean, +) { + try { + if (!start()) onMessage(empty) + } catch (e: CancellationException) { + throw e + } catch ( + @Suppress("TooGenericExceptionCaught") e: Throwable, + ) { + onMessage("Couldn't start playback: ${ErrorCopy.fromThrowable(e)}") + } +} diff --git a/android/app/src/main/java/com/fabledsword/minstrel/library/ui/ArtistDetailViewModel.kt b/android/app/src/main/java/com/fabledsword/minstrel/library/ui/ArtistDetailViewModel.kt index ee27ba6d..76ec2991 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/library/ui/ArtistDetailViewModel.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/library/ui/ArtistDetailViewModel.kt @@ -8,6 +8,7 @@ import com.fabledsword.minstrel.api.ErrorCopy import com.fabledsword.minstrel.connectivity.NetworkStatusController import com.fabledsword.minstrel.connectivity.recoveries import com.fabledsword.minstrel.library.data.LibraryRepository +import com.fabledsword.minstrel.library.data.shufflePlayArtist import com.fabledsword.minstrel.likes.data.LikesRepository import com.fabledsword.minstrel.models.ArtistDetailRef import com.fabledsword.minstrel.models.ArtistRef @@ -145,24 +146,7 @@ class ArtistDetailViewModel @Inject constructor( */ fun playArtist() { viewModelScope.launch { - try { - val tracks = repository.fetchArtistTracks(artistId).shuffled() - if (tracks.isEmpty()) { - transientMessagesChannel.trySend("No tracks to play for this artist.") - } else { - player.setQueue( - tracks = tracks, - initialIndex = 0, - source = "artist:$artistId", - ) - } - } catch ( - @Suppress("TooGenericExceptionCaught") e: Throwable, - ) { - transientMessagesChannel.trySend( - "Couldn't start playback: ${ErrorCopy.fromThrowable(e)}", - ) - } + shufflePlayArtist(artistId, repository, player) { transientMessagesChannel.trySend(it) } } }