Merge pull request #6533 from Bnyro/master

fix: queue refill jobs don't finish when video session ended
This commit is contained in:
Bnyro 2024-09-25 14:44:55 +02:00 committed by GitHub
commit 10fa84c0be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 20 deletions

View File

@ -4,10 +4,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
fun runCatchingIO(block: suspend () -> Unit) { fun runCatchingIO(block: suspend () -> Unit) = CoroutineScope(Dispatchers.IO).launch {
CoroutineScope(Dispatchers.IO).launch {
runCatching { runCatching {
block.invoke() block.invoke()
} }
}
} }

View File

@ -10,6 +10,7 @@ import com.github.libretube.extensions.move
import com.github.libretube.extensions.runCatchingIO import com.github.libretube.extensions.runCatchingIO
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import kotlinx.coroutines.Job
import java.util.Collections import java.util.Collections
object PlayingQueue { object PlayingQueue {
@ -17,6 +18,8 @@ object PlayingQueue {
private val queue = Collections.synchronizedList(mutableListOf<StreamItem>()) private val queue = Collections.synchronizedList(mutableListOf<StreamItem>())
private var currentStream: StreamItem? = null private var currentStream: StreamItem? = null
private val queueJobs = mutableListOf<Job>()
/** /**
* Listener that gets called when the user selects an item from the queue * Listener that gets called when the user selects an item from the queue
*/ */
@ -24,7 +27,13 @@ object PlayingQueue {
var repeatMode: Int = Player.REPEAT_MODE_OFF var repeatMode: Int = Player.REPEAT_MODE_OFF
fun clear() = queue.clear() fun clear() {
queueJobs.forEach {
it.cancel()
}
queueJobs.clear()
queue.clear()
}
/** /**
* @param skipExisting Whether to skip the [streamItem] if it's already part of the queue * @param skipExisting Whether to skip the [streamItem] if it's already part of the queue
@ -143,8 +152,11 @@ object PlayingQueue {
} }
} }
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) = private suspend fun fetchMoreFromPlaylist(
runCatchingIO { playlistId: String,
nextPage: String?,
isMainList: Boolean
) {
var playlistNextPage = nextPage var playlistNextPage = nextPage
while (playlistNextPage != null) { while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run { RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
@ -160,14 +172,16 @@ object PlayingQueue {
addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList) addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList)
if (playlist.nextpage == null) return@runCatchingIO if (playlist.nextpage == null) return@runCatchingIO
fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList) fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
} }.let { queueJobs.add(it) }
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) = runCatchingIO { private suspend fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
var channelNextPage = nextPage var channelNextPage = nextPage
while (channelNextPage != null) { var pageIndex = 1
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run { while (channelNextPage != null && pageIndex < 10) {
RetrofitInstance.api.getChannelNextPage(channelId, channelNextPage).run {
addToQueueAsync(relatedStreams) addToQueueAsync(relatedStreams)
channelNextPage = this.nextpage channelNextPage = this.nextpage
pageIndex++
} }
} }
} }
@ -177,7 +191,7 @@ object PlayingQueue {
addToQueueAsync(channel.relatedStreams, newCurrentStream) addToQueueAsync(channel.relatedStreams, newCurrentStream)
if (channel.nextpage == null) return@runCatchingIO if (channel.nextpage == null) return@runCatchingIO
fetchMoreFromChannel(channelId, channel.nextpage) fetchMoreFromChannel(channelId, channel.nextpage)
} }.let { queueJobs.add(it) }
fun insertByVideoId(videoId: String) = runCatchingIO { fun insertByVideoId(videoId: String) = runCatchingIO {
val streams = StreamsExtractor.extractStreams(videoId.toID()) val streams = StreamsExtractor.extractStreams(videoId.toID())