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 clear() {
|
|
|
|
queue.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-20 01:37:15 +05:30
|
|
|
fun playNext(videoId: String) {
|
|
|
|
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? {
|
|
|
|
val currentIndex = queue.indexOf(currentVideoId)
|
2022-09-20 01:37:15 +05:30
|
|
|
return if (currentIndex >= queue.size) {
|
2022-09-20 01:07:30 +05:30
|
|
|
null
|
|
|
|
} else {
|
|
|
|
queue[currentIndex + 1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getPrev(): String {
|
|
|
|
return queue[
|
|
|
|
queue.indexOf(currentVideoId) - 1
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fun hasPrev(): Boolean {
|
|
|
|
val currentIndex = queue.indexOf(currentVideoId)
|
2022-09-20 01:37:15 +05:30
|
|
|
return queue.size > currentIndex + 1
|
2022-09-20 01:07:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (!contains(videoId)) add(videoId)
|
2022-09-19 23:43:25 +05:30
|
|
|
}
|
2022-09-20 01:13:13 +05:30
|
|
|
|
|
|
|
fun isNotEmpty() = queue.isNotEmpty()
|
2022-09-19 23:37:55 +05:30
|
|
|
}
|