diff --git a/app/src/main/java/org/oxycblt/auxio/playback/PlaybackPanelFragment.kt b/app/src/main/java/org/oxycblt/auxio/playback/PlaybackPanelFragment.kt index ccbb4ddbd..98d537688 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/PlaybackPanelFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/PlaybackPanelFragment.kt @@ -119,7 +119,7 @@ class PlaybackPanelFragment : UserAwarePagerCallback(this) { // Posting the queue goto command prevents the seekbar pos from desyncing // from the song's duration, which creates a visual flicker in the seekbar. - post { queueModel.goto(it) } + post { queueModel.gotoTimelinePosition(it) } } .also { it.attach() } setPageTransformer(CarouselTransformer()) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueViewModel.kt b/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueViewModel.kt index 54ee8c6ce..b5d7e44ee 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueViewModel.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/queue/QueueViewModel.kt @@ -33,7 +33,8 @@ import org.oxycblt.musikr.Song import timber.log.Timber as L /** - * A [ViewModel] that manages the current queue state and allows navigation through the queue. + * A [ViewModel] that exposes the deck as the queue drawer's rows and translates drawer-row + * actions into deck coordinates. * * @author Alexander Capehart (OxygenCobalt) */ @@ -42,7 +43,7 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt ViewModel(), PlaybackStateManager.Listener { private val _queue = MutableStateFlow(listOf()) - /** The current queue. */ + /** The drawer rows: the playing deck song (if any) followed by the waiting deck songs. */ val queue: StateFlow> = _queue private val _queueInstructions = MutableEvent() /** Instructions for how to update [queue] in the UI. */ @@ -52,39 +53,34 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt val scrollTo: Event field = MutableEvent() - /** The index of the currently playing song in the queue. */ + /** The row of the playing deck song (0), or -1 when no deck song is playing. */ val index: StateFlow - field = MutableStateFlow(playbackManager.index) + field = MutableStateFlow(-1) + + /** The parent the heap is playing from, for the drawer's Now Playing header. */ + val parent: StateFlow + field = MutableStateFlow(null) init { playbackManager.addListener(this) + republish() } override fun onIndexMoved(index: Int) { - L.d("Index moved, synchronizing and scrolling to new position") - scrollTo.put(index) - this.index.value = index - } - - override fun onQueueChanged(queue: List, index: Int, change: QueueChange) { - // Queue changed trivially due to item mo -> Diff queue, stay at current index. - L.d("Updating queue display") - _queueInstructions.put(change.instructions) - _queue.value = queue - if (change.type != QueueChange.Type.MAPPING) { - // Index changed, make sure it remains updated without actually scrolling to it. - L.d("Index changed with queue, synchronizing new position") - this.index.value = index + val hadPlayingDeckSong = this.index.value == 0 + republish() + if (!hadPlayingDeckSong && this.index.value == 0) { + // A deck song started playing; make sure it is visible. + scrollTo.put(0) } } + override fun onQueueChanged(queue: List, index: Int, change: QueueChange) { + republish() + } + override fun onQueueReordered(queue: List, index: Int, isShuffled: Boolean) { - // Queue changed completely -> Replace queue, update index - L.d("Queue changed completely, replacing queue and position") - _queueInstructions.put(UpdateInstructions.Replace(0)) - scrollTo.put(index) - _queue.value = queue - this.index.value = index + republish() } override fun onNewPlayback( @@ -93,12 +89,7 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt index: Int, isShuffled: Boolean, ) { - // Entirely new queue -> Replace queue, update index - L.d("New playback, replacing queue and position") - _queueInstructions.put(UpdateInstructions.Replace(0)) - scrollTo.put(index) - _queue.value = queue - this.index.value = index + republish() } override fun onCleared() { @@ -106,46 +97,72 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt } /** - * Start playing the the queue item at the given index. + * Start playing the deck row at the given index. * - * @param adapterIndex The index of the queue item to play. Does nothing if the index is out of - * range. + * @param adapterIndex The deck row to play. Does nothing if the row is out of range. */ fun goto(adapterIndex: Int) { if (adapterIndex !in queue.value.indices) { return } - L.d("Going to position $adapterIndex in queue") - playbackManager.goto(adapterIndex) + L.d("Going to deck row $adapterIndex") + playbackManager.gotoDeck(adapterIndex) } /** - * Remove a queue item at the given index. + * Jump to the timeline position at the given index, as the cover pager does. The pager + * mirrors the whole timeline, so this speaks timeline coordinates rather than deck rows. * - * @param adapterIndex The index of the queue item to play. Does nothing if the index is out of - * range. + * @param timelineIndex The timeline position to play. Does nothing if out of range. + */ + fun gotoTimelinePosition(timelineIndex: Int) { + if (timelineIndex !in playbackManager.queue.indices) { + return + } + L.d("Going to timeline position $timelineIndex") + playbackManager.goto(timelineIndex) + } + + /** + * Remove the deck row at the given index. + * + * @param adapterIndex The deck row to remove. Does nothing if the row is out of range. */ fun removeQueueDataItem(adapterIndex: Int) { if (adapterIndex !in queue.value.indices) { return } - L.d("Removing item $adapterIndex in queue") - playbackManager.removeQueueItem(adapterIndex) + L.d("Removing deck row $adapterIndex") + playbackManager.removeDeckItem(adapterIndex) } /** - * Move a queue item from one index to another index. + * Move a deck row from one index to another. * - * @param adapterFrom The index of the queue item to move. - * @param adapterTo The destination index for the queue item. - * @return true if the items were moved, false otherwise. + * @param adapterFrom The deck row to move. + * @param adapterTo The destination deck row. + * @return true if the rows were moved, false otherwise. */ fun moveQueueDataItems(adapterFrom: Int, adapterTo: Int): Boolean { if (adapterFrom !in queue.value.indices || adapterTo !in queue.value.indices) { return false } - L.d("Moving $adapterFrom to $adapterFrom in queue") - playbackManager.moveQueueItem(adapterFrom, adapterTo) + L.d("Moving deck row $adapterFrom to row $adapterTo") + playbackManager.moveDeckItems(adapterFrom, adapterTo) return true } + + /** Clear the queue of all waiting deck songs. */ + fun clearQueue() { + L.d("Clearing the queue") + playbackManager.clearQueue() + } + + /** Republish the drawer rows from the manager's deck state. */ + private fun republish() { + _queue.value = listOfNotNull(playbackManager.deckCurrent) + playbackManager.deck + index.value = if (playbackManager.deckCurrent != null) 0 else -1 + parent.value = playbackManager.parent + _queueInstructions.put(UpdateInstructions.Replace(0)) + } } diff --git a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt index 07ce88a49..55b592418 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt @@ -60,6 +60,12 @@ interface PlaybackStateManager { /** The current queue of [Song]s. */ val queue: List + /** The upcoming deck [Song]s, in order. The deck sits directly behind the current song. */ + val deck: List + + /** The current [Song] when it came from the deck. Null when the heap is playing. */ + val deckCurrent: Song? + /** The index of the currently playing [Song] in the queue. */ val index: Int @@ -417,10 +423,12 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { @Volatile private var isInitialized = false /** The upcoming deck songs, in order. The deck sits directly behind the current song. */ - private var deck = emptyList() + override var deck = emptyList() + private set /** The current song when it came from the deck. Null when the heap is playing. */ - private var deckCurrent: Song? = null + override var deckCurrent: Song? = null + private set override val progression get() = stateMirror.progression