From fc300de8d7540858b637339f4019a98328f662cb Mon Sep 17 00:00:00 2001 From: Tangem Date: Sat, 7 Dec 2024 01:55:50 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../features/details/utils/UserWalletSaver.kt | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletSaver.kt b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletSaver.kt index b3dc6173e0..a427f0fc29 100644 --- a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletSaver.kt +++ b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletSaver.kt @@ -1,6 +1,7 @@ package com.tangem.features.details.utils import androidx.compose.ui.res.stringResource +import arrow.core.Either import arrow.core.raise.Raise import arrow.core.raise.ensureNotNull import arrow.core.raise.fold @@ -46,21 +47,31 @@ internal class UserWalletSaver @Inject constructor( private val router: Router, ) { - suspend fun scanAndSaveUserWallet(scope: CoroutineScope) = recover( - block = { - val response = scanCard(scope) ?: return@recover - val userWallet = createUserWallet(response) + suspend fun scanAndSaveUserWallet(scope: CoroutineScope) { + val response = scanCard(scope) + response.fold( + ifLeft = { + val message = it.message - saveWallet(userWallet) - }, - recover = { error -> - val message = error.message + if (!message.isNullOrEmpty()) { + messageSender.send(SnackbarMessage(message)) + } + }, + ifRight = { scanResponse -> + recover(block = { + scanResponse ?: return + val userWallet = createUserWallet(scanResponse) + saveWallet(userWallet) + }, recover = { + val message = it.message - if (!message.isNullOrEmpty()) { - messageSender.send(SnackbarMessage(message)) - } - }, - ) + if (!message.isNullOrEmpty()) { + messageSender.send(SnackbarMessage(message)) + } + },) + }, + ) + } private suspend fun Raise.saveWallet(userWallet: UserWallet) { fold( @@ -109,7 +120,7 @@ internal class UserWalletSaver @Inject constructor( return ensureNotNull(userWallet) { Error.Unknown } } - private suspend fun Raise.scanCard(scope: CoroutineScope) = suspendCancellableCoroutine { continuation -> + private suspend fun scanCard(scope: CoroutineScope) = suspendCancellableCoroutine { continuation -> scope.launch { scanCardProcessor.scan( analyticsSource = AnalyticsParam.ScreensSources.Settings, @@ -117,14 +128,14 @@ internal class UserWalletSaver @Inject constructor( /* no-op */ }, disclaimerWillShow = { - continuation.resume(null) + continuation.resume(Either.Right(null)) router.pop() }, onSuccess = { - continuation.resume(it) + continuation.resume(Either.Right(it)) }, onCancel = { - continuation.resume(null) + continuation.resume(Either.Right(null)) }, onFailure = { tangemError -> val error = if (!tangemError.silent) { @@ -136,8 +147,7 @@ internal class UserWalletSaver @Inject constructor( } else { Error.Silent } - continuation.resume(null) - raise(error) + continuation.resume(Either.Left(error)) }, ) }