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 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 10:19:26 -05:00
co-authored by Claude Opus 5.5
parent e049c27bdf
commit d2918aeeb0
3 changed files with 66 additions and 48 deletions
@@ -64,6 +64,8 @@ import com.fabledsword.minstrel.connectivity.ServerHealth
import com.fabledsword.minstrel.connectivity.recoveries import com.fabledsword.minstrel.connectivity.recoveries
import com.fabledsword.minstrel.home.data.HomeRepository import com.fabledsword.minstrel.home.data.HomeRepository
import com.fabledsword.minstrel.library.data.LibraryRepository 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.AlbumCard
import com.fabledsword.minstrel.library.widgets.ArtistCard import com.fabledsword.minstrel.library.widgets.ArtistCard
import com.fabledsword.minstrel.models.AlbumRef import com.fabledsword.minstrel.models.AlbumRef
@@ -272,44 +274,18 @@ class HomeViewModel @Inject constructor(
*/ */
suspend fun playAlbum(albumId: String) { suspend fun playAlbum(albumId: String) {
viewModelScope.launch { viewModelScope.launch {
try { playAlbumFromStart(albumId, libraryRepository, player) { snackbarMessages.trySend(it) }
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)}",
)
}
}.join() }.join()
} }
/** /**
* Tap the artist play-button overlay: fetch every track across the * Tap the artist play-button overlay: fetch every track across the
* artist's albums, Fisher-Yates shuffle, play from index 0. Empty + * artist's albums, shuffle, play from index 0 (shufflePlayArtist, as
* error semantics follow [playAlbum]. * the Artist page's Play does).
*/ */
suspend fun playArtistShuffled(artistId: String) { suspend fun playArtistShuffled(artistId: String) {
viewModelScope.launch { viewModelScope.launch {
try { shufflePlayArtist(artistId, libraryRepository, player) { snackbarMessages.trySend(it) }
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)}",
)
}
}.join() }.join()
} }
@@ -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)}")
}
}
@@ -8,6 +8,7 @@ import com.fabledsword.minstrel.api.ErrorCopy
import com.fabledsword.minstrel.connectivity.NetworkStatusController import com.fabledsword.minstrel.connectivity.NetworkStatusController
import com.fabledsword.minstrel.connectivity.recoveries import com.fabledsword.minstrel.connectivity.recoveries
import com.fabledsword.minstrel.library.data.LibraryRepository 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.likes.data.LikesRepository
import com.fabledsword.minstrel.models.ArtistDetailRef import com.fabledsword.minstrel.models.ArtistDetailRef
import com.fabledsword.minstrel.models.ArtistRef import com.fabledsword.minstrel.models.ArtistRef
@@ -145,24 +146,7 @@ class ArtistDetailViewModel @Inject constructor(
*/ */
fun playArtist() { fun playArtist() {
viewModelScope.launch { viewModelScope.launch {
try { shufflePlayArtist(artistId, repository, player) { transientMessagesChannel.trySend(it) }
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)}",
)
}
} }
} }