metatext/ViewModels/Sources/ViewModels/StatusListViewModel.swift

179 lines
7.5 KiB
Swift
Raw Normal View History

2020-08-18 05:13:37 +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-18 05:13:37 +00:00
2020-10-01 02:35:06 +00:00
final public class StatusListViewModel: ObservableObject {
@Published public private(set) var items = [[CollectionItemIdentifier]]()
2020-09-01 07:33:49 +00:00
@Published public var alertItem: AlertItem?
2020-09-24 01:33:13 +00:00
public private(set) var nextPageMaxID: String?
public private(set) var maintainScrollPositionOfItem: CollectionItemIdentifier?
2020-08-30 05:31:30 +00:00
private var timelineItems = [CollectionItemIdentifier: Timeline.Item]()
2020-08-18 05:13:37 +00:00
private let statusListService: StatusListService
private var viewModelCache = [Timeline.Item: (Any, AnyCancellable)]()
2020-09-23 01:00:56 +00:00
private let navigationEventsSubject = PassthroughSubject<NavigationEvent, Never>()
private let loadingSubject = PassthroughSubject<Bool, Never>()
2020-08-18 05:13:37 +00:00
private var cancellables = Set<AnyCancellable>()
init(statusListService: StatusListService) {
self.statusListService = statusListService
statusListService.sections
.handleEvents(receiveOutput: { [weak self] in self?.process(sections: $0) })
.map { $0.map { $0.map(CollectionItemIdentifier.init(timelineItem:)) } }
2020-09-01 07:33:49 +00:00
.receive(on: DispatchQueue.main)
2020-08-18 05:13:37 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-09-23 01:00:56 +00:00
.assign(to: &$items)
2020-09-24 01:33:13 +00:00
statusListService.nextPageMaxIDs
.sink { [weak self] in self?.nextPageMaxID = $0 }
.store(in: &cancellables)
2020-08-18 05:13:37 +00:00
}
2020-10-01 02:35:06 +00:00
}
2020-09-18 00:16:41 +00:00
2020-10-01 02:35:06 +00:00
extension StatusListViewModel: CollectionViewModel {
public var collectionItems: AnyPublisher<[[CollectionItemIdentifier]], Never> { $items.eraseToAnyPublisher() }
2020-09-27 01:23:56 +00:00
2020-09-23 01:43:06 +00:00
public var title: AnyPublisher<String?, Never> { Just(statusListService.title).eraseToAnyPublisher() }
2020-10-01 02:35:06 +00:00
public var alertItems: AnyPublisher<AlertItem, Never> { $alertItem.compactMap { $0 }.eraseToAnyPublisher() }
public var loading: AnyPublisher<Bool, Never> { loadingSubject.eraseToAnyPublisher() }
public var navigationEvents: AnyPublisher<NavigationEvent, Never> { navigationEventsSubject.eraseToAnyPublisher() }
2020-09-18 00:16:41 +00:00
public func request(maxID: String? = nil, minID: String? = nil) {
statusListService.request(maxID: maxID, minID: minID)
.receive(on: DispatchQueue.main)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.handleEvents(
2020-09-23 01:00:56 +00:00
receiveSubscription: { [weak self] _ in self?.loadingSubject.send(true) },
receiveCompletion: { [weak self] _ in self?.loadingSubject.send(false) })
2020-09-18 00:16:41 +00:00
.sink { _ in }
.store(in: &cancellables)
}
2020-09-23 01:00:56 +00:00
public func itemSelected(_ item: CollectionItemIdentifier) {
guard let timelineItem = timelineItems[item] else { return }
2020-09-23 01:00:56 +00:00
switch timelineItem {
case let .status(configuration):
2020-09-23 01:00:56 +00:00
navigationEventsSubject.send(
.collectionNavigation(
StatusListViewModel(
2020-09-25 05:39:06 +00:00
statusListService: statusListService
.navigationService
.contextStatusListService(id: configuration.status.displayStatus.id))))
2020-10-04 08:39:54 +00:00
case .loadMore:
loadMoreViewModel(item: item)?.loadMore()
2020-09-23 01:00:56 +00:00
}
}
public func canSelect(item: CollectionItemIdentifier) -> Bool {
2020-09-23 01:00:56 +00:00
if case .status = item.kind, item.id == statusListService.contextParentID {
return false
}
return true
}
public func viewModel(item: CollectionItemIdentifier) -> Any? {
2020-09-23 01:00:56 +00:00
switch item.kind {
case .status:
2020-09-27 01:23:56 +00:00
return statusViewModel(item: item)
2020-10-02 07:41:30 +00:00
case .loadMore:
2020-10-04 08:39:54 +00:00
return loadMoreViewModel(item: item)
2020-09-23 01:00:56 +00:00
default:
return nil
}
2020-09-15 01:39:35 +00:00
}
}
2020-09-25 05:39:06 +00:00
private extension StatusListViewModel {
func statusViewModel(item: CollectionItemIdentifier) -> StatusViewModel? {
guard let timelineItem = timelineItems[item],
case let .status(configuration) = timelineItem
else { return nil }
2020-08-26 08:25:34 +00:00
2020-08-24 02:50:54 +00:00
var statusViewModel: StatusViewModel
2020-09-15 01:39:35 +00:00
if let cachedViewModel = viewModelCache[timelineItem]?.0 as? StatusViewModel {
2020-08-24 02:50:54 +00:00
statusViewModel = cachedViewModel
} else {
2020-09-25 05:39:06 +00:00
statusViewModel = StatusViewModel(
statusService: statusListService.navigationService.statusService(status: configuration.status))
viewModelCache[timelineItem] = (statusViewModel,
statusViewModel.events
.flatMap { $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { [weak self] in
guard
let self = self,
let event = NavigationEvent($0)
else { return }
self.navigationEventsSubject.send(event)
})
2020-08-24 02:50:54 +00:00
}
2020-08-21 02:29:01 +00:00
statusViewModel.isContextParent = configuration.status.id == statusListService.contextParentID
statusViewModel.isPinned = configuration.pinned
statusViewModel.isReplyInContext = configuration.isReplyInContext
statusViewModel.hasReplyFollowing = configuration.hasReplyFollowing
2020-08-21 02:29:01 +00:00
return statusViewModel
}
2020-09-15 01:39:35 +00:00
2020-10-04 08:39:54 +00:00
func loadMoreViewModel(item: CollectionItemIdentifier) -> LoadMoreViewModel? {
guard let timelineItem = timelineItems[item],
case let .loadMore(loadMore) = timelineItem
else { return nil }
if let cachedViewModel = viewModelCache[timelineItem]?.0 as? LoadMoreViewModel {
return cachedViewModel
}
let loadMoreViewModel = LoadMoreViewModel(
loadMoreService: statusListService.navigationService.loadMoreService(loadMore: loadMore))
viewModelCache[timelineItem] = (loadMoreViewModel, loadMoreViewModel.events
.flatMap { $0 }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { [weak self] in
guard
let self = self,
let event = NavigationEvent($0)
else { return }
self.navigationEventsSubject.send(event)
})
return loadMoreViewModel
}
func process(sections: [[Timeline.Item]]) {
determineIfScrollPositionShouldBeMaintained(newSections: sections)
2020-08-24 02:50:54 +00:00
let timelineItemKeys = Set(sections.reduce([], +))
2020-08-24 02:50:54 +00:00
timelineItems = Dictionary(uniqueKeysWithValues: timelineItemKeys.map { (.init(timelineItem: $0), $0) })
viewModelCache = viewModelCache.filter { timelineItemKeys.contains($0.key) }
2020-08-24 02:50:54 +00:00
}
2020-09-02 09:07:09 +00:00
func determineIfScrollPositionShouldBeMaintained(newSections: [[Timeline.Item]]) {
maintainScrollPositionOfItem = nil // clear old value
2020-09-02 09:07:09 +00:00
// Maintain scroll position of parent after initial load of context
if let contextParentID = statusListService.contextParentID {
let contextParentIdentifier = CollectionItemIdentifier(id: contextParentID, kind: .status, info: [:])
2020-09-02 09:07:09 +00:00
if items == [[], [contextParentIdentifier], []] || items.isEmpty {
maintainScrollPositionOfItem = contextParentIdentifier
}
}
2020-09-02 09:07:09 +00:00
}
2020-08-21 02:29:01 +00:00
}