Skip to content

Commit

Permalink
Fixes #816
Browse files Browse the repository at this point in the history
  • Loading branch information
fire-light42 authored Mar 8, 2024
1 parent e3f9f25 commit 694e7ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
liveData.removeObservers(this)
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) {
liveData.removeObservers(this)
liveData.observe(this) { action(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ 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) {
val currentAccount = accounts.firstOrNull { it.keyIndex == selectedKeyIndex }
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(
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -481,6 +485,7 @@ class LibraryFragment : Fragment() {
}

observe(libraryViewModel.currentPage) { position ->
updateRandom()
val all = binding?.viewpager?.allViews?.toList()
?.filterIsInstance<AutofitRecyclerView>()

Expand Down

1 comment on commit 694e7ab

@fire-light42
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets hope no one observes twice on a value

Please sign in to comment.