From 694e7abbdfca247c6865834b90d058be55bec9f3 Mon Sep 17 00:00:00 2001 From: firelight <147925818+fire-light42@users.noreply.github.com> Date: Fri, 8 Mar 2024 03:00:00 +0100 Subject: [PATCH] Fixes #816 --- .../cloudstream3/mvvm/ArchComponentExt.kt | 4 ++++ .../ui/account/AccountSelectActivity.kt | 19 ++++++--------- .../ui/library/LibraryFragment.kt | 23 +++++++++++-------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt b/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt index eb575775..817d7db3 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt @@ -49,11 +49,15 @@ inline fun debugWarning(assert: () -> Boolean, message: () -> String) { } } +/** NOTE: Only one observer at a time per value */ fun LifecycleOwner.observe(liveData: LiveData, action: (t: T) -> Unit) { + liveData.removeObservers(this) liveData.observe(this) { it?.let { t -> action(t) } } } +/** NOTE: Only one observer at a time per value */ fun LifecycleOwner.observeNullable(liveData: LiveData, action: (t: T) -> Unit) { + liveData.removeObservers(this) liveData.observe(this) { action(it) } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt index c6e1e1fe..ccaa38f0 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt @@ -68,6 +68,13 @@ class AccountSelectActivity : AppCompatActivity(), BiometricAuthenticator.Biomet } } + observe(viewModel.isAllowedLogin) { isAllowedLogin -> + if (isAllowedLogin) { + // We are allowed to continue to MainActivity + navigateToMainActivity() + } + } + // Don't show account selection if there is only // one account that exists if (!isEditingFromMainActivity && skipStartup) { @@ -75,12 +82,6 @@ class AccountSelectActivity : AppCompatActivity(), BiometricAuthenticator.Biomet if (currentAccount?.lockPin != null) { CommonActivity.init(this) viewModel.handleAccountSelect(currentAccount, this, true) - observe(viewModel.isAllowedLogin) { isAllowedLogin -> - if (isAllowedLogin) { - // We are allowed to continue to MainActivity - navigateToMainActivity() - } - } } else { if (accounts.count() > 1) { showToast(this, getString( @@ -108,12 +109,6 @@ class AccountSelectActivity : AppCompatActivity(), BiometricAuthenticator.Biomet // Handle the selected account accountSelectCallback = { viewModel.handleAccountSelect(it, this) - observe(viewModel.isAllowedLogin) { isAllowedLogin -> - if (isAllowedLogin) { - // We are allowed to continue to MainActivity - navigateToMainActivity() - } - } }, accountCreateCallback = { viewModel.handleAccountUpdate(it, this) }, accountEditCallback = { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt index 8e9c8521..fa91d990 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt @@ -131,6 +131,18 @@ class LibraryFragment : Fragment() { super.onSaveInstanceState(outState) } + private fun updateRandom() { + val position = libraryViewModel.currentPage.value ?: 0 + val pages = (libraryViewModel.pages.value as? Resource.Success)?.value ?: return + if (toggleRandomButton) { + listLibraryItems.clear() + listLibraryItems.addAll(pages[position].items) + binding?.libraryRandom?.isVisible = listLibraryItems.isNotEmpty() + } else { + binding?.libraryRandom?.isGone = true + } + } + @SuppressLint("ResourceType", "CutPasteId") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -395,15 +407,7 @@ class LibraryFragment : Fragment() { binding?.viewpager?.setCurrentItem(page, false) } - observe(libraryViewModel.currentPage){ - if (toggleRandomButton) { - listLibraryItems.clear() - listLibraryItems.addAll(pages[it].items) - libraryRandom.isVisible = listLibraryItems.isNotEmpty() - } else { - libraryRandom.isGone = true - } - } + updateRandom() // Only stop loading after 300ms to hide the fade effect the viewpager produces when updating // Without this there would be a flashing effect: @@ -481,6 +485,7 @@ class LibraryFragment : Fragment() { } observe(libraryViewModel.currentPage) { position -> + updateRandom() val all = binding?.viewpager?.allViews?.toList() ?.filterIsInstance()