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?
|
|
|
|
|
|
|
|
private let identitiesService: IdentitiesService
|
2020-08-03 15:20:51 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2020-08-09 05:37:04 +00:00
|
|
|
init(identitiesService: IdentitiesService) {
|
|
|
|
self.identitiesService = identitiesService
|
|
|
|
|
|
|
|
newIdentitySelected(id: identitiesService.mostRecentlyUsedIdentityID)
|
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-09 05:37:04 +00:00
|
|
|
func deleteIdentity(id: UUID) {
|
|
|
|
identitiesService.deleteIdentity(id: id)
|
|
|
|
.sink(receiveCompletion: { _ in }, receiveValue: {})
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addIdentityViewModel() -> AddIdentityViewModel {
|
2020-08-09 11:27:38 +00:00
|
|
|
AddIdentityViewModel(identitiesService: identitiesService)
|
2020-08-03 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|