Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

fun runCatchingIO(block: suspend () -> Unit) {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
block.invoke()
}
fun runCatchingIO(block: suspend () -> Unit) = CoroutineScope(Dispatchers.IO).launch {
runCatching {
block.invoke()
}
}
}
42 changes: 28 additions & 14 deletions app/src/main/java/com/github/libretube/util/PlayingQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ 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.Job
import java.util.Collections

object PlayingQueue {
// queue is a synchronized list to be safely accessible from different coroutine threads
private val queue = Collections.synchronizedList(mutableListOf<StreamItem>())
private var currentStream: StreamItem? = null

private val queueJobs = mutableListOf<Job>()

/**
* Listener that gets called when the user selects an item from the queue
*/
private var onQueueTapListener: (StreamItem) -> Unit = {}

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
Expand Down Expand Up @@ -143,31 +152,36 @@ object PlayingQueue {
}
}

private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) =
runCatchingIO {
var playlistNextPage = nextPage
while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
}
private suspend fun fetchMoreFromPlaylist(
playlistId: String,
nextPage: String?,
isMainList: Boolean
) {
var playlistNextPage = nextPage
while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
}
}
}

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@runCatchingIO
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
while (channelNextPage != null) {
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
var pageIndex = 1
while (channelNextPage != null && pageIndex < 10) {
RetrofitInstance.api.getChannelNextPage(channelId, channelNextPage).run {
addToQueueAsync(relatedStreams)
channelNextPage = this.nextpage
pageIndex++
}
}
}
Expand All @@ -177,7 +191,7 @@ object PlayingQueue {
addToQueueAsync(channel.relatedStreams, newCurrentStream)
if (channel.nextpage == null) return@runCatchingIO
fetchMoreFromChannel(channelId, channel.nextpage)
}
}.let { queueJobs.add(it) }

fun insertByVideoId(videoId: String) = runCatchingIO {
val streams = StreamsExtractor.extractStreams(videoId.toID())
Expand Down
Loading