metatext/Shared/View Models/MainNavigationViewModel.swift

86 lines
2.6 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 MainNavigationViewModel: ObservableObject {
2020-08-04 20:26:09 +00:00
@Published private(set) var identity: Identity
2020-08-05 11:48:50 +00:00
@Published private(set) var recentIdentities = [Identity]()
2020-08-08 04:08:57 +00:00
@Published var presentingSecondaryNavigation = false
2020-08-04 20:26:09 +00:00
@Published var alertItem: AlertItem?
var selectedTab: Tab? = .timelines
2020-08-03 15:20:51 +00:00
2020-08-07 22:18:54 +00:00
private let identityRepository: IdentityRepository
2020-08-03 15:20:51 +00:00
private var cancellables = Set<AnyCancellable>()
2020-08-07 22:18:54 +00:00
init(identityRepository: IdentityRepository) {
self.identityRepository = identityRepository
identity = identityRepository.identity
identityRepository.$identity.dropFirst().assign(to: &$identity)
2020-08-03 15:20:51 +00:00
2020-08-07 22:18:54 +00:00
identityRepository.recentIdentitiesObservation()
2020-08-05 11:48:50 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$recentIdentities)
2020-08-03 15:20:51 +00:00
}
}
extension MainNavigationViewModel {
func refreshIdentity() {
2020-08-07 22:18:54 +00:00
if identityRepository.isAuthorized {
identityRepository.verifyCredentials()
2020-08-03 15:20:51 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
2020-08-07 10:14:14 +00:00
2020-08-07 10:59:48 +00:00
if identity.preferences.useServerPostingReadingPreferences {
2020-08-07 22:18:54 +00:00
identityRepository.refreshServerPreferences()
2020-08-07 10:14:14 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
}
2020-08-03 15:20:51 +00:00
}
2020-08-07 22:18:54 +00:00
identityRepository.refreshInstance()
2020-08-03 15:20:51 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
}
2020-08-08 04:08:57 +00:00
func secondaryNavigationViewModel() -> SecondaryNavigationViewModel {
2020-08-07 22:18:54 +00:00
SecondaryNavigationViewModel(identityRepository: identityRepository)
2020-08-03 15:20:51 +00:00
}
}
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 }
}