playback: use single-queue system

Switch auxio to a single-queue system. "Play next" adds songs to the top
of the queue, similar to before, and then "Add to queue" adds songs to
the bottom of the queue. This enables many more enhancements to be made
to the playback experience, at the cost of a feature I preferred.

Resolves #44.
This commit is contained in:
OxygenCobalt
2021-12-22 16:37:07 -07:00
parent b92b08f8ab
commit 25dd276bd8
43 changed files with 187 additions and 478 deletions
@@ -29,7 +29,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar
import org.oxycblt.auxio.databinding.FragmentMainBinding
import org.oxycblt.auxio.detail.DetailViewModel
@@ -66,7 +66,7 @@ class AlbumDetailFragment : DetailFragment() {
setupToolbar(detailModel.curAlbum.value!!, R.menu.menu_album_detail) { itemId ->
if (itemId == R.id.action_queue_add) {
playbackModel.addToUserQueue(detailModel.curAlbum.value!!)
playbackModel.playNext(detailModel.curAlbum.value!!)
requireContext().showToast(R.string.lbl_queue_added)
true
} else {
@@ -147,12 +147,6 @@ class AlbumDetailFragment : DetailFragment() {
}
}
playbackModel.isInUserQueue.observe(viewLifecycleOwner) { inUserQueue ->
if (inUserQueue) {
detailAdapter.highlightSong(null, binding.detailRecycler)
}
}
logD("Fragment created.")
return binding.root
@@ -115,12 +115,6 @@ class GenreDetailFragment : DetailFragment() {
}
}
playbackModel.isInUserQueue.observe(viewLifecycleOwner) { inUserQueue ->
if (inUserQueue) {
detailAdapter.highlightSong(null, binding.detailRecycler)
}
}
logD("Fragment created.")
return binding.root
@@ -15,6 +15,7 @@ class AdaptiveFloatingActionButton @JvmOverloads constructor(
size = SIZE_NORMAL
if (resources.configuration.smallestScreenWidthDp >= 640) {
// Use a large FAB on large screens, as it makes it easier to touch.
customSize = resources.getDimensionPixelSize(MaterialR.dimen.m3_large_fab_size)
setMaxImageSize(
resources.getDimensionPixelSize(MaterialR.dimen.m3_large_fab_max_image_size)
@@ -131,8 +131,10 @@ class PlaybackFragment : Fragment() {
binding.playbackSeekBar.setProgress(pos)
}
playbackModel.displayQueue.observe(viewLifecycleOwner) {
updateQueueIcon(queueItem)
playbackModel.nextItemsInQueue.observe(viewLifecycleOwner) {
// The queue icon uses a selector that will automatically tint the icon as active or
// inactive. We just need to set the flag.
queueItem.isEnabled = playbackModel.nextItemsInQueue.value!!.isNotEmpty()
}
playbackModel.isPlaying.observe(viewLifecycleOwner) { isPlaying ->
@@ -155,13 +157,4 @@ class PlaybackFragment : Fragment() {
// so we can't really do much
(requireView().parent.parent.parent as PlaybackLayout).collapse()
}
private fun updateQueueIcon(queueItem: MenuItem) {
val userQueue = playbackModel.userQueue.value!!
val nextQueue = playbackModel.nextItemsInQueue.value!!
// The queue icon uses a selector that will automatically tint the icon as active or
// inactive. We just need to set the flag.
queueItem.isEnabled = !(userQueue.isEmpty() && nextQueue.isEmpty())
}
}
@@ -21,24 +21,17 @@ package org.oxycblt.auxio.playback
import android.content.Context
import android.net.Uri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.ActionHeader
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.BaseModel
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Header
import org.oxycblt.auxio.music.HeaderString
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.queue.QueueAdapter
import org.oxycblt.auxio.playback.state.LoopMode
import org.oxycblt.auxio.playback.state.PlaybackMode
import org.oxycblt.auxio.playback.state.PlaybackStateManager
@@ -61,7 +54,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
// Queue
private val mQueue = MutableLiveData(listOf<Song>())
private val mUserQueue = MutableLiveData(listOf<Song>())
private val mIndex = MutableLiveData(0)
private val mMode = MutableLiveData(PlaybackMode.ALL_SONGS)
@@ -69,7 +61,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
private val mIsPlaying = MutableLiveData(false)
private val mIsShuffling = MutableLiveData(false)
private val mLoopMode = MutableLiveData(LoopMode.NONE)
private val mIsInUserQueue = MutableLiveData(false)
// Other
private var mIntentUri: Uri? = null
@@ -83,12 +74,8 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
/** The current queue determined by [playbackMode] and [parent] */
val queue: LiveData<List<Song>> get() = mQueue
/** The queue created by the user. */
val userQueue: LiveData<List<Song>> get() = mUserQueue
/** The current [PlaybackMode] that also determines the queue */
val playbackMode: LiveData<PlaybackMode> get() = mMode
/** Whether playback is originating from the user-generated queue or not */
val isInUserQueue: LiveData<Boolean> = mIsInUserQueue
val isPlaying: LiveData<Boolean> get() = mIsPlaying
val isShuffling: LiveData<Boolean> get() = mIsShuffling
@@ -100,62 +87,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
queue.slice((mIndex.value!! + 1) until queue.size)
}
/** The combined queue data used for UIs, with header data included */
val displayQueue = MediatorLiveData<List<BaseModel>>().apply {
val combine: (userQueue: List<Song>, nextQueue: List<Song>) -> List<BaseModel> =
{ userQueue, nextQueue ->
val queue = mutableListOf<BaseModel>()
if (userQueue.isNotEmpty()) {
queue += ActionHeader(
id = -2,
string = HeaderString.Single(R.string.lbl_next_user_queue),
icon = R.drawable.ic_clear,
desc = R.string.desc_clear_user_queue,
onClick = { playbackManager.clearUserQueue() }
)
queue += userQueue
}
if (nextQueue.isNotEmpty()) {
val parentName = parent.value?.name
queue += Header(
id = -3,
string = HeaderString.WithArg(
R.string.fmt_next_from,
if (parentName != null) {
HeaderString.Arg.Value(parentName)
} else {
HeaderString.Arg.Resource(R.string.lbl_all_songs)
}
)
)
queue += nextQueue
}
queue
}
// Do not move these around. The transformed value must be generated through this
// observer call first before the userQueue source uses it assuming that it's not
// null.
addSource(nextItemsInQueue) { nextQueue ->
value = combine(userQueue.value!!, nextQueue)
}
addSource(userQueue) { userQueue ->
value = combine(
userQueue,
requireNotNull(nextItemsInQueue.value) {
"Transformed value was not generated yet."
}
)
}
}
private val playbackManager = PlaybackStateManager.maybeGetInstance()
private val settingsManager = SettingsManager.getInstance()
@@ -285,95 +216,65 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
}
/**
* Remove an item at [adapterIndex], being a non-header data index.
* @param queueAdapter [QueueAdapter] instance to push changes to when successful.
* Remove a queue item using it's recyclerview adapter index. If the indices are valid,
* [apply] is called just before the change is committed so that the adapter can be updated.
*/
fun removeQueueDataItem(adapterIndex: Int, queueAdapter: QueueAdapter) {
var index = adapterIndex.dec()
fun removeQueueDataItem(adapterIndex: Int, apply: () -> Unit) {
val adjusted = adapterIndex + (mQueue.value!!.size - nextItemsInQueue.value!!.size)
// If the item is in the user queue, then remove it from there after accounting for the header.
if (index < mUserQueue.value!!.size) {
queueAdapter.removeItem(adapterIndex)
playbackManager.removeUserQueueItem(index)
} else {
// Translate the indices into proper queue indices if removing an item from there.
index += (mQueue.value!!.size - nextItemsInQueue.value!!.size)
if (userQueue.value!!.isNotEmpty()) {
index -= mUserQueue.value!!.size.inc()
}
queueAdapter.removeItem(adapterIndex)
playbackManager.removeQueueItem(index)
if (adjusted in mQueue.value!!.indices) {
apply()
playbackManager.removeQueueItem(adjusted)
}
}
/**
* Move a queue OR user queue item from [adapterFrom] to [adapterTo], as long as both
* indices are non-header data indices.
* @param queueAdapter [QueueAdapter] instance to push changes to when successful.
* Move queue items using their recyclerview adapter indices. If the indices are valid,
* [apply] is called just before the change is committed so that the adapter can be updated.
*/
fun moveQueueDataItems(
adapterFrom: Int,
adapterTo: Int,
queueAdapter: QueueAdapter
): Boolean {
var from = adapterFrom.dec()
var to = adapterTo.dec()
fun moveQueueDataItems(adapterFrom: Int, adapterTo: Int, apply: () -> Unit): Boolean {
val delta = (mQueue.value!!.size - nextItemsInQueue.value!!.size)
if (from < mUserQueue.value!!.size) {
// Ignore invalid movements to out of bounds, header, or queue positions
if (to >= mUserQueue.value!!.size || to < 0) return false
queueAdapter.moveItems(adapterFrom, adapterTo)
playbackManager.moveUserQueueItems(from, to)
} else {
// Ignore invalid movements to out of bounds or header positions
if (to < 0) return false
// Get the real queue positions from the nextInQueue positions
val delta = mQueue.value!!.size - nextItemsInQueue.value!!.size
from += delta
to += delta
if (userQueue.value!!.isNotEmpty()) {
// Ignore user queue positions
if (to <= mUserQueue.value!!.size.inc()) return false
from -= mUserQueue.value!!.size.inc()
to -= mUserQueue.value!!.size.inc()
// Ignore movements that are past the next songs
if (to <= mIndex.value!!) return false
}
queueAdapter.moveItems(adapterFrom, adapterTo)
val from = adapterFrom + delta
val to = adapterTo + delta
if (from in mQueue.value!!.indices && to in mQueue.value!!.indices) {
apply()
playbackManager.moveQueueItems(from, to)
return true
}
return true
return false
}
/**
* Add a [Song] to the user queue.
* Add a [Song] to the top of the queue.
*/
fun addToUserQueue(song: Song) {
playbackManager.addToUserQueue(song)
fun playNext(song: Song) {
playbackManager.playNext(song)
}
/**
* Add an [Album] to the user queue
* Add an [Album] to the top of the queue.
*/
fun addToUserQueue(album: Album) {
playbackManager.addToUserQueue(settingsManager.detailAlbumSort.sortAlbum(album))
fun playNext(album: Album) {
playbackManager.playNext(settingsManager.detailAlbumSort.sortAlbum(album))
}
// --- STATUS FUNCTIONS ---
/**
* Add a [Song] to the end of the queue.
*/
fun addToQueue(song: Song) {
playbackManager.addToQueue(song)
}
/**
* Add an [Album] to the end of the queue.
*/
fun addToQueue(album: Album) {
playbackManager.addToQueue(settingsManager.detailAlbumSort.sortAlbum(album))
}
// --- STATUS FUNCTIONS ---
/**
* Flip the playing status, e.g from playing to paused
@@ -405,7 +306,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
fun savePlaybackState(context: Context, onDone: () -> Unit) {
viewModelScope.launch {
playbackManager.saveStateToDatabase(context)
onDone()
}
}
@@ -445,7 +345,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
mParent.value = playbackManager.parent
mQueue.value = playbackManager.queue
mMode.value = playbackManager.playbackMode
mUserQueue.value = playbackManager.userQueue
mIndex.value = playbackManager.index
mIsPlaying.value = playbackManager.isPlaying
mIsShuffling.value = playbackManager.isShuffling
@@ -474,10 +373,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
mQueue.value = queue
}
override fun onUserQueueUpdate(userQueue: List<Song>) {
mUserQueue.value = userQueue
}
override fun onIndexUpdate(index: Int) {
mIndex.value = index
}
@@ -497,8 +392,4 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
override fun onLoopUpdate(loopMode: LoopMode) {
mLoopMode.value = loopMode
}
override fun onInUserQueueUpdate(isInUserQueue: Boolean) {
mIsInUserQueue.value = isInUserQueue
}
}
@@ -39,7 +39,6 @@ import org.oxycblt.auxio.ui.BaseViewHolder
import org.oxycblt.auxio.ui.DiffCallback
import org.oxycblt.auxio.ui.HeaderViewHolder
import org.oxycblt.auxio.util.inflater
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.logE
/**
@@ -105,9 +104,7 @@ class QueueAdapter(
* Used since [submitList] will cause QueueAdapter to freak out.
*/
fun moveItems(adapterFrom: Int, adapterTo: Int) {
val item = data.removeAt(adapterFrom)
data.add(adapterTo, item)
data.add(adapterTo, data.removeAt(adapterFrom))
notifyItemMoved(adapterFrom, adapterTo)
}
@@ -117,30 +114,7 @@ class QueueAdapter(
*/
fun removeItem(adapterIndex: Int) {
data.removeAt(adapterIndex)
/*
* If the data from the next queue is now entirely empty [Signified by a header at the
* end, remove the next queue header as notify as such.
*
* If the user queue is empty [Signified by there being two headers at the beginning with
* nothing in between], then remove the user queue header and notify as such.
*
* Otherwise just remove the item as usual.
*/
if (data[data.lastIndex] is Header) {
logD("Queue is empty, removing header")
val lastIndex = data.lastIndex
data.removeAt(lastIndex)
notifyItemRangeRemoved(lastIndex, 2)
} else if (data.lastIndex >= 1 && data[0] is ActionHeader && data[1] is Header) {
logD("User queue is empty, removing header")
data.removeAt(0)
notifyItemRangeRemoved(0, 2)
} else {
notifyItemRemoved(adapterIndex)
}
notifyItemRemoved(adapterIndex)
}
/**
@@ -44,14 +44,9 @@ class QueueDragCallback(private val playbackModel: PlaybackViewModel) : ItemTouc
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int {
// Only allow dragging/swiping with the queue item ViewHolder, not the headers.
return if (viewHolder is QueueAdapter.QueueSongViewHolder) {
makeFlag(
ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP or ItemTouchHelper.DOWN
) or makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE, ItemTouchHelper.START)
} else 0
}
): Int =
makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP or ItemTouchHelper.DOWN) or
makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE, ItemTouchHelper.START)
override fun interpolateOutOfBoundsScroll(
recyclerView: RecyclerView,
@@ -152,15 +147,18 @@ class QueueDragCallback(private val playbackModel: PlaybackViewModel) : ItemTouc
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return playbackModel.moveQueueDataItems(
viewHolder.bindingAdapterPosition,
target.bindingAdapterPosition,
queueAdapter
)
val from = viewHolder.bindingAdapterPosition
val to = target.bindingAdapterPosition
return playbackModel.moveQueueDataItems(from, to) {
queueAdapter.moveItems(from, to)
}
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
playbackModel.removeQueueDataItem(viewHolder.bindingAdapterPosition, queueAdapter)
playbackModel.removeQueueDataItem(viewHolder.bindingAdapterPosition) {
queueAdapter.removeItem(viewHolder.bindingAdapterPosition)
}
}
/**
@@ -30,8 +30,7 @@ import org.oxycblt.auxio.databinding.FragmentQueueBinding
import org.oxycblt.auxio.playback.PlaybackViewModel
/**
* A [Fragment] that contains both the user queue and the next queue, with the ability to
* edit them as well.
* A [Fragment] that shows the queue and enables editing as well.
* @author OxygenCobalt
*/
class QueueFragment : Fragment() {
@@ -68,7 +67,7 @@ class QueueFragment : Fragment() {
// --- VIEWMODEL SETUP ----
playbackModel.displayQueue.observe(viewLifecycleOwner) { queue ->
playbackModel.nextItemsInQueue.observe(viewLifecycleOwner) { queue ->
if (queue.isEmpty()) {
findNavController().navigateUp()
return@observe
@@ -44,7 +44,10 @@ class PlaybackStateDatabase(context: Context) :
createTable(db, TABLE_NAME_QUEUE)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) = nuke(db)
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) = nuke(db)
private fun nuke(db: SQLiteDatabase) {
db.apply {
execSQL("DROP TABLE IF EXISTS $TABLE_NAME_STATE")
execSQL("DROP TABLE IF EXISTS $TABLE_NAME_QUEUE")
@@ -53,10 +56,6 @@ class PlaybackStateDatabase(context: Context) :
}
}
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
onUpgrade(db, newVersion, oldVersion)
}
// --- DATABASE CONSTRUCTION FUNCTIONS ---
/**
@@ -86,8 +85,7 @@ class PlaybackStateDatabase(context: Context) :
.append("${StateColumns.COLUMN_QUEUE_INDEX} INTEGER NOT NULL,")
.append("${StateColumns.COLUMN_PLAYBACK_MODE} INTEGER NOT NULL,")
.append("${StateColumns.COLUMN_IS_SHUFFLING} BOOLEAN NOT NULL,")
.append("${StateColumns.COLUMN_LOOP_MODE} INTEGER NOT NULL,")
.append("${StateColumns.COLUMN_IS_IN_USER_QUEUE} BOOLEAN NOT NULL)")
.append("${StateColumns.COLUMN_LOOP_MODE} INTEGER NOT NULL)")
return command
}
@@ -98,8 +96,7 @@ class PlaybackStateDatabase(context: Context) :
private fun constructQueueTable(command: StringBuilder): StringBuilder {
command.append("${QueueColumns.ID} LONG PRIMARY KEY,")
.append("${QueueColumns.SONG_HASH} INTEGER NOT NULL,")
.append("${QueueColumns.ALBUM_HASH} INTEGER NOT NULL,")
.append("${QueueColumns.IS_USER_QUEUE} BOOLEAN NOT NULL)")
.append("${QueueColumns.ALBUM_HASH} INTEGER NOT NULL)")
return command
}
@@ -126,7 +123,6 @@ class PlaybackStateDatabase(context: Context) :
put(StateColumns.COLUMN_PLAYBACK_MODE, state.playbackMode.toInt())
put(StateColumns.COLUMN_IS_SHUFFLING, state.isShuffling)
put(StateColumns.COLUMN_LOOP_MODE, state.loopMode.toInt())
put(StateColumns.COLUMN_IS_IN_USER_QUEUE, state.isInUserQueue)
}
insert(TABLE_NAME_STATE, null, stateData)
@@ -155,9 +151,6 @@ class PlaybackStateDatabase(context: Context) :
val modeIndex = cursor.getColumnIndexOrThrow(StateColumns.COLUMN_PLAYBACK_MODE)
val shuffleIndex = cursor.getColumnIndexOrThrow(StateColumns.COLUMN_IS_SHUFFLING)
val loopModeIndex = cursor.getColumnIndexOrThrow(StateColumns.COLUMN_LOOP_MODE)
val isInUserQueueIndex = cursor.getColumnIndexOrThrow(
StateColumns.COLUMN_IS_IN_USER_QUEUE
)
cursor.moveToFirst()
@@ -184,7 +177,6 @@ class PlaybackStateDatabase(context: Context) :
playbackMode = mode,
isShuffling = cursor.getInt(shuffleIndex) == 1,
loopMode = LoopMode.fromInt(cursor.getInt(loopModeIndex)) ?: LoopMode.NONE,
isInUserQueue = cursor.getInt(isInUserQueueIndex) == 1
)
}
@@ -192,9 +184,9 @@ class PlaybackStateDatabase(context: Context) :
}
/**
* Write a [SavedQueue] to the database.
* Write a queue to the database.
*/
fun writeQueue(queue: SavedQueue) {
fun writeQueue(queue: MutableList<Song>) {
assertBackgroundThread()
val database = writableDatabase
@@ -205,12 +197,11 @@ class PlaybackStateDatabase(context: Context) :
logD("Wiped queue db.")
writeQueueBatch(queue.user, true, 0)
writeQueueBatch(queue.queue, false, queue.user.size)
writeQueueBatch(queue, queue.size)
}
private fun writeQueueBatch(queue: List<Song>, isUserQueue: Boolean, idStart: Int) {
logD("Beginning queue write [start: $idStart, userQueue: $isUserQueue]")
private fun writeQueueBatch(queue: List<Song>, idStart: Int) {
logD("Beginning queue write [start: $idStart]")
val database = writableDatabase
var position = 0
@@ -227,7 +218,6 @@ class PlaybackStateDatabase(context: Context) :
put(QueueColumns.ID, idStart + i)
put(QueueColumns.SONG_HASH, song.hash)
put(QueueColumns.ALBUM_HASH, song.album.hash)
put(QueueColumns.IS_USER_QUEUE, isUserQueue)
}
insert(TABLE_NAME_QUEUE, null, itemData)
@@ -243,29 +233,25 @@ class PlaybackStateDatabase(context: Context) :
}
/**
* Read a [SavedQueue] from this database.
* Read a list of queue items from this database.
* @param musicStore Required to transform database songs into actual song instances
*/
fun readQueue(musicStore: MusicStore): SavedQueue {
fun readQueue(musicStore: MusicStore): MutableList<Song> {
assertBackgroundThread()
val queue = SavedQueue(mutableListOf(), mutableListOf())
val queue = mutableListOf<Song>()
readableDatabase.queryAll(TABLE_NAME_QUEUE) { cursor ->
if (cursor.count == 0) return@queryAll
val songIndex = cursor.getColumnIndexOrThrow(QueueColumns.SONG_HASH)
val albumIndex = cursor.getColumnIndexOrThrow(QueueColumns.ALBUM_HASH)
val isUserQueueIndex = cursor.getColumnIndexOrThrow(QueueColumns.IS_USER_QUEUE)
while (cursor.moveToNext()) {
musicStore.findSongFast(cursor.getLong(songIndex), cursor.getLong(albumIndex))?.let { song ->
if (cursor.getInt(isUserQueueIndex) == 1) {
queue.user.add(song)
} else {
queue.queue.add(song)
musicStore.findSongFast(cursor.getLong(songIndex), cursor.getLong(albumIndex))
?.let { song ->
queue.add(song)
}
}
}
}
@@ -280,11 +266,8 @@ class PlaybackStateDatabase(context: Context) :
val playbackMode: PlaybackMode,
val isShuffling: Boolean,
val loopMode: LoopMode,
val isInUserQueue: Boolean
)
data class SavedQueue(val user: MutableList<Song>, val queue: MutableList<Song>)
private object StateColumns {
const val COLUMN_ID = "id"
const val COLUMN_SONG_HASH = "song"
@@ -294,19 +277,17 @@ class PlaybackStateDatabase(context: Context) :
const val COLUMN_PLAYBACK_MODE = "playback_mode"
const val COLUMN_IS_SHUFFLING = "is_shuffling"
const val COLUMN_LOOP_MODE = "loop_mode"
const val COLUMN_IS_IN_USER_QUEUE = "is_in_user_queue"
}
private object QueueColumns {
const val ID = "id"
const val SONG_HASH = "song"
const val ALBUM_HASH = "album"
const val IS_USER_QUEUE = "is_user_queue"
}
companion object {
const val DB_NAME = "auxio_state_database.db"
const val DB_VERSION = 5
const val DB_VERSION = 6
const val TABLE_NAME_STATE = "playback_state_table"
const val TABLE_NAME_QUEUE = "queue_table"
@@ -30,6 +30,8 @@ import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.logE
import kotlin.math.max
import kotlin.math.min
/**
* Master class (and possible god object) for the playback state.
@@ -65,11 +67,6 @@ class PlaybackStateManager private constructor() {
field = value
callbacks.forEach { it.onQueueUpdate(value) }
}
private var mUserQueue = mutableListOf<Song>()
set(value) {
field = value
callbacks.forEach { it.onUserQueueUpdate(value) }
}
private var mIndex = 0
set(value) {
field = value
@@ -98,11 +95,7 @@ class PlaybackStateManager private constructor() {
field = value
callbacks.forEach { it.onLoopUpdate(value) }
}
private var mIsInUserQueue = false
set(value) {
field = value
callbacks.forEach { it.onInUserQueueUpdate(value) }
}
private var mIsRestored = false
private var mHasPlayed = false
@@ -114,8 +107,6 @@ class PlaybackStateManager private constructor() {
val position: Long get() = mPosition
/** The current queue determined by [parent] and [playbackMode] */
val queue: List<Song> get() = mQueue
/** The queue created by the user. */
val userQueue: List<Song> get() = mUserQueue
/** The current index of the queue */
val index: Int get() = mIndex
/** The current [PlaybackMode] */
@@ -250,8 +241,6 @@ class PlaybackStateManager private constructor() {
* Update the playback to a new [song], doing all the required logic.
*/
private fun updatePlayback(song: Song, shouldPlay: Boolean = true) {
mIsInUserQueue = false
mSong = song
mPosition = 0
@@ -264,29 +253,17 @@ class PlaybackStateManager private constructor() {
* Go to the next song, along with doing all the checks that entails.
*/
fun next() {
// If there's anything in the user queue, go to the first song in there instead
// of incrementing the index.
if (mUserQueue.isNotEmpty()) {
updatePlayback(mUserQueue[0])
mUserQueue.removeAt(0)
// Mark that the playback state is currently in the user queue, for later.
mIsInUserQueue = true
forceUserQueueUpdate()
// Increment the index, if it cannot be incremented any further, then
// loop and pause/resume playback depending on the setting
if (mIndex < mQueue.lastIndex) {
mIndex = mIndex.inc()
updatePlayback(mQueue[mIndex])
} else {
// Increment the index, if it cannot be incremented any further, then
// loop and pause/resume playback depending on the setting
if (mIndex < mQueue.lastIndex) {
mIndex = mIndex.inc()
updatePlayback(mQueue[mIndex])
} else {
mIndex = 0
updatePlayback(mQueue[mIndex], shouldPlay = mLoopMode == LoopMode.ALL)
}
forceQueueUpdate()
mIndex = 0
updatePlayback(mQueue[mIndex], shouldPlay = mLoopMode == LoopMode.ALL)
}
forceQueueUpdate()
}
/**
@@ -297,9 +274,8 @@ class PlaybackStateManager private constructor() {
if (settingsManager.rewindWithPrev && mPosition >= REWIND_THRESHOLD) {
rewind()
} else {
// Only decrement the index if there's a song to move back to AND if we are not exiting
// the user queue.
if (mIndex > 0 && !mIsInUserQueue) {
// Only decrement the index if there's a song to move back to
if (mIndex > 0) {
mIndex = mIndex.dec()
}
@@ -348,63 +324,35 @@ class PlaybackStateManager private constructor() {
}
/**
* Add a [song] to the user queue.
* Add a [song] to the top of the queue.
*/
fun addToUserQueue(song: Song) {
mUserQueue.add(song)
forceUserQueueUpdate()
fun playNext(song: Song) {
mQueue.add(min(mIndex + 1, max(mQueue.lastIndex, 0)), song)
forceQueueUpdate()
}
/**
* Add a list of [songs] to the user queue.
* Add a list of [songs] to the top of the queue.
*/
fun addToUserQueue(songs: List<Song>) {
mUserQueue.addAll(songs)
forceUserQueueUpdate()
fun playNext(songs: List<Song>) {
mQueue.addAll(min(mIndex + 1, max(mQueue.lastIndex, 0)), songs)
forceQueueUpdate()
}
/**
* Remove a USER queue item at [index]. Will ignore invalid indexes.
* Add a [song] to the end of the queue.
*/
fun removeUserQueueItem(index: Int) {
logD("Removing item ${mUserQueue[index].name}.")
if (index > mUserQueue.size || index < 0) {
logE("Index is out of bounds, did not remove user queue item.")
return
}
mUserQueue.removeAt(index)
forceUserQueueUpdate()
fun addToQueue(song: Song) {
mQueue.add(song)
forceQueueUpdate()
}
/**
* Move a USER queue item at [from] to a position at [to]. Will ignore invalid indexes.
* Add a list of [songs] to the end of the queue.
*/
fun moveUserQueueItems(from: Int, to: Int) {
if (from > mUserQueue.size || from < 0 || to > mUserQueue.size || to < 0) {
logE("Indices were out of bounds, did not move user queue item")
return
}
val item = mUserQueue.removeAt(from)
mUserQueue.add(to, item)
forceUserQueueUpdate()
}
/**
* Clear the user queue. Forces a user queue update.
*/
fun clearUserQueue() {
mUserQueue.clear()
forceUserQueueUpdate()
fun addToQueue(songs: List<Song>) {
mQueue.addAll(songs)
forceQueueUpdate()
}
/**
@@ -414,36 +362,28 @@ class PlaybackStateManager private constructor() {
mQueue = mQueue
}
/**
* Force any callbacks to recieve a user queue update.
*/
private fun forceUserQueueUpdate() {
mUserQueue = mUserQueue
}
// --- SHUFFLE FUNCTIONS ---
/**
* Set whether this instance is [shuffled]. Updates the queue accordingly
* Set whether this instance is [shuffled]. Updates the queue accordingly.
* @param keepSong Whether the current song should be kept as the queue is shuffled/unshuffled
*/
fun setShuffling(shuffled: Boolean, keepSong: Boolean) {
mIsShuffling = shuffled
if (mIsShuffling) {
genShuffle(keepSong, mIsInUserQueue)
genShuffle(keepSong)
} else {
resetShuffle(keepSong, mIsInUserQueue)
resetShuffle(keepSong)
}
}
/**
* Generate a new shuffled queue.
* @param keepSong Whether the current song should be kept as the queue is shuffled
* @param useLastSong Whether to use the last song in the queue instead of the current one
*/
private fun genShuffle(keepSong: Boolean, useLastSong: Boolean) {
val lastSong = if (useLastSong) mQueue[0] else mSong
private fun genShuffle(keepSong: Boolean) {
val lastSong = mSong
logD("Shuffling queue")
@@ -464,11 +404,10 @@ class PlaybackStateManager private constructor() {
/**
* Reset the queue to its normal, ordered state.
* @param keepSong Whether the current song should be kept as the queue is unshuffled
* @param useLastSong Whether to use the previous song for the index calculations.
*/
private fun resetShuffle(keepSong: Boolean, useLastSong: Boolean) {
private fun resetShuffle(keepSong: Boolean) {
val musicStore = MusicStore.maybeGetInstance() ?: return
val lastSong = if (useLastSong) mQueue[mIndex] else mSong
val lastSong = mSong
mQueue = when (mPlaybackMode) {
PlaybackMode.ALL_SONGS ->
@@ -585,11 +524,12 @@ class PlaybackStateManager private constructor() {
database.writeState(
PlaybackStateDatabase.SavedState(
mSong, mPosition, mParent, mIndex,
mPlaybackMode, mIsShuffling, mLoopMode, mIsInUserQueue
mPlaybackMode, mIsShuffling, mLoopMode,
)
)
database.writeQueue(PlaybackStateDatabase.SavedQueue(mUserQueue, mQueue))
// TODO: Re-add state saving
database.writeQueue(mQueue)
this@PlaybackStateManager.logD(
"Save finished in ${System.currentTimeMillis() - start}ms"
@@ -608,7 +548,7 @@ class PlaybackStateManager private constructor() {
val start: Long
val playbackState: PlaybackStateDatabase.SavedState?
val queue: PlaybackStateDatabase.SavedQueue
val queue: MutableList<Song>
withContext(Dispatchers.IO) {
start = System.currentTimeMillis()
@@ -622,7 +562,7 @@ class PlaybackStateManager private constructor() {
// Get off the IO coroutine since it will cause LiveData updates to throw an exception
if (playbackState != null) {
logD("Found playback state $playbackState with queue size ${queue.user.size + queue.queue.size}")
logD("Found playback state $playbackState")
unpackFromPlaybackState(playbackState)
unpackQueue(queue)
@@ -649,30 +589,21 @@ class PlaybackStateManager private constructor() {
mSong = playbackState.song
mLoopMode = playbackState.loopMode
mIsShuffling = playbackState.isShuffling
mIsInUserQueue = playbackState.isInUserQueue
seekTo(playbackState.position)
}
/**
* Unpack a list of queue items into a queue & user queue.
*/
private fun unpackQueue(queue: PlaybackStateDatabase.SavedQueue) {
mUserQueue = queue.user
mQueue = queue.queue
private fun unpackQueue(queue: MutableList<Song>) {
mQueue = queue
// When done, get a more accurate index to prevent issues with queue songs that were saved
// to the db but are now deleted when the restore occurred.
// Not done if in user queue because that could result in a bad index being created.
if (!mIsInUserQueue) {
mSong?.let { song ->
val index = mQueue.indexOf(song)
mIndex = if (index != -1) index else mIndex
// Sanity check: Ensure that the
mSong?.let { song ->
while (mQueue.getOrNull(mIndex) != song) {
mIndex--
}
}
forceQueueUpdate()
forceUserQueueUpdate()
}
/**
@@ -702,14 +633,12 @@ class PlaybackStateManager private constructor() {
fun onParentUpdate(parent: MusicParent?) {}
fun onPositionUpdate(position: Long) {}
fun onQueueUpdate(queue: List<Song>) {}
fun onUserQueueUpdate(userQueue: List<Song>) {}
fun onModeUpdate(mode: PlaybackMode) {}
fun onIndexUpdate(index: Int) {}
fun onPlayingUpdate(isPlaying: Boolean) {}
fun onShuffleUpdate(isShuffling: Boolean) {}
fun onLoopUpdate(loopMode: LoopMode) {}
fun onSeek(position: Long) {}
fun onInUserQueueUpdate(isInUserQueue: Boolean) {}
}
companion object {
@@ -145,7 +145,7 @@ class SettingsListFragment : PreferenceFragmentCompat() {
}
}
SettingsManager.KEY_SHOW_COVERS, SettingsManager.KEY_QUALITY_COVERS, -> {
SettingsManager.KEY_SHOW_COVERS, SettingsManager.KEY_QUALITY_COVERS -> {
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ ->
Coil.imageLoader(requireContext()).apply {
this.memoryCache?.clear()
@@ -144,15 +144,31 @@ class ActionMenu(
}
}
R.id.action_queue_add -> {
R.id.action_play_next -> {
when (data) {
is Song -> {
playbackModel.addToUserQueue(data)
playbackModel.playNext(data)
context.showToast(R.string.lbl_queue_added)
}
is Album -> {
playbackModel.addToUserQueue(data)
playbackModel.playNext(data)
context.showToast(R.string.lbl_queue_added)
}
else -> {}
}
}
R.id.action_queue_add -> {
when (data) {
is Song -> {
playbackModel.addToQueue(data)
context.showToast(R.string.lbl_queue_added)
}
is Album -> {
playbackModel.addToQueue(data)
context.showToast(R.string.lbl_queue_added)
}