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:
2026-11-27 10:16:40 -05:00
co-authored by Claude Opus 5.5
parent ebb8e331a2
commit d5d9952cd7
5 changed files with 56 additions and 26 deletions
@@ -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.
@@ -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) {