-
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 #329 from tangem/IOS-4025_refactor_prefligh_filtering
- Loading branch information
Showing
11 changed files
with
240 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// SessionFilter.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 09.11.2023. | ||
// Copyright © 2023 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@available(iOS 13.0, *) | ||
public enum SessionFilter { | ||
case cardId(String) | ||
case custom(PreflightReadFilter) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension SessionFilter { | ||
var preflightReadFilter: PreflightReadFilter { | ||
switch self { | ||
case .cardId(let cardId): | ||
return CardIdPreflightReadFilter(cardId: cardId) | ||
case .custom(let filter): | ||
return filter | ||
} | ||
} | ||
|
||
init?(from cardId: String?) { | ||
guard let cardId else { | ||
return nil | ||
} | ||
|
||
self = .cardId(cardId) | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
TangemSdk/TangemSdk/Operations/PreflightReadFilter/CardIdPreflightReadFilter.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,30 @@ | ||
// | ||
// CardIdPreflightReadFilter.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 09.11.2023. | ||
// Copyright © 2023 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@available(iOS 13.0, *) | ||
struct CardIdPreflightReadFilter: PreflightReadFilter { | ||
private let expectedCardId: String | ||
|
||
init(cardId: String) { | ||
expectedCardId = cardId | ||
} | ||
|
||
func onCardRead(_ card: Card, environment: SessionEnvironment) throws { | ||
if expectedCardId.caseInsensitiveCompare(card.cardId) == .orderedSame { | ||
return | ||
} | ||
|
||
let formatter = CardIdFormatter(style: environment.config.cardIdDisplayFormat) | ||
let expectedCardIdFormatted = formatter.string(from: expectedCardId) | ||
throw TangemSdkError.wrongCardNumber(expectedCardId: expectedCardIdFormatted) | ||
} | ||
|
||
func onFullCardRead(_ card: Card, environment: SessionEnvironment) throws {} | ||
} |
25 changes: 25 additions & 0 deletions
25
TangemSdk/TangemSdk/Operations/PreflightReadFilter/PreflightReadFilter.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,25 @@ | ||
// | ||
// PreflightReadFilter.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 30.10.2023. | ||
// Copyright © 2023 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@available(iOS 13.0, *) | ||
/// Use this filter to filter out cards on preflight read stage. If preflight mode is set to `readCardOnly` or `fullCardRead`. `HandleErrors` flag must be switched on. | ||
public protocol PreflightReadFilter { | ||
/// This method calls right after public information is read. User code is not required. If preflight mode is set to `readCardOnly` or `fullCardRead` | ||
/// - Parameter card: The card that was read | ||
/// - Parameter environment: Current environment | ||
/// - Throws: Throw an error with a localized message to the user, if the card should not be worked with. | ||
func onCardRead(_ card: Card, environment: SessionEnvironment) throws | ||
|
||
/// This method calls right after full card information is read. User code is required. If preflight mode is set to `fullCardRead` | ||
/// - Parameter card: The card that was read | ||
/// - Parameter environment: Current environment | ||
/// - Throws: Throw an error with a localized message to the user, if the card should not be worked with. | ||
func onFullCardRead(_ card: Card, environment: SessionEnvironment) throws | ||
} |
Oops, something went wrong.