Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS-7652 Return the card in the callback #365

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading