Fix randomized homepage

This commit is contained in:
Blatzar 2022-08-24 04:21:46 +02:00
parent 1e11a78698
commit abeda3e758
2 changed files with 15 additions and 7 deletions

View file

@ -168,8 +168,13 @@ object PluginManager {
// var allCurrentOutDatedPlugins: Set<OnlinePluginData> = emptySet() // var allCurrentOutDatedPlugins: Set<OnlinePluginData> = emptySet()
suspend fun loadSinglePlugin(activity: Activity, apiName: String): Boolean { suspend fun loadSinglePlugin(activity: Activity, apiName: String): Boolean {
return (getPluginsOnline().firstOrNull { it.internalName == apiName } return (getPluginsOnline().firstOrNull {
?: getPluginsLocal().firstOrNull { it.internalName == apiName })?.let { savedData -> // Most of the time the provider ends with Provider which isn't part of the api name
it.internalName.replace("provider", "", ignoreCase = true) == apiName
}
?: getPluginsLocal().firstOrNull {
it.internalName.replace("provider", "", ignoreCase = true) == apiName
})?.let { savedData ->
// OnlinePluginData(savedData, onlineData) // OnlinePluginData(savedData, onlineData)
loadPlugin( loadPlugin(
activity, activity,

View file

@ -267,15 +267,18 @@ class HomeViewModel : ViewModel() {
fun loadAndCancel(preferredApiName: String?, forceReload: Boolean = true) = fun loadAndCancel(preferredApiName: String?, forceReload: Boolean = true) =
viewModelScope.launchSafe { viewModelScope.launchSafe {
// Since plugins are loaded in stages this function can get called multiple times. // Since plugins are loaded in stages this function can get called multiple times.
// This makes the home page reload only if it's a failure or loading // The issue with this is that the homepage may be fetched multiple times while the first request is loading
if (!forceReload && page.value is Resource.Success) { val api = getApiFromNameNull(preferredApiName)
if (!forceReload && api?.let { expandable[it.name]?.list?.list?.isNotEmpty() } == true) {
return@launchSafe return@launchSafe
} }
val api = getApiFromNameNull(preferredApiName) // If the plugin isn't loaded yet. (Does not set the key)
if (preferredApiName == noneApi.name) { if (api == null) {
loadAndCancel(noneApi)
} else if (preferredApiName == noneApi.name) {
setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name) setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name)
loadAndCancel(noneApi) loadAndCancel(noneApi)
} else if (preferredApiName == randomApi.name || api == null) { } else if (preferredApiName == randomApi.name) {
val validAPIs = context?.filterProviderByPreferredMedia() val validAPIs = context?.filterProviderByPreferredMedia()
if (validAPIs.isNullOrEmpty()) { if (validAPIs.isNullOrEmpty()) {
// Do not set USER_SELECTED_HOMEPAGE_API when there is no plugins loaded // Do not set USER_SELECTED_HOMEPAGE_API when there is no plugins loaded