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 Combine
class IdentitiesService {
@Published var mostRecentlyUsedIdentityID: UUID?
struct IdentitiesService {
let mostRecentlyUsedIdentityID: AnyPublisher<UUID?, Never>
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<Void, Error> {
identityDatabase.identitiesWithOutdatedDeviceTokens(deviceToken: deviceToken)
.tryMap { [weak self] identities -> [AnyPublisher<Void, Never>] in
guard let self = self else { return [Empty().eraseToAnyPublisher()] }
return try identities.map {
try self.identityService(id: $0.id)
.tryMap { identities -> [AnyPublisher<Void, Never>] 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()
}
}

View File

@ -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)