From 845b6fe2adacadc0d16623f5bce01eae926de533 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 23 Sep 2022 14:12:41 +0200 Subject: [PATCH] fix the playing queue --- .../sheets/VideoOptionsBottomSheet.kt | 2 +- .../github/libretube/util/AutoPlayHelper.kt | 2 +- .../com/github/libretube/util/PlayingQueue.kt | 31 +++++++++---------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt index a9a39b859..4fa406f94 100644 --- a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt @@ -78,7 +78,7 @@ class VideoOptionsBottomSheet( shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) } context?.getString(R.string.play_next) -> { - PlayingQueue.playNext(videoId) + PlayingQueue.addAsNext(videoId) } context?.getString(R.string.add_to_queue) -> { PlayingQueue.add(videoId) diff --git a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt index 497b4fb26..1e1f17e1f 100644 --- a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt +++ b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt @@ -40,7 +40,7 @@ class AutoPlayHelper( if (relatedStreams == null || relatedStreams.isEmpty()) return null var index = 0 var nextStreamId: String? = null - while (nextStreamId == null || PlayingQueue.containsBefore(nextStreamId)) { + while (nextStreamId == null || PlayingQueue.containsBeforeCurrent(nextStreamId)) { nextStreamId = relatedStreams[index].url!!.toID() if (index + 1 < relatedStreams.size) { index += 1 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 651ff963c..7063c1d7d 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -4,17 +4,13 @@ object PlayingQueue { private val queue = mutableListOf() private var currentVideoId: String? = null - fun clear() { - queue.clear() - } - fun add(videoId: String) { if (currentVideoId == videoId) return if (queue.contains(videoId)) queue.remove(videoId) queue.add(videoId) } - fun playNext(videoId: String) { + fun addAsNext(videoId: String) { if (currentVideoId == videoId) return if (queue.contains(videoId)) queue.remove(videoId) queue.add( @@ -24,11 +20,10 @@ object PlayingQueue { } fun getNext(): String? { - val currentIndex = queue.indexOf(currentVideoId) - return if (currentIndex >= queue.size) { + return try { + queue[currentIndex() + 1] + } catch (e: Exception) { null - } else { - queue[currentIndex + 1] } } @@ -41,18 +36,20 @@ object PlayingQueue { return queue.indexOf(currentVideoId) > 0 } - fun contains(videoId: String): Boolean { - return queue.contains(videoId) - } - - fun containsBefore(videoId: String): Boolean { - return queue.contains(videoId) && queue.indexOf(videoId) < queue.indexOf(currentVideoId) - } - fun updateCurrent(videoId: String) { currentVideoId = videoId queue.add(videoId) } fun isNotEmpty() = queue.isNotEmpty() + + fun clear() = queue.clear() + + fun currentIndex() = queue.indexOf(currentVideoId) + + fun contains(videoId: String) = queue.contains(videoId) + + fun containsBeforeCurrent(videoId: String): Boolean { + return queue.contains(videoId) && queue.indexOf(videoId) < currentIndex() + } }