delay mainpage

This commit is contained in:
reduplicated 2022-10-09 01:36:06 +02:00
parent 020b3b7472
commit 61fb302a37
3 changed files with 37 additions and 3 deletions

View file

@ -381,7 +381,16 @@ abstract class MainAPI {
open var storedCredentials: String? = null open var storedCredentials: String? = null
open var canBeOverridden: Boolean = true open var canBeOverridden: Boolean = true
//open val uniqueId : Int by lazy { this.name.hashCode() } // in case of duplicate providers you can have a shared id /** if this is turned on then it will request the homepage one after the other,
used to delay if they block many request at the same time*/
open var sequentialMainPage : Boolean = false
/** in milliseconds, this can be used to add more delay between homepage requests
* on first load if sequentialMainPage is turned on */
open var sequentialMainPageDelay : Long = 0L
/** in milliseconds, this can be used to add more delay between homepage requests when scrolling */
open var sequentialMainPageScrollDelay : Long = 0L
/** used to keep track when last homepage request was in unixtime ms */
var lastHomepageRequest : Long = 0L
open var lang = "en" // ISO_639_1 check SubtitleHelper open var lang = "en" // ISO_639_1 check SubtitleHelper

View file

@ -1,10 +1,12 @@
package com.lagradost.cloudstream3.ui package com.lagradost.cloudstream3.ui
import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.unixTimeMS
import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.mvvm.safeApiCall import com.lagradost.cloudstream3.mvvm.safeApiCall
import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.ExtractorLink
import kotlinx.coroutines.delay
class APIRepository(val api: MainAPI) { class APIRepository(val api: MainAPI) {
companion object { companion object {
@ -62,12 +64,33 @@ class APIRepository(val api: MainAPI) {
} }
} }
suspend fun waitForHomeDelay() {
val delta = api.sequentialMainPageScrollDelay + api.lastHomepageRequest - unixTimeMS
if(delta < 0) return
delay(delta)
}
suspend fun getMainPage(page: Int, nameIndex: Int? = null): Resource<List<HomePageResponse?>> { suspend fun getMainPage(page: Int, nameIndex: Int? = null): Resource<List<HomePageResponse?>> {
return safeApiCall { return safeApiCall {
api.lastHomepageRequest = unixTimeMS
nameIndex?.let { api.mainPage.getOrNull(it) }?.let { data -> nameIndex?.let { api.mainPage.getOrNull(it) }?.let { data ->
listOf(api.getMainPage(page, MainPageRequest(data.name, data.data))) listOf(api.getMainPage(page, MainPageRequest(data.name, data.data)))
} ?: api.mainPage.apmap { data -> } ?: run {
api.getMainPage(page, MainPageRequest(data.name, data.data)) if (api.sequentialMainPage) {
var first = true
api.mainPage.map { data ->
if (!first) // dont want to sleep on first request
delay(api.sequentialMainPageDelay)
first = false
api.getMainPage(page, MainPageRequest(data.name, data.data))
}
} else {
api.mainPage.apmap { data ->
api.getMainPage(page, MainPageRequest(data.name, data.data))
}
}
} }
} }
} }

View file

@ -162,6 +162,8 @@ class HomeViewModel : ViewModel() {
lock += name lock += name
repo?.apply { repo?.apply {
waitForHomeDelay()
expandable[name]?.let { current -> expandable[name]?.let { current ->
debugAssert({ !current.hasNext }) { debugAssert({ !current.hasNext }) {
"Expand called when not needed" "Expand called when not needed"