From f8be0ffa38d9bff6bc4a5352406d43f25c712f69 Mon Sep 17 00:00:00 2001 From: coxju Date: Wed, 10 Jan 2024 16:59:49 +0530 Subject: [PATCH] feat : invalidate link cache after 20 mins - additionaly clear cache if there is player errors or no links found --- .../cloudstream3/ui/player/AbstractPlayerFragment.kt | 5 +++++ .../cloudstream3/ui/player/GeneratorPlayer.kt | 11 +++++++++++ .../ui/player/PlayerGeneratorViewModel.kt | 4 +++- .../cloudstream3/ui/player/PlayerSubtitleHelper.kt | 4 +++- .../cloudstream3/ui/player/RepoLinkGenerator.kt | 9 +++++++++ .../com/lagradost/cloudstream3/utils/ExtractorApi.kt | 2 ++ 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/AbstractPlayerFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/AbstractPlayerFragment.kt index 17a90da9..f43f0c67 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/AbstractPlayerFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/AbstractPlayerFragment.kt @@ -102,6 +102,10 @@ abstract class AbstractPlayerFragment( throw NotImplementedError() } + open fun playerStatusChanged(){ + throw NotImplementedError() + } + open fun playerDimensionsLoaded(width: Int, height: Int) { throw NotImplementedError() } @@ -431,6 +435,7 @@ abstract class AbstractPlayerFragment( is StatusEvent -> { updateIsPlaying(wasPlaying = event.wasPlaying, isPlaying = event.isPlaying) + playerStatusChanged() } is PositionEvent -> { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt index 0a626471..3a2e46fa 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt @@ -146,6 +146,12 @@ class GeneratorPlayer : FullScreenPlayer() { } } + override fun playerStatusChanged() { + if(player.getIsPlaying()){ + viewModel.clearCache = false + } + } + private fun noSubtitles(): Boolean { return setSubtitles(null) } @@ -913,10 +919,15 @@ class GeneratorPlayer : FullScreenPlayer() { override fun playerError(exception: Throwable) { Log.i(TAG, "playerError = $currentSelectedLink") + if(!hasNextMirror()){ + viewModel.clearCache = true + } super.playerError(exception) } private fun noLinksFound() { + viewModel.clearCache = true + showToast(R.string.no_links_found_toast, Toast.LENGTH_SHORT) activity?.popCurrentPage() } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt index 3179cb9f..1bf7fa19 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt @@ -45,6 +45,8 @@ class PlayerGeneratorViewModel : ViewModel() { */ private var currentLoadingEpisodeId: Int? = null + var clearCache = false + fun setSubtitleYear(year: Int?) { _currentSubtitleYear.postValue(year) } @@ -168,7 +170,7 @@ class PlayerGeneratorViewModel : ViewModel() { } } - fun loadLinks(clearCache: Boolean = false, type: LoadType = LoadType.InApp) { + fun loadLinks(type: LoadType = LoadType.InApp) { Log.i(TAG, "loadLinks") currentJob?.cancel() diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerSubtitleHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerSubtitleHelper.kt index 25d7e3dd..8b78f799 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerSubtitleHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerSubtitleHelper.kt @@ -6,6 +6,7 @@ import android.view.ViewGroup import android.widget.FrameLayout import androidx.media3.common.MimeTypes import androidx.media3.ui.SubtitleView +import com.lagradost.cloudstream3.APIHolder import com.lagradost.cloudstream3.SubtitleFile import com.lagradost.cloudstream3.ui.player.CustomDecoder.Companion.regexSubtitlesToRemoveBloat import com.lagradost.cloudstream3.ui.player.CustomDecoder.Companion.regexSubtitlesToRemoveCaptions @@ -38,7 +39,8 @@ data class SubtitleData( val origin: SubtitleOrigin, val mimeType: String, val headers: Map, - val languageCode: String? + val languageCode: String?, + val createdTime: Long = APIHolder.unixTime ) { /** Internal ID for exoplayer, unique for each link*/ fun getId(): String { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/RepoLinkGenerator.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/RepoLinkGenerator.kt index 5626e9a0..67c234e5 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/RepoLinkGenerator.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/RepoLinkGenerator.kt @@ -2,6 +2,7 @@ package com.lagradost.cloudstream3.ui.player import android.util.Log import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull +import com.lagradost.cloudstream3.APIHolder.unixTime import com.lagradost.cloudstream3.LoadResponse import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.result.ResultEpisode @@ -89,6 +90,14 @@ class RepoLinkGenerator( val currentSubsUrls = mutableSetOf() // makes all subs urls unique val currentSubsNames = mutableSetOf() // makes all subs names unique + val invalidateCache = currentLinkCache.size - currentLinkCache.filter { link -> unixTime-link.createdTime < 60 * 20 }.size !=0 // 20 minutes + if(invalidateCache) + currentLinkCache.clear() + + val invalidateSubtitleCache = currentSubsCache.size - currentSubsCache.filter { link -> unixTime-link.createdTime < 60 * 20 }.size !=0 // 20 minutes + if(invalidateSubtitleCache) + currentSubsCache.clear() + currentLinkCache.filter { allowedTypes.contains(it.type) }.forEach { link -> currentLinks.add(link.url) callback(link to null) diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/ExtractorApi.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/ExtractorApi.kt index 1ae1b9b5..786a5668 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/ExtractorApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/ExtractorApi.kt @@ -2,6 +2,7 @@ package com.lagradost.cloudstream3.utils import android.net.Uri import com.fasterxml.jackson.annotation.JsonIgnore +import com.lagradost.cloudstream3.APIHolder import com.lagradost.cloudstream3.SubtitleFile import com.lagradost.cloudstream3.TvType import com.lagradost.cloudstream3.USER_AGENT @@ -394,6 +395,7 @@ open class ExtractorLink constructor( /** Used for getExtractorVerifierJob() */ open val extractorData: String? = null, open val type: ExtractorLinkType, + open val createdTime: Long = APIHolder.unixTime ) : VideoDownloadManager.IDownloadableMinimum { val isM3u8 : Boolean get() = type == ExtractorLinkType.M3U8 val isDash : Boolean get() = type == ExtractorLinkType.DASH