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

59 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 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]
}
}
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 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
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-19 23:37:55 +05:30
}