Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM-10094: Disable double-navigation by default #3660

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.x8bit.bitwarden.ui.platform.base.util

/**
* Almost all the events in the app involve navigation or toasts. To prevent accidentally
* navigating to the same view twice, by default, events are ignored if the view is not currently
* resumed. To avoid that restriction, specific events can implement [BackgroundEvent].
*/
interface BackgroundEvent
shannon-livefront marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ package com.x8bit.bitwarden.ui.platform.base.util

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

/**
* Convenience method for observing event flow from [BaseViewModel].
*
* By default, events will only be consumed when the associated screen is
* resumed, to avoid bugs like duplicate navigation calls. To override
* this behavior, a given event type can implement [BackgroundEvent].
*/
@Composable
fun <E> EventsEffect(
viewModel: BaseViewModel<*, E, *>,
lifecycleOwner: Lifecycle = LocalLifecycleOwner.current.lifecycle,
handler: suspend (E) -> Unit,
) {
LaunchedEffect(key1 = Unit) {
viewModel.eventFlow
.filter {
it is BackgroundEvent ||
shannon-livefront marked this conversation as resolved.
Show resolved Hide resolved
lifecycleOwner.currentState.isAtLeast(Lifecycle.State.RESUMED)
}
.onEach { handler.invoke(it) }
.launchIn(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.x8bit.bitwarden.data.vault.repository.model.TotpCodeResult
import com.x8bit.bitwarden.data.vault.repository.model.UpdateCipherResult
import com.x8bit.bitwarden.data.vault.repository.model.VaultData
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
import com.x8bit.bitwarden.ui.platform.base.util.BackgroundEvent
import com.x8bit.bitwarden.ui.platform.base.util.Text
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.base.util.concat
Expand Down Expand Up @@ -2358,8 +2359,7 @@ sealed class VaultAddEditEvent {
/**
* Navigate the user to the tooltip URI.
*/
data object NavigateToTooltipUri :
VaultAddEditEvent()
data object NavigateToTooltipUri : VaultAddEditEvent()
Copy link
Collaborator

Choose a reason for hiding this comment

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

๐Ÿ‘


/**
* Navigate to the QR code scan screen.
Expand All @@ -2385,7 +2385,7 @@ sealed class VaultAddEditEvent {
*/
data class CompleteFido2Registration(
val result: Fido2RegisterCredentialResult,
) : VaultAddEditEvent()
) : BackgroundEvent, VaultAddEditEvent()

/**
* Perform user verification for a FIDO 2 credential operation.
Expand All @@ -2394,7 +2394,7 @@ sealed class VaultAddEditEvent {
*/
data class Fido2UserVerification(
val isRequired: Boolean,
) : VaultAddEditEvent()
) : BackgroundEvent, VaultAddEditEvent()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.x8bit.bitwarden.data.vault.repository.model.GenerateTotpResult
import com.x8bit.bitwarden.data.vault.repository.model.RemovePasswordSendResult
import com.x8bit.bitwarden.data.vault.repository.model.VaultData
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
import com.x8bit.bitwarden.ui.platform.base.util.BackgroundEvent
import com.x8bit.bitwarden.ui.platform.base.util.Text
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.base.util.concat
Expand Down Expand Up @@ -2102,15 +2103,15 @@ sealed class VaultItemListingEvent {
*/
data class CompleteFido2Registration(
val result: Fido2RegisterCredentialResult,
) : VaultItemListingEvent()
) : BackgroundEvent, VaultItemListingEvent()

/**
* Perform user verification for a FIDO 2 credential operation.
*/
data class Fido2UserVerification(
val isRequired: Boolean,
val selectedCipherView: CipherView,
) : VaultItemListingEvent()
) : BackgroundEvent, VaultItemListingEvent()

/**
* FIDO 2 credential assertion result has been received and the process is ready to be
Expand All @@ -2120,7 +2121,7 @@ sealed class VaultItemListingEvent {
*/
data class CompleteFido2Assertion(
val result: Fido2CredentialAssertionResult,
) : VaultItemListingEvent()
) : BackgroundEvent, VaultItemListingEvent()

/**
* FIDO 2 credential lookup result has been received and the process is ready to be completed.
Expand All @@ -2129,7 +2130,7 @@ sealed class VaultItemListingEvent {
*/
data class CompleteFido2GetCredentialsRequest(
val result: Fido2GetCredentialsResult,
) : VaultItemListingEvent()
) : BackgroundEvent, VaultItemListingEvent()
}

/**
Expand Down