Skip to content

Commit

Permalink
IOS-7652 Return the card in the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Aug 24, 2024
1 parent 22272c7 commit a587e97
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
12 changes: 7 additions & 5 deletions TangemSdk/TangemSdk/Operations/Backup/BackupService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class BackupService: ObservableObject {
updateState()
}

public func addBackupCard(completion: @escaping CompletionResult<Void>) {
public func addBackupCard(completion: @escaping CompletionResult<Card>) {
guard let primaryCard = repo.data.primaryCard else {
completion(.failure(.missingPrimaryCard))
return
Expand Down Expand Up @@ -226,11 +226,13 @@ public class BackupService: ObservableObject {
return currentState
}

private func addBackupCard(_ backupCard: BackupCard, completion: @escaping CompletionResult<Void>) {
private func addBackupCard(_ backupCardResponse: StartBackupCardLinkingTaskResponse, completion: @escaping CompletionResult<Card>) {
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
Expand All @@ -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<Void>) {
private func readBackupCard(_ primaryCard: PrimaryCard, completion: @escaping CompletionResult<Card>) {
let command = StartBackupCardLinkingTask(primaryCard: primaryCard,
addedBackupCards: repo.data.backupCards.map { $0.cardId },
skipCompatibilityChecks: skipCompatibilityChecks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -27,7 +37,7 @@ final class StartBackupCardLinkingTask: CardSessionRunnable {
Log.debug("StartBackupCardLinkingTask deinit")
}

func run(in session: CardSession, completion: @escaping CompletionResult<BackupCard>) {
func run(in session: CardSession, completion: @escaping CompletionResult<StartBackupCardLinkingTaskResponse>) {
if session.environment.config.handleErrors {
guard let card = session.environment.card else {
completion(.failure(.missingPreflightRead))
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a587e97

Please sign in to comment.