Skip to content
Draft
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
13 changes: 11 additions & 2 deletions app/src/main/java/com/lagradost/cloudstream3/ui/APIRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class APIRepository(val api: MainAPI) {
private val cache = atomicListOf<SavedLoadResponse>()
private var cacheIndex: Int = 0
const val CACHE_SIZE = 20
fun invalidateLoadResponse(apiName: String, url: String) {
val fixedUrl = com.lagradost.cloudstream3.APIHolder.getApiFromNameNull(apiName)?.fixUrl(url) ?: url
cache.withLock { cache.removeAll { it.hash == (apiName to fixedUrl) } }
}

fun getTimeout(desired: Long?): Long {
return (desired ?: DEFAULT_TIMEOUT).coerceIn(MIN_TIMEOUT, MAX_TIMEOUT)
Expand Down Expand Up @@ -93,7 +97,7 @@ class APIRepository(val api: MainAPI) {
var found: LoadResponse? = null
for (item in cache) {
// 10 min save
if (item.hash == lookingForHash && (unixTime - item.unixTime) < 60 * 10) {
if (item.hash == lookingForHash && LoadResponseCachePolicy.isFresh(item.unixTime, unixTime, item.response.comingSoon)) {
found = item.response
break
}
Expand All @@ -108,7 +112,7 @@ class APIRepository(val api: MainAPI) {
val add = SavedLoadResponse(unixTime, response, lookingForHash)

cache.withLock {
if (cache.size > CACHE_SIZE) {
if (cache.size >= CACHE_SIZE) {
cache[cacheIndex] = add // rolling cache
cacheIndex = (cacheIndex + 1) % CACHE_SIZE
} else {
Expand Down Expand Up @@ -212,6 +216,11 @@ class APIRepository(val api: MainAPI) {
withTimeout(getTimeout(api.loadLinksTimeoutMs)) {
api.loadLinks(data, isCasting, subtitleCallback, callback)
}
} catch (timeout: kotlinx.coroutines.TimeoutCancellationException) {
logError(timeout)
return false
} catch (cancelled: kotlinx.coroutines.CancellationException) {
throw cancelled
} catch (throwable: Throwable) {
logError(throwable)
return false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.lagradost.cloudstream3.ui

internal object LoadResponseCachePolicy {
fun isFresh(savedAt: Long, now: Long, unavailable: Boolean): Boolean {
val age = now - savedAt
return age >= 0 && age < if (unavailable) 30 else 600
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ open class ResultFragmentPhone : BaseFragment<FragmentResultSwipeBinding>(

resultBinding?.apply {
resultReloadConnectionerror.setOnClickListener {
com.lagradost.cloudstream3.ui.APIRepository.invalidateLoadResponse(storedData.apiName, storedData.url)
viewModel.load(
activity,
storedData.url,
Expand Down Expand Up @@ -989,6 +990,8 @@ open class ResultFragmentPhone : BaseFragment<FragmentResultSwipeBinding>(
populateChips(resultTag, d.tags)

resultComingSoon.isVisible = d.comingSoon
resultComingSoon.setText(R.string.playback_availability_unknown)
resultComingSoon.setOnClickListener { resultReloadConnectionerror.performClick() }
resultDataHolder.isGone = d.comingSoon

val prefs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ class ResultFragmentTv : BaseFragment<FragmentResultTvBinding>(
}*/

resultReloadConnectionerror.setOnClickListener {
com.lagradost.cloudstream3.ui.APIRepository.invalidateLoadResponse(storedData.apiName, storedData.url)
viewModel.load(
activity,
storedData.url,
Expand Down Expand Up @@ -922,6 +923,9 @@ class ResultFragmentTv : BaseFragment<FragmentResultTvBinding>(

comingSoon = d.comingSoon
resultTvComingSoon.isVisible = d.comingSoon
resultTvComingSoon.setText(R.string.playback_availability_unknown)
resultTvComingSoon.isFocusable = d.comingSoon
resultTvComingSoon.setOnClickListener { resultReloadConnectionerror.performClick() }

populateChips(resultTag, d.tags)
val prefs =
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-es/availability_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="playback_availability_unknown">Esta fuente todavía no ha proporcionado contenido reproducible. Puede no estar disponible o no haberse estrenado. Pulsa para reintentar.</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/availability_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="playback_availability_unknown">This provider has not supplied playable content yet. It may be unavailable or upcoming. Tap to retry.</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lagradost.cloudstream3.ui

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class LoadResponseCachePolicyTest {
@Test fun transientUnavailableResultsDoNotStickForTenMinutes() {
assertTrue(LoadResponseCachePolicy.isFresh(100, 120, true))
assertFalse(LoadResponseCachePolicy.isFresh(100, 131, true))
assertTrue(LoadResponseCachePolicy.isFresh(100, 131, false))
}
@Test fun normalEntriesExpireAndClockRollbackInvalidates() {
assertFalse(LoadResponseCachePolicy.isFresh(100, 700, false))
assertFalse(LoadResponseCachePolicy.isFresh(100, 99, false))
}
}
Loading