diff --git a/app/src/main/java/com/github/libretube/ui/sheets/PlayingQueueSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/PlayingQueueSheet.kt index 8a5e024fd..2f728121d 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/PlayingQueueSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/PlayingQueueSheet.kt @@ -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() diff --git a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt index 8c183c7b2..82e7d1997 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -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) { queue.clear()