metatext/Shared/View Models/RootViewModel.swift

43 lines
1.2 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-04 20:26:09 +00:00
@Published private(set) var mainNavigationViewModel: MainNavigationViewModel?
2020-08-03 15:20:51 +00:00
2020-08-04 20:26:09 +00:00
@Published private var identityID: String?
2020-08-03 15:20:51 +00:00
private let environment: AppEnvironment
private var cancellables = Set<AnyCancellable>()
init(environment: AppEnvironment) {
self.environment = environment
2020-08-04 20:26:09 +00:00
identityID = environment.identityDatabase.mostRecentlyUsedIdentityID
$identityID
.tryMap {
guard let id = $0 else { return nil }
return try MainNavigationViewModel(identityID: id, environment: environment)
}
.replaceError(with: nil)
.assign(to: &$mainNavigationViewModel)
2020-08-03 15:20:51 +00:00
}
}
extension RootViewModel {
2020-08-04 20:26:09 +00:00
func newIdentitySelected(id: String) {
identityID = id
2020-08-03 15:20:51 +00:00
}
2020-08-04 20:26:09 +00:00
func addIdentityViewModel() -> AddIdentityViewModel {
let addAccountViewModel = AddIdentityViewModel(environment: environment)
2020-08-03 15:20:51 +00:00
2020-08-04 20:26:09 +00:00
addAccountViewModel.addedIdentityID
.sink(receiveValue: newIdentitySelected(id:))
2020-08-03 15:20:51 +00:00
.store(in: &cancellables)
2020-08-04 20:26:09 +00:00
return addAccountViewModel
2020-08-03 15:20:51 +00:00
}
}