2020-08-03 15:20:51 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class RootViewModel: ObservableObject {
|
2020-08-07 23:19:13 +00:00
|
|
|
@Published private(set) var identityID: UUID?
|
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
|
2020-08-03 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension RootViewModel {
|
2020-08-07 23:19:13 +00:00
|
|
|
func newIdentitySelected(id: UUID) {
|
2020-08-04 20:26:09 +00:00
|
|
|
identityID = id
|
2020-08-07 10:14:14 +00:00
|
|
|
|
|
|
|
environment.identityDatabase
|
|
|
|
.updateLastUsedAt(identityID: id)
|
|
|
|
.sink(receiveCompletion: { _ in }, receiveValue: {})
|
|
|
|
.store(in: &cancellables)
|
2020-08-03 15:20:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 07:43:06 +00:00
|
|
|
func deleteIdentity(id: UUID) {
|
|
|
|
environment.identityDatabase.deleteIdentity(id: id)
|
|
|
|
.sink(receiveCompletion: { _ in }, receiveValue: {})
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
2020-08-04 20:26:09 +00:00
|
|
|
func addIdentityViewModel() -> AddIdentityViewModel {
|
2020-08-07 01:41:59 +00:00
|
|
|
AddIdentityViewModel(environment: environment)
|
|
|
|
}
|
|
|
|
|
2020-08-07 23:19:13 +00:00
|
|
|
func mainNavigationViewModel(identityID: UUID) -> MainNavigationViewModel? {
|
2020-08-07 22:18:54 +00:00
|
|
|
let identityRepository: IdentityRepository
|
2020-08-07 01:41:59 +00:00
|
|
|
|
|
|
|
do {
|
2020-08-07 22:18:54 +00:00
|
|
|
identityRepository = try IdentityRepository(identityID: identityID, appEnvironment: environment)
|
2020-08-07 01:41:59 +00:00
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-03 15:20:51 +00:00
|
|
|
|
2020-08-07 22:18:54 +00:00
|
|
|
identityRepository.observationErrors
|
2020-08-07 01:41:59 +00:00
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.map { [weak self] _ in self?.environment.identityDatabase.mostRecentlyUsedIdentityID }
|
|
|
|
.assign(to: &$identityID)
|
2020-08-03 15:20:51 +00:00
|
|
|
|
2020-08-07 22:18:54 +00:00
|
|
|
return MainNavigationViewModel(identityRepository: identityRepository)
|
2020-08-03 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|