Compare commits
79
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f69c230c4 | ||
|
|
725ddca950 | ||
|
|
d145fee35d | ||
|
|
611715154b | ||
|
|
23a82fb38d | ||
|
|
0de2437689 | ||
|
|
a251dce7e3 | ||
|
|
938dae7163 | ||
|
|
93365cb555 | ||
|
|
eeabdf1f2c | ||
|
|
3e258507bb | ||
|
|
9550d8daaf | ||
|
|
db393bbe65 | ||
|
|
7b7bd0c3e8 | ||
|
|
8019537b02 | ||
|
|
e41d603c12 | ||
|
|
a62f07bd3a | ||
|
|
3c646c6974 | ||
|
|
9a57dc4bec | ||
|
|
f167ddfbfb | ||
|
|
962b4dbc8c | ||
|
|
aa4089118e | ||
|
|
7c791dc8e4 | ||
|
|
11466e1525 | ||
|
|
301c3bfb86 | ||
|
|
4d8c7d6566 | ||
|
|
d6e6caa223 | ||
|
|
8b08482d13 | ||
|
|
7cf04fe24b | ||
|
|
222a0ff636 | ||
|
|
d75c1ae37f | ||
|
|
3c4c27fb08 | ||
|
|
a62a20b599 | ||
|
|
d9b2dd957c | ||
|
|
46dcd38fd8 | ||
|
|
7838038047 | ||
|
|
b64965b38d | ||
|
|
bf2f9f3811 | ||
|
|
deb726a285 | ||
|
|
7ede83a586 | ||
|
|
1d7b91333f | ||
|
|
883d416d26 | ||
|
|
09471a8f5c | ||
|
|
7de238e91e | ||
|
|
9440c5860b | ||
|
|
082d31bfa9 | ||
|
|
8847b43d9e | ||
|
|
09cc810e5a | ||
|
|
2534384ed1 | ||
|
|
9ff1b30e3f | ||
|
|
c01853577b | ||
|
|
747ed4134b | ||
|
|
ccbd3b62a0 | ||
|
|
19de0c2874 | ||
|
|
22a4649bfc | ||
|
|
8e7660c05e | ||
|
|
53be834e89 | ||
|
|
0d410630a2 | ||
|
|
62db8edcdb | ||
|
|
ec0cc37bc9 | ||
|
|
e772938a3b | ||
|
|
29fee5aa37 | ||
|
|
e5ab471ce1 | ||
|
|
573aa4226d | ||
|
|
7339815ea9 | ||
|
|
baa601765e | ||
|
|
0d009b34e2 | ||
|
|
f1b4652c77 | ||
|
|
e610948307 | ||
|
|
fb811804d2 | ||
|
|
37134950a5 | ||
|
|
fcded9294c | ||
|
|
42abb7adff | ||
|
|
04933f2d9f | ||
|
|
626fc7502c | ||
|
|
9bf3b8a2f2 | ||
|
|
492460cf4a | ||
|
|
1c775905d7 | ||
|
|
cd2ff648d0 |
+24
-106
@@ -17,7 +17,6 @@ import com.fabledsword.minstrel.player.output.ActiveUpnpHolder
|
||||
import com.fabledsword.minstrel.player.output.upnp.SoapFaultException
|
||||
import com.fabledsword.minstrel.player.output.upnp.TransportState
|
||||
import java.io.IOException
|
||||
import kotlin.math.abs
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
@@ -59,10 +58,8 @@ import timber.log.Timber
|
||||
* native queue via ClearQueue + AddURIToQueue, then points the
|
||||
* transport at x-rincon-queue:<udn>#0. Skip/prev/seekTo delegate to
|
||||
* AVTransport Next/Previous/SeekToTrack so Sonos manages gap-free
|
||||
* advance natively. PollLoop keeps the local cursor aligned to the
|
||||
* track the renderer is actually playing, matched by track identity
|
||||
* (the id in GetPositionInfo's TrackURI) so it survives queue-reload
|
||||
* index drift -- see [syncLocalCursorToRemote].
|
||||
* advance natively. PollLoop syncs the local cursor by comparing the
|
||||
* 1-based Track index from GetPositionInfo.
|
||||
*/
|
||||
class MinstrelForwardingPlayer(
|
||||
private val delegate: Player,
|
||||
@@ -342,23 +339,6 @@ class MinstrelForwardingPlayer(
|
||||
}
|
||||
}
|
||||
|
||||
// The system media notification / lock-screen / Android Auto / Wear 'next'
|
||||
// and 'previous' buttons issue COMMAND_SEEK_TO_NEXT / COMMAND_SEEK_TO_PREVIOUS
|
||||
// -> Player.seekToNext() / seekToPrevious(), which are DISTINCT from the
|
||||
// *MediaItem variants the in-app transport buttons call. Un-overridden, the
|
||||
// base ForwardingPlayer forwards these to the paused local delegate -- so
|
||||
// the notification next/prev only nudged the local cursor (which the poll
|
||||
// then re-synced back to Sonos), reading as dead buttons while casting.
|
||||
// Route them through the same Sonos path as the media-item variants when a
|
||||
// UPnP route is engaged; plain local playback keeps the default behaviour.
|
||||
override fun seekToNext() {
|
||||
if (isRemote() || isLoadingUpnp()) seekToNextMediaItem() else super.seekToNext()
|
||||
}
|
||||
|
||||
override fun seekToPrevious() {
|
||||
if (isRemote() || isLoadingUpnp()) seekToPreviousMediaItem() else super.seekToPrevious()
|
||||
}
|
||||
|
||||
override fun getCurrentPosition(): Long =
|
||||
if (isRemote()) remoteState.positionMs else super.getCurrentPosition()
|
||||
|
||||
@@ -546,8 +526,18 @@ class MinstrelForwardingPlayer(
|
||||
|
||||
/**
|
||||
* One poll tick: read position + transport state from Sonos, apply to
|
||||
* [remoteState], and sync the local cursor to the track the renderer is
|
||||
* actually playing (see [syncLocalCursorToRemote]).
|
||||
* [remoteState], and forward-sync the local cursor to Sonos's Track
|
||||
* index when not in queue load.
|
||||
*
|
||||
* Cursor sync is gated on `holder.target == null` (= not loading)
|
||||
* because during load Sonos reports Track=1 while we're still
|
||||
* appending, and syncing would race the SetAV+Seek that lands
|
||||
* after. Outside load, forward sync catches Sonos auto-advances
|
||||
* (queue end-of-track), Sonos-app driven Next presses, and any
|
||||
* drift after a brief poll-failure burst that didn't trip the
|
||||
* drop threshold. Forward-only because a Next override we just
|
||||
* issued can race with a poll still reporting the prior Track --
|
||||
* the next poll catches up safely.
|
||||
*/
|
||||
private suspend fun pollOnce(active: ActiveUpnp) {
|
||||
val info = active.avTransport.getPositionInfo()
|
||||
@@ -563,7 +553,7 @@ class MinstrelForwardingPlayer(
|
||||
trackUri = info.trackUri,
|
||||
trackNumber = info.track,
|
||||
)
|
||||
syncLocalCursorToRemote(sonosTrack = info.track, trackUri = info.trackUri)
|
||||
maybeSyncLocalCursor(info.track)
|
||||
val transport = active.avTransport.getTransportInfo()
|
||||
when (transport.state) {
|
||||
TransportState.PLAYING -> {
|
||||
@@ -587,95 +577,23 @@ class MinstrelForwardingPlayer(
|
||||
notifyRemoteStateChanged()
|
||||
}
|
||||
|
||||
/**
|
||||
* Align the paused local delegate cursor to the track the renderer is
|
||||
* actually playing, so the un-overridden current-item getters
|
||||
* (getCurrentMediaItem/Index -- what both the in-app player UI via
|
||||
* PlayerController.onEvents AND the MediaSession notification read) always
|
||||
* resolve to that track. This is THE single source of truth for "what's
|
||||
* playing" during a cast.
|
||||
*
|
||||
* Identity-first: match the track-id embedded in the renderer's current URI
|
||||
* against the delegate's MediaItem.mediaId (PlayerController sets mediaId =
|
||||
* TrackRef.id), so the index-offset wobble across queue reloads / re-casts
|
||||
* can never park the cursor on a stale (pre-cast) track. Falls back to the
|
||||
* 1-based Sonos Track index only when the URI can't be resolved to a queue
|
||||
* item. Unlike the old forward-only sync this moves the cursor in BOTH
|
||||
* directions -- a renderer Previous / re-cast to a lower track pulls it back
|
||||
* too. Runs on the application looper (delegate access contract).
|
||||
*
|
||||
* Suppressed while:
|
||||
* - loading ([isLoadingUpnp]) -- the native Sonos queue is still being
|
||||
* (re)uploaded, so its Track index is meaningless; and
|
||||
* - a user transport (next/prev/seekTo idx) is pending Sonos's ack --
|
||||
* the override already advanced the delegate optimistically, and Sonos
|
||||
* still reports the OLD track for a beat, so syncing now would undo the
|
||||
* user's press. The pending deadline is honoured directly so a missed
|
||||
* clear can't wedge the sync.
|
||||
*/
|
||||
private fun syncLocalCursorToRemote(sonosTrack: Int, trackUri: String) {
|
||||
if (isLoadingUpnp()) return
|
||||
val pendingDeadline = remoteState.pendingTransportDeadlineMs
|
||||
if (pendingDeadline > 0L && SystemClock.elapsedRealtime() < pendingDeadline) return
|
||||
private fun maybeSyncLocalCursor(sonosTrack: Int) {
|
||||
if (holder.target.value != null) return
|
||||
if (sonosTrack <= 0) return
|
||||
val sonosIdx = sonosTrack - 1
|
||||
handler.post {
|
||||
val target = resolveRemoteCursorIndex(sonosTrack, trackUri) ?: return@post
|
||||
if (target != delegate.currentMediaItemIndex) {
|
||||
val localIdx = delegate.currentMediaItemIndex
|
||||
if (sonosIdx > localIdx && sonosIdx < delegate.mediaItemCount) {
|
||||
Timber.w(
|
||||
"UPnP cursor sync: local=%d -> %d",
|
||||
delegate.currentMediaItemIndex, target,
|
||||
"UPnP cursor catch-up: local=%d -> sonos=%d",
|
||||
localIdx, sonosIdx,
|
||||
)
|
||||
delegate.seekTo(target, 0L)
|
||||
delegate.seekTo(sonosIdx, 0L)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Most-authoritative -> most-degraded (project rule #48): the track-id
|
||||
* parsed from the renderer's current URI wins; else the 1-based Sonos Track
|
||||
* index; else null (leave the cursor put rather than surface a wrong track).
|
||||
* Must run on the application looper -- reads the delegate timeline.
|
||||
*/
|
||||
private fun resolveRemoteCursorIndex(sonosTrack: Int, trackUri: String): Int? {
|
||||
val byId = trackIdFromStreamUri(trackUri)?.let { id ->
|
||||
nearestIndexWithMediaId(id, preferNear = sonosTrack - 1)
|
||||
}
|
||||
return byId ?: (sonosTrack - 1).takeIf { it in 0 until delegate.mediaItemCount }
|
||||
}
|
||||
|
||||
/**
|
||||
* Index of the delegate MediaItem whose mediaId == [mediaId], preferring the
|
||||
* occurrence nearest [preferNear] so a track that appears twice in the queue
|
||||
* doesn't snap the cursor across the queue. null if the id isn't present.
|
||||
*/
|
||||
private fun nearestIndexWithMediaId(mediaId: String, preferNear: Int): Int? {
|
||||
var best: Int? = null
|
||||
var bestDist = Int.MAX_VALUE
|
||||
for (i in 0 until delegate.mediaItemCount) {
|
||||
if (delegate.getMediaItemAt(i).mediaId == mediaId) {
|
||||
val dist = abs(i - preferNear)
|
||||
if (dist < bestDist) {
|
||||
best = i
|
||||
bestDist = dist
|
||||
}
|
||||
}
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
/** Extract `{id}` from a `.../api/tracks/{id}/stream...` stream URI. */
|
||||
private fun trackIdFromStreamUri(uri: String): String? {
|
||||
val start = uri.indexOf(TRACKS_PATH_MARKER)
|
||||
if (start < 0) return null
|
||||
val idStart = start + TRACKS_PATH_MARKER.length
|
||||
val idEnd = uri.indexOf('/', idStart)
|
||||
return if (idEnd > idStart) uri.substring(idStart, idEnd) else null
|
||||
}
|
||||
|
||||
private companion object {
|
||||
// Path segment that precedes the track-id in a stream URI
|
||||
// (…/api/tracks/{id}/stream…) — used to map the renderer's current URI
|
||||
// back to a delegate MediaItem by identity.
|
||||
const val TRACKS_PATH_MARKER = "/api/tracks/"
|
||||
const val POLL_INTERVAL_MS = 1_000L
|
||||
const val NON_PLAYING_CONFIRM = 2
|
||||
const val SEEK_ACK_WINDOW_MS = 2_000L
|
||||
|
||||
@@ -598,19 +598,18 @@ class PlayerController @Inject constructor(
|
||||
}
|
||||
|
||||
/**
|
||||
* One position-polling tick. Patches ONLY the smoothly-changing transport
|
||||
* fields (position / duration / play-pause / buffer) via `.copy()`. The
|
||||
* queue index + current track are owned solely by [onEvents], which reads
|
||||
* the authoritative delegate cursor: during a cast,
|
||||
* [MinstrelForwardingPlayer.syncLocalCursorToRemote] keeps that cursor on
|
||||
* the track the renderer is actually playing (matched by identity), so there
|
||||
* is exactly one writer of "what's playing" and a stale tick can never
|
||||
* revert it to the old track. (Previously the tick was a second index writer
|
||||
* that fought onEvents — the flicker-to-stale-track bug.)
|
||||
* One position-polling tick. Owns track-change detection too: when UPnP
|
||||
* is active and the wrapped ExoPlayer is paused, `delegate.seekTo` from
|
||||
* `maybeSyncLocalCursor` may not fire `onMediaItemTransition`, leaving
|
||||
* uiState.queueIndex stuck on the old track even after Sonos has
|
||||
* advanced. So the tick reads Sonos's reported Track as the source of
|
||||
* truth, rebuilds the index/title fields itself, and force-syncs the
|
||||
* wrapped player as defense in depth.
|
||||
*/
|
||||
private fun tickPositionPoll(controller: MediaController) {
|
||||
val upnpActive = activeUpnpHolder.active.value != null
|
||||
resolvePendingTransport(controller, upnpActive)
|
||||
val pendingTransport = upnpActive && remoteState.pendingTransportDeadlineMs > 0L
|
||||
val effectiveIsPlaying =
|
||||
if (upnpActive) remoteState.isPlaying else controller.isPlaying
|
||||
val effectivePosition = if (upnpActive) {
|
||||
@@ -618,29 +617,27 @@ class PlayerController @Inject constructor(
|
||||
} else {
|
||||
controller.currentPosition
|
||||
}
|
||||
val idx = controller.currentMediaItemIndex
|
||||
val desiredIdx = desiredQueueIndex(controller, upnpActive)
|
||||
val current = uiStateInternal.value
|
||||
val newPos = effectivePosition.coerceAtLeast(0)
|
||||
val newDur = effectiveDuration(
|
||||
upnpActive,
|
||||
remoteState.durationMs,
|
||||
controller.duration,
|
||||
desiredIdx = idx,
|
||||
controllerIdx = idx,
|
||||
desiredIdx = desiredIdx,
|
||||
controllerIdx = controller.currentMediaItemIndex,
|
||||
)
|
||||
val newBuf = controller.bufferedPosition.coerceAtLeast(0)
|
||||
val somethingChanged = current.isPlaying != effectiveIsPlaying ||
|
||||
current.positionMs != newPos ||
|
||||
current.durationMs != newDur ||
|
||||
current.bufferedPositionMs != newBuf
|
||||
if (somethingChanged) {
|
||||
uiStateInternal.value = current.copy(
|
||||
isPlaying = effectiveIsPlaying,
|
||||
positionMs = newPos,
|
||||
durationMs = newDur,
|
||||
bufferedPositionMs = newBuf,
|
||||
)
|
||||
}
|
||||
// Track adjustments are forward-only AND suppressed while a user
|
||||
// transport press is pending Sonos confirmation. Together those keep
|
||||
// either direction of user input from being undone by a stale poll.
|
||||
val trackChanged = !pendingTransport &&
|
||||
desiredIdx > current.queueIndex &&
|
||||
desiredIdx in queueRefs.indices
|
||||
publishTickIfChanged(
|
||||
current, trackChanged, desiredIdx,
|
||||
effectiveIsPlaying, newPos, newDur, newBuf,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -657,6 +654,48 @@ class PlayerController @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("LongParameterList") // assembled at one tick call site; refactor would cost clarity
|
||||
private fun publishTickIfChanged(
|
||||
current: PlayerUiState,
|
||||
trackChanged: Boolean,
|
||||
desiredIdx: Int,
|
||||
effectiveIsPlaying: Boolean,
|
||||
newPos: Long,
|
||||
newDur: Long,
|
||||
newBuf: Long,
|
||||
) {
|
||||
val somethingChanged = trackChanged ||
|
||||
current.isPlaying != effectiveIsPlaying ||
|
||||
current.positionMs != newPos ||
|
||||
current.durationMs != newDur
|
||||
if (!somethingChanged) return
|
||||
val newTrack = if (trackChanged) queueRefs[desiredIdx] else current.currentTrack
|
||||
val newIdx = if (trackChanged) desiredIdx else current.queueIndex
|
||||
uiStateInternal.value = current.copy(
|
||||
currentTrack = newTrack,
|
||||
queueIndex = newIdx,
|
||||
isPlaying = effectiveIsPlaying,
|
||||
positionMs = newPos,
|
||||
durationMs = newDur,
|
||||
bufferedPositionMs = newBuf,
|
||||
)
|
||||
// Intentionally do NOT call controller.seekTo here. That would route
|
||||
// through MinstrelForwardingPlayer's seekTo override and re-issue
|
||||
// AVTransport.SeekToTrack to Sonos -- which seeks Sonos back to the
|
||||
// start of the same track it's already playing, restarting the song.
|
||||
// The wrapped player's index is kept in sync by maybeSyncLocalCursor's
|
||||
// delegate.seekTo (which bypasses the override). If it lags briefly,
|
||||
// the next pollOnce catches up; the uiState above already reflects
|
||||
// Sonos's truth for the user.
|
||||
}
|
||||
|
||||
private fun desiredQueueIndex(controller: MediaController, upnpActive: Boolean): Int =
|
||||
if (upnpActive) {
|
||||
(remoteState.trackNumber - 1).coerceAtLeast(0)
|
||||
} else {
|
||||
controller.currentMediaItemIndex
|
||||
}
|
||||
|
||||
// ── Remote position interpolation state ──────────────────────────────
|
||||
// remoteState.positionMs is only refreshed by ForwardingPlayer's 1Hz
|
||||
// SOAP poll (and only when the round-trip completes -- screen-off WiFi
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
package com.fabledsword.minstrel.player.ui
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.fabledsword.minstrel.likes.data.LikesRepository
|
||||
import com.fabledsword.minstrel.player.PlayerController
|
||||
import com.fabledsword.minstrel.player.PlayerUiState
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
@@ -27,26 +22,11 @@ import javax.inject.Inject
|
||||
@HiltViewModel
|
||||
class PlayerViewModel @Inject constructor(
|
||||
private val controller: PlayerController,
|
||||
private val likes: LikesRepository,
|
||||
) : ViewModel() {
|
||||
|
||||
val uiState: StateFlow<PlayerUiState> = controller.uiState
|
||||
val dropEvents: SharedFlow<String> = controller.dropEvents
|
||||
|
||||
/**
|
||||
* Reactive set of liked track ids — the set-based pattern the queue
|
||||
* (and playlist/album/artist detail) uses to color each row's
|
||||
* [com.fabledsword.minstrel.shared.widgets.LikeButton] without a
|
||||
* per-row Flow subscription.
|
||||
*/
|
||||
val likedTrackIds: StateFlow<Set<String>> =
|
||||
likes.observeLikedTrackIds()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(SHARE_STOP_TIMEOUT_MS),
|
||||
initialValue = emptySet(),
|
||||
)
|
||||
|
||||
fun play() = controller.play()
|
||||
fun pause() = controller.pause()
|
||||
fun seekTo(positionMs: Long) = controller.seekTo(positionMs)
|
||||
@@ -55,13 +35,4 @@ class PlayerViewModel @Inject constructor(
|
||||
fun seekToIndex(index: Int) = controller.seekToIndex(index)
|
||||
fun toggleShuffle() = controller.toggleShuffle()
|
||||
fun cycleRepeat() = controller.cycleRepeat()
|
||||
|
||||
fun toggleLikeTrack(trackId: String) {
|
||||
val desired = trackId !in likedTrackIds.value
|
||||
viewModelScope.launch {
|
||||
likes.toggleLike(LikesRepository.ENTITY_TRACK, trackId, desired)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val SHARE_STOP_TIMEOUT_MS = 5_000L
|
||||
|
||||
@@ -11,7 +11,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -37,7 +36,6 @@ import com.composables.icons.lucide.Volume2
|
||||
import com.fabledsword.minstrel.models.TrackRef
|
||||
import com.fabledsword.minstrel.shared.formatDuration
|
||||
import com.fabledsword.minstrel.shared.widgets.EmptyState
|
||||
import com.fabledsword.minstrel.shared.widgets.LikeButton
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -46,7 +44,6 @@ fun QueueScreen(
|
||||
viewModel: PlayerViewModel = hiltViewModel(),
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val likedTrackIds by viewModel.likedTrackIds.collectAsStateWithLifecycle()
|
||||
Scaffold(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
topBar = {
|
||||
@@ -70,9 +67,7 @@ fun QueueScreen(
|
||||
QueueList(
|
||||
tracks = state.queue,
|
||||
currentIndex = state.queueIndex,
|
||||
likedTrackIds = likedTrackIds,
|
||||
onJumpTo = viewModel::seekToIndex,
|
||||
onToggleLike = viewModel::toggleLikeTrack,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -83,25 +78,14 @@ fun QueueScreen(
|
||||
private fun QueueList(
|
||||
tracks: List<TrackRef>,
|
||||
currentIndex: Int,
|
||||
likedTrackIds: Set<String>,
|
||||
onJumpTo: (Int) -> Unit,
|
||||
onToggleLike: (String) -> Unit,
|
||||
) {
|
||||
// Open scrolled to the now-playing track so it's in view immediately.
|
||||
// Seeding the initial index (rather than animating post-layout) avoids a
|
||||
// flash of the list top; it's captured once per entry, so the view stays
|
||||
// put as the track later auto-advances — matching "show me where I am now."
|
||||
val listState = rememberLazyListState(
|
||||
initialFirstVisibleItemIndex = currentIndex.coerceIn(0, tracks.lastIndex),
|
||||
)
|
||||
LazyColumn(state = listState, modifier = Modifier.fillMaxSize()) {
|
||||
LazyColumn(modifier = Modifier.fillMaxSize()) {
|
||||
itemsIndexed(items = tracks, key = { _, track -> track.id }) { index, track ->
|
||||
QueueRow(
|
||||
track = track,
|
||||
isCurrent = index == currentIndex,
|
||||
liked = track.id in likedTrackIds,
|
||||
onClick = { onJumpTo(index) },
|
||||
onToggleLike = { onToggleLike(track.id) },
|
||||
)
|
||||
HorizontalDivider()
|
||||
}
|
||||
@@ -109,13 +93,7 @@ private fun QueueList(
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun QueueRow(
|
||||
track: TrackRef,
|
||||
isCurrent: Boolean,
|
||||
liked: Boolean,
|
||||
onClick: () -> Unit,
|
||||
onToggleLike: () -> Unit,
|
||||
) {
|
||||
private fun QueueRow(track: TrackRef, isCurrent: Boolean, onClick: () -> Unit) {
|
||||
val highlight = if (isCurrent) {
|
||||
MaterialTheme.colorScheme.primary.copy(alpha = HIGHLIGHT_ALPHA)
|
||||
} else {
|
||||
@@ -164,7 +142,6 @@ private fun QueueRow(
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
LikeButton(liked = liked, onToggle = onToggleLike)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"extends": [
|
||||
"config:recommended",
|
||||
":semanticCommits"
|
||||
],
|
||||
"baseBranches": ["dev"],
|
||||
"timezone": "America/New_York",
|
||||
"schedule": ["every weekend"],
|
||||
"prHourlyLimit": 2,
|
||||
"prConcurrentLimit": 8,
|
||||
"ignorePaths": [
|
||||
"**/node_modules/**",
|
||||
"**/vendor/**",
|
||||
"flutter_client/**"
|
||||
],
|
||||
"lockFileMaintenance": {
|
||||
"enabled": true,
|
||||
"schedule": ["before 5am on the first day of the month"]
|
||||
},
|
||||
"packageRules": [
|
||||
{
|
||||
"description": "Auto-merge patch/minor/digest/pin bumps once CI is green",
|
||||
"matchUpdateTypes": ["minor", "patch", "digest", "pin"],
|
||||
"automerge": true
|
||||
},
|
||||
{
|
||||
"description": "Hold all major bumps for manual approval via the dependency dashboard",
|
||||
"matchUpdateTypes": ["major"],
|
||||
"automerge": false,
|
||||
"dependencyDashboardApproval": true,
|
||||
"addLabels": ["deps", "deps:major"]
|
||||
},
|
||||
{
|
||||
"description": "Group Go module updates into one PR",
|
||||
"matchManagers": ["gomod"],
|
||||
"groupName": "go modules"
|
||||
},
|
||||
{
|
||||
"description": "Group CI workflow action bumps",
|
||||
"matchManagers": ["github-actions"],
|
||||
"groupName": "ci actions"
|
||||
},
|
||||
{
|
||||
"description": "Group Docker base-image bumps (Dockerfile + compose)",
|
||||
"matchManagers": ["dockerfile", "docker-compose"],
|
||||
"groupName": "docker images"
|
||||
},
|
||||
{
|
||||
"description": "Group the Android Gradle/Kotlin toolchain",
|
||||
"matchManagers": ["gradle", "gradle-wrapper"],
|
||||
"groupName": "android gradle"
|
||||
},
|
||||
{
|
||||
"description": "Group web npm non-major bumps",
|
||||
"matchManagers": ["npm"],
|
||||
"matchUpdateTypes": ["minor", "patch"],
|
||||
"groupName": "web npm (non-major)"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -35,9 +35,5 @@
|
||||
transition-transform duration-200
|
||||
{player.queueDrawerOpen ? 'translate-x-0' : 'translate-x-full'}"
|
||||
>
|
||||
<QueueList
|
||||
onClose={() => closeQueueDrawer()}
|
||||
active={player.queueDrawerOpen}
|
||||
bind:closeButtonRef={closeButton}
|
||||
/>
|
||||
<QueueList onClose={() => closeQueueDrawer()} bind:closeButtonRef={closeButton} />
|
||||
</aside>
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/svelte';
|
||||
import { emptyLikesMock } from '../../test-utils/mocks/likes';
|
||||
import QueueDrawer from './QueueDrawer.svelte';
|
||||
import type { TrackRef } from '$lib/api/types';
|
||||
import { makeTrack } from '$test-utils/fixtures/track';
|
||||
|
||||
// QueueTrackRow (rendered inside QueueDrawer → QueueList) now renders a
|
||||
// LikeButton, which reads createLikedIdsQuery. Stub the likes API so the
|
||||
// rows don't need a real QueryClient in context. Both mocks — and the
|
||||
// emptyLikesMock import — must precede the QueueDrawer import below, since
|
||||
// loading the component evaluates the mocked modules and the hoisted
|
||||
// factories need their referenced bindings already initialized.
|
||||
vi.mock('$lib/api/likes', () => emptyLikesMock());
|
||||
|
||||
const closeQueueDrawer = vi.fn();
|
||||
let queueValue: TrackRef[] = [];
|
||||
let indexValue = 0;
|
||||
@@ -25,16 +17,12 @@ vi.mock('$lib/player/store.svelte', () => ({
|
||||
get queueDrawerOpen() { return openValue; }
|
||||
},
|
||||
// QueueTrackRow imports these from the store; provide stubs so its
|
||||
// module-load doesn't break when QueueDrawer renders rows. QueueList
|
||||
// imports clearQueue for its header action.
|
||||
// module-load doesn't break when QueueDrawer renders rows.
|
||||
playFromQueueIndex: vi.fn(),
|
||||
removeFromQueue: vi.fn(),
|
||||
moveQueueItem: vi.fn(),
|
||||
clearQueue: vi.fn()
|
||||
moveQueueItem: vi.fn()
|
||||
}));
|
||||
|
||||
import QueueDrawer from './QueueDrawer.svelte';
|
||||
|
||||
const t = (id: string): TrackRef => makeTrack({ id, title: `Song ${id}` });
|
||||
|
||||
describe('QueueDrawer', () => {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { untrack } from 'svelte';
|
||||
import { X, Trash2, ArrowDown } from 'lucide-svelte';
|
||||
import { player, clearQueue } from '$lib/player/store.svelte';
|
||||
import { X } from 'lucide-svelte';
|
||||
import { player } from '$lib/player/store.svelte';
|
||||
import QueueTrackRow from './QueueTrackRow.svelte';
|
||||
|
||||
// onClose: when provided, renders an X button in the header so the
|
||||
@@ -9,66 +8,12 @@
|
||||
// now-playing route (visible at lg+ widths) omits it.
|
||||
// closeButtonRef: bind:this hook so the drawer can focus the X for
|
||||
// keyboard users on open.
|
||||
// active: true when the queue is on-screen (drawer open, or the always-
|
||||
// visible now-playing panel). Gates the scroll-to-current behavior.
|
||||
type Props = {
|
||||
onClose?: () => void;
|
||||
closeButtonRef?: HTMLButtonElement;
|
||||
active?: boolean;
|
||||
};
|
||||
|
||||
let { onClose, closeButtonRef = $bindable(), active = true }: Props = $props();
|
||||
|
||||
let scrollBody: HTMLElement | undefined = $state();
|
||||
// Whether the now-playing row is (at least partly) within the scroll
|
||||
// viewport. Drives auto-follow (only follow while the user is watching the
|
||||
// current track) and the "Jump to current" pill (shown when it's off-screen).
|
||||
let currentInView = $state(true);
|
||||
let sawFirstIndex = false;
|
||||
|
||||
function scrollToCurrent(block: ScrollLogicalPosition, behavior: ScrollBehavior = 'auto') {
|
||||
(scrollBody?.children[player.index] as HTMLElement | undefined)?.scrollIntoView({
|
||||
block,
|
||||
behavior,
|
||||
});
|
||||
currentInView = true;
|
||||
}
|
||||
|
||||
function recomputeInView() {
|
||||
const row = scrollBody?.children[player.index] as HTMLElement | undefined;
|
||||
if (!scrollBody || !row) {
|
||||
currentInView = true;
|
||||
return;
|
||||
}
|
||||
const b = scrollBody.getBoundingClientRect();
|
||||
const r = row.getBoundingClientRect();
|
||||
currentInView = r.bottom > b.top && r.top < b.bottom;
|
||||
}
|
||||
|
||||
// On open (active flips true, or on mount for the always-visible panel),
|
||||
// center the now-playing row — parity with the Android queue.
|
||||
$effect(() => {
|
||||
if (!active) return;
|
||||
if (untrack(() => player.queue.length) === 0) return;
|
||||
requestAnimationFrame(() => scrollToCurrent('center'));
|
||||
});
|
||||
|
||||
// Follow the current track as it auto-advances, but only while the user is
|
||||
// still watching it — if they've scrolled away, leave them there (the pill
|
||||
// offers the way back). block:'nearest' keeps it minimal (no yank when the
|
||||
// row is already visible). Index is tracked; currentInView is read untracked
|
||||
// so a scroll that hides the row doesn't itself re-trigger a scroll.
|
||||
$effect(() => {
|
||||
player.index; // subscribe: follow on advance
|
||||
if (!sawFirstIndex) {
|
||||
sawFirstIndex = true;
|
||||
return; // the open effect already handled the initial position
|
||||
}
|
||||
if (!active) return;
|
||||
if (untrack(() => player.queue.length) === 0) return;
|
||||
if (!untrack(() => currentInView)) return;
|
||||
requestAnimationFrame(() => scrollToCurrent('nearest'));
|
||||
});
|
||||
let { onClose, closeButtonRef = $bindable() }: Props = $props();
|
||||
|
||||
function totalDurationLabel(tracks: { duration_sec: number }[]): string {
|
||||
const totalSec = tracks.reduce((s, tr) => s + (tr.duration_sec ?? 0), 0);
|
||||
@@ -78,7 +23,7 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="relative flex h-full flex-col">
|
||||
<div class="flex h-full flex-col">
|
||||
<div class="flex items-center justify-between border-b border-border px-4 py-3">
|
||||
<div>
|
||||
<h2 class="text-lg font-semibold">Queue</h2>
|
||||
@@ -87,33 +32,20 @@
|
||||
{#if player.queue.length > 0} · {totalDurationLabel(player.queue)}{/if}
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex items-center gap-1">
|
||||
{#if player.queue.length > 0}
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Clear queue"
|
||||
title="Clear queue"
|
||||
onclick={() => clearQueue()}
|
||||
class="rounded p-1 text-text-secondary hover:text-text-primary"
|
||||
>
|
||||
<Trash2 size={18} />
|
||||
</button>
|
||||
{/if}
|
||||
{#if onClose}
|
||||
<button
|
||||
type="button"
|
||||
bind:this={closeButtonRef}
|
||||
aria-label="Close queue"
|
||||
onclick={onClose}
|
||||
class="rounded p-1 text-text-secondary hover:text-text-primary"
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{#if onClose}
|
||||
<button
|
||||
type="button"
|
||||
bind:this={closeButtonRef}
|
||||
aria-label="Close queue"
|
||||
onclick={onClose}
|
||||
class="text-text-secondary hover:text-text-primary"
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div bind:this={scrollBody} onscroll={recomputeInView} class="flex-1 overflow-y-auto">
|
||||
<div class="flex-1 overflow-y-auto">
|
||||
{#if player.queue.length === 0}
|
||||
<p class="text-text-secondary text-center p-8">No tracks queued.</p>
|
||||
{:else}
|
||||
@@ -122,17 +54,4 @@
|
||||
{/each}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if active && player.queue.length > 0 && !currentInView}
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => scrollToCurrent('center', 'smooth')}
|
||||
class="absolute bottom-4 left-1/2 flex -translate-x-1/2 items-center gap-1.5
|
||||
rounded-full bg-action-secondary px-3 py-1.5 text-xs font-medium
|
||||
text-action-fg shadow-lg"
|
||||
>
|
||||
<ArrowDown size={14} />
|
||||
Jump to current
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
import { draggable, type DragEventData } from '@neodrag/svelte';
|
||||
import type { TrackRef } from '$lib/api/types';
|
||||
import { playFromQueueIndex, removeFromQueue, moveQueueItem } from '$lib/player/store.svelte';
|
||||
import { coverUrl, FALLBACK_COVER } from '$lib/media/covers';
|
||||
import { offsetToDelta } from './queue-row-math';
|
||||
import LikeButton from './LikeButton.svelte';
|
||||
|
||||
let { track, index, isCurrent } = $props<{
|
||||
track: TrackRef;
|
||||
@@ -67,13 +65,6 @@
|
||||
<GripVertical size={16} />
|
||||
</button>
|
||||
|
||||
<img
|
||||
src={coverUrl(track.album_id)}
|
||||
alt=""
|
||||
onerror={(e) => ((e.currentTarget as HTMLImageElement).src = FALLBACK_COVER)}
|
||||
class="h-10 w-10 flex-shrink-0 rounded object-cover"
|
||||
/>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onclick={handleBodyClick}
|
||||
@@ -89,8 +80,6 @@
|
||||
<div class="text-xs text-text-secondary truncate">{track.artist_name}</div>
|
||||
</button>
|
||||
|
||||
<LikeButton entityType="track" entityId={track.id} />
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onclick={handleRemove}
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/svelte';
|
||||
import { emptyLikesMock } from '../../test-utils/mocks/likes';
|
||||
import { makeTrack } from '$test-utils/fixtures/track';
|
||||
|
||||
// QueueTrackRow now renders a LikeButton, which reads createLikedIdsQuery.
|
||||
// Stub the likes API so the row doesn't need a real QueryClient in context.
|
||||
// The mock (and its emptyLikesMock import) must precede the component import
|
||||
// below: importing QueueTrackRow transitively loads LikeButton → the mocked
|
||||
// module, and the hoisted factory needs emptyLikesMock already initialized.
|
||||
vi.mock('$lib/api/likes', () => emptyLikesMock());
|
||||
|
||||
import QueueTrackRow from './QueueTrackRow.svelte';
|
||||
import { makeTrack } from '$test-utils/fixtures/track';
|
||||
|
||||
const playFromQueueIndex = vi.fn();
|
||||
const removeFromQueue = vi.fn();
|
||||
|
||||
@@ -505,21 +505,6 @@ export function removeFromQueue(idx: number): void {
|
||||
_error = null;
|
||||
}
|
||||
|
||||
// Clear the whole queue and stop playback — mirrors removeFromQueue's
|
||||
// empty-queue branch. Also drops the radio/system source + self-heal closure
|
||||
// so the emptied player doesn't try to refill from a now-irrelevant source.
|
||||
export function clearQueue(): void {
|
||||
_queue = [];
|
||||
_index = 0;
|
||||
_state = 'idle';
|
||||
_position = 0;
|
||||
_duration = 0;
|
||||
_error = null;
|
||||
_radioSeedId = null;
|
||||
_queueSource = null;
|
||||
_queueRefetch = null;
|
||||
}
|
||||
|
||||
export function playFromQueueIndex(idx: number): void {
|
||||
if (idx < 0 || idx >= _queue.length) return;
|
||||
_radioSeedId = null;
|
||||
|
||||
@@ -168,14 +168,9 @@
|
||||
style="display: none"
|
||||
></audio>
|
||||
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<!-- QueueDrawer must be inside the provider: its rows render LikeButton,
|
||||
which calls useQueryClient() at init. The drawer's <aside> is always
|
||||
mounted, so the moment the queue is populated (on first play) those
|
||||
LikeButtons instantiate — outside the provider they throw
|
||||
"No QueryClient was found" and abort the play flush. -->
|
||||
<QueueDrawer />
|
||||
<QueueDrawer />
|
||||
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{#if user.value !== null && page.url.pathname !== '/login' && page.url.pathname !== '/now-playing'}
|
||||
<Shell>{@render children()}</Shell>
|
||||
{:else}
|
||||
|
||||
@@ -37,14 +37,6 @@ if (typeof window !== 'undefined') {
|
||||
Object.defineProperty(window, 'sessionStorage', { configurable: true, value: memSession });
|
||||
}
|
||||
|
||||
// jsdom doesn't implement Element.prototype.scrollIntoView. Components that
|
||||
// call it (queue auto-scroll to the now-playing row, the alphabetical rail)
|
||||
// would throw an unhandled TypeError in tests — which fails the run even when
|
||||
// every assertion passes. No-op it; tests never assert on scroll position.
|
||||
if (typeof Element !== 'undefined' && !Element.prototype.scrollIntoView) {
|
||||
Element.prototype.scrollIntoView = () => {};
|
||||
}
|
||||
|
||||
// W-T3 moved toast rendering out of per-page markup into a single
|
||||
// <ToastHost /> mounted in +layout.svelte. Tests render individual pages
|
||||
// without the layout, so we mount ToastHost here so `pushToast()` calls
|
||||
|
||||
Reference in New Issue
Block a user