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