2022-09-19 23:37:55 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
2022-10-23 18:13:47 +05:30
|
|
|
import com.github.libretube.api.RetrofitInstance
|
2022-10-23 15:03:35 +05:30
|
|
|
import com.github.libretube.api.obj.StreamItem
|
2022-10-23 18:39:58 +05:30
|
|
|
import com.github.libretube.extensions.move
|
2022-10-23 17:09:15 +05:30
|
|
|
import com.github.libretube.extensions.toID
|
2022-10-23 18:13:47 +05:30
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
2022-10-23 15:03:35 +05:30
|
|
|
|
2022-09-19 23:37:55 +05:30
|
|
|
object PlayingQueue {
|
2022-10-23 17:09:15 +05:30
|
|
|
private val queue = mutableListOf<StreamItem>()
|
|
|
|
private var currentStream: StreamItem? = null
|
|
|
|
|
|
|
|
fun add(vararg streamItem: StreamItem) {
|
|
|
|
streamItem.forEach {
|
|
|
|
if (currentStream != it) {
|
|
|
|
if (queue.contains(it)) queue.remove(it)
|
|
|
|
queue.add(it)
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 23:37:55 +05:30
|
|
|
}
|
|
|
|
|
2022-10-23 17:09:15 +05:30
|
|
|
fun addAsNext(streamItem: StreamItem) {
|
|
|
|
if (currentStream == streamItem) return
|
|
|
|
if (queue.contains(streamItem)) queue.remove(streamItem)
|
2022-09-19 23:37:55 +05:30
|
|
|
queue.add(
|
2022-10-23 17:09:15 +05:30
|
|
|
currentIndex() + 1,
|
|
|
|
streamItem
|
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 {
|
2022-10-23 17:09:15 +05:30
|
|
|
queue[currentIndex() + 1].url?.toID()
|
2022-09-23 17:42:41 +05:30
|
|
|
} catch (e: Exception) {
|
2022-09-20 01:07:30 +05:30
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 01:51:30 +05:30
|
|
|
fun getPrev(): String? {
|
2022-10-23 17:09:15 +05:30
|
|
|
val index = queue.indexOf(currentStream)
|
|
|
|
return if (index > 0) queue[index - 1].url?.toID() else null
|
2022-09-20 01:07:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fun hasPrev(): Boolean {
|
2022-10-23 17:09:15 +05:30
|
|
|
return queue.indexOf(currentStream) > 0
|
2022-09-20 01:07:30 +05:30
|
|
|
}
|
|
|
|
|
2022-10-23 17:09:15 +05:30
|
|
|
fun updateCurrent(streamItem: StreamItem) {
|
|
|
|
currentStream = streamItem
|
2022-10-23 18:13:47 +05:30
|
|
|
if (!contains(streamItem)) queue.add(streamItem)
|
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
|
|
|
|
2022-10-23 17:09:15 +05:30
|
|
|
fun isEmpty() = queue.isEmpty()
|
|
|
|
|
2022-09-23 17:42:41 +05:30
|
|
|
fun clear() = queue.clear()
|
|
|
|
|
2022-10-23 17:09:15 +05:30
|
|
|
fun size() = queue.size
|
2022-09-23 17:42:41 +05:30
|
|
|
|
2022-10-23 18:49:14 +05:30
|
|
|
fun currentIndex(): Int {
|
|
|
|
return try {
|
|
|
|
queue.indexOf(
|
|
|
|
queue.first { it.url?.toID() == currentStream?.url?.toID() }
|
|
|
|
)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 17:42:41 +05:30
|
|
|
|
2022-10-23 18:13:47 +05:30
|
|
|
fun contains(streamItem: StreamItem) = queue.any { it.url?.toID() == streamItem.url?.toID() }
|
2022-10-23 17:09:15 +05:30
|
|
|
|
|
|
|
fun getStreams() = queue
|
2022-10-23 18:13:47 +05:30
|
|
|
|
2022-10-29 02:34:44 +05:30
|
|
|
fun setStreams(streams: List<StreamItem>) {
|
|
|
|
queue.clear()
|
|
|
|
queue.addAll(streams)
|
|
|
|
}
|
|
|
|
|
2022-10-23 18:24:02 +05:30
|
|
|
fun remove(index: Int) = queue.removeAt(index)
|
|
|
|
|
2022-10-23 18:39:58 +05:30
|
|
|
fun move(from: Int, to: Int) = queue.move(from, to)
|
|
|
|
|
2022-10-23 18:13:47 +05:30
|
|
|
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) {
|
|
|
|
var playlistNextPage: String? = nextPage
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
while (playlistNextPage != null) {
|
|
|
|
RetrofitInstance.authApi.getPlaylistNextPage(
|
|
|
|
playlistId,
|
|
|
|
playlistNextPage!!
|
|
|
|
).apply {
|
|
|
|
add(
|
|
|
|
*this.relatedStreams.orEmpty().toTypedArray()
|
|
|
|
)
|
|
|
|
playlistNextPage = this.nextpage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) {
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
try {
|
|
|
|
val response = RetrofitInstance.authApi.getPlaylist(playlistId)
|
|
|
|
add(
|
|
|
|
*response.relatedStreams
|
|
|
|
.orEmpty()
|
|
|
|
.toTypedArray()
|
|
|
|
)
|
|
|
|
updateCurrent(newCurrentStream)
|
|
|
|
fetchMoreFromPlaylist(playlistId, response.nextpage)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 23:37:55 +05:30
|
|
|
}
|