metatext/View Models/TabNavigationViewModel.swift

142 lines
4.3 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-03 15:20:51 +00:00
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-29 03:50:58 +00:00
@Published var timeline = Timeline.home
@Published private(set) var timelinesAndLists = Timeline.nonLists
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-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-08-08 06:01:45 +00:00
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 title(timeline: Timeline) -> String {
switch timeline {
case .home:
return NSLocalizedString("timelines.home", comment: "")
case .local:
return NSLocalizedString("timelines.local", comment: "")
case .federated:
return NSLocalizedString("timelines.federated", comment: "")
case let .list(list):
return list.title
2020-08-30 19:50:34 +00:00
case let .tag(tag):
return "#" + tag
2020-08-29 00:06:09 +00:00
}
}
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-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 }
}