Updated on 2026-08-14

This commit is contained in:
Tangem 2023-03-14 12:39:04 +03:00
parent ea34514ca0
commit aa4c282379
4 changed files with 43 additions and 4 deletions

View file

@ -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<com.tangem.tap.features.wallet.models.Currency>,
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<ByteArrayKey, List<DerivationPath>>,
)

View file

@ -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)

View file

@ -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<ReferralData?>(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)
}
}
}
}

View file

@ -0,0 +1,3 @@
package com.tangem.lib.crypto.models.errors
class UserCancelledException : Exception()