Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-26 01:40:54 -07:00
parent e74daa4df0
commit 244c190f2a
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 13 additions and 13 deletions

View file

@ -3,8 +3,8 @@
import Foundation import Foundation
import Combine import Combine
class IdentitiesService { struct IdentitiesService {
@Published var mostRecentlyUsedIdentityID: UUID? let mostRecentlyUsedIdentityID: AnyPublisher<UUID?, Never>
private let identityDatabase: IdentityDatabase private let identityDatabase: IdentityDatabase
private let environment: AppEnvironment private let environment: AppEnvironment
@ -13,9 +13,9 @@ class IdentitiesService {
self.identityDatabase = identityDatabase self.identityDatabase = identityDatabase
self.environment = environment self.environment = environment
identityDatabase.mostRecentlyUsedIdentityIDObservation() mostRecentlyUsedIdentityID = identityDatabase.mostRecentlyUsedIdentityIDObservation()
.replaceError(with: nil) .replaceError(with: nil)
.assign(to: &$mostRecentlyUsedIdentityID) .eraseToAnyPublisher()
} }
} }
@ -65,19 +65,16 @@ extension IdentitiesService {
} }
.flatMap(networkClient.request) .flatMap(networkClient.request)
.tryMap { _ in try secretsService.deleteAllItems() } .tryMap { _ in try secretsService.deleteAllItems() }
.print()
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func updatePushSubscriptions(deviceToken: String) -> AnyPublisher<Void, Error> { func updatePushSubscriptions(deviceToken: String) -> AnyPublisher<Void, Error> {
identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken) identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken)
.tryMap { [weak self] identities -> [AnyPublisher<Void, Never>] in .tryMap { identities -> [AnyPublisher<Void, Never>] in
guard let self = self else { return [Empty().eraseToAnyPublisher()] } try identities.map {
try identityService(id: $0.id)
return try identities.map {
try self.identityService(id: $0.id)
.createPushSubscription(deviceToken: deviceToken, alerts: $0.pushSubscriptionAlerts) .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() .eraseToAnyPublisher()
} }
} }

View file

@ -5,6 +5,7 @@ import Combine
class RootViewModel: ObservableObject { class RootViewModel: ObservableObject {
@Published private(set) var mainNavigationViewModel: MainNavigationViewModel? @Published private(set) var mainNavigationViewModel: MainNavigationViewModel?
@Published private var mostRecentlyUsedIdentityID: UUID?
// swiftlint:disable weak_delegate // swiftlint:disable weak_delegate
private let appDelegate: AppDelegate private let appDelegate: AppDelegate
@ -20,7 +21,9 @@ class RootViewModel: ObservableObject {
self.identitiesService = identitiesService self.identitiesService = identitiesService
self.userNotificationService = userNotificationService self.userNotificationService = userNotificationService
newIdentitySelected(id: identitiesService.mostRecentlyUsedIdentityID) identitiesService.mostRecentlyUsedIdentityID.assign(to: &$mostRecentlyUsedIdentityID)
newIdentitySelected(id: mostRecentlyUsedIdentityID)
userNotificationService.isAuthorized() userNotificationService.isAuthorized()
.filter { $0 } .filter { $0 }
@ -50,7 +53,7 @@ extension RootViewModel {
identityService.observationErrors identityService.observationErrors
.receive(on: RunLoop.main) .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) } .sink { [weak self] in self?.newIdentitySelected(id: $0) }
.store(in: &cancellables) .store(in: &cancellables)