fix: queue refill jobs don't finish when video session ended

This commit is contained in:
Bnyro 2024-09-25 14:44:33 +02:00
parent f1d93b6840
commit 2ef1f39d19
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,16 +152,19 @@ object PlayingQueue {
} }
} }
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) = private suspend fun fetchMoreFromPlaylist(
runCatchingIO { playlistId: String,
var playlistNextPage = nextPage nextPage: String?,
while (playlistNextPage != null) { isMainList: Boolean
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run { ) {
addToQueueAsync(relatedStreams, isMainList = isMainList) var playlistNextPage = nextPage
playlistNextPage = this.nextpage while (playlistNextPage != null) {
} RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
} }
} }
}
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) = runCatchingIO { fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) = runCatchingIO {
val playlist = PlaylistsHelper.getPlaylist(playlistId) val playlist = PlaylistsHelper.getPlaylist(playlistId)
@ -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())