mirror of
https://github.com/metabolist/metatext.git
synced 2025-01-06 12:08:41 +00:00
47 lines
1.4 KiB
Swift
47 lines
1.4 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
class RootViewModel: ObservableObject {
|
|
@Published private(set) var identityID: String?
|
|
private let environment: AppEnvironment
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
init(environment: AppEnvironment) {
|
|
self.environment = environment
|
|
identityID = environment.identityDatabase.mostRecentlyUsedIdentityID
|
|
}
|
|
}
|
|
|
|
extension RootViewModel {
|
|
func newIdentitySelected(id: String) {
|
|
identityID = id
|
|
|
|
environment.identityDatabase
|
|
.updateLastUsedAt(identityID: id)
|
|
.sink(receiveCompletion: { _ in }, receiveValue: {})
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
func addIdentityViewModel() -> AddIdentityViewModel {
|
|
AddIdentityViewModel(environment: environment)
|
|
}
|
|
|
|
func mainNavigationViewModel(identityID: String) -> MainNavigationViewModel? {
|
|
let identityRepository: IdentityRepository
|
|
|
|
do {
|
|
identityRepository = try IdentityRepository(identityID: identityID, appEnvironment: environment)
|
|
} catch {
|
|
return nil
|
|
}
|
|
|
|
identityRepository.observationErrors
|
|
.receive(on: RunLoop.main)
|
|
.map { [weak self] _ in self?.environment.identityDatabase.mostRecentlyUsedIdentityID }
|
|
.assign(to: &$identityID)
|
|
|
|
return MainNavigationViewModel(identityRepository: identityRepository)
|
|
}
|
|
}
|