diff --git a/strawApp/src/main/kotlin/com/sulkta/straw/feature/player/PlaybackService.kt b/strawApp/src/main/kotlin/com/sulkta/straw/feature/player/PlaybackService.kt index 7ddd1a85b..e39194ee0 100644 --- a/strawApp/src/main/kotlin/com/sulkta/straw/feature/player/PlaybackService.kt +++ b/strawApp/src/main/kotlin/com/sulkta/straw/feature/player/PlaybackService.kt @@ -206,50 +206,44 @@ class PlaybackService : MediaSessionService() { } } - private fun pickAutoplayCandidate( + private suspend fun pickAutoplayCandidate( mode: AutoplayMode, currentStreamUrl: String, uploaderUrl: String?, - ): String? = when (mode) { - AutoplayMode.Off -> null - AutoplayMode.SameChannel -> { - if (uploaderUrl.isNullOrBlank()) null - else runCatching { - val ch = uniffi.strawcore.channelInfo(uploaderUrl) - val watched = if (Settings.get().autoplaySkipWatched.value) { - History.get().watches.value.map { it.videoId }.toSet() - } else emptySet() - ch.videos - .asSequence() - .filter { it.url != currentStreamUrl } - .filter { - if (watched.isEmpty()) true - else { - val id = com.sulkta.straw.feature.detail.extractYtVideoId(it.url) - id == null || id !in watched - } - } - .firstOrNull()?.url - }.getOrNull() + ): String? { + val watched = if (Settings.get().autoplaySkipWatched.value) { + History.get().watches.value.map { it.videoId }.toSet() + } else emptySet() + fun unwatched(url: String): Boolean { + if (watched.isEmpty()) return true + val id = com.sulkta.straw.feature.detail.extractYtVideoId(url) + return id == null || id !in watched } - AutoplayMode.YtRelated -> { - runCatching { - val info = uniffi.strawcore.streamInfo(currentStreamUrl) - val watched = if (Settings.get().autoplaySkipWatched.value) { - History.get().watches.value.map { it.videoId }.toSet() - } else emptySet() - info.related - .asSequence() - .filter { it.url != currentStreamUrl } - .filter { - if (watched.isEmpty()) true - else { - val id = com.sulkta.straw.feature.detail.extractYtVideoId(it.url) - id == null || id !in watched - } - } - .firstOrNull()?.url - }.getOrNull() + return try { + when (mode) { + AutoplayMode.Off -> null + AutoplayMode.SameChannel -> { + if (uploaderUrl.isNullOrBlank()) return null + val ch = uniffi.strawcore.channelInfo(uploaderUrl) + ch.videos + .asSequence() + .filter { it.url != currentStreamUrl } + .filter { unwatched(it.url) } + .firstOrNull()?.url + } + AutoplayMode.YtRelated -> { + val info = uniffi.strawcore.streamInfo(currentStreamUrl) + info.related + .asSequence() + .filter { it.url != currentStreamUrl } + .filter { unwatched(it.url) } + .firstOrNull()?.url + } + } + } catch (c: kotlinx.coroutines.CancellationException) { + throw c + } catch (_: Throwable) { + null } }