metatext/Shared/View Models/RootViewModel.swift

83 lines
2.8 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-12 07:24:39 +00:00
// swiftlint:disable weak_delegate
private let appDelegate: AppDelegate
// swiftlint:enable weak_delegate
2020-08-09 05:37:04 +00:00
private let identitiesService: IdentitiesService
private let userNotificationService: UserNotificationService
2020-08-03 15:20:51 +00:00
private var cancellables = Set<AnyCancellable>()
init(appDelegate: AppDelegate,
identitiesService: IdentitiesService,
userNotificationService: UserNotificationService) {
2020-08-12 07:24:39 +00:00
self.appDelegate = appDelegate
2020-08-09 05:37:04 +00:00
self.identitiesService = identitiesService
self.userNotificationService = userNotificationService
2020-08-09 05:37:04 +00:00
newIdentitySelected(id: identitiesService.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 }
.flatMap(identitiesService.updatePushSubscriptions(deviceToken:))
.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-09 05:37:04 +00:00
identityService = try identitiesService.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-09 05:37:04 +00:00
.map { [weak self] _ in self?.identitiesService.mostRecentlyUsedIdentityID }
.sink(receiveValue: newIdentitySelected(id:))
.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) {
identitiesService.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 {
AddIdentityViewModel(identitiesService: identitiesService)
2020-08-03 15:20:51 +00:00
}
}