metatext/Shared/View Models/RootViewModel.swift

86 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()
.sink(receiveCompletion: { _ in }, receiveValue: {})
.store(in: &cancellables)
mainNavigationViewModel = MainNavigationViewModel(identityService: identityService)
}
2020-08-03 15:20:51 +00:00
2020-08-12 07:24:39 +00:00
func newIdentityCreated(id: UUID, instanceURL: URL) {
newIdentitySelected(id: id)
userNotificationService.isAuthorized()
2020-08-12 07:24:39 +00:00
.filter { $0 }
.zip(appDelegate.registerForRemoteNotifications())
.map { (id, instanceURL, $1, nil) }
.flatMap(identitiesService.updatePushSubscription(identityID:instanceURL:deviceToken:alerts:))
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
}
2020-08-09 05:37:04 +00:00
func deleteIdentity(id: UUID) {
identitiesService.deleteIdentity(id: id)
.sink(receiveCompletion: { _ in }, receiveValue: {})
.store(in: &cancellables)
}
func addIdentityViewModel() -> AddIdentityViewModel {
AddIdentityViewModel(identitiesService: identitiesService)
2020-08-03 15:20:51 +00:00
}
}