From 9cb0994177738e3035e20338f089450ee2efb4b8 Mon Sep 17 00:00:00 2001 From: Alexander Osokin Date: Thu, 5 Sep 2024 19:19:23 +0100 Subject: [PATCH] IOS-7867 Remove Published from services --- Example/TangemSdkExample/AppModel.swift | 8 ++++---- .../TangemSdkExample/Developer/BackupView.swift | 7 ++----- .../Developer/ResetPinView.swift | 7 ++----- .../Operations/Backup/BackupService.swift | 4 ++-- .../ResetCode/ResetCodesController.swift | 2 +- .../Operations/ResetCode/ResetPinService.swift | 16 ++++++++++------ 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/Example/TangemSdkExample/AppModel.swift b/Example/TangemSdkExample/AppModel.swift index 23ba2a4e3..2b56eb0ed 100644 --- a/Example/TangemSdkExample/AppModel.swift +++ b/Example/TangemSdkExample/AppModel.swift @@ -816,18 +816,18 @@ extension AppModel { @ViewBuilder func makeBackupDestination() -> some View { if let service = self.backupService { - BackupView().environmentObject(service) + BackupView(backupService: service) } else { - BackupView() + EmptyView() } } @ViewBuilder func makePinResetDestination() -> some View { if let service = self.resetPinService { - ResetPinView().environmentObject(service) + ResetPinView(resetPinService: service) } else { - ResetPinView() + EmptyView() } } } diff --git a/Example/TangemSdkExample/Developer/BackupView.swift b/Example/TangemSdkExample/Developer/BackupView.swift index a34b91d75..cb0bb494c 100644 --- a/Example/TangemSdkExample/Developer/BackupView.swift +++ b/Example/TangemSdkExample/Developer/BackupView.swift @@ -10,7 +10,7 @@ import SwiftUI import TangemSdk struct BackupView: View { - @EnvironmentObject var backupService: BackupService + var backupService: BackupService @State private var count: Int = 2 @State private var accessCode: String = "" @@ -158,10 +158,7 @@ struct BackupView: View { } struct BackupView_Previews: PreviewProvider { - static let sdk = TangemSdk() - static var previews: some View { - BackupView() - .environmentObject(BackupService(sdk: sdk)) + BackupView(backupService: BackupService(sdk: TangemSdk())) } } diff --git a/Example/TangemSdkExample/Developer/ResetPinView.swift b/Example/TangemSdkExample/Developer/ResetPinView.swift index 0941c0d52..240b5111a 100644 --- a/Example/TangemSdkExample/Developer/ResetPinView.swift +++ b/Example/TangemSdkExample/Developer/ResetPinView.swift @@ -10,7 +10,7 @@ import SwiftUI import TangemSdk struct ResetPinView: View { - @EnvironmentObject var resetPinService: ResetPinService + var resetPinService: ResetPinService private var stateTitle: String { "Current state is: \(resetPinService.currentState)" } @State private var accessCode: String = "" @@ -80,10 +80,7 @@ struct ResetPinView: View { } struct ResetPinView_Previews: PreviewProvider { - static let sdk = TangemSdk() - static var previews: some View { - ResetPinView() - .environmentObject(ResetPinService(config: Config())) + ResetPinView(resetPinService: ResetPinService(config: Config())) } } diff --git a/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift b/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift index 8ee24d336..fad4885b0 100644 --- a/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift +++ b/TangemSdk/TangemSdk/Operations/Backup/BackupService.swift @@ -10,10 +10,10 @@ import Foundation import Combine @available(iOS 13.0, *) -public class BackupService: ObservableObject { +public class BackupService { public static let maxBackupCardsCount = 2 - @Published public private(set) var currentState: State = .preparing + public private(set) var currentState: State = .preparing public var canAddBackupCards: Bool { addedBackupCardsCount < BackupService.maxBackupCardsCount diff --git a/TangemSdk/TangemSdk/Operations/ResetCode/ResetCodesController.swift b/TangemSdk/TangemSdk/Operations/ResetCode/ResetCodesController.swift index 19d38ae19..eeebee3a7 100644 --- a/TangemSdk/TangemSdk/Operations/ResetCode/ResetCodesController.swift +++ b/TangemSdk/TangemSdk/Operations/ResetCode/ResetCodesController.swift @@ -45,7 +45,7 @@ public class ResetCodesController { private func bind() { self.resetService - .$currentState + .currentStatePublisher .dropFirst() .sink {[weak self] newState in guard let self else { return } diff --git a/TangemSdk/TangemSdk/Operations/ResetCode/ResetPinService.swift b/TangemSdk/TangemSdk/Operations/ResetCode/ResetPinService.swift index 3de358e7e..ade59bbf0 100644 --- a/TangemSdk/TangemSdk/Operations/ResetCode/ResetPinService.swift +++ b/TangemSdk/TangemSdk/Operations/ResetCode/ResetPinService.swift @@ -7,16 +7,20 @@ // import Foundation +import Combine @available(iOS 13.0, *) -public class ResetPinService: ObservableObject { - @Published public private(set) var currentState: State = .needCode - @Published public private(set) var error: TangemSdkError? = nil +public class ResetPinService { + public var currentState: State { _currentState.value } + public var currentStatePublisher: AnyPublisher { _currentState.eraseToAnyPublisher() } + + public private(set) var error: TangemSdkError? = nil private var session: CardSession? private let config: Config private var repo: ResetPinRepo = .init() private var currentCommand: AnyObject? = nil + private var _currentState: CurrentValueSubject = .init(.needCode) public init(config: Config) { self.config = config @@ -47,7 +51,7 @@ public class ResetPinService: ObservableObject { repo.accessCode = code.sha256() if currentState == .needCode { - currentState = currentState.next() + _currentState.value = currentState.next() } } @@ -72,7 +76,7 @@ public class ResetPinService: ObservableObject { repo.passcode = code.sha256() if currentState == .needCode { - currentState = currentState.next() + _currentState.value = currentState.next() } } @@ -92,7 +96,7 @@ public class ResetPinService: ObservableObject { private func handleCompletion(_ result: Result) -> Void { switch result { case .success: - currentState = currentState.next() + _currentState.value = currentState.next() case .failure(let error): self.error = error }