Skip to content

Commit

Permalink
IOS-4905 re-indent and fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Oct 23, 2023
1 parent 5b2671a commit 29bee07
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions TangemSdk/TangemSdk/Common/NFC/NFCReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ import UIKit
final class NFCReader: NSObject {
var viewEventsPublisher = CurrentValueSubject<CardReaderViewEvent, Never>(.none)
private(set) var tag = CurrentValueSubject<NFCTagType, TangemSdkError>(.none)

var isReady: Bool { isSessionReady }

/// Session paused indicator for pins UI
private(set) var isPaused = false
/// Current connected tag
private var connectedTag: NFCTag? = nil
private var isSilentRestartPolling: Bool = false
/// Active nfc session
private var readerSession: NFCTagReaderSession?

/// Session cancellation flag
@Published private var cancelled: Bool = false

/// Session invalidation flag
@Published private var invalidatedWithError: TangemSdkError? = nil

@Published private var isSessionReady: Bool = false

/// Session cancellation publisher. Transforms cancellation to error
Expand All @@ -50,35 +50,35 @@ final class NFCReader: NSObject {
}
/// Session restart polling publisher
private var restartPollingPublisher: CurrentValueSubject<Bool, Never> = .init(false)

/// Workaround for session timeout error (60 sec)
private var sessionTimerCancellable: AnyCancellable? = nil

/// Workaround for tag timeout connection error (20 sec)
private var tagTimerCancellable: AnyCancellable? = nil

/// Workaround for nfc stuck
private var nfcStuckTimerCancellable: AnyCancellable? = nil

// Idle timer
private var idleTimerCancellable: AnyCancellable? = nil

// Tag search timer. Sends tagLost event after timeout, if restartPolling called with silent mode
private var searchTimerCancellable: AnyCancellable? = nil

/// Keep alert message for restore after pause
private var _alertMessage: String? = nil

//Store session live subscriptions
private var bag = Set<AnyCancellable>()
private var sessionConnectCancellable: AnyCancellable? = nil

private var sendRetryCount = Constants.retryCount
private var startRetryCount = Constants.startRetryCount
private let pollingOption: NFCTagReaderSession.PollingOption
private var sessionDidBecomeActiveTimestamp: Date = .init()

/// Starting from iOS is no longer possible to invoke restart polling after 20 seconds from first connection on some devices
/// Starting from iOS 17 is no longer possible to invoke restart polling after 20 seconds from first connection on some devices
private lazy var shouldReduceRestartPolling: Bool = {
if #available(iOS 17, *), NFCUtils.isBrokenRestartPollingDevice {
return true
Expand All @@ -94,11 +94,11 @@ final class NFCReader: NSObject {
init(pollingOption: NFCTagReaderSession.PollingOption = [.iso14443]) {
self.pollingOption = pollingOption
}

deinit {
Log.debug("Reader deinit")
}

private var queue: DispatchQueue = .init(label: "tangem_sdk_reader_queue")
}

Expand All @@ -112,7 +112,7 @@ extension NFCReader: CardReader {
_alertMessage = newValue
}
}

/// Start session and try to connect with tag
func startSession(with message: String?) {
Log.nfc("Start NFC session")
Expand All @@ -122,16 +122,16 @@ extension NFCReader: CardReader {
invalidatedWithError = nil
cancelled = false
connectedTag = nil

let alertMessage = message ?? "view_delegate_scan_description".localized
_alertMessage = alertMessage

let isExistingSessionActive = readerSession?.isReady ?? false
if !isExistingSessionActive {
startNFCStuckTimer()
start()
}

NotificationCenter //For instant cancellation
.default
.publisher(for: UIApplication.didBecomeActiveNotification)
Expand All @@ -149,7 +149,7 @@ extension NFCReader: CardReader {
}
.weakAssign(to: \.cancelled, on: self)
.store(in: &bag)

$cancelled //speed up cancellation if no tag interaction
.receive(on: queue)
.dropFirst()
Expand All @@ -158,7 +158,7 @@ extension NFCReader: CardReader {
.map { _ in return TangemSdkError.userCancelled }
.weakAssign(to: \.invalidatedWithError, on: self)
.store(in: &bag)

$invalidatedWithError //speed up cancellation if no tag interaction
.receive(on: queue)
.dropFirst()
Expand All @@ -180,7 +180,7 @@ extension NFCReader: CardReader {
self.isSessionReady = false
}
.store(in: &bag)

$isSessionReady //Handle session state
.receive(on: queue)
.dropFirst()
Expand All @@ -195,13 +195,13 @@ extension NFCReader: CardReader {
} else { //clenup resources
self.stopTimers()
}

if !self.isPaused {
self.viewEventsPublisher.send(isReady ? .sessionStarted : .sessionStopped)
}
}
.store(in: &bag)

restartPollingPublisher //handle restart polling events
.receive(on: queue)
.dropFirst()
Expand All @@ -226,26 +226,26 @@ extension NFCReader: CardReader {
Log.nfc("Restart polling invoked")
self.tagDidDisconnect()
session.restartPolling()

if isSilent {
self.startSearchTimer()
}
}
.store(in: &bag)
}

func resumeSession() {
Log.nfc("Resume reader session invoked")
isPaused = false
startSession(with: _alertMessage)
}

func pauseSession(with errorMessage: String? = nil) {
Log.nfc("Pause reader session invoked")
isPaused = true
stopSession(with: errorMessage)
}

func stopSession(with errorMessage: String? = nil) {
Log.nfc("Stop reader session invoked")
stopTimers()
Expand All @@ -255,11 +255,11 @@ extension NFCReader: CardReader {
readerSession?.invalidate()
}
}

func restartPolling(silent: Bool) {
restartPollingPublisher.send(silent)
}

/// Send apdu command to connected tag
/// - Parameter apdu: serialized apdu
/// - Parameter completion: result with ResponseApdu or NFCError otherwise
Expand Down Expand Up @@ -335,15 +335,15 @@ extension NFCReader: CardReader {
}
.eraseToAnyPublisher()
}

private func start() {
firstConnectionTimestamp = nil
readerSession?.invalidate() //Important! We must keep invalidate/begin in balance after start retries
readerSession = NFCTagReaderSession(pollingOption: self.pollingOption, delegate: self, queue: queue)!
readerSession!.alertMessage = _alertMessage!
readerSession!.begin()
}

//MARK: Timers
private func startNFCStuckTimer() {
startRetryCount = Constants.startRetryCount
Expand All @@ -365,7 +365,7 @@ extension NFCReader: CardReader {
}
}
}

private func startTagTimer() {
tagTimerCancellable = Timer
.TimerPublisher(interval: Constants.tagTimeout, tolerance: 0, runLoop: RunLoop.main, mode: .common)
Expand All @@ -380,7 +380,7 @@ extension NFCReader: CardReader {
self.tagTimerCancellable = nil
}
}

private func startSessionTimer() {
sessionTimerCancellable = Timer
.TimerPublisher(interval: Constants.sessionTimeout, runLoop: RunLoop.main, mode: .common)
Expand All @@ -394,7 +394,7 @@ extension NFCReader: CardReader {
self.sessionTimerCancellable = nil
}
}

private func startIdleTimer() {
idleTimerCancellable = Timer
.TimerPublisher(interval: Constants.idleTimeout, runLoop: RunLoop.main, mode: .common)
Expand All @@ -408,7 +408,7 @@ extension NFCReader: CardReader {
self.idleTimerCancellable = nil
}
}

private func startSearchTimer() {
searchTimerCancellable = Timer
.TimerPublisher(interval: Constants.searchTagTimeout, tolerance: 0, runLoop: RunLoop.main, mode: .common)
Expand All @@ -424,7 +424,7 @@ extension NFCReader: CardReader {
self.searchTimerCancellable = nil
}
}

private func stopTimers() {
nfcStuckTimerCancellable = nil
sessionTimerCancellable = nil
Expand Down Expand Up @@ -473,14 +473,14 @@ extension NFCReader: NFCTagReaderSessionDelegate {
sessionDidBecomeActiveTimestamp = Date()
isSessionReady = true
}

func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
Log.nfc("NFC Session did invalidate with: \(error.localizedDescription)")
if nfcStuckTimerCancellable == nil { //handle stuck retries ios14
invalidatedWithError = TangemSdkError.parse(error as! NFCReaderError)
}
}

func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
Log.nfc("NFC tag detected: \(tags)")

Expand All @@ -489,7 +489,7 @@ extension NFCReader: NFCTagReaderSessionDelegate {
}

let nfcTag = tags.first!

sessionConnectCancellable = session.connectPublisher(tag: nfcTag)
.receive(on: queue)
.sink {[weak self] completion in
Expand Down

0 comments on commit 29bee07

Please sign in to comment.