Improve the queue shuffle behavior

This commit is contained in:
Bnyro 2023-01-04 18:49:06 +01:00
parent c3182edc73
commit c5a543bdf0
2 changed files with 12 additions and 6 deletions

View File

@ -33,13 +33,18 @@ class PlayingQueueSheet : ExpandedBottomSheet() {
binding.optionsRecycler.adapter = adapter
binding.shuffle.setOnClickListener {
var streams = PlayingQueue.getStreams()
val streams = PlayingQueue.getStreams().toMutableList()
val currentIndex = PlayingQueue.currentIndex()
val current = streams[currentIndex]
streams = streams.shuffled().toMutableList()
streams.remove(current)
streams.add(currentIndex, current)
// save all streams that need to be shuffled to a copy of the list
val toShuffle = streams.filterIndexed { index, _ ->
index > currentIndex
}
// re-add all streams in the new, shuffled order after removing them
streams.removeAll(toShuffle)
streams.addAll(toShuffle.shuffled())
PlayingQueue.setStreams(streams)
adapter.notifyDataSetChanged()

View File

@ -79,7 +79,8 @@ object PlayingQueue {
fun contains(streamItem: StreamItem) = queue.any { it.url?.toID() == streamItem.url?.toID() }
fun getStreams() = queue
// only returns a copy of the queue, no write access
fun getStreams() = queue.toList()
fun setStreams(streams: List<StreamItem>) {
queue.clear()