Updated on 2026-08-14

This commit is contained in:
Tangem 2024-12-07 01:10:38 +03:00
commit 6dbe6359dd
5 changed files with 50 additions and 33 deletions

View file

@ -32,6 +32,7 @@ internal class DefaultScanCardProcessor : ScanCardProcessor {
onProgressStateChange: suspend (showProgress: Boolean) -> Unit,
onWalletNotCreated: suspend () -> Unit,
disclaimerWillShow: () -> Unit,
onCancel: suspend () -> Unit,
onFailure: suspend (error: TangemError) -> Unit,
onSuccess: suspend (scanResponse: ScanResponse) -> Unit,
) {
@ -52,6 +53,7 @@ internal class DefaultScanCardProcessor : ScanCardProcessor {
onProgressStateChange,
onWalletNotCreated,
disclaimerWillShow,
onCancel,
onFailure,
onSuccess,
)

View file

@ -57,6 +57,7 @@ internal object LegacyScanProcessor {
onProgressStateChange: suspend (showProgress: Boolean) -> Unit,
onWalletNotCreated: suspend () -> Unit,
disclaimerWillShow: () -> Unit,
onCancel: suspend () -> Unit,
onFailure: suspend (error: TangemError) -> Unit,
onSuccess: suspend (scanResponse: ScanResponse) -> Unit,
) = withMainContext {
@ -88,6 +89,7 @@ internal object LegacyScanProcessor {
onProgressStateChange = onProgressStateChange,
onSuccess = onSuccess,
onWalletNotCreated = onWalletNotCreated,
onCancel = onCancel,
)
},
)
@ -138,13 +140,19 @@ internal object LegacyScanProcessor {
scanResponse: ScanResponse,
crossinline onProgressStateChange: suspend (showProgress: Boolean) -> Unit,
crossinline onWalletNotCreated: suspend () -> Unit,
crossinline onCancel: suspend () -> Unit,
crossinline onSuccess: suspend (ScanResponse) -> Unit,
) {
store.dispatchOnMain(TwinCardsAction.IfTwinsPrepareState(scanResponse))
checkCardWasUsedInApp(
scanResponse = scanResponse,
onCancel = { mainScope.launch { onProgressStateChange.invoke(false) } },
onCancel = {
mainScope.launch {
onProgressStateChange.invoke(false)
onCancel()
}
},
) {
if (OnboardingHelper.isOnboardingCase(scanResponse)) {
Analytics.addContext(scanResponse)

View file

@ -18,6 +18,7 @@ interface ScanCardProcessor {
onProgressStateChange: suspend (showProgress: Boolean) -> Unit = {},
onWalletNotCreated: suspend () -> Unit = {},
disclaimerWillShow: () -> Unit = {},
onCancel: suspend () -> Unit = {},
onFailure: suspend (error: TangemError) -> Unit = {},
onSuccess: suspend (scanResponse: ScanResponse) -> Unit = {},
)

View file

@ -64,6 +64,6 @@ internal class UserWalletListModel @Inject constructor(
}
private fun addUserWallet() = withProgress(isWalletSavingInProgress) {
userWalletSaver.scanAndSaveUserWallet()
userWalletSaver.scanAndSaveUserWallet(modelScope)
}
}

View file

@ -28,7 +28,11 @@ import com.tangem.domain.wallets.usecase.GenerateWalletNameUseCase
import com.tangem.domain.wallets.usecase.SaveWalletUseCase
import com.tangem.domain.wallets.usecase.ShouldSaveUserWalletsSyncUseCase
import com.tangem.features.details.impl.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import javax.inject.Inject
import kotlin.coroutines.resume
@ComponentScoped
@Suppress("LongParameterList")
@ -42,9 +46,9 @@ internal class UserWalletSaver @Inject constructor(
private val router: Router,
) {
suspend fun scanAndSaveUserWallet() = recover(
suspend fun scanAndSaveUserWallet(scope: CoroutineScope) = recover(
block = {
val response = scanCard() ?: return@recover
val response = scanCard(scope) ?: return@recover
val userWallet = createUserWallet(response)
saveWallet(userWallet)
@ -105,36 +109,38 @@ internal class UserWalletSaver @Inject constructor(
return ensureNotNull(userWallet) { Error.Unknown }
}
private suspend fun Raise<Error>.scanCard(): ScanResponse? {
var response: ScanResponse? = null
private suspend fun Raise<Error>.scanCard(scope: CoroutineScope) = suspendCancellableCoroutine { continuation ->
scope.launch {
scanCardProcessor.scan(
analyticsSource = AnalyticsParam.ScreensSources.Settings,
onWalletNotCreated = {
/* no-op */
},
disclaimerWillShow = {
continuation.resume(null)
router.pop()
},
onSuccess = {
continuation.resume(it)
},
onCancel = {
continuation.resume(null)
},
onFailure = { tangemError ->
val error = if (!tangemError.silent) {
val message = tangemError.messageResId
?.let(::resourceReference)
?: stringReference(tangemError.customMessage)
scanCardProcessor.scan(
analyticsSource = AnalyticsParam.ScreensSources.Settings,
onWalletNotCreated = {
/* no-op */
},
disclaimerWillShow = {
router.pop()
},
onSuccess = {
response = it
},
onFailure = { tangemError ->
val error = if (!tangemError.silent) {
val message = tangemError.messageResId
?.let(::resourceReference)
?: stringReference(tangemError.customMessage)
Error.Message(message)
} else {
Error.Silent
}
raise(error)
},
)
return response
Error.Message(message)
} else {
Error.Silent
}
continuation.resume(null)
raise(error)
},
)
}
}
sealed class Error {