From 87dfe99c21883681e5e53e6ca52cf3569b27b60d Mon Sep 17 00:00:00 2001 From: neerajlovecyber Date: Thu, 23 Jul 2026 21:17:58 +0530 Subject: [PATCH] fix: reuse loaded streams when Next Episode tapped during autoplay countdown When the autoplay job has already located a stream for the next episode and the countdown (3-2-1) is running, tapping the Next button cancels the countdown coroutine and relaunches the full stream search from scratch. This causes a visible 'Searching...' flicker and unnecessary re-fetching of streams that were already found. Fix: in playNextEpisode(), check episodeStreamsState for already-loaded streams before starting a new search. If streams are present, cancel the running job and immediately play the first one. Falls back to the full search path only when no streams are yet available. --- .../player/PlayerScreenRuntimeSourceActions.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreenRuntimeSourceActions.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreenRuntimeSourceActions.kt index 86b44aedb..ba126fd27 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreenRuntimeSourceActions.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreenRuntimeSourceActions.kt @@ -371,6 +371,22 @@ internal fun PlayerScreenRuntime.switchToDownloadedEpisode(downloadItem: Downloa } internal fun PlayerScreenRuntime.playNextEpisode() { + // If streams for the next episode are already loaded (e.g. autoplay found one + // during its countdown), re-use them immediately instead of restarting the + // entire search. This prevents a visible "Searching…" flicker when the user + // manually taps the Next Episode button while the countdown is in progress. + val nextVideoId = nextEpisodeInfo?.videoId + val nextVideo = nextVideoId?.let { id -> playerMetaVideos.firstOrNull { it.id == id } } + if (nextVideo != null && nextEpisodeInfo?.hasAired == true) { + val cachedStreams = PlayerStreamsRepository.episodeStreamsState.value.groups + .flatMap { it.streams } + if (cachedStreams.isNotEmpty()) { + nextEpisodeAutoPlayJob?.cancel() + switchToEpisodeStream(cachedStreams.first(), nextVideo) + return + } + } + scope.launchPlayerNextEpisodeAutoPlay( previousJob = nextEpisodeAutoPlayJob, nextEpisodeInfo = nextEpisodeInfo,