metatext/Shared/View Models/RootViewModel.swift

86 lines
3 KiB
Swift
Raw Normal View History

2020-08-03 15:20:51 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
class RootViewModel: ObservableObject {
2020-08-09 05:37:04 +00:00
@Published private(set) var mainNavigationViewModel: MainNavigationViewModel?
2020-08-26 08:40:54 +00:00
@Published private var mostRecentlyUsedIdentityID: UUID?
2020-08-09 05:37:04 +00:00
2020-08-12 07:24:39 +00:00
// swiftlint:disable weak_delegate
private let appDelegate: AppDelegate
// swiftlint:enable weak_delegate
2020-08-26 21:35:06 +00:00
private let allIdentitiesService: AllIdentitiesService
private let userNotificationService: UserNotificationService
2020-08-03 15:20:51 +00:00
private var cancellables = Set<AnyCancellable>()
init(appDelegate: AppDelegate,
2020-08-26 21:35:06 +00:00
allIdentitiesService: AllIdentitiesService,
userNotificationService: UserNotificationService) {
2020-08-12 07:24:39 +00:00
self.appDelegate = appDelegate
2020-08-26 21:35:06 +00:00
self.allIdentitiesService = allIdentitiesService
self.userNotificationService = userNotificationService
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 }
.zip(appDelegate.registerForRemoteNotifications())
.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
}
}
extension RootViewModel {
2020-08-09 05:37:04 +00:00
func newIdentitySelected(id: UUID?) {
guard let id = id else {
mainNavigationViewModel = 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-08-08 23:08:47 +00:00
let identityService: IdentityService
2020-08-07 01:41:59 +00:00
do {
2020-08-26 21:35:06 +00:00
identityService = try allIdentitiesService.identityService(id: id)
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-08-08 23:08:47 +00:00
identityService.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)
identityService.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 }
.zip(appDelegate.registerForRemoteNotifications())
2020-08-14 01:24:53 +00:00
.filter { identityService.identity.lastRegisteredDeviceToken != $1 }
.map { ($1, identityService.identity.pushSubscriptionAlerts) }
.flatMap(identityService.createPushSubscription(deviceToken:alerts:))
2020-08-12 07:24:39 +00:00
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2020-08-14 01:24:53 +00:00
mainNavigationViewModel = MainNavigationViewModel(identityService: identityService)
2020-08-12 07:24:39 +00:00
}
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
}
}