mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 06:40:30 +05:30
fix: queue when not starting with first playlist/channel video
This commit is contained in:
parent
77fd3e9288
commit
81c576506c
@ -16,7 +16,6 @@ import android.os.PowerManager
|
|||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import android.text.method.LinkMovementMethod
|
import android.text.method.LinkMovementMethod
|
||||||
import android.text.util.Linkify
|
import android.text.util.Linkify
|
||||||
import android.util.Log
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
@ -54,9 +54,12 @@ object PlayingQueue {
|
|||||||
|
|
||||||
fun hasNext() = getNext() != null
|
fun hasNext() = getNext() != null
|
||||||
|
|
||||||
fun updateCurrent(streamItem: StreamItem) {
|
fun updateCurrent(streamItem: StreamItem, asFirst: Boolean = true) {
|
||||||
currentStream = streamItem
|
currentStream = streamItem
|
||||||
if (!contains(streamItem)) queue.add(0, streamItem)
|
if (!contains(streamItem)) {
|
||||||
|
val indexToAdd = if (asFirst) 0 else size()
|
||||||
|
queue.add(indexToAdd, streamItem)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isNotEmpty() = queue.isNotEmpty()
|
fun isNotEmpty() = queue.isNotEmpty()
|
||||||
@ -85,17 +88,33 @@ object PlayingQueue {
|
|||||||
|
|
||||||
fun move(from: Int, to: Int) = queue.move(from, to)
|
fun move(from: Int, to: Int) = queue.move(from, to)
|
||||||
|
|
||||||
|
private fun addToQueueAsync(streams: List<StreamItem>, currentStreamItem: StreamItem? = null) {
|
||||||
|
val currentStream = currentStreamItem ?: this.currentStream
|
||||||
|
// if the stream already got added to the queue earlier, although it's not yet
|
||||||
|
// been found in the playlist, remove it and re-add it later
|
||||||
|
currentStream?.let { stream ->
|
||||||
|
if (streams.includes(stream)) {
|
||||||
|
queue.removeAll {
|
||||||
|
it.url?.toID() == currentStream.url?.toID()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// whether the current stream is not yet part of the list and should be added later
|
||||||
|
val reAddStream = currentStream?.let { !queue.includes(it) } ?: false
|
||||||
|
// add all new stream items to the queue
|
||||||
|
add(*streams.toTypedArray())
|
||||||
|
currentStream?.let {
|
||||||
|
// re-add the stream to the end of the queue,
|
||||||
|
if (reAddStream) updateCurrent(it, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) {
|
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) {
|
||||||
var playlistNextPage: String? = nextPage
|
var playlistNextPage = nextPage
|
||||||
scope.launch {
|
scope.launch(Dispatchers.IO) {
|
||||||
while (playlistNextPage != null) {
|
while (playlistNextPage != null) {
|
||||||
RetrofitInstance.authApi.getPlaylistNextPage(
|
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage!!).run {
|
||||||
playlistId,
|
addToQueueAsync(relatedStreams)
|
||||||
playlistNextPage!!
|
|
||||||
).apply {
|
|
||||||
add(
|
|
||||||
*this.relatedStreams.toTypedArray()
|
|
||||||
)
|
|
||||||
playlistNextPage = this.nextpage
|
playlistNextPage = this.nextpage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,25 +122,22 @@ object PlayingQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) {
|
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) {
|
||||||
scope.launch {
|
scope.launch(Dispatchers.IO) {
|
||||||
try {
|
runCatching {
|
||||||
val playlist = PlaylistsHelper.getPlaylist(playlistId)
|
val playlist = PlaylistsHelper.getPlaylist(playlistId)
|
||||||
add(*playlist.relatedStreams.toTypedArray())
|
addToQueueAsync(playlist.relatedStreams, newCurrentStream)
|
||||||
updateCurrent(newCurrentStream)
|
|
||||||
if (playlist.nextpage == null) return@launch
|
if (playlist.nextpage == null) return@launch
|
||||||
fetchMoreFromPlaylist(playlistId, playlist.nextpage)
|
fetchMoreFromPlaylist(playlistId, playlist.nextpage)
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
|
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
|
||||||
var channelNextPage: String? = nextPage
|
var channelNextPage = nextPage
|
||||||
scope.launch {
|
scope.launch(Dispatchers.IO) {
|
||||||
while (channelNextPage != null) {
|
while (channelNextPage != null) {
|
||||||
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).apply {
|
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
|
||||||
add(*relatedStreams.toTypedArray())
|
addToQueueAsync(relatedStreams)
|
||||||
channelNextPage = this.nextpage
|
channelNextPage = this.nextpage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,11 +145,10 @@ object PlayingQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
|
fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
|
||||||
scope.launch {
|
scope.launch(Dispatchers.IO) {
|
||||||
runCatching {
|
runCatching {
|
||||||
val channel = RetrofitInstance.api.getChannel(channelId)
|
val channel = RetrofitInstance.api.getChannel(channelId)
|
||||||
add(*channel.relatedStreams.toTypedArray())
|
addToQueueAsync(channel.relatedStreams, newCurrentStream)
|
||||||
updateCurrent(newCurrentStream)
|
|
||||||
if (channel.nextpage == null) return@launch
|
if (channel.nextpage == null) return@launch
|
||||||
fetchMoreFromChannel(channelId, channel.nextpage)
|
fetchMoreFromChannel(channelId, channel.nextpage)
|
||||||
}
|
}
|
||||||
@ -167,4 +182,8 @@ object PlayingQueue {
|
|||||||
repeatQueue = false
|
repeatQueue = false
|
||||||
onQueueTapListener = {}
|
onQueueTapListener = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun List<StreamItem>.includes(item: StreamItem) = any {
|
||||||
|
it.url?.toID() == item.url?.toID()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user