-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from tangem/IOS-2463_add_reset_to_demo
IOS-2463 Add reset to factory to demo
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Example/TangemSdkExample/Developer/ResetToFactorySettingsTask.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// ResetToFactorySettingsTask.swift | ||
// Tangem | ||
// | ||
// Created by Alexander Osokin on 22.11.2021. | ||
// Copyright © 2021 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import TangemSdk | ||
|
||
class ResetToFactorySettingsTask: CardSessionRunnable { | ||
func run(in session: CardSession, completion: @escaping CompletionResult<Card>) { | ||
deteleWallets(in: session, completion: completion) | ||
} | ||
|
||
private func deteleWallets(in session: CardSession, completion: @escaping CompletionResult<Card>) { | ||
guard let wallet = session.environment.card?.wallets.last else { | ||
resetBackup(in: session, completion: completion) | ||
return | ||
} | ||
|
||
PurgeWalletCommand(publicKey: wallet.publicKey).run(in: session) { result in | ||
switch result { | ||
case .success: | ||
self.deteleWallets(in: session, completion: completion) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
private func resetBackup(in session: CardSession, completion: @escaping CompletionResult<Card>) { | ||
guard let backupStatus = session.environment.card?.backupStatus, | ||
backupStatus != .noBackup else { | ||
completion(.success(session.environment.card!)) | ||
return | ||
} | ||
|
||
ResetBackupCommand().run(in: session) { result in | ||
switch result { | ||
case .success: | ||
completion(.success(session.environment.card!)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} |