mirror of
https://github.com/metabolist/metatext.git
synced 2024-06-08 08:30:32 +00:00
92 lines
2.8 KiB
Swift
92 lines
2.8 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
class MainNavigationViewModel: ObservableObject {
|
|
@Published private(set) var identity: Identity
|
|
@Published private(set) var recentIdentities = [Identity]()
|
|
@Published var presentingSettings = false
|
|
@Published var alertItem: AlertItem?
|
|
var selectedTab: Tab? = .timelines
|
|
|
|
private let environment: IdentifiedEnvironment
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
init(environment: IdentifiedEnvironment) {
|
|
self.environment = environment
|
|
identity = environment.identity
|
|
environment.$identity.dropFirst().assign(to: &$identity)
|
|
|
|
environment.appEnvironment.identityDatabase
|
|
.recentIdentitiesObservation(excluding: environment.identity.id)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.assign(to: &$recentIdentities)
|
|
|
|
environment.appEnvironment.identityDatabase
|
|
.updateLastUsedAt(identityID: environment.identity.id)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink(receiveValue: {})
|
|
.store(in: &cancellables)
|
|
}
|
|
}
|
|
|
|
extension MainNavigationViewModel {
|
|
func refreshIdentity() {
|
|
let id = identity.id
|
|
|
|
if environment.networkClient.accessToken != nil {
|
|
environment.networkClient.request(AccountEndpoint.verifyCredentials)
|
|
.map { ($0, id) }
|
|
.flatMap(environment.appEnvironment.identityDatabase.updateAccount)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink(receiveValue: {})
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
environment.networkClient.request(InstanceEndpoint.instance)
|
|
.map { ($0, id) }
|
|
.flatMap(environment.appEnvironment.identityDatabase.updateInstance)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink(receiveValue: {})
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
func settingsViewModel() -> SettingsViewModel {
|
|
SettingsViewModel(environment: environment)
|
|
}
|
|
}
|
|
|
|
extension MainNavigationViewModel {
|
|
enum Tab: CaseIterable {
|
|
case timelines
|
|
case search
|
|
case notifications
|
|
case messages
|
|
}
|
|
}
|
|
|
|
extension MainNavigationViewModel.Tab {
|
|
var title: String {
|
|
switch self {
|
|
case .timelines: return "Timelines"
|
|
case .search: return "Search"
|
|
case .notifications: return "Notifications"
|
|
case .messages: return "Messages"
|
|
}
|
|
}
|
|
|
|
var systemImageName: String {
|
|
switch self {
|
|
case .timelines: return "house"
|
|
case .search: return "magnifyingglass"
|
|
case .notifications: return "bell"
|
|
case .messages: return "envelope"
|
|
}
|
|
}
|
|
}
|
|
|
|
extension MainNavigationViewModel.Tab: Identifiable {
|
|
var id: Self { self }
|
|
}
|