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

206 lines
7.7 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-27 23:49:13 +00:00
public let navigations: AnyPublisher<Navigation, Never>
2021-01-21 08:45:09 +00:00
2020-09-01 07:33:49 +00:00
@Published public private(set) var recentIdentities = [Identity]()
2021-04-25 19:38:36 +00:00
@Published public private(set) var announcementCount: (total: Int, unread: Int) = (0, 0)
2021-02-04 21:33:29 +00:00
@Published public var presentedNewStatusViewModel: NewStatusViewModel?
2020-09-01 07:33:49 +00:00
@Published public var presentingSecondaryNavigation = false
@Published public var alertItem: AlertItem?
2021-01-20 23:33:53 +00:00
2021-01-27 23:49:13 +00:00
private let navigationsSubject = PassthroughSubject<Navigation, 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-27 23:49:13 +00:00
navigations = navigationsSubject.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)
2021-04-25 19:38:36 +00:00
identityContext.service.announcementCountPublisher()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$announcementCount)
2020-08-03 15:20:51 +00:00
}
}
2020-09-09 23:00:10 +00:00
public extension NavigationViewModel {
2021-01-27 23:49:13 +00:00
enum Tab: Int, CaseIterable {
2021-01-21 08:45:09 +00:00
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-27 23:49:13 +00:00
func navigateToProfile(id: Account.Id) {
presentingSecondaryNavigation = false
2021-02-05 02:56:14 +00:00
presentedNewStatusViewModel = nil
2021-01-27 23:49:13 +00:00
navigationsSubject.send(.profile(identityContext.service.navigationService.profileService(id: id)))
}
2021-01-21 08:45:09 +00:00
func navigate(timeline: Timeline) {
presentingSecondaryNavigation = false
2021-02-05 02:56:14 +00:00
presentedNewStatusViewModel = nil
2021-01-27 23:49:13 +00:00
navigationsSubject.send(
.collection(identityContext.service.navigationService.timelineService(timeline: timeline)))
2020-12-01 03:07:38 +00:00
}
2020-12-01 18:40:19 +00:00
2021-01-26 06:57:44 +00:00
func navigateToFollowerRequests() {
presentingSecondaryNavigation = false
2021-02-05 02:56:14 +00:00
presentedNewStatusViewModel = nil
2021-01-27 23:49:13 +00:00
navigationsSubject.send(.collection(identityContext.service.service(
accountList: .followRequests,
titleComponents: ["follow-requests"])))
2021-01-26 06:57:44 +00:00
}
2021-02-11 02:04:04 +00:00
func navigateToMutedUsers() {
presentingSecondaryNavigation = false
presentedNewStatusViewModel = nil
navigationsSubject.send(.collection(identityContext.service.service(
accountList: .mutes,
titleComponents: ["preferences.muted-users"])))
}
func navigateToBlockedUsers() {
presentingSecondaryNavigation = false
presentedNewStatusViewModel = nil
navigationsSubject.send(.collection(identityContext.service.service(
accountList: .blocks,
titleComponents: ["preferences.blocked-users"])))
}
func navigateToURL(_ url: URL) {
2021-03-03 06:55:35 +00:00
presentingSecondaryNavigation = false
presentedNewStatusViewModel = nil
identityContext.service.navigationService.item(url: url)
2021-03-03 06:55:35 +00:00
.sink { [weak self] in self?.navigationsSubject.send($0) }
.store(in: &cancellables)
}
2021-02-04 21:33:29 +00:00
func navigate(pushNotification: PushNotification) {
2021-02-05 02:56:14 +00:00
switch pushNotification.notificationType {
case .followRequest:
navigateToFollowerRequests()
default:
identityContext.service.notificationService(pushNotification: pushNotification)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { [weak self] in
self?.presentingSecondaryNavigation = false
self?.presentedNewStatusViewModel = nil
self?.navigationsSubject.send(.notification($0))
}
.store(in: &cancellables)
}
2021-02-04 21:33:29 +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-27 23:49:13 +00:00
collectionService: identityContext.service.navigationService.timelineService(timeline: timeline),
2021-01-26 00:06:35 +00:00
identityContext: identityContext)
2020-12-01 18:40:19 +00:00
}
2021-01-26 02:10:24 +00:00
2021-01-27 22:23:52 +00:00
func exploreViewModel() -> ExploreViewModel {
let exploreViewModel = ExploreViewModel(
service: identityContext.service.exploreService(),
identityContext: identityContext)
2021-01-30 22:27:49 +00:00
exploreViewModel.refresh()
2021-01-27 22:23:52 +00:00
return exploreViewModel
}
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)
2021-02-21 23:00:56 +00:00
if excludeTypes.isEmpty {
viewModel.request(maxId: nil, minId: nil, search: nil)
}
2021-01-26 02:10:24 +00:00
return viewModel
}
2021-01-27 22:23:52 +00:00
func conversationsViewModel() -> CollectionViewModel {
let conversationsViewModel = CollectionItemsViewModel(
collectionService: identityContext.service.conversationsService(),
identityContext: identityContext)
conversationsViewModel.request(maxId: nil, minId: nil, search: nil)
return conversationsViewModel
}
2021-04-25 19:38:36 +00:00
func announcementsViewModel() -> CollectionViewModel {
CollectionItemsViewModel(
collectionService: identityContext.service.announcementsService(),
identityContext: identityContext)
}
2020-08-03 15:20:51 +00:00
}