diff --git a/Shared/Services/IdentitiesService.swift b/Shared/Services/IdentitiesService.swift index 4bb39b5..aacc25a 100644 --- a/Shared/Services/IdentitiesService.swift +++ b/Shared/Services/IdentitiesService.swift @@ -3,8 +3,8 @@ import Foundation import Combine -class IdentitiesService { - @Published var mostRecentlyUsedIdentityID: UUID? +struct IdentitiesService { + let mostRecentlyUsedIdentityID: AnyPublisher private let identityDatabase: IdentityDatabase private let environment: AppEnvironment @@ -13,9 +13,9 @@ class IdentitiesService { self.identityDatabase = identityDatabase self.environment = environment - identityDatabase.mostRecentlyUsedIdentityIDObservation() + mostRecentlyUsedIdentityID = identityDatabase.mostRecentlyUsedIdentityIDObservation() .replaceError(with: nil) - .assign(to: &$mostRecentlyUsedIdentityID) + .eraseToAnyPublisher() } } @@ -65,19 +65,16 @@ extension IdentitiesService { } .flatMap(networkClient.request) .tryMap { _ in try secretsService.deleteAllItems() } - .print() .eraseToAnyPublisher() } func updatePushSubscriptions(deviceToken: String) -> AnyPublisher { identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken) - .tryMap { [weak self] identities -> [AnyPublisher] in - guard let self = self else { return [Empty().eraseToAnyPublisher()] } - - return try identities.map { - try self.identityService(id: $0.id) + .tryMap { identities -> [AnyPublisher] in + try identities.map { + try identityService(id: $0.id) .createPushSubscription(deviceToken: deviceToken, alerts: $0.pushSubscriptionAlerts) - .catch { _ in Empty() } // don't want to disrupt pipeline, consider future telemetry + .catch { _ in Empty() } // don't want to disrupt pipeline .eraseToAnyPublisher() } } diff --git a/Shared/View Models/RootViewModel.swift b/Shared/View Models/RootViewModel.swift index 0376c0f..229d9cd 100644 --- a/Shared/View Models/RootViewModel.swift +++ b/Shared/View Models/RootViewModel.swift @@ -5,6 +5,7 @@ import Combine class RootViewModel: ObservableObject { @Published private(set) var mainNavigationViewModel: MainNavigationViewModel? + @Published private var mostRecentlyUsedIdentityID: UUID? // swiftlint:disable weak_delegate private let appDelegate: AppDelegate @@ -20,7 +21,9 @@ class RootViewModel: ObservableObject { self.identitiesService = identitiesService self.userNotificationService = userNotificationService - newIdentitySelected(id: identitiesService.mostRecentlyUsedIdentityID) + identitiesService.mostRecentlyUsedIdentityID.assign(to: &$mostRecentlyUsedIdentityID) + + newIdentitySelected(id: mostRecentlyUsedIdentityID) userNotificationService.isAuthorized() .filter { $0 } @@ -50,7 +53,7 @@ extension RootViewModel { identityService.observationErrors .receive(on: RunLoop.main) - .map { [weak self] _ in self?.identitiesService.mostRecentlyUsedIdentityID } + .map { [weak self] _ in self?.mostRecentlyUsedIdentityID } .sink { [weak self] in self?.newIdentitySelected(id: $0) } .store(in: &cancellables)