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