metatext/ViewModels/Sources/ViewModels/TabNavigationViewModel.swift

108 lines
3.4 KiB
Swift
Raw Normal View History

2020-08-03 15:20:51 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-30 23:33:11 +00:00
import Mastodon
2020-08-31 18:57:02 +00:00
import ServiceLayer
2020-08-03 15:20:51 +00:00
2020-09-01 07:33:49 +00:00
public class TabNavigationViewModel: ObservableObject {
@Published public private(set) var identity: Identity
@Published public private(set) var recentIdentities = [Identity]()
@Published public var timeline = Timeline.home
@Published public private(set) var timelinesAndLists = Timeline.nonLists
@Published public var presentingSecondaryNavigation = false
@Published public var alertItem: AlertItem?
public 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-29 03:50:58 +00:00
identityService.listsObservation()
.map { Timeline.nonLists + $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$timelinesAndLists)
2020-08-03 15:20:51 +00:00
}
}
2020-09-01 07:33:49 +00:00
public extension TabNavigationViewModel {
2020-08-29 00:06:09 +00:00
var timelineSubtitle: String {
switch timeline {
case .home, .list:
return identity.handle
2020-08-30 19:50:34 +00:00
case .local, .federated, .tag:
2020-08-29 00:06:09 +00:00
return identity.instance?.uri ?? ""
}
}
func systemImageName(timeline: Timeline) -> String {
switch timeline {
case .home: return "house"
case .local: return "person.3"
case .federated: return "globe"
case .list: return "scroll"
2020-08-30 19:50:34 +00:00
case .tag: return "number"
2020-08-29 00:06:09 +00:00
}
}
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)
2020-08-26 09:19:38 +00:00
.sink { _ in }
2020-08-03 15:20:51 +00:00
.store(in: &cancellables)
2020-08-07 10:14:14 +00:00
2020-08-29 03:50:58 +00:00
identityService.refreshLists()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
2020-08-29 10:26:26 +00:00
identityService.refreshFilters()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
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)
2020-08-26 09:19:38 +00:00
.sink { _ in }
2020-08-07 10:14:14 +00:00
.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)
2020-08-26 09:19:38 +00:00
.sink { _ in }
2020-08-03 15:20:51 +00:00
.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-29 00:06:09 +00:00
func viewModel(timeline: Timeline) -> StatusListViewModel {
StatusListViewModel(statusListService: identityService.service(timeline: timeline))
}
2020-08-03 15:20:51 +00:00
}
2020-09-01 07:33:49 +00:00
public 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: Identifiable {
2020-09-01 07:33:49 +00:00
public var id: Self { self }
2020-08-03 15:20:51 +00:00
}