metatext/ViewModels/Sources/ViewModels/View Models/NavigationViewModel.swift

140 lines
4.8 KiB
Swift
Raw Normal View History

2020-08-03 15:20:51 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-09-05 02:31:43 +00:00
import Foundation
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-09 23:00:10 +00:00
public final class NavigationViewModel: ObservableObject {
2021-01-26 00:06:35 +00:00
public let identityContext: IdentityContext
2021-01-21 08:45:09 +00:00
public let timelineNavigations: AnyPublisher<Timeline, Never>
2020-09-01 07:33:49 +00:00
@Published public private(set) var recentIdentities = [Identity]()
@Published public var presentingSecondaryNavigation = false
@Published public var alertItem: AlertItem?
2021-01-20 23:33:53 +00:00
2021-01-23 03:48:33 +00:00
public lazy var exploreViewModel: ExploreViewModel = {
let exploreViewModel = ExploreViewModel(
2021-01-26 00:06:35 +00:00
service: identityContext.service.exploreService(),
identityContext: identityContext)
2021-01-23 03:48:33 +00:00
// TODO: initial request
return exploreViewModel
}()
2021-01-20 23:33:53 +00:00
public lazy var conversationsViewModel: CollectionViewModel? = {
2021-01-26 00:06:35 +00:00
if identityContext.identity.authenticated {
2021-01-20 23:33:53 +00:00
let conversationsViewModel = CollectionItemsViewModel(
2021-01-26 00:06:35 +00:00
collectionService: identityContext.service.conversationsService(),
identityContext: identityContext)
2020-10-29 06:03:45 +00:00
2021-01-23 03:48:33 +00:00
conversationsViewModel.request(maxId: nil, minId: nil, search: nil)
2021-01-20 23:33:53 +00:00
return conversationsViewModel
2020-10-29 06:03:45 +00:00
} else {
return nil
}
2021-01-20 23:33:53 +00:00
}()
2020-10-29 06:03:45 +00:00
2021-01-21 08:45:09 +00:00
private let timelineNavigationsSubject = PassthroughSubject<Timeline, Never>()
2020-08-03 15:20:51 +00:00
private var cancellables = Set<AnyCancellable>()
2021-01-26 00:06:35 +00:00
public init(identityContext: IdentityContext) {
self.identityContext = identityContext
2021-01-21 08:45:09 +00:00
timelineNavigations = timelineNavigationsSubject.eraseToAnyPublisher()
2020-08-03 15:20:51 +00:00
2021-01-26 00:06:35 +00:00
identityContext.$identity
2020-09-09 22:48:56 +00:00
.sink { [weak self] _ in self?.objectWillChange.send() }
.store(in: &cancellables)
2021-01-26 00:06:35 +00:00
identityContext.service.recentIdentitiesPublisher()
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-09-09 23:00:10 +00:00
public extension NavigationViewModel {
2021-01-21 08:45:09 +00:00
enum Tab: CaseIterable {
case timelines
case explore
case notifications
case messages
}
2020-09-09 05:40:49 +00:00
var tabs: [Tab] {
2021-01-26 00:06:35 +00:00
if identityContext.identity.authenticated {
2020-09-09 05:40:49 +00:00
return Tab.allCases
} else {
return [.timelines, .explore]
}
}
2021-01-21 08:45:09 +00:00
var timelines: [Timeline] {
2021-01-26 00:06:35 +00:00
if identityContext.identity.authenticated {
2021-01-21 08:45:09 +00:00
return Timeline.authenticatedDefaults
} else {
return Timeline.unauthenticatedDefaults
2020-08-29 00:06:09 +00:00
}
}
2020-08-03 15:20:51 +00:00
func refreshIdentity() {
2021-01-26 00:06:35 +00:00
if identityContext.identity.pending {
identityContext.service.verifyCredentials()
2020-09-13 08:03:08 +00:00
.collect()
.map { _ in () }
2021-01-26 00:06:35 +00:00
.flatMap(identityContext.service.confirmIdentity)
2020-09-13 08:03:08 +00:00
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2021-01-26 00:06:35 +00:00
} else if identityContext.identity.authenticated {
identityContext.service.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)
2021-01-26 00:06:35 +00:00
identityContext.service.refreshLists()
2020-09-13 08:03:08 +00:00
.sink { _ in } receiveValue: { _ in }
2020-08-29 03:50:58 +00:00
.store(in: &cancellables)
2021-01-26 00:06:35 +00:00
identityContext.service.refreshFilters()
2020-09-13 08:03:08 +00:00
.sink { _ in } receiveValue: { _ in }
2020-08-29 10:26:26 +00:00
.store(in: &cancellables)
2021-01-26 00:06:35 +00:00
identityContext.service.refreshEmojis()
2021-01-14 17:49:53 +00:00
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2021-01-26 00:06:35 +00:00
identityContext.service.refreshAnnouncements()
2021-01-16 05:31:06 +00:00
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2020-08-29 10:26:26 +00:00
2021-01-26 00:06:35 +00:00
if identityContext.identity.preferences.useServerPostingReadingPreferences {
identityContext.service.refreshServerPreferences()
2020-09-13 08:03:08 +00:00
.sink { _ in } receiveValue: { _ in }
2020-08-07 10:14:14 +00:00
.store(in: &cancellables)
}
2020-08-03 15:20:51 +00:00
}
2021-01-26 00:06:35 +00:00
identityContext.service.refreshInstance()
2020-09-13 08:03:08 +00:00
.sink { _ in } receiveValue: { _ in }
2020-08-03 15:20:51 +00:00
.store(in: &cancellables)
}
2020-12-01 03:07:38 +00:00
2021-01-21 08:45:09 +00:00
func navigate(timeline: Timeline) {
presentingSecondaryNavigation = false
timelineNavigationsSubject.send(timeline)
2020-12-01 03:07:38 +00:00
}
2020-12-01 18:40:19 +00:00
2021-01-21 08:45:09 +00:00
func viewModel(timeline: Timeline) -> CollectionItemsViewModel {
2020-12-01 18:40:19 +00:00
CollectionItemsViewModel(
2021-01-26 00:06:35 +00:00
collectionService: identityContext.service.service(timeline: timeline),
identityContext: identityContext)
2020-12-01 18:40:19 +00:00
}
2021-01-26 02:10:24 +00:00
func notificationsViewModel(excludeTypes: Set<MastodonNotification.NotificationType>) -> CollectionItemsViewModel {
let viewModel = CollectionItemsViewModel(
collectionService: identityContext.service.notificationsService(excludeTypes: excludeTypes),
identityContext: identityContext)
viewModel.request(maxId: nil, minId: nil, search: nil)
return viewModel
}
2020-08-03 15:20:51 +00:00
}