small fix to home load

This commit is contained in:
LagradOst 2023-08-15 02:05:07 +02:00
parent 74867bed1c
commit 4d98690adb
4 changed files with 81 additions and 45 deletions

View file

@ -165,6 +165,9 @@ object PluginManager {
var loadedLocalPlugins = false var loadedLocalPlugins = false
private set private set
var loadedOnlinePlugins = false
private set
private val gson = Gson() private val gson = Gson()
private suspend fun maybeLoadPlugin(context: Context, file: File) { private suspend fun maybeLoadPlugin(context: Context, file: File) {
@ -278,6 +281,7 @@ object PluginManager {
} }
// ioSafe { // ioSafe {
loadedOnlinePlugins = true
afterPluginsLoadedEvent.invoke(false) afterPluginsLoadedEvent.invoke(false)
// } // }

View file

@ -103,7 +103,7 @@ class HomeViewModel : ViewModel() {
private var currentShuffledList: List<SearchResponse> = listOf() private var currentShuffledList: List<SearchResponse> = listOf()
private fun autoloadRepo(): APIRepository { private fun autoloadRepo(): APIRepository {
return APIRepository(synchronized(apis) { apis.first { it.hasMainPage }}) return APIRepository(synchronized(apis) { apis.first { it.hasMainPage } })
} }
private val _availableWatchStatusTypes = private val _availableWatchStatusTypes =
@ -185,8 +185,9 @@ class HomeViewModel : ViewModel() {
} }
private var onGoingLoad: Job? = null private var onGoingLoad: Job? = null
private var isCurrentlyLoadingName : String? = null private var isCurrentlyLoadingName: String? = null
private fun loadAndCancel(api: MainAPI) { private fun loadAndCancel(api: MainAPI) {
//println("loaded ${api.name}")
onGoingLoad?.cancel() onGoingLoad?.cancel()
isCurrentlyLoadingName = api.name isCurrentlyLoadingName = api.name
onGoingLoad = load(api) onGoingLoad = load(api)
@ -290,7 +291,7 @@ class HomeViewModel : ViewModel() {
} }
} }
private fun load(api: MainAPI) : Job = ioSafe { private fun load(api: MainAPI): Job = ioSafe {
repo = //if (api != null) { repo = //if (api != null) {
APIRepository(api) APIRepository(api)
//} else { //} else {
@ -455,9 +456,9 @@ class HomeViewModel : ViewModel() {
fromUI: Boolean = false fromUI: Boolean = false
) = ) =
ioSafe { ioSafe {
//println("trying to load $preferredApiName")
// 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.
// The issue with this is that the homepage may be fetched multiple times while the first request is loading // The issue with this is that the homepage may be fetched multiple times while the first request is loading
val api = getApiFromNameNull(preferredApiName)
// api?.let { expandable[it.name]?.list?.list?.isNotEmpty() } == true // api?.let { expandable[it.name]?.list?.list?.isNotEmpty() } == true
val currentPage = page.value val currentPage = page.value
@ -467,6 +468,7 @@ class HomeViewModel : ViewModel() {
return@ioSafe return@ioSafe
} }
val api = getApiFromNameNull(preferredApiName)
if (preferredApiName == noneApi.name) { if (preferredApiName == noneApi.name) {
// just set to random // just set to random
if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name) if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name)
@ -485,10 +487,12 @@ class HomeViewModel : ViewModel() {
} else if (api == null) { } else if (api == null) {
// API is not found aka not loaded or removed, post the loading // API is not found aka not loaded or removed, post the loading
// progress if waiting for plugins, otherwise nothing // progress if waiting for plugins, otherwise nothing
if(PluginManager.loadedLocalPlugins || PluginManager.checkSafeModeFile() || lastError != null) { if (PluginManager.loadedOnlinePlugins || PluginManager.checkSafeModeFile() || lastError != null) {
loadAndCancel(noneApi) loadAndCancel(noneApi)
} else { } else {
_page.postValue(Resource.Loading()) _page.postValue(Resource.Loading())
if (preferredApiName != null)
_apiName.postValue(preferredApiName)
} }
} else { } else {
// if the api is found, then set it to it and save key // if the api is found, then set it to it and save key

View file

@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.ui.settings.extensions
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -12,6 +13,8 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.marginBottom
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
@ -33,7 +36,6 @@ import com.lagradost.cloudstream3.utils.AppUtils.setDefaultFocus
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.widget.LinearRecycleViewLayoutManager
class ExtensionsFragment : Fragment() { class ExtensionsFragment : Fragment() {
var binding: FragmentExtensionsBinding? = null var binding: FragmentExtensionsBinding? = null
@ -84,51 +86,76 @@ class ExtensionsFragment : Fragment() {
setUpToolbar(R.string.extensions) setUpToolbar(R.string.extensions)
binding?.repoRecyclerView?.setLinearListLayout(
isHorizontal = false,
nextUp = R.id.settings_toolbar, //FOCUS_SELF, // back has no id so we cant :pensive:
nextDown = R.id.plugin_storage_appbar,
nextRight = FOCUS_SELF,
nextLeft = R.id.nav_rail_view
)
binding?.repoRecyclerView?.adapter = RepoAdapter(false, { binding?.repoRecyclerView?.apply {
findNavController().navigate( setLinearListLayout(
R.id.navigation_settings_extensions_to_navigation_settings_plugins, isHorizontal = false,
PluginsFragment.newInstance( nextUp = R.id.settings_toolbar, //FOCUS_SELF, // back has no id so we cant :pensive:
it.name, nextDown = R.id.plugin_storage_appbar,
it.url, nextRight = FOCUS_SELF,
false nextLeft = R.id.nav_rail_view
)
) )
}, { repo ->
// Prompt user before deleting repo
main {
val builder = AlertDialog.Builder(context ?: view.context)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
ioSafe {
RepositoryManager.removeRepository(view.context, repo)
extensionViewModel.loadStats()
extensionViewModel.loadRepositories()
}
}
DialogInterface.BUTTON_NEGATIVE -> {} if (!isTrueTvSettings())
} binding?.addRepoButton?.let { button ->
button.post {
setPadding(
paddingLeft,
paddingTop,
paddingRight,
button.measuredHeight + button.marginTop + button.marginBottom
)
} }
}
builder.setTitle(R.string.delete_repository) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
.setMessage( setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
context?.getString(R.string.delete_repository_plugins) val dy = scrollY - oldScrollY
) if (dy > 0) { //check for scroll down
.setPositiveButton(R.string.delete, dialogClickListener) binding?.addRepoButton?.shrink() // hide
.setNegativeButton(R.string.cancel, dialogClickListener) } else if (dy < -5) {
.show().setDefaultFocus() binding?.addRepoButton?.extend() // show
}
}
} }
}) adapter = RepoAdapter(false, {
findNavController().navigate(
R.id.navigation_settings_extensions_to_navigation_settings_plugins,
PluginsFragment.newInstance(
it.name,
it.url,
false
)
)
}, { repo ->
// Prompt user before deleting repo
main {
val builder = AlertDialog.Builder(context ?: view.context)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
ioSafe {
RepositoryManager.removeRepository(view.context, repo)
extensionViewModel.loadStats()
extensionViewModel.loadRepositories()
}
}
DialogInterface.BUTTON_NEGATIVE -> {}
}
}
builder.setTitle(R.string.delete_repository)
.setMessage(
context?.getString(R.string.delete_repository_plugins)
)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
}
})
}
observe(extensionViewModel.repositories) { observe(extensionViewModel.repositories) {
binding?.repoRecyclerView?.isVisible = it.isNotEmpty() binding?.repoRecyclerView?.isVisible = it.isNotEmpty()

View file

@ -11,6 +11,7 @@
<include layout="@layout/standard_toolbar" /> <include layout="@layout/standard_toolbar" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:clipToPadding="false"
android:id="@+id/repo_recycler_view" android:id="@+id/repo_recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"