Skip to content

Commit

Permalink
IOS-8947 Reset env (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y authored Jan 24, 2025
1 parent c786fca commit 0371448
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions TangemSdk/TangemSdk/Common/Core/CardSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CardSession {
}

// initial environment to be able to reset a current one
private let _environment: SessionEnvironment
private var _environment: SessionEnvironment
public internal(set) var environment: SessionEnvironment

private let reader: CardReader
Expand Down Expand Up @@ -350,6 +350,7 @@ public class CardSession {

private func sessionDidStop(completion: (() -> Void)?) {
nfcReaderSubscriptions = []
resetEnvironment()
preflightReadMode = .fullCardRead
sendSubscription = []
viewDelegate.sessionStopped(completion: completion)
Expand Down Expand Up @@ -422,7 +423,7 @@ public class CardSession {
case .preflightFiltered:
self.viewDelegate.wrongCard(message: error.localizedDescription)
// We have to return environment to initial state to reset all the changes
self.environment = self._environment
self.resetEnvironment()
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
guard self.reader.isReady else {
onSessionStarted(self, .userCancelled)
Expand Down Expand Up @@ -584,6 +585,11 @@ public class CardSession {
resetCodesController!.cardIdDisplayFormat = config.cardIdDisplayFormat
resetCodesController!.start(codeType: type, cardId: cardId, completion: completion)
}

private func resetEnvironment() {
_environment.resetCodes()
environment = _environment
}
}
//MARK: - JSON RPC
extension CardSession {
Expand All @@ -596,7 +602,10 @@ extension CardSession {
do {
request = try JSONRPCRequest(jsonString: jsonRequest)
let runnable = try jsonConverter.convert(request: request)
runnable.run(in: self) { completion($0.toJsonResponse(id: request.id).json) }
runnable.run(in: self) {
completion($0.toJsonResponse(id: request.id).json)
self.resetEnvironment()
}
} catch {
completion(error.toJsonResponse(id: request?.id).json)
}
Expand Down
5 changes: 5 additions & 0 deletions TangemSdk/TangemSdk/Common/Core/SessionEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public struct SessionEnvironment {
return passcode.value != type.defaultValue.sha256()
}
}

mutating func resetCodes() {
accessCode = .init(.accessCode)
passcode = .init(.passcode)
}
}

0 comments on commit 0371448

Please sign in to comment.