2020-08-03 15:20:51 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
class TabNavigationViewModel: 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-08 23:08:47 +00:00
|
|
|
private let identityService: IdentityService
|
2020-08-03 15:20:51 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2020-08-08 23:08:47 +00:00
|
|
|
init(identityService: IdentityService) {
|
|
|
|
self.identityService = identityService
|
|
|
|
identity = identityService.identity
|
|
|
|
identityService.$identity.dropFirst().assign(to: &$identity)
|
2020-08-03 15:20:51 +00:00
|
|
|
|
2020-08-08 23:08:47 +00:00
|
|
|
identityService.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
extension TabNavigationViewModel {
|
2020-08-03 15:20:51 +00:00
|
|
|
func refreshIdentity() {
|
2020-08-08 23:08:47 +00:00
|
|
|
if identityService.isAuthorized {
|
|
|
|
identityService.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-08 23:08:47 +00:00
|
|
|
identityService.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-08 23:08:47 +00:00
|
|
|
identityService.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-08 23:08:47 +00:00
|
|
|
SecondaryNavigationViewModel(identityService: identityService)
|
2020-08-03 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
extension TabNavigationViewModel {
|
2020-08-03 15:20:51 +00:00
|
|
|
enum Tab: CaseIterable {
|
|
|
|
case timelines
|
|
|
|
case search
|
|
|
|
case notifications
|
|
|
|
case messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
extension TabNavigationViewModel.Tab {
|
2020-08-03 15:20:51 +00:00
|
|
|
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 {
|
2020-08-08 23:36:35 +00:00
|
|
|
case .timelines: return "newspaper"
|
2020-08-03 15:20:51 +00:00
|
|
|
case .search: return "magnifyingglass"
|
|
|
|
case .notifications: return "bell"
|
|
|
|
case .messages: return "envelope"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
extension TabNavigationViewModel.Tab: Identifiable {
|
2020-08-03 15:20:51 +00:00
|
|
|
var id: Self { self }
|
|
|
|
}
|