diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt index a72d1f56..3c3fbc66 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt @@ -396,6 +396,22 @@ class PlayerController @Inject constructor( } } + /** + * Plays radio seeded from [trackId] now, replacing the queue: the seed + * plays first, then songs like it. Tapping a search result uses this. + * The track menu's Start radio ([startRadio]) differs on purpose: it + * keeps the current song playing and queues the radio after it. + * + * Returns false on an empty server response; throws on transport or + * non-2xx for the caller to handle. + */ + suspend fun playRadio(trackId: String): Boolean { + val tracks = radio.seed(trackId) + if (tracks.isEmpty()) return false + setQueue(tracks, initialIndex = 0, source = "radio:$trackId") + return true + } + /** * Mid-queue radio insert. The current track keeps playing; every * upcoming item is removed; the radio results are appended after. diff --git a/android/app/src/main/java/com/fabledsword/minstrel/search/ui/SearchViewModel.kt b/android/app/src/main/java/com/fabledsword/minstrel/search/ui/SearchViewModel.kt index 24a08477..fc090c84 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/search/ui/SearchViewModel.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/search/ui/SearchViewModel.kt @@ -10,6 +10,7 @@ import com.fabledsword.minstrel.models.TrackRef import com.fabledsword.minstrel.player.PlayerController import com.fabledsword.minstrel.search.data.SearchRepository import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -20,6 +21,7 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject private const val DEBOUNCE_MS = 250L @@ -104,21 +106,29 @@ class SearchViewModel @Inject constructor( } /** - * Tapping a search result builds a queue from the full visible - * track-results list starting at the tapped entry. - * Falls back to single-track playback when the loaded state isn't - * a Success (shouldn't happen in normal flow but keeps the call - * safe). + * Tapping a search result starts radio seeded from it: the tapped song + * plays first, then songs like it. The list of matches for a few typed + * words is rarely what someone wants to hear next. The web's search does + * the same (radioFromRow). + * + * When radio can't be had (offline, or the server returns nothing), the + * tapped song plays on its own, so a tap always plays what was tapped. */ fun playTrack(track: TrackRef) { if (track.streamUrl.isEmpty()) return - val loaded = internal.value.results as? SearchResultsState.Loaded - val playable = loaded?.response?.tracks - ?.filter { it.streamUrl.isNotEmpty() } - ?.takeIf { it.isNotEmpty() } - ?: listOf(track) - val startIndex = playable.indexOfFirst { it.id == track.id }.coerceAtLeast(0) - player.setQueue(playable, initialIndex = startIndex, source = "search") + viewModelScope.launch { + val started = try { + player.playRadio(track.id) + } catch (e: CancellationException) { + throw e + } catch ( + @Suppress("TooGenericExceptionCaught") e: Throwable, + ) { + Timber.i(e, "search: radio for %s failed; playing the track alone", track.id) + false + } + if (!started) player.setQueue(listOf(track), initialIndex = 0, source = "search") + } } private suspend fun runSearch(q: String) { diff --git a/web/src/lib/player/radioFromRow.ts b/web/src/lib/player/radioFromRow.ts new file mode 100644 index 00000000..db53230d --- /dev/null +++ b/web/src/lib/player/radioFromRow.ts @@ -0,0 +1,14 @@ +import { playRadio } from './store.svelte'; +import type { TrackRef } from '#lib/api/types.js'; + +/** + * A track row's play handler that starts radio seeded from the tapped + * track, rather than playing the list it sits in. Search results use it: + * a list of matches for a few typed words is rarely what someone wants to + * hear next, and radio plays the tapped song first, then songs like it. + * Android's search does the same (SearchViewModel.playTrack). + */ +export function radioFromRow(tracks: TrackRef[], index: number): void { + const track = tracks[index]; + if (track) void playRadio(track.id); +} diff --git a/web/src/routes/search/+page.svelte b/web/src/routes/search/+page.svelte index 119e5c28..cd4acefe 100644 --- a/web/src/routes/search/+page.svelte +++ b/web/src/routes/search/+page.svelte @@ -9,8 +9,7 @@ import SearchSkeleton from '#lib/components/SearchSkeleton.svelte'; import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte'; import { useDelayed } from '#lib/utils/useDelayed.svelte.js'; - import { playRadio } from '#lib/player/store.svelte.js'; - import type { TrackRef } from '#lib/api/types.js'; + import { radioFromRow } from '#lib/player/radioFromRow.js'; const q = $derived((page.url.searchParams.get('q') ?? '').trim()); const queryStore = $derived(q ? createSearchQuery(q) : null); @@ -28,10 +27,6 @@ ); const showSkeleton = useDelayed(() => !!query?.isPending); - - function onTrackPlay(_tracks: TrackRef[], index: number) { - playRadio(_tracks[index].id); - } {pageTitle('Search')} @@ -107,7 +102,7 @@ {#each tracks.items as t, i (t.id)} - + {/each} diff --git a/web/src/routes/search/tracks/+page.svelte b/web/src/routes/search/tracks/+page.svelte index 868eff1d..8ea67765 100644 --- a/web/src/routes/search/tracks/+page.svelte +++ b/web/src/routes/search/tracks/+page.svelte @@ -10,8 +10,7 @@ import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte'; import ListContinuation from '#lib/components/ListContinuation.svelte'; import { useDelayed } from '#lib/utils/useDelayed.svelte.js'; - import { playRadio } from '#lib/player/store.svelte.js'; - import type { TrackRef } from '#lib/api/types.js'; + import { radioFromRow } from '#lib/player/radioFromRow.js'; const q = $derived((page.url.searchParams.get('q') ?? '').trim()); const queryStore = $derived(q ? createSearchTracksInfiniteQuery(q) : null); @@ -20,10 +19,6 @@ const tracks = $derived(query?.data?.pages?.flatMap((p) => p.items) ?? []); const total = $derived(query?.data?.pages?.[0]?.total ?? 0); const showSkeleton = useDelayed(() => !!query?.isPending); - - function onTrackPlay(_tracks: TrackRef[], index: number) { - playRadio(_tracks[index].id); - } {pageTitle('Search ยท Tracks')} @@ -50,7 +45,7 @@ {:else} {#each tracks as t, i (t.id)} - + {/each}