From 34cbcd83117e892aaa6f29fb79e124c448ebe491 Mon Sep 17 00:00:00 2001 From: reduplicated <110570621+reduplicated@users.noreply.github.com> Date: Sun, 21 Aug 2022 03:44:49 +0200 Subject: [PATCH] faster init time for homepage dex --- .../lagradost/cloudstream3/MainActivity.kt | 10 +++++++++ .../cloudstream3/plugins/PluginManager.kt | 13 ++++++++++- .../cloudstream3/ui/home/HomeFragment.kt | 22 ++++++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index 7fb5cad2..49864e65 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -41,6 +41,7 @@ import com.lagradost.cloudstream3.CommonActivity.updateLocale import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.network.initClient import com.lagradost.cloudstream3.plugins.PluginManager +import com.lagradost.cloudstream3.plugins.PluginManager.loadSinglePlugin import com.lagradost.cloudstream3.receivers.VideoDownloadRestartReceiver import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.OAuth2Apis import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.accountManagers @@ -78,6 +79,7 @@ import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard import com.lagradost.cloudstream3.utils.UIHelper.navigate import com.lagradost.cloudstream3.utils.UIHelper.requestRW import com.lagradost.cloudstream3.utils.USER_PROVIDER_API +import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import com.lagradost.nicehttp.Requests import com.lagradost.nicehttp.ResponseParser import kotlinx.android.synthetic.main.activity_main.* @@ -130,6 +132,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { companion object { const val TAG = "MAINACT" val afterPluginsLoadedEvent = Event() + val mainPluginsLoadedEvent = + Event() // homepage api, used to speed up time to load for homepage val afterRepositoryLoadedEvent = Event() } @@ -436,6 +440,12 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { changeStatusBarState(isEmulatorSettings()) ioSafe { + getKey(USER_SELECTED_HOMEPAGE_API)?.let { homeApi -> + mainPluginsLoadedEvent.invoke(loadSinglePlugin(this@MainActivity, homeApi)) + } ?: run { + mainPluginsLoadedEvent.invoke(false) + } + if (settingsManager.getBoolean(getString(R.string.auto_update_plugins_key), true)) { PluginManager.updateAllOnlinePluginsAndLoadThem(this@MainActivity) } else { diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt index cab3b197..ab9d5d49 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt @@ -164,6 +164,17 @@ object PluginManager { var allCurrentOutDatedPlugins: Set = emptySet() + suspend fun loadSinglePlugin(activity: Activity, apiName: String) : Boolean { + return getPluginsOnline().firstOrNull { it.internalName == apiName }?.let { savedData -> + // OnlinePluginData(savedData, onlineData) + loadPlugin( + activity, + File(savedData.filePath), + savedData + ) + } ?: false + } + /** * Needs to be run before other plugin loading because plugin loading can not be overwritten * 1. Gets all online data about the downloaded plugins @@ -376,7 +387,7 @@ object PluginManager { file ?: return false, PluginData(internalName, pluginUrl, true, file.absolutePath, PLUGIN_VERSION_NOT_SET) ) - } catch (e : Exception) { + } catch (e: Exception) { logError(e) return false } 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 20f215d2..975545ee 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 @@ -31,8 +31,8 @@ import com.lagradost.cloudstream3.APIHolder.filterProviderByPreferredMedia 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.MainActivity.Companion.mainPluginsLoadedEvent import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.observe @@ -60,7 +60,6 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.deleteAllResumeStateIds import com.lagradost.cloudstream3.utils.DataStoreHelper.removeLastWatched import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultWatchState import com.lagradost.cloudstream3.utils.Event -import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showOptionSelectStringRes import com.lagradost.cloudstream3.utils.SubtitleHelper.getFlagFromIso import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe @@ -70,6 +69,7 @@ import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes import com.lagradost.cloudstream3.utils.UIHelper.setImage import com.lagradost.cloudstream3.utils.UIHelper.setImageBlur +import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import com.lagradost.cloudstream3.widget.CenterZoomLayoutManager import kotlinx.android.synthetic.main.fragment_home.* import kotlinx.android.synthetic.main.fragment_home.home_api_fab @@ -437,11 +437,13 @@ class HomeFragment : Fragment() { override fun onResume() { super.onResume() reloadStored() - afterPluginsLoadedEvent += ::loadHomePage + afterPluginsLoadedEvent += ::firstLoadHomePage + mainPluginsLoadedEvent += ::firstLoadHomePage } override fun onStop() { - afterPluginsLoadedEvent -= ::loadHomePage + afterPluginsLoadedEvent -= ::firstLoadHomePage + mainPluginsLoadedEvent -= ::firstLoadHomePage super.onStop() } @@ -454,6 +456,14 @@ class HomeFragment : Fragment() { homeViewModel.loadStoredData(list) } + private var hasBeenConsumed = false + private fun firstLoadHomePage(successful: Boolean = false) { + // dirty hack to make it only load once + if(hasBeenConsumed) return + hasBeenConsumed = true + loadHomePage(successful) + } + private fun loadHomePage(successful: Boolean = false) { val apiName = context?.getKey(USER_SELECTED_HOMEPAGE_API) @@ -962,13 +972,13 @@ class HomeFragment : Fragment() { reloadStored() loadHomePage() - home_loaded.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { view, _, scrollY, _, oldScrollY -> + home_loaded.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, _, scrollY, _, oldScrollY -> val dy = scrollY - oldScrollY if (dy > 0) { //check for scroll down home_api_fab?.shrink() // hide home_random?.shrink() } else if (dy < -5) { - if (view?.context?.isTvSettings() == false) { + if (v.context?.isTvSettings() == false) { home_api_fab?.extend() // show home_random?.extend() }