This commit is contained in:
firelight 2024-03-08 03:00:00 +01:00 committed by GitHub
parent e3f9f255c7
commit 694e7abbdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 21 deletions

View file

@ -49,11 +49,15 @@ inline fun debugWarning(assert: () -> Boolean, message: () -> String) {
} }
} }
/** NOTE: Only one observer at a time per value */
fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) { fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) {
liveData.removeObservers(this)
liveData.observe(this) { it?.let { t -> action(t) } } liveData.observe(this) { it?.let { t -> action(t) } }
} }
/** NOTE: Only one observer at a time per value */
fun <T> LifecycleOwner.observeNullable(liveData: LiveData<T>, action: (t: T) -> Unit) { fun <T> LifecycleOwner.observeNullable(liveData: LiveData<T>, action: (t: T) -> Unit) {
liveData.removeObservers(this)
liveData.observe(this) { action(it) } liveData.observe(this) { action(it) }
} }

View file

@ -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 // Don't show account selection if there is only
// one account that exists // one account that exists
if (!isEditingFromMainActivity && skipStartup) { if (!isEditingFromMainActivity && skipStartup) {
@ -75,12 +82,6 @@ class AccountSelectActivity : AppCompatActivity(), BiometricAuthenticator.Biomet
if (currentAccount?.lockPin != null) { if (currentAccount?.lockPin != null) {
CommonActivity.init(this) CommonActivity.init(this)
viewModel.handleAccountSelect(currentAccount, this, true) viewModel.handleAccountSelect(currentAccount, this, true)
observe(viewModel.isAllowedLogin) { isAllowedLogin ->
if (isAllowedLogin) {
// We are allowed to continue to MainActivity
navigateToMainActivity()
}
}
} else { } else {
if (accounts.count() > 1) { if (accounts.count() > 1) {
showToast(this, getString( showToast(this, getString(
@ -108,12 +109,6 @@ class AccountSelectActivity : AppCompatActivity(), BiometricAuthenticator.Biomet
// Handle the selected account // Handle the selected account
accountSelectCallback = { accountSelectCallback = {
viewModel.handleAccountSelect(it, this) viewModel.handleAccountSelect(it, this)
observe(viewModel.isAllowedLogin) { isAllowedLogin ->
if (isAllowedLogin) {
// We are allowed to continue to MainActivity
navigateToMainActivity()
}
}
}, },
accountCreateCallback = { viewModel.handleAccountUpdate(it, this) }, accountCreateCallback = { viewModel.handleAccountUpdate(it, this) },
accountEditCallback = { accountEditCallback = {

View file

@ -131,6 +131,18 @@ class LibraryFragment : Fragment() {
super.onSaveInstanceState(outState) 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") @SuppressLint("ResourceType", "CutPasteId")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -395,15 +407,7 @@ class LibraryFragment : Fragment() {
binding?.viewpager?.setCurrentItem(page, false) binding?.viewpager?.setCurrentItem(page, false)
} }
observe(libraryViewModel.currentPage){ updateRandom()
if (toggleRandomButton) {
listLibraryItems.clear()
listLibraryItems.addAll(pages[it].items)
libraryRandom.isVisible = listLibraryItems.isNotEmpty()
} else {
libraryRandom.isGone = true
}
}
// Only stop loading after 300ms to hide the fade effect the viewpager produces when updating // Only stop loading after 300ms to hide the fade effect the viewpager produces when updating
// Without this there would be a flashing effect: // Without this there would be a flashing effect:
@ -481,6 +485,7 @@ class LibraryFragment : Fragment() {
} }
observe(libraryViewModel.currentPage) { position -> observe(libraryViewModel.currentPage) { position ->
updateRandom()
val all = binding?.viewpager?.allViews?.toList() val all = binding?.viewpager?.allViews?.toList()
?.filterIsInstance<AutofitRecyclerView>() ?.filterIsInstance<AutofitRecyclerView>()