Skip to content

Commit

Permalink
PM-10094: Disable double-navigation by default
Browse files Browse the repository at this point in the history
  • Loading branch information
shannon-livefront committed Jul 31, 2024
1 parent 1f8d50e commit dcf6bac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ 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

Expand All @@ -12,11 +15,23 @@ import kotlinx.coroutines.flow.onEach
@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 ||
lifecycleOwner.currentState.isAtLeast(Lifecycle.State.RESUMED)
}
.onEach { handler.invoke(it) }
.launchIn(this)
}
}

/**
* 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 conform to [BackgroundEvent].
*/
interface BackgroundEvent
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()

/**
* Navigate to the QR code scan screen.
Expand All @@ -2376,7 +2376,7 @@ sealed class VaultAddEditEvent {
*/
data class NavigateToGeneratorModal(
val generatorMode: GeneratorMode.Modal,
) : VaultAddEditEvent()
) : BackgroundEvent, VaultAddEditEvent()

/**
* Complete the current FIDO 2 credential registration process.
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

0 comments on commit dcf6bac

Please sign in to comment.