From 1b70a89edadc61ff6b6b7e18f0c09d788ef425a6 Mon Sep 17 00:00:00 2001 From: Alexander Osokin <4981841+tureck1y@users.noreply.github.com> Date: Tue, 27 Aug 2024 10:38:18 +0100 Subject: [PATCH] IOS-7652 Return the card in the callback (#365) --- .../Operations/Backup/BackupService.swift | 12 +++++---- .../Backup/StartBackupCardLinkingTask.swift | 26 +++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift b/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift index 01e7fdea..8ee24d33 100644 --- a/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift +++ b/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift @@ -64,7 +64,7 @@ public class BackupService: ObservableObject { updateState() } - public func addBackupCard(completion: @escaping CompletionResult) { + public func addBackupCard(completion: @escaping CompletionResult) { guard let primaryCard = repo.data.primaryCard else { completion(.failure(.missingPrimaryCard)) return @@ -226,11 +226,13 @@ public class BackupService: ObservableObject { return currentState } - private func addBackupCard(_ backupCard: BackupCard, completion: @escaping CompletionResult) { + private func addBackupCard(_ backupCardResponse: StartBackupCardLinkingTaskResponse, completion: @escaping CompletionResult) { + let backupCard = backupCardResponse.backupCard + if let existingIndex = repo.data.backupCards.firstIndex(where: { $0.cardId == backupCard.cardId }) { repo.data.backupCards.remove(at: existingIndex) } - + fetchCertificate(for: backupCard.cardId, cardPublicKey: backupCard.cardPublicKey, firmwareVersion: backupCard.firmwareVersion) { [weak self] result in @@ -242,14 +244,14 @@ public class BackupService: ObservableObject { backupCard.certificate = certificate self.repo.data.backupCards.append(backupCard) self.updateState() - completion(.success(())) + completion(.success(backupCardResponse.card)) case .failure(let error): completion(.failure(error)) } } } - private func readBackupCard(_ primaryCard: PrimaryCard, completion: @escaping CompletionResult) { + private func readBackupCard(_ primaryCard: PrimaryCard, completion: @escaping CompletionResult) { let command = StartBackupCardLinkingTask(primaryCard: primaryCard, addedBackupCards: repo.data.backupCards.map { $0.cardId }, skipCompatibilityChecks: skipCompatibilityChecks) diff --git a/TangemSdk/TangemSdk/Operations/Backup/StartBackupCardLinkingTask.swift b/TangemSdk/TangemSdk/Operations/Backup/StartBackupCardLinkingTask.swift index c9e84224..65f40486 100644 --- a/TangemSdk/TangemSdk/Operations/Backup/StartBackupCardLinkingTask.swift +++ b/TangemSdk/TangemSdk/Operations/Backup/StartBackupCardLinkingTask.swift @@ -8,6 +8,16 @@ import Foundation +/// Response from the Tangem card after `StartBackupCardLinkingTask +@available(iOS 13.0, *) +struct StartBackupCardLinkingTaskResponse: JSONStringConvertible { + /// Backup data frrom the card + let backupCard: BackupCard + + /// Card being added + let card: Card +} + @available(iOS 13.0, *) final class StartBackupCardLinkingTask: CardSessionRunnable { var shouldAskForAccessCode: Bool { false } @@ -27,7 +37,7 @@ final class StartBackupCardLinkingTask: CardSessionRunnable { Log.debug("StartBackupCardLinkingTask deinit") } - func run(in session: CardSession, completion: @escaping CompletionResult) { + func run(in session: CardSession, completion: @escaping CompletionResult) { if session.environment.config.handleErrors { guard let card = session.environment.card else { completion(.failure(.missingPreflightRead)) @@ -86,7 +96,19 @@ final class StartBackupCardLinkingTask: CardSessionRunnable { } linkingCommand = StartBackupCardLinkingCommand(primaryCardLinkingKey: primaryCard.linkingKey) - linkingCommand!.run(in: session, completion: completion) + linkingCommand!.run(in: session) { result in + switch result { + case .success(let backupCard): + guard let card = session.environment.card else { + completion(.failure(.missingPreflightRead)) + return + } + + completion(.success(.init(backupCard: backupCard, card: card))) + case .failure(let error): + completion(.failure(error)) + } + } } private func isBatchIdCompatible(_ batchId: String) -> Bool {