fix: crash when blocked due to too many requests in playing queue

This commit is contained in:
Bnyro 2023-10-29 15:40:22 +01:00
parent 47bd510aef
commit d03a58a296
2 changed files with 42 additions and 38 deletions

View File

@ -0,0 +1,13 @@
package com.github.libretube.extensions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
fun runCatchingIO(block: suspend () -> Unit) {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
block.invoke()
}
}
}

View File

@ -6,6 +6,7 @@ import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move
import com.github.libretube.extensions.runCatchingIO
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper
import kotlinx.coroutines.CoroutineScope
@ -15,7 +16,6 @@ import kotlinx.coroutines.launch
object PlayingQueue {
private val queue = mutableListOf<StreamItem>()
private var currentStream: StreamItem? = null
private val scope = CoroutineScope(Dispatchers.IO)
/**
* Listener that gets called when the user selects an item from the queue
@ -145,34 +145,26 @@ object PlayingQueue {
}
}
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) {
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) = runCatchingIO {
var playlistNextPage = nextPage
scope.launch(Dispatchers.IO) {
while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage!!).run {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
}
}
}
}
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) {
scope.launch(Dispatchers.IO) {
runCatching {
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) = runCatchingIO {
val playlist = PlaylistsHelper.getPlaylist(playlistId)
val isMainList = newCurrentStream != null
addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList)
if (playlist.nextpage == null) return@launch
if (playlist.nextpage == null) return@runCatchingIO
fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
}
}
}
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) = runCatchingIO {
var channelNextPage = nextPage
scope.launch(Dispatchers.IO) {
runCatching {
while (channelNextPage != null) {
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
addToQueueAsync(relatedStreams)
@ -180,22 +172,16 @@ object PlayingQueue {
}
}
}
}
}
private fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
scope.launch(Dispatchers.IO) {
runCatching {
private fun insertChannel(channelId: String, newCurrentStream: StreamItem) = runCatchingIO {
val channel = RetrofitInstance.api.getChannel(channelId)
addToQueueAsync(channel.relatedStreams, newCurrentStream)
if (channel.nextpage == null) return@launch
if (channel.nextpage == null) return@runCatchingIO
fetchMoreFromChannel(channelId, channel.nextpage)
}
}
}
fun insertByVideoId(videoId: String) {
scope.launch {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
val streams = RetrofitInstance.api.getStreams(videoId.toID())
add(streams.toStreamItem(videoId))
@ -203,7 +189,12 @@ object PlayingQueue {
}
}
fun updateQueue(streamItem: StreamItem, playlistId: String?, channelId: String?, relatedStreams: List<StreamItem> = emptyList()) {
fun updateQueue(
streamItem: StreamItem,
playlistId: String?,
channelId: String?,
relatedStreams: List<StreamItem> = emptyList()
) {
if (playlistId != null) {
insertPlaylist(playlistId, streamItem)
} else if (channelId != null) {