metatext/ViewModels/Sources/ViewModels/RootViewModel.swift

83 lines
3 KiB
Swift
Raw Normal View History

2020-08-03 15:20:51 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-09-05 02:31:43 +00:00
import Foundation
2020-08-31 18:57:02 +00:00
import ServiceLayer
2020-08-03 15:20:51 +00:00
2020-09-01 07:33:49 +00:00
public final class RootViewModel: ObservableObject {
2020-09-08 02:12:38 +00:00
@Published public private(set) var identification: Identification?
2020-08-09 05:37:04 +00:00
2020-09-01 07:33:49 +00:00
@Published private var mostRecentlyUsedIdentityID: UUID?
2020-08-26 21:35:06 +00:00
private let allIdentitiesService: AllIdentitiesService
private let userNotificationService: UserNotificationService
2020-09-06 21:37:54 +00:00
private let registerForRemoteNotifications: () -> AnyPublisher<Data, Error>
2020-08-03 15:20:51 +00:00
private var cancellables = Set<AnyCancellable>()
2020-09-01 07:33:49 +00:00
public init(environment: AppEnvironment,
2020-09-06 21:37:54 +00:00
registerForRemoteNotifications: @escaping () -> AnyPublisher<Data, Error>) throws {
2020-09-01 07:33:49 +00:00
allIdentitiesService = try AllIdentitiesService(environment: environment)
userNotificationService = UserNotificationService(environment: environment)
self.registerForRemoteNotifications = registerForRemoteNotifications
2020-08-09 05:37:04 +00:00
2020-08-26 21:35:06 +00:00
allIdentitiesService.mostRecentlyUsedIdentityID.assign(to: &$mostRecentlyUsedIdentityID)
2020-08-26 08:40:54 +00:00
newIdentitySelected(id: mostRecentlyUsedIdentityID)
2020-08-12 07:24:39 +00:00
userNotificationService.isAuthorized()
2020-08-12 07:24:39 +00:00
.filter { $0 }
2020-09-01 07:33:49 +00:00
.zip(registerForRemoteNotifications())
2020-08-12 07:24:39 +00:00
.map { $1 }
2020-08-26 21:35:06 +00:00
.flatMap(allIdentitiesService.updatePushSubscriptions(deviceToken:))
2020-08-12 07:24:39 +00:00
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2020-08-03 15:20:51 +00:00
}
}
2020-09-01 07:33:49 +00:00
public extension RootViewModel {
2020-08-09 05:37:04 +00:00
func newIdentitySelected(id: UUID?) {
guard let id = id else {
2020-09-08 02:12:38 +00:00
identification = nil
2020-08-07 10:14:14 +00:00
2020-08-09 05:37:04 +00:00
return
}
2020-08-07 01:41:59 +00:00
2020-09-08 02:12:38 +00:00
let identification: Identification
2020-08-07 01:41:59 +00:00
do {
2020-09-08 02:12:38 +00:00
identification = try Identification(service: allIdentitiesService.identityService(id: id))
self.identification = identification
2020-08-07 01:41:59 +00:00
} catch {
2020-08-09 05:37:04 +00:00
return
2020-08-07 01:41:59 +00:00
}
2020-08-03 15:20:51 +00:00
2020-09-08 02:12:38 +00:00
identification.observationErrors
2020-08-07 01:41:59 +00:00
.receive(on: RunLoop.main)
2020-08-26 08:40:54 +00:00
.map { [weak self] _ in self?.mostRecentlyUsedIdentityID }
2020-08-24 05:23:50 +00:00
.sink { [weak self] in self?.newIdentitySelected(id: $0) }
2020-08-09 05:37:04 +00:00
.store(in: &cancellables)
2020-09-08 02:12:38 +00:00
identification.service.updateLastUse()
2020-08-14 01:24:53 +00:00
.sink { _ in } receiveValue: { _ in }
2020-08-09 05:37:04 +00:00
.store(in: &cancellables)
userNotificationService.isAuthorized()
2020-08-12 07:24:39 +00:00
.filter { $0 }
2020-09-01 07:33:49 +00:00
.zip(registerForRemoteNotifications())
2020-09-08 02:12:38 +00:00
.filter { identification.identity.lastRegisteredDeviceToken != $1 }
.map { ($1, identification.identity.pushSubscriptionAlerts) }
.flatMap(identification.service.createPushSubscription(deviceToken:alerts:))
2020-08-12 07:24:39 +00:00
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
}
func deleteIdentity(_ identity: Identity) {
2020-08-26 21:35:06 +00:00
allIdentitiesService.deleteIdentity(identity)
2020-08-14 01:59:17 +00:00
.sink { _ in } receiveValue: { _ in }
2020-08-09 05:37:04 +00:00
.store(in: &cancellables)
}
func addIdentityViewModel() -> AddIdentityViewModel {
2020-08-26 21:35:06 +00:00
AddIdentityViewModel(allIdentitiesService: allIdentitiesService)
2020-08-03 15:20:51 +00:00
}
}