From d48b1db31bd90d1525e5e0ce993fa85f96edd6ab Mon Sep 17 00:00:00 2001 From: Vlad Kozarez Date: Tue, 3 Sep 2024 17:57:28 +0300 Subject: [PATCH] AND-8164 return Card dto on addBackupCard --- .../tangem/operations/backup/BackupService.kt | 12 +++++++---- .../backup/StartBackupCardLinkingTask.kt | 20 ++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/tangem-sdk-core/src/main/java/com/tangem/operations/backup/BackupService.kt b/tangem-sdk-core/src/main/java/com/tangem/operations/backup/BackupService.kt index 7b8fbc90..0d0162a6 100644 --- a/tangem-sdk-core/src/main/java/com/tangem/operations/backup/BackupService.kt +++ b/tangem-sdk-core/src/main/java/com/tangem/operations/backup/BackupService.kt @@ -91,7 +91,7 @@ class BackupService( updateState() } - fun addBackupCard(callback: CompletionCallback) { + fun addBackupCard(callback: CompletionCallback) { val primaryCard = repo.data.primaryCard.guard { callback(CompletionResult.Failure(TangemSdkError.MissingPrimaryCard())) return @@ -259,8 +259,12 @@ class BackupService( return currentState } - private fun addBackupCard(backupCard: BackupCard, callback: CompletionCallback) { + private fun addBackupCard( + backupCardResponse: StartBackupCardLinkingTaskResponse, + callback: CompletionCallback, + ) { backupScope.launch { + val backupCard = backupCardResponse.backupCard fetchCertificate( cardId = backupCard.cardId, cardPublicKey = backupCard.cardPublicKey, @@ -273,12 +277,12 @@ class BackupService( .plus(updatedBackupCard) repo.data = repo.data.copy(backupCards = updatedList) updateState() - callback(CompletionResult.Success(Unit)) + callback(CompletionResult.Success(backupCardResponse.card)) } } } - private fun readBackupCard(primaryCard: PrimaryCard, callback: CompletionCallback) { + private fun readBackupCard(primaryCard: PrimaryCard, callback: CompletionCallback) { sdk.startSessionWithRunnable( runnable = StartBackupCardLinkingTask( primaryCard = primaryCard, diff --git a/tangem-sdk-core/src/main/java/com/tangem/operations/backup/StartBackupCardLinkingTask.kt b/tangem-sdk-core/src/main/java/com/tangem/operations/backup/StartBackupCardLinkingTask.kt index d3c548a9..7d2de9fa 100644 --- a/tangem-sdk-core/src/main/java/com/tangem/operations/backup/StartBackupCardLinkingTask.kt +++ b/tangem-sdk-core/src/main/java/com/tangem/operations/backup/StartBackupCardLinkingTask.kt @@ -1,23 +1,31 @@ package com.tangem.operations.backup +import com.squareup.moshi.JsonClass import com.tangem.common.CompletionResult +import com.tangem.common.card.Card import com.tangem.common.core.CardSession import com.tangem.common.core.CardSessionRunnable import com.tangem.common.core.CompletionCallback import com.tangem.common.core.TangemSdkError import com.tangem.common.extensions.guard +@JsonClass(generateAdapter = true) +data class StartBackupCardLinkingTaskResponse( + val backupCard: BackupCard, + val card: Card, +) + class StartBackupCardLinkingTask( private val primaryCard: PrimaryCard, private val addedBackupCards: List, private val skipCompatibilityChecks: Boolean = false, -) : CardSessionRunnable { +) : CardSessionRunnable { override val allowsRequestAccessCodeFromRepository: Boolean get() = false @Suppress("CyclomaticComplexMethod") - override fun run(session: CardSession, callback: CompletionCallback) { + override fun run(session: CardSession, callback: CompletionCallback) { val card = session.environment.card.guard { callback(CompletionResult.Failure(TangemSdkError.MissingPreflightRead())) return @@ -79,7 +87,13 @@ class StartBackupCardLinkingTask( .run(session) { result -> when (result) { is CompletionResult.Success -> { - callback(CompletionResult.Success(result.data)) + session.environment.card?.let { updatedCard -> + val response = StartBackupCardLinkingTaskResponse( + backupCard = result.data, + card = updatedCard, + ) + callback(CompletionResult.Success(response)) + } ?: callback(CompletionResult.Failure(TangemSdkError.MissingPreflightRead())) } is CompletionResult.Failure -> callback(CompletionResult.Failure(result.error))