mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Merge pull request #4985 from Bnyro/master
fix: queue moves first video to end in background mode
This commit is contained in:
commit
6ca662859c
@ -172,10 +172,9 @@ class OnlinePlayerService : LifecycleService() {
|
||||
if (!keepQueue) PlayingQueue.clear()
|
||||
|
||||
if (PlayingQueue.isEmpty()) {
|
||||
PlayingQueue.updateQueue(streams!!.toStreamItem(videoId), playlistId, channelId)
|
||||
insertRelatedStreamsToQueue()
|
||||
PlayingQueue.updateQueue(streams!!.toStreamItem(videoId), playlistId, channelId, streams!!.relatedStreams)
|
||||
} else if (PlayingQueue.isLast() && playlistId == null && channelId == null) {
|
||||
insertRelatedStreamsToQueue()
|
||||
PlayingQueue.insertRelatedStreams(streams!!.relatedStreams)
|
||||
}
|
||||
|
||||
// save the current stream to the queue
|
||||
@ -356,13 +355,6 @@ class OnlinePlayerService : LifecycleService() {
|
||||
player?.checkForSegments(this, segments, sponsorBlockConfig)
|
||||
}
|
||||
|
||||
private fun insertRelatedStreamsToQueue() {
|
||||
if (!PlayerHelper.autoInsertRelatedVideos) return
|
||||
streams?.relatedStreams?.toTypedArray()?.let {
|
||||
PlayingQueue.add(*it, skipExisting = true)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the service when app is removed from the task manager.
|
||||
*/
|
||||
|
@ -7,6 +7,7 @@ import com.github.libretube.api.RetrofitInstance
|
||||
import com.github.libretube.api.obj.StreamItem
|
||||
import com.github.libretube.extensions.move
|
||||
import com.github.libretube.extensions.toID
|
||||
import com.github.libretube.helpers.PlayerHelper
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@ -30,13 +31,8 @@ object PlayingQueue {
|
||||
*/
|
||||
fun add(vararg streamItem: StreamItem, skipExisting: Boolean = false) {
|
||||
for (stream in streamItem) {
|
||||
if (skipExisting && contains(stream)) continue
|
||||
if (currentStream?.url?.toID() == stream.url?.toID() ||
|
||||
stream.title.isNullOrBlank()
|
||||
) {
|
||||
continue
|
||||
}
|
||||
// remove if already present
|
||||
if ((skipExisting && contains(stream)) || stream.title.isNullOrBlank()) continue
|
||||
|
||||
queue.remove(stream)
|
||||
queue.add(stream)
|
||||
}
|
||||
@ -119,7 +115,7 @@ object PlayingQueue {
|
||||
|
||||
/**
|
||||
* Adds a list of videos to the current queue while updating the position of the current stream
|
||||
* @param isMainList: whether the videos are part of the list, that initially has been used to
|
||||
* @param isMainList whether the videos are part of the list that initially has been used to
|
||||
* start the queue, either from a channel or playlist. If it's false, the current stream won't
|
||||
* be touched, since it's an independent list.
|
||||
*/
|
||||
@ -135,20 +131,17 @@ object PlayingQueue {
|
||||
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()
|
||||
var reAddStream = true
|
||||
if (currentStream != null && streams.includes(currentStream)) {
|
||||
queue.removeAll { it.url?.toID() == currentStream.url?.toID() }
|
||||
reAddStream = false
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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)
|
||||
|
||||
if (currentStream != null && reAddStream) {
|
||||
// re-add the stream to the end of the queue
|
||||
updateCurrent(currentStream, false)
|
||||
}
|
||||
}
|
||||
|
||||
@ -179,6 +172,7 @@ object PlayingQueue {
|
||||
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
|
||||
var channelNextPage = nextPage
|
||||
scope.launch(Dispatchers.IO) {
|
||||
runCatching {
|
||||
while (channelNextPage != null) {
|
||||
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
|
||||
addToQueueAsync(relatedStreams)
|
||||
@ -187,6 +181,7 @@ object PlayingQueue {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
|
||||
scope.launch(Dispatchers.IO) {
|
||||
@ -208,14 +203,20 @@ object PlayingQueue {
|
||||
}
|
||||
}
|
||||
|
||||
fun updateQueue(streamItem: StreamItem, playlistId: String?, channelId: String?) {
|
||||
fun updateQueue(streamItem: StreamItem, playlistId: String?, channelId: String?, relatedStreams: List<StreamItem> = emptyList()) {
|
||||
if (playlistId != null) {
|
||||
insertPlaylist(playlistId, streamItem)
|
||||
} else if (channelId != null) {
|
||||
insertChannel(channelId, streamItem)
|
||||
} else {
|
||||
} else if (relatedStreams.isNotEmpty()) {
|
||||
insertRelatedStreams(relatedStreams)
|
||||
}
|
||||
updateCurrent(streamItem)
|
||||
}
|
||||
|
||||
fun insertRelatedStreams(streams: List<StreamItem>) {
|
||||
if (!PlayerHelper.autoInsertRelatedVideos) return
|
||||
add(*streams.toTypedArray(), skipExisting = true)
|
||||
}
|
||||
|
||||
fun onQueueItemSelected(index: Int) {
|
||||
|
Loading…
Reference in New Issue
Block a user