From aa4c282379f824985e2f35e9309768b772586bd6 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 14 Mar 2023 12:39:04 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/tap/proxy/DerivationManagerImpl.kt | 23 +++++++++++++++++-- .../referral/presentation/build.gradle.kts | 1 + .../referral/viewmodels/ReferralViewModel.kt | 20 ++++++++++++++-- .../models/errors/UserCancelledException.kt | 3 +++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 libs/crypto/src/main/java/com/tangem/lib/crypto/models/errors/UserCancelledException.kt diff --git a/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt b/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt index 4fc1c8bfb4..705cd2824f 100644 --- a/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt +++ b/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt @@ -4,6 +4,8 @@ import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.Token import com.tangem.common.CompletionResult import com.tangem.common.card.EllipticCurve +import com.tangem.common.core.TangemError +import com.tangem.common.core.TangemSdkError import com.tangem.common.extensions.ByteArrayKey import com.tangem.common.extensions.toMapKey import com.tangem.common.hdWallet.DerivationPath @@ -13,6 +15,7 @@ import com.tangem.domain.common.extensions.fromNetworkId import com.tangem.lib.crypto.DerivationManager import com.tangem.lib.crypto.models.Currency import com.tangem.lib.crypto.models.Currency.NonNativeToken +import com.tangem.lib.crypto.models.errors.UserCancelledException import com.tangem.operations.derivation.ExtendedPublicKeysMap import com.tangem.tap.DELAY_SDK_DIALOG_CLOSE import com.tangem.tap.common.extensions.dispatchDebugErrorNotification @@ -49,9 +52,10 @@ class DerivationManagerImpl( if (scanResponse != null) { deriveMissingBlockchains( scanResponse = scanResponse, - listOf(appCurrency), + currencyList = listOf(appCurrency), + onSuccess = { continuation.resumeWith(Result.success(true)) }, ) { - continuation.resumeWith(Result.success(true)) + continuation.resumeWith(Result.failure(it)) } } } else { @@ -84,6 +88,7 @@ class DerivationManagerImpl( scanResponse: ScanResponse, currencyList: List, onSuccess: (ScanResponse) -> Unit, + onFailure: (Exception) -> Unit, ) { val derivationDataList = listOfNotNull( getDerivations(EllipticCurve.Secp256k1, scanResponse, currencyList), @@ -135,6 +140,7 @@ class DerivationManagerImpl( "Error derivation", ), ) + onFailure.invoke(handleTangemError(result.error)) } else -> { error("result result is null") @@ -176,6 +182,19 @@ class DerivationManagerImpl( ) } + /** + * Simple error handler + * for now specifically handle only UserCancelled + * + * @param error [TangemError] + */ + private fun handleTangemError(error: TangemError): Exception { + if (error is TangemSdkError.UserCancelled) { + return UserCancelledException() + } + return IllegalStateException(error.customMessage) + } + private class DerivationData( val derivations: Pair>, ) diff --git a/features/referral/presentation/build.gradle.kts b/features/referral/presentation/build.gradle.kts index 46f4b5007c..1f66fc2708 100644 --- a/features/referral/presentation/build.gradle.kts +++ b/features/referral/presentation/build.gradle.kts @@ -12,6 +12,7 @@ dependencies { implementation(project(":core:res")) implementation(project(":core:utils")) implementation(project(":core:ui")) + implementation(project(":libs:crypto")) /** AndroidX */ implementation(deps.androidx.appCompat) diff --git a/features/referral/presentation/src/main/java/com/tangem/feature/referral/viewmodels/ReferralViewModel.kt b/features/referral/presentation/src/main/java/com/tangem/feature/referral/viewmodels/ReferralViewModel.kt index 2432d8c1e0..8b1bf84d3d 100644 --- a/features/referral/presentation/src/main/java/com/tangem/feature/referral/viewmodels/ReferralViewModel.kt +++ b/features/referral/presentation/src/main/java/com/tangem/feature/referral/viewmodels/ReferralViewModel.kt @@ -15,6 +15,7 @@ import com.tangem.feature.referral.models.ReferralStateHolder import com.tangem.feature.referral.models.ReferralStateHolder.ErrorSnackbar import com.tangem.feature.referral.models.ReferralStateHolder.ReferralInfoState import com.tangem.feature.referral.router.ReferralRouter +import com.tangem.lib.crypto.models.errors.UserCancelledException import com.tangem.utils.coroutines.CoroutineDispatcherProvider import com.tangem.utils.coroutines.runCatching import dagger.hilt.android.lifecycle.HiltViewModel @@ -34,6 +35,8 @@ internal class ReferralViewModel @Inject constructor( private var referralRouter: ReferralRouter by Delegates.notNull() + private val lastReferralData = mutableStateOf(null) + init { loadReferralData() } @@ -61,7 +64,11 @@ internal class ReferralViewModel @Inject constructor( private fun loadReferralData() { uiState = uiState.copy(referralInfoState = ReferralInfoState.Loading) viewModelScope.launch(dispatchers.main) { - runCatching(dispatchers.io) { referralInteractor.getReferralStatus() } + runCatching(dispatchers.io) { + referralInteractor.getReferralStatus().apply { + lastReferralData.value = this + } + } .onSuccess(::showContent) .onFailure(::showErrorSnackbar) } @@ -73,7 +80,16 @@ internal class ReferralViewModel @Inject constructor( viewModelScope.launch(dispatchers.main) { runCatching(dispatchers.io) { referralInteractor.startReferral() } .onSuccess(::showContent) - .onFailure(::showErrorSnackbar) + .onFailure { + if (it is UserCancelledException) { + val lastRefData = lastReferralData.value + if (lastRefData != null) { + showContent(lastRefData) + } + } else { + showErrorSnackbar(it) + } + } } } diff --git a/libs/crypto/src/main/java/com/tangem/lib/crypto/models/errors/UserCancelledException.kt b/libs/crypto/src/main/java/com/tangem/lib/crypto/models/errors/UserCancelledException.kt new file mode 100644 index 0000000000..bdaf360494 --- /dev/null +++ b/libs/crypto/src/main/java/com/tangem/lib/crypto/models/errors/UserCancelledException.kt @@ -0,0 +1,3 @@ +package com.tangem.lib.crypto.models.errors + +class UserCancelledException : Exception() \ No newline at end of file