LibreTube/app/src/main/java/com/github/libretube/util/PlayingQueue.kt

56 lines
1.4 KiB
Kotlin
Raw Normal View History

2022-09-19 23:37:55 +05:30
package com.github.libretube.util
object PlayingQueue {
2022-09-20 01:07:30 +05:30
private val queue = mutableListOf<String>()
private var currentVideoId: String? = null
2022-09-19 23:37:55 +05:30
fun add(videoId: String) {
2022-09-20 01:37:15 +05:30
if (currentVideoId == videoId) return
if (queue.contains(videoId)) queue.remove(videoId)
2022-09-19 23:37:55 +05:30
queue.add(videoId)
}
2022-09-23 17:42:41 +05:30
fun addAsNext(videoId: String) {
2022-09-20 01:37:15 +05:30
if (currentVideoId == videoId) return
if (queue.contains(videoId)) queue.remove(videoId)
2022-09-19 23:37:55 +05:30
queue.add(
2022-09-20 01:37:15 +05:30
queue.indexOf(currentVideoId) + 1,
videoId
2022-09-19 23:37:55 +05:30
)
}
2022-09-19 23:43:25 +05:30
fun getNext(): String? {
2022-09-23 17:42:41 +05:30
return try {
queue[currentIndex() + 1]
} catch (e: Exception) {
2022-09-20 01:07:30 +05:30
null
}
}
2022-09-20 01:51:30 +05:30
fun getPrev(): String? {
val index = queue.indexOf(currentVideoId)
return if (index > 0) queue[index - 1] else null
2022-09-20 01:07:30 +05:30
}
fun hasPrev(): Boolean {
2022-09-20 01:51:30 +05:30
return queue.indexOf(currentVideoId) > 0
2022-09-20 01:07:30 +05:30
}
fun updateCurrent(videoId: String) {
currentVideoId = videoId
2022-09-20 01:51:30 +05:30
queue.add(videoId)
2022-09-19 23:43:25 +05:30
}
2022-09-20 01:13:13 +05:30
fun isNotEmpty() = queue.isNotEmpty()
2022-09-23 17:42:41 +05:30
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()
}
2022-09-19 23:37:55 +05:30
}