Make My Wave play: run effects returned from async results

Inside scope.launch, `apply { core.waveBatch(...) }` resolved to
Kotlin's CoroutineScope.apply, not the controller's helper, so the
effects were dropped and nothing was published. My Wave got its first
batch but never fetched a stream; skipping an unplayable track could
stall the same way. Rename the helper to `update`.

Also, if a station's first batch fails, Play asks again instead of
doing nothing, and the hero shows why My Wave stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yuriy.pandClaude Opus 5.5 committed 2026-10-16 22:34:58 +06:00
1 parent ed4e2d0ac6
commit d7e173a644
5 files changed
+38 -20

No files matched your search

@@ -71,20 +71,20 @@ class PlaybackController(
// ---- Commands ----
fun play(tracks: List<Track>, start: Int, shuffle: Boolean = false) =
apply { core.play(tracks, start, if (shuffle) true else null) }
update { core.play(tracks, start, if (shuffle) true else null) }
fun playNext(track: Track) = apply { core.playNext(track) }
fun append(track: Track) = apply { core.append(track) }
fun playWave(station: String) = apply { core.playWave(station) }
fun waveRestart() = apply { core.waveRestart() }
fun toggle() = apply { core.toggle() }
fun next() = apply { core.next() }
fun previous() = apply { core.previous() }
fun toggleShuffle() = apply { core.toggleShuffle() }
fun cycleRepeat() = apply { core.cycleRepeat() }
fun jumpTo(orderPos: Int) = apply { core.jumpTo(orderPos) }
fun playNext(track: Track) = update { core.playNext(track) }
fun append(track: Track) = update { core.append(track) }
fun playWave(station: String) = update { core.playWave(station) }
fun waveRestart() = update { core.waveRestart() }
fun toggle() = update { core.toggle() }
fun next() = update { core.next() }
fun previous() = update { core.previous() }
fun toggleShuffle() = update { core.toggleShuffle() }
fun cycleRepeat() = update { core.cycleRepeat() }
fun jumpTo(orderPos: Int) = update { core.jumpTo(orderPos) }
fun seek(toMs: Long) = apply {
fun seek(toMs: Long) = update {
core.seek(toMs)
emptyList()
}
@@ -103,7 +103,7 @@ class PlaybackController(
publish()
}
private fun apply(change: () -> List<Effect>) {
private fun update(change: () -> List<Effect>) {
run(change())
publish()
}
@@ -126,16 +126,16 @@ class PlaybackController(
// ---- Engine events ----
override fun loaded(generation: Long, durationMs: Long?) = apply { core.onLoaded(generation, durationMs) }
override fun loaded(generation: Long, durationMs: Long?) = update { core.onLoaded(generation, durationMs) }
override fun position(generation: Long, positionMs: Long) {
core.onPosition(generation, positionMs)
this.positionMs = core.positionMs
}
override fun ended(generation: Long) = apply { core.onEnded(generation) }
override fun ended(generation: Long) = update { core.onEnded(generation) }
override fun failed(generation: Long, reason: String) = apply { core.onFailed(generation, reason) }
override fun failed(generation: Long, reason: String) = update { core.onFailed(generation, reason) }
override fun playingChanged(generation: Long, playing: Boolean) {
core.onPlayingChanged(generation, playing)
@@ -152,7 +152,7 @@ class PlaybackController(
is Effect.Fetch -> api?.let { c ->
scope.launch {
val result = runCatching { resolve(c, effect.trackId) }
apply { core.fetched(effect.generation, result) }
update { core.fetched(effect.generation, result) }
}
}
is Effect.Prefetch -> api?.let { c ->
@@ -169,7 +169,7 @@ class PlaybackController(
val session = effect.sessionId
if (session != null) c.waveMore(session, effect.queue) else c.waveStart(effect.station, settings)
}
apply { core.waveBatch(effect.station, effect.epoch, result) }
update { core.waveBatch(effect.station, effect.epoch, result) }
}
}
// Feedback and reports are fire-and-forget: a lost one only blunts recommendations.
@@ -205,6 +205,8 @@ class PlayerCore(private val random: Random = Random.Default) {
status = Status.Playing
}
queue != null -> return loadCurrent()
// A station whose first batch never came: ask again.
else -> wave?.let { return playWave(it.station) }
}
Status.Ended -> return loadCurrent()
Status.Loading -> {
@@ -138,7 +138,11 @@ fun WaveHero(state: AppState, big: Boolean, entry: FocusEntry?) {
val playing = status == Status.Playing
val settings = state.waveShown
val blurb = if (settings != null && !settings.isDefault) {
// Why My Wave stopped, when it did, so a failed start isn't silent here.
val notice = state.playback.state.takeIf { it.waveStation == MY_WAVE && status != Status.Playing }?.notice
val blurb = if (notice != null) {
noticeText(notice)
} else if (settings != null && !settings.isDefault) {
stringResource(
R.string.wave_tuned,
stringResource(moodLabel(settings.mood)).lowercase(),
@@ -77,7 +77,7 @@ import it.panov.musyx.tv.ui.theme.tabular
private const val QUEUE_SHOWN = 100
@Composable
private fun noticeText(notice: Notice): String = when (notice) {
internal fun noticeText(notice: Notice): String = when (notice) {
Notice.StationDry -> stringResource(R.string.notice_station_dry)
Notice.TooManyFailures -> stringResource(R.string.notice_too_many)
is Notice.Skipped -> stringResource(R.string.notice_skipped, notice.title)
@@ -191,6 +191,18 @@ class PlayerCoreTest {
assertEquals(0, p.queue!!.upcomingCount)
}
@Test
fun playAfterAFailedStartAsksTheStationAgain() {
val p = PlayerCore()
p.playWave("user:onyourwave")
assertTrue(p.waveBatch("user:onyourwave", 1, Result.failure(YmError.Network("down"))).isEmpty())
assertEquals(Status.Paused, p.status)
assertEquals(Notice.StationDry, p.notice)
assertEquals(listOf<Effect>(Effect.WaveFetch("user:onyourwave", null, emptyList(), 2)), p.toggle())
assertEquals(Status.Loading, p.status)
assertNull(p.notice)
}
@Test
fun batchesForAnotherStationAreIgnored() {
val p = waveWith(2)