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

Fix derive public key dialog when user dismiss #1020

Merged
merged 2 commits into from
Jul 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
@@ -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
Loading