Skip to content

Commit

Permalink
Merge pull request #1020 from radixdlt/fix/derive-piblic-key-dialog-w…
Browse files Browse the repository at this point in the history
…hen-dismissing

Fix derive public key dialog when user dismiss
  • Loading branch information
giannis-rdx committed Jul 1, 2024
2 parents bde973f + 2f621b0 commit 8f5ed05
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.babylon.wallet.android.presentation.accessfactorsources.derivepublickey

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -45,6 +46,10 @@ fun DerivePublicKeyDialog(
val state by viewModel.state.collectAsState()
val context = LocalContext.current

BackHandler {
viewModel.onUserDismiss()
}

LaunchedEffect(Unit) {
viewModel.oneOffEvent.collect { event ->
when (event) {
Expand All @@ -58,6 +63,7 @@ fun DerivePublicKeyDialog(
}
}
DerivePublicKeyViewModel.Event.AccessingFactorSourceCompleted -> onDismiss()
DerivePublicKeyViewModel.Event.UserDismissed -> onDismiss()
}
}
}
Expand All @@ -66,7 +72,7 @@ fun DerivePublicKeyDialog(
modifier = modifier,
showContentForFactorSource = state.showContentForFactorSource,
shouldShowRetryButton = state.shouldShowRetryButton,
onDismiss = onDismiss,
onDismiss = viewModel::onUserDismiss,
onRetryClick = viewModel::onRetryClick
)
}
Expand All @@ -81,9 +87,7 @@ private fun DerivePublicKeyBottomSheetContent(
) {
BottomSheetDialogWrapper(
modifier = modifier,
onDismiss = {
onDismiss()
}
onDismiss = onDismiss
) {
Column(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.radixdlt.sargon.PublicKey
import com.radixdlt.sargon.extensions.init
import com.radixdlt.sargon.extensions.string
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -114,6 +115,16 @@ class DerivePublicKeyViewModel @Inject constructor(
}
}

fun onUserDismiss() {
viewModelScope.launch {
derivePublicKeyJob?.cancel()
accessFactorSourcesUiProxy.setOutput(
output = AccessFactorSourcesOutput.Failure(CancellationException("User cancelled"))
)
sendEvent(Event.UserDismissed)
}
}

private suspend fun derivePublicKey(): Result<Unit> {
return when (val factorSource = input.factorSource) {
is FactorSource.Device -> {
Expand Down Expand Up @@ -192,5 +203,6 @@ class DerivePublicKeyViewModel @Inject constructor(
sealed interface Event : OneOffEvent {
data object RequestBiometricPrompt : Event
data object AccessingFactorSourceCompleted : Event
data object UserDismissed : Event
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.extensions.asGeneral
import com.radixdlt.sargon.extensions.string
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -63,6 +65,8 @@ class CreateAccountViewModel @Inject constructor(
val buttonEnabled = savedStateHandle.getStateFlow(CREATE_ACCOUNT_BUTTON_ENABLED, false)
val isAccountNameLengthMoreThanTheMax = savedStateHandle.getStateFlow(IS_ACCOUNT_NAME_LENGTH_MORE_THAN_THE_MAX, false)

private var accessFactorSourcesJob: Job? = null

override fun initialState(): CreateAccountUiState = CreateAccountUiState(
isFirstAccount = args.requestSource?.isFirstTime() == true
)
Expand Down Expand Up @@ -115,40 +119,43 @@ class CreateAccountViewModel @Inject constructor(
}

// when called the access factor source bottom sheet dialog is presented
private suspend fun accessFactorSourceForAccountCreation(
private fun accessFactorSourceForAccountCreation(
isFirstAccount: Boolean,
isWithLedger: Boolean
) {
val selectedFactorSource = if (isWithLedger) { // then get the selected ledger device
sendEvent(CreateAccountEvent.AddLedgerDevice)
appEventBus.events
.filterIsInstance<AppEvent.AccessFactorSources.SelectedLedgerDevice>()
.first()
.ledgerFactorSource
} else {
getProfileUseCase().mainBabylonFactorSource ?: return
}
accessFactorSourcesJob?.cancel()
accessFactorSourcesJob = viewModelScope.launch {
val selectedFactorSource = if (isWithLedger) { // then get the selected ledger device
sendEvent(CreateAccountEvent.AddLedgerDevice)
appEventBus.events
.filterIsInstance<AppEvent.AccessFactorSources.SelectedLedgerDevice>()
.first()
.ledgerFactorSource
} else {
getProfileUseCase().mainBabylonFactorSource ?: return@launch
}

accessFactorSourcesProxy.getPublicKeyAndDerivationPathForFactorSource(
accessFactorSourcesInput = AccessFactorSourcesInput.ToDerivePublicKey(
forNetworkId = args.networkIdToSwitch ?: getProfileUseCase().currentGateway.network.id,
factorSource = selectedFactorSource,
isBiometricsProvided = isFirstAccount
)
).onSuccess {
handleAccountCreation { nameOfAccount ->
val factorSourceId = when (selectedFactorSource) {
is FactorSource.Device -> selectedFactorSource.value.id.asGeneral()
is FactorSource.Ledger -> selectedFactorSource.value.id.asGeneral()
}
createAccountUseCase(
displayName = DisplayName(nameOfAccount),
factorSourceId = factorSourceId,
hdPublicKey = it.value
accessFactorSourcesProxy.getPublicKeyAndDerivationPathForFactorSource(
accessFactorSourcesInput = AccessFactorSourcesInput.ToDerivePublicKey(
forNetworkId = args.networkIdToSwitch ?: getProfileUseCase().currentGateway.network.id,
factorSource = selectedFactorSource,
isBiometricsProvided = isFirstAccount
)
).onSuccess {
handleAccountCreation { nameOfAccount ->
val factorSourceId = when (selectedFactorSource) {
is FactorSource.Device -> selectedFactorSource.value.id.asGeneral()
is FactorSource.Ledger -> selectedFactorSource.value.id.asGeneral()
}
createAccountUseCase(
displayName = DisplayName(nameOfAccount),
factorSourceId = factorSourceId,
hdPublicKey = it.value
)
}
}.onFailure { throwable ->
handleAccountCreationError(throwable)
}
}.onFailure { throwable ->
handleAccountCreationError(throwable)
}
}

Expand All @@ -160,6 +167,9 @@ class CreateAccountViewModel @Inject constructor(
if (throwable is ProfileException.NoMnemonic) {
state.copy(shouldShowNoMnemonicError = true)
} else {
if (throwable is CancellationException) { // user cancelled, don't print it
return
}
state.copy(
uiMessage = UiMessage.ErrorMessage(throwable)
)
Expand Down

0 comments on commit 8f5ed05

Please sign in to comment.