feat: tapping a search result starts radio from that track, on web and Android (DRY A3)
Android played the visible search results as a queue; the web started radio. Both now start radio seeded from the tapped track: the song plays first, then songs like it. Android falls back to playing the track alone when radio can't be had (offline, or an empty answer). The web's two identical onTrackPlay handlers (search and search/tracks) become one, radioFromRow. PlayerController gains playRadio, which replaces the queue; the track menu's startRadio still keeps the current song playing and queues radio after it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
* Mid-queue radio insert. The current track keeps playing; every
|
||||||
* upcoming item is removed; the radio results are appended after.
|
* upcoming item is removed; the radio results are appended after.
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.fabledsword.minstrel.models.TrackRef
|
|||||||
import com.fabledsword.minstrel.player.PlayerController
|
import com.fabledsword.minstrel.player.PlayerController
|
||||||
import com.fabledsword.minstrel.search.data.SearchRepository
|
import com.fabledsword.minstrel.search.data.SearchRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
@@ -20,6 +21,7 @@ import kotlinx.coroutines.flow.map
|
|||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
private const val DEBOUNCE_MS = 250L
|
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
|
* Tapping a search result starts radio seeded from it: the tapped song
|
||||||
* track-results list starting at the tapped entry.
|
* plays first, then songs like it. The list of matches for a few typed
|
||||||
* Falls back to single-track playback when the loaded state isn't
|
* words is rarely what someone wants to hear next. The web's search does
|
||||||
* a Success (shouldn't happen in normal flow but keeps the call
|
* the same (radioFromRow).
|
||||||
* safe).
|
*
|
||||||
|
* 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) {
|
fun playTrack(track: TrackRef) {
|
||||||
if (track.streamUrl.isEmpty()) return
|
if (track.streamUrl.isEmpty()) return
|
||||||
val loaded = internal.value.results as? SearchResultsState.Loaded
|
viewModelScope.launch {
|
||||||
val playable = loaded?.response?.tracks
|
val started = try {
|
||||||
?.filter { it.streamUrl.isNotEmpty() }
|
player.playRadio(track.id)
|
||||||
?.takeIf { it.isNotEmpty() }
|
} catch (e: CancellationException) {
|
||||||
?: listOf(track)
|
throw e
|
||||||
val startIndex = playable.indexOfFirst { it.id == track.id }.coerceAtLeast(0)
|
} catch (
|
||||||
player.setQueue(playable, initialIndex = startIndex, source = "search")
|
@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) {
|
private suspend fun runSearch(q: String) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -9,8 +9,7 @@
|
|||||||
import SearchSkeleton from '#lib/components/SearchSkeleton.svelte';
|
import SearchSkeleton from '#lib/components/SearchSkeleton.svelte';
|
||||||
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
|
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
|
||||||
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
|
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
|
||||||
import { playRadio } from '#lib/player/store.svelte.js';
|
import { radioFromRow } from '#lib/player/radioFromRow.js';
|
||||||
import type { TrackRef } from '#lib/api/types.js';
|
|
||||||
|
|
||||||
const q = $derived((page.url.searchParams.get('q') ?? '').trim());
|
const q = $derived((page.url.searchParams.get('q') ?? '').trim());
|
||||||
const queryStore = $derived(q ? createSearchQuery(q) : null);
|
const queryStore = $derived(q ? createSearchQuery(q) : null);
|
||||||
@@ -28,10 +27,6 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
const showSkeleton = useDelayed(() => !!query?.isPending);
|
const showSkeleton = useDelayed(() => !!query?.isPending);
|
||||||
|
|
||||||
function onTrackPlay(_tracks: TrackRef[], index: number) {
|
|
||||||
playRadio(_tracks[index].id);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head><title>{pageTitle('Search')}</title></svelte:head>
|
<svelte:head><title>{pageTitle('Search')}</title></svelte:head>
|
||||||
@@ -107,7 +102,7 @@
|
|||||||
</header>
|
</header>
|
||||||
<TrackList>
|
<TrackList>
|
||||||
{#each tracks.items as t, i (t.id)}
|
{#each tracks.items as t, i (t.id)}
|
||||||
<TrackRow tracks={tracks.items} index={i} onPlay={onTrackPlay} />
|
<TrackRow tracks={tracks.items} index={i} onPlay={radioFromRow} />
|
||||||
{/each}
|
{/each}
|
||||||
</TrackList>
|
</TrackList>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -10,8 +10,7 @@
|
|||||||
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
|
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
|
||||||
import ListContinuation from '#lib/components/ListContinuation.svelte';
|
import ListContinuation from '#lib/components/ListContinuation.svelte';
|
||||||
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
|
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
|
||||||
import { playRadio } from '#lib/player/store.svelte.js';
|
import { radioFromRow } from '#lib/player/radioFromRow.js';
|
||||||
import type { TrackRef } from '#lib/api/types.js';
|
|
||||||
|
|
||||||
const q = $derived((page.url.searchParams.get('q') ?? '').trim());
|
const q = $derived((page.url.searchParams.get('q') ?? '').trim());
|
||||||
const queryStore = $derived(q ? createSearchTracksInfiniteQuery(q) : null);
|
const queryStore = $derived(q ? createSearchTracksInfiniteQuery(q) : null);
|
||||||
@@ -20,10 +19,6 @@
|
|||||||
const tracks = $derived(query?.data?.pages?.flatMap((p) => p.items) ?? []);
|
const tracks = $derived(query?.data?.pages?.flatMap((p) => p.items) ?? []);
|
||||||
const total = $derived(query?.data?.pages?.[0]?.total ?? 0);
|
const total = $derived(query?.data?.pages?.[0]?.total ?? 0);
|
||||||
const showSkeleton = useDelayed(() => !!query?.isPending);
|
const showSkeleton = useDelayed(() => !!query?.isPending);
|
||||||
|
|
||||||
function onTrackPlay(_tracks: TrackRef[], index: number) {
|
|
||||||
playRadio(_tracks[index].id);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head><title>{pageTitle('Search · Tracks')}</title></svelte:head>
|
<svelte:head><title>{pageTitle('Search · Tracks')}</title></svelte:head>
|
||||||
@@ -50,7 +45,7 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<TrackList>
|
<TrackList>
|
||||||
{#each tracks as t, i (t.id)}
|
{#each tracks as t, i (t.id)}
|
||||||
<TrackRow tracks={tracks} index={i} onPlay={onTrackPlay} />
|
<TrackRow tracks={tracks} index={i} onPlay={radioFromRow} />
|
||||||
{/each}
|
{/each}
|
||||||
</TrackList>
|
</TrackList>
|
||||||
<ListContinuation
|
<ListContinuation
|
||||||
|
|||||||
Reference in New Issue
Block a user