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

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