Merge pull request #2582 from Bnyro/master

Improve the queue shuffle behavior
This commit is contained in:
Bnyro 2023-01-04 18:49:41 +01:00 committed by GitHub
commit 645e3e2bf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View File

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

View File

@ -79,7 +79,8 @@ object PlayingQueue {
fun contains(streamItem: StreamItem) = queue.any { it.url?.toID() == streamItem.url?.toID() } 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>) { fun setStreams(streams: List<StreamItem>) {
queue.clear() queue.clear()