From b2ee9578339a2d0c81bfa136be50addb3b17f276 Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Sun, 14 Aug 2022 23:34:47 +0200 Subject: [PATCH] Fix homepage loading with plugins --- .../lagradost/cloudstream3/MainActivity.kt | 8 ++++-- .../lagradost/cloudstream3/plugins/Plugin.kt | 1 + .../cloudstream3/ui/home/HomeFragment.kt | 25 ++++++++++++------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index 57b0a72b..73017eeb 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -87,6 +87,7 @@ import com.lagradost.cloudstream3.plugins.RepositoryManager.PREBUILT_REPOSITORIE import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringRepo import com.lagradost.cloudstream3.ui.settings.extensions.RepositoryData import com.lagradost.cloudstream3.utils.Coroutines.main +import com.lagradost.cloudstream3.utils.Event const val VLC_PACKAGE = "org.videolan.vlc" @@ -131,6 +132,7 @@ var app = Requests(responseParser = object : ResponseParser { class MainActivity : AppCompatActivity(), ColorPickerDialogListener { companion object { const val TAG = "MAINACT" + val afterPluginsLoadedEvent = Event() } override fun onColorSelected(dialogId: Int, color: Int) { @@ -462,6 +464,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { } PluginManager.loadAllLocalPlugins(this@MainActivity) + afterPluginsLoadedEvent.invoke(true) } // ioSafe { @@ -495,7 +498,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { ioSafe { initAll() - apis = allProviders + // No duplicates (which can happen by registerMainAPI) + apis = allProviders.distinctBy { it } try { getKey>(USER_PROVIDER_API)?.let { list -> @@ -511,7 +515,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { } } } - apis = allProviders + apis = allProviders.distinctBy { it } APIHolder.apiMap = null } catch (e: Exception) { logError(e) diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/Plugin.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/Plugin.kt index 5218def2..242baf59 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/plugins/Plugin.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/Plugin.kt @@ -35,6 +35,7 @@ abstract class Plugin { fun registerMainAPI(element: MainAPI) { Log.i(PLUGIN_TAG, "Adding ${element.name} (${element.mainUrl}) MainAPI") element.sourcePlugin = this.__filename + // Race condition causing which would case duplicates if not for distinctBy APIHolder.allProviders.add(element) APIHolder.addPluginMapping(element) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt index cf5f0484..8a9c1de2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt @@ -32,6 +32,7 @@ import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings import com.lagradost.cloudstream3.AcraApplication.Companion.getKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey +import com.lagradost.cloudstream3.MainActivity.Companion.afterPluginsLoadedEvent import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.observe @@ -304,7 +305,8 @@ class HomeFragment : Fragment() { val cancelBtt = dialog.findViewById(R.id.cancel_btt) val applyBtt = dialog.findViewById(R.id.apply_btt) - val pairList = getPairList(anime, cartoons, tvs, docs, movies, asian, livestream, nsfw, others) + val pairList = + getPairList(anime, cartoons, tvs, docs, movies, asian, livestream, nsfw, others) cancelBtt?.setOnClickListener { dialog.dismissSafe() @@ -435,12 +437,13 @@ class HomeFragment : Fragment() { override fun onResume() { super.onResume() reloadStored() + afterPluginsLoadedEvent += ::loadHomePage } -/* + override fun onStop() { - backEvent -= ::handleBack + afterPluginsLoadedEvent -= ::loadHomePage super.onStop() - }*/ + } private fun reloadStored() { homeViewModel.loadResumeWatching() @@ -451,6 +454,14 @@ class HomeFragment : Fragment() { homeViewModel.loadStoredData(list) } + private fun loadHomePage(successful: Boolean = true) { + val apiName = context?.getKey(HOMEPAGE_API) + if (homeViewModel.apiName.value != apiName || apiName == null) { + //println("Caught home: " + homeViewModel.apiName.value + " at " + apiName) + homeViewModel.loadAndCancel(apiName) + } + } + /*private fun handleBack(poppedFragment: Boolean) { if (poppedFragment) { reloadStored() @@ -948,11 +959,7 @@ class HomeFragment : Fragment() { } reloadStored() - val apiName = context?.getKey(HOMEPAGE_API) - if (homeViewModel.apiName.value != apiName || apiName == null) { - //println("Caught home: " + homeViewModel.apiName.value + " at " + apiName) - homeViewModel.loadAndCancel(apiName) - } + loadHomePage() home_loaded.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { view, _, scrollY, _, oldScrollY -> val dy = scrollY - oldScrollY