-
Notifications
You must be signed in to change notification settings - Fork 36
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 #3134 from tangem/IOS-5193_refactor_scanner
IOS-5193 Refactor scanning
- Loading branch information
Showing
20 changed files
with
262 additions
and
65 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
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
15 changes: 15 additions & 0 deletions
15
Tangem/Common/TangemSdk/CardInitializer/CardInitializer.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,15 @@ | ||
// | ||
// CardInitializer.swift | ||
// Tangem | ||
// | ||
// Created by Alexander Osokin on 11.04.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import TangemSdk | ||
|
||
protocol CardInitializer { | ||
var shouldReset: Bool { get set } | ||
func initializeCard(mnemonic: Mnemonic?, passphrase: String?, completion: @escaping (Result<CardInfo, TangemSdkError>) -> Void) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// CardScanner.swift | ||
// Tangem | ||
// | ||
// Created by Alexander Osokin on 11.04.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import TangemSdk | ||
|
||
protocol CardScanner { | ||
init( | ||
tangemSdk: TangemSdk, | ||
parameters: CardScannerParameters | ||
) | ||
|
||
func scanCard(completion: @escaping (Result<AppScanTaskResponse, TangemSdkError>) -> Void) | ||
func scanCardPublisher() -> AnyPublisher<AppScanTaskResponse, TangemSdkError> | ||
} |
16 changes: 16 additions & 0 deletions
16
Tangem/Common/TangemSdk/CardScanner/CardScannerParameters.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,16 @@ | ||
// | ||
// CardScannerParameters.swift | ||
// Tangem | ||
// | ||
// Created by Alexander Osokin on 12.04.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import TangemSdk | ||
|
||
struct CardScannerParameters { | ||
let shouldAskForAccessCodes: Bool | ||
let performDerivations: Bool | ||
let sessionFilter: SessionFilter? | ||
} |
62 changes: 62 additions & 0 deletions
62
Tangem/Common/TangemSdk/CardScanner/CommonCardScanner.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,62 @@ | ||
// | ||
// CommonCardScanner.swift | ||
// Tangem | ||
// | ||
// Created by Alexander Osokin on 11.04.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import CombineExt | ||
import TangemSdk | ||
|
||
class CommonCardScanner: CardScanner { | ||
private let tangemSdk: TangemSdk | ||
private let parameters: CardScannerParameters | ||
private var cancellable: AnyCancellable? | ||
|
||
required init( | ||
tangemSdk: TangemSdk, | ||
parameters: CardScannerParameters | ||
) { | ||
self.tangemSdk = tangemSdk | ||
self.parameters = parameters | ||
} | ||
|
||
func scanCardPublisher() -> AnyPublisher<AppScanTaskResponse, TangemSdkError> { | ||
let task = AppScanTask( | ||
shouldAskForAccessCode: parameters.shouldAskForAccessCodes, | ||
performDerivations: parameters.performDerivations | ||
) | ||
|
||
let didBecomeActivePublisher = NotificationCenter.didBecomeActivePublisher | ||
.mapError { $0.toTangemSdkError() } | ||
.mapToVoid() | ||
.first() | ||
|
||
return tangemSdk.startSessionPublisher(with: task, filter: parameters.sessionFilter) | ||
.combineLatest(didBecomeActivePublisher) | ||
.map { $0.0 } | ||
.handleEvents(receiveCompletion: { _ in | ||
withExtendedLifetime(task) {} | ||
}) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func scanCard(completion: @escaping (Result<AppScanTaskResponse, TangemSdkError>) -> Void) { | ||
cancellable = scanCardPublisher() | ||
.sink(receiveCompletion: { [weak self] completionResult in | ||
switch completionResult { | ||
case .finished: | ||
break | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
|
||
self?.cancellable = nil | ||
}, receiveValue: { | ||
completion(.success($0)) | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.