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

144 lines
4.9 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 {
2020-09-09 22:48:56 +00:00
public let identification: Identification
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(
service: identification.service.exploreService(),
identification: identification)
// TODO: initial request
return exploreViewModel
}()
2021-01-20 23:33:53 +00:00
public lazy var notificationsViewModel: CollectionViewModel? = {
if identification.identity.authenticated {
let notificationsViewModel = CollectionItemsViewModel(
2020-10-30 07:11:24 +00:00
collectionService: identification.service.notificationsService(),
identification: identification)
2021-01-23 03:48:33 +00:00
notificationsViewModel.request(maxId: nil, minId: nil, search: nil)
2021-01-20 23:33:53 +00:00
return notificationsViewModel
2020-10-30 07:11:24 +00:00
} else {
return nil
}
2021-01-20 23:33:53 +00:00
}()
2020-10-30 07:11:24 +00:00
2021-01-20 23:33:53 +00:00
public lazy var conversationsViewModel: CollectionViewModel? = {
2020-10-29 06:03:45 +00:00
if identification.identity.authenticated {
2021-01-20 23:33:53 +00:00
let conversationsViewModel = CollectionItemsViewModel(
2020-10-29 06:03:45 +00:00
collectionService: identification.service.conversationsService(),
identification: identification)
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>()
2020-09-08 02:12:38 +00:00
public init(identification: Identification) {
self.identification = identification
2021-01-21 08:45:09 +00:00
timelineNavigations = timelineNavigationsSubject.eraseToAnyPublisher()
2020-08-03 15:20:51 +00:00
2020-09-09 22:48:56 +00:00
identification.$identity
.sink { [weak self] _ in self?.objectWillChange.send() }
.store(in: &cancellables)
2020-10-06 20:44:22 +00:00
identification.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] {
2020-09-10 04:26:13 +00:00
if identification.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] {
if identification.identity.authenticated {
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() {
2020-09-13 08:03:08 +00:00
if identification.identity.pending {
identification.service.verifyCredentials()
.collect()
.map { _ in () }
.flatMap(identification.service.confirmIdentity)
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
} else if identification.identity.authenticated {
2020-09-08 02:12:38 +00:00
identification.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)
2020-09-08 02:12:38 +00:00
identification.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)
2020-09-08 02:12:38 +00:00
identification.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-14 17:49:53 +00:00
identification.service.refreshEmojis()
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2021-01-16 05:31:06 +00:00
identification.service.refreshAnnouncements()
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
2020-08-29 10:26:26 +00:00
2020-09-09 22:48:56 +00:00
if identification.identity.preferences.useServerPostingReadingPreferences {
2020-09-08 02:12:38 +00:00
identification.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
}
2020-09-08 02:12:38 +00:00
identification.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-21 08:45:09 +00:00
collectionService: identification.service.service(timeline: timeline),
2020-12-01 18:40:19 +00:00
identification: identification)
}
2020-08-03 15:20:51 +00:00
}