metatext/ViewModels/Sources/ViewModels/NavigationViewModel.swift

161 lines
5.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 {
2020-09-09 22:48:56 +00:00
public let identification: Identification
2020-09-01 07:33:49 +00:00
@Published public private(set) var recentIdentities = [Identity]()
2020-10-29 01:55:34 +00:00
@Published public var timeline: Timeline {
didSet {
timelineViewModel = CollectionItemsViewModel(
collectionService: identification.service.service(timeline: timeline),
identification: identification)
}
}
2020-09-09 05:40:49 +00:00
@Published public private(set) var timelinesAndLists: [Timeline]
2020-09-01 07:33:49 +00:00
@Published public var presentingSecondaryNavigation = false
2020-12-06 03:10:27 +00:00
@Published public var presentingNewStatus = false
2020-09-01 07:33:49 +00:00
@Published public var alertItem: AlertItem?
2020-10-29 01:55:34 +00:00
public private(set) var timelineViewModel: CollectionItemsViewModel
2020-08-03 15:20:51 +00:00
2020-10-30 07:11:24 +00:00
public var notificationsViewModel: CollectionViewModel? {
if identification.identity.authenticated {
if _notificationsViewModel == nil {
_notificationsViewModel = CollectionItemsViewModel(
collectionService: identification.service.notificationsService(),
identification: identification)
2020-11-09 00:34:59 +00:00
_notificationsViewModel?.request(maxId: nil, minId: nil)
2020-10-30 07:11:24 +00:00
}
return _notificationsViewModel
} else {
return nil
}
}
2020-10-29 06:03:45 +00:00
public var conversationsViewModel: CollectionViewModel? {
if identification.identity.authenticated {
if _conversationsViewModel == nil {
_conversationsViewModel = CollectionItemsViewModel(
collectionService: identification.service.conversationsService(),
identification: identification)
2020-11-09 00:34:59 +00:00
_conversationsViewModel?.request(maxId: nil, minId: nil)
2020-10-29 06:03:45 +00:00
}
return _conversationsViewModel
} else {
return nil
}
}
2020-10-30 07:11:24 +00:00
private var _notificationsViewModel: CollectionViewModel?
2020-10-29 06:03:45 +00:00
private var _conversationsViewModel: CollectionViewModel?
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
2020-10-29 01:55:34 +00:00
let timeline: Timeline = identification.identity.authenticated ? .home : .local
self.timeline = timeline
timelineViewModel = CollectionItemsViewModel(
collectionService: identification.service.service(timeline: timeline),
identification: identification)
2020-09-10 04:26:13 +00:00
timelinesAndLists = identification.identity.authenticated
2020-09-09 05:40:49 +00:00
? Timeline.authenticatedDefaults
: Timeline.unauthenticatedDefaults
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-29 03:50:58 +00:00
2020-09-10 04:26:13 +00:00
if identification.identity.authenticated {
2020-10-06 20:44:22 +00:00
identification.service.listsPublisher()
2020-09-09 05:40:49 +00:00
.map { Timeline.authenticatedDefaults + $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$timelinesAndLists)
}
2020-08-03 15:20:51 +00:00
}
}
2020-09-09 23:00:10 +00:00
public extension NavigationViewModel {
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]
}
}
2020-08-29 00:06:09 +00:00
var timelineSubtitle: String {
switch timeline {
2020-12-03 20:41:28 +00:00
case .home, .favorites, .bookmarks, .list:
2020-09-09 22:48:56 +00:00
return identification.identity.handle
2020-12-03 20:41:28 +00:00
case .local, .federated, .tag, .profile:
2020-09-09 22:48:56 +00:00
return identification.identity.instance?.uri ?? ""
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)
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-09-09 23:00:10 +00:00
public extension NavigationViewModel {
2020-08-03 15:20:51 +00:00
enum Tab: CaseIterable {
case timelines
2020-09-09 05:40:49 +00:00
case explore
2020-08-03 15:20:51 +00:00
case notifications
case messages
}
2020-12-01 03:07:38 +00:00
func favoritesViewModel() -> CollectionViewModel {
CollectionItemsViewModel(
collectionService: identification.service.service(timeline: .favorites),
identification: identification)
}
2020-12-01 18:40:19 +00:00
func bookmarksViewModel() -> CollectionViewModel {
CollectionItemsViewModel(
collectionService: identification.service.service(timeline: .bookmarks),
identification: identification)
}
2020-08-03 15:20:51 +00:00
}
2020-09-09 23:00:10 +00:00
extension NavigationViewModel.Tab: Identifiable {
2020-09-01 07:33:49 +00:00
public var id: Self { self }
2020-08-03 15:20:51 +00:00
}