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-05 06:36:22 +00:00
|
|
|
final public class ListViewModel: ObservableObject {
|
|
|
|
@Published public private(set) var identifiers = [[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?
|
2020-10-02 03:19:14 +00:00
|
|
|
public private(set) var maintainScrollPositionOfItem: CollectionItemIdentifier?
|
2020-08-30 05:31:30 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
private var items = [CollectionItemIdentifier: CollectionItem]()
|
|
|
|
private let collectionService: CollectionService
|
|
|
|
private var viewModelCache = [CollectionItem: (CollectionItemViewModel, 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>()
|
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
init(collectionService: CollectionService) {
|
|
|
|
self.collectionService = collectionService
|
2020-08-18 05:13:37 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
collectionService.sections
|
2020-10-02 03:19:14 +00:00
|
|
|
.handleEvents(receiveOutput: { [weak self] in self?.process(sections: $0) })
|
2020-10-05 06:36:22 +00:00
|
|
|
.map { $0.map { $0.map(CollectionItemIdentifier.init(item:)) } }
|
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-10-05 06:36:22 +00:00
|
|
|
.assign(to: &$identifiers)
|
2020-09-24 01:33:13 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
collectionService.nextPageMaxIDs
|
2020-09-24 01:33:13 +00:00
|
|
|
.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-05 06:36:22 +00:00
|
|
|
extension ListViewModel: CollectionViewModel {
|
|
|
|
public var sections: AnyPublisher<[[CollectionItemIdentifier]], Never> { $identifiers.eraseToAnyPublisher() }
|
2020-09-27 01:23:56 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
public var title: AnyPublisher<String?, Never> { Just(collectionService.title).eraseToAnyPublisher() }
|
2020-09-23 01:43:06 +00:00
|
|
|
|
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) {
|
2020-10-05 06:36:22 +00:00
|
|
|
collectionService.request(maxID: maxID, minID: minID)
|
2020-09-18 00:16:41 +00:00
|
|
|
.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
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
public func select(identifier: CollectionItemIdentifier) {
|
|
|
|
guard let item = items[identifier] else { return }
|
2020-09-23 01:00:56 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
switch item {
|
2020-10-02 03:19:14 +00:00
|
|
|
case let .status(configuration):
|
2020-09-23 01:00:56 +00:00
|
|
|
navigationEventsSubject.send(
|
|
|
|
.collectionNavigation(
|
2020-10-05 06:36:22 +00:00
|
|
|
ListViewModel(
|
|
|
|
collectionService: collectionService
|
2020-09-25 05:39:06 +00:00
|
|
|
.navigationService
|
2020-10-05 07:04:15 +00:00
|
|
|
.contextService(id: configuration.status.displayStatus.id))))
|
2020-10-04 08:39:54 +00:00
|
|
|
case .loadMore:
|
2020-10-05 06:36:22 +00:00
|
|
|
loadMoreViewModel(item: identifier)?.loadMore()
|
|
|
|
case let .account(account):
|
|
|
|
navigationEventsSubject.send(
|
|
|
|
.profileNavigation(
|
|
|
|
ProfileViewModel(
|
|
|
|
profileService: collectionService.navigationService.profileService(account: account))))
|
2020-09-23 01:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
public func canSelect(identifier: CollectionItemIdentifier) -> Bool {
|
|
|
|
if case .status = identifier.kind, identifier.id == collectionService.contextParentID {
|
2020-09-23 01:00:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
public func viewModel(identifier: CollectionItemIdentifier) -> CollectionItemViewModel? {
|
|
|
|
switch identifier.kind {
|
2020-09-23 01:00:56 +00:00
|
|
|
case .status:
|
2020-10-05 06:36:22 +00:00
|
|
|
return statusViewModel(item: identifier)
|
2020-10-02 07:41:30 +00:00
|
|
|
case .loadMore:
|
2020-10-05 06:36:22 +00:00
|
|
|
return loadMoreViewModel(item: identifier)
|
|
|
|
case .account:
|
|
|
|
return accountViewModel(item: identifier)
|
2020-09-23 01:00:56 +00:00
|
|
|
}
|
2020-09-15 01:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
private extension ListViewModel {
|
2020-10-02 03:19:14 +00:00
|
|
|
func statusViewModel(item: CollectionItemIdentifier) -> StatusViewModel? {
|
2020-10-05 06:36:22 +00:00
|
|
|
guard let timelineItem = items[item],
|
2020-10-02 03:19:14 +00:00
|
|
|
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
|
|
|
|
2020-10-02 03:19:14 +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(
|
2020-10-05 06:36:22 +00:00
|
|
|
statusService: collectionService.navigationService.statusService(status: configuration.status))
|
|
|
|
cache(viewModel: statusViewModel, forItem: timelineItem)
|
2020-08-24 02:50:54 +00:00
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
statusViewModel.isContextParent = configuration.status.id == collectionService.contextParentID
|
2020-10-02 03:19:14 +00:00
|
|
|
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? {
|
2020-10-05 06:36:22 +00:00
|
|
|
guard let timelineItem = items[item],
|
2020-10-04 08:39:54 +00:00
|
|
|
case let .loadMore(loadMore) = timelineItem
|
|
|
|
else { return nil }
|
|
|
|
|
|
|
|
if let cachedViewModel = viewModelCache[timelineItem]?.0 as? LoadMoreViewModel {
|
|
|
|
return cachedViewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
let loadMoreViewModel = LoadMoreViewModel(
|
2020-10-05 06:36:22 +00:00
|
|
|
loadMoreService: collectionService.navigationService.loadMoreService(loadMore: loadMore))
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
cache(viewModel: loadMoreViewModel, forItem: timelineItem)
|
2020-10-04 08:39:54 +00:00
|
|
|
|
|
|
|
return loadMoreViewModel
|
|
|
|
}
|
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
func accountViewModel(item: CollectionItemIdentifier) -> AccountViewModel? {
|
|
|
|
guard let timelineItem = items[item],
|
|
|
|
case let .account(account) = timelineItem
|
|
|
|
else { return nil }
|
|
|
|
|
|
|
|
var accountViewModel: AccountViewModel
|
|
|
|
|
|
|
|
if let cachedViewModel = viewModelCache[timelineItem]?.0 as? AccountViewModel {
|
|
|
|
accountViewModel = cachedViewModel
|
|
|
|
} else {
|
|
|
|
accountViewModel = AccountViewModel(
|
|
|
|
accountService: collectionService.navigationService.accountService(account: account))
|
|
|
|
cache(viewModel: accountViewModel, forItem: timelineItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
return accountViewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func cache(viewModel: CollectionItemViewModel, forItem item: CollectionItem) {
|
|
|
|
viewModelCache[item] = (viewModel, viewModel.events.flatMap { $0.compactMap(NavigationEvent.init) }
|
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.sink { [weak self] in self?.navigationEventsSubject.send($0) })
|
|
|
|
}
|
|
|
|
|
|
|
|
func process(sections: [[CollectionItem]]) {
|
2020-10-02 03:19:14 +00:00
|
|
|
determineIfScrollPositionShouldBeMaintained(newSections: sections)
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2020-10-02 03:19:14 +00:00
|
|
|
let timelineItemKeys = Set(sections.reduce([], +))
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
items = Dictionary(uniqueKeysWithValues: timelineItemKeys.map { (.init(item: $0), $0) })
|
2020-10-02 03:19:14 +00:00
|
|
|
viewModelCache = viewModelCache.filter { timelineItemKeys.contains($0.key) }
|
2020-08-24 02:50:54 +00:00
|
|
|
}
|
2020-09-02 09:07:09 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
func determineIfScrollPositionShouldBeMaintained(newSections: [[CollectionItem]]) {
|
2020-10-02 03:19:14 +00:00
|
|
|
maintainScrollPositionOfItem = nil // clear old value
|
2020-09-02 09:07:09 +00:00
|
|
|
|
2020-10-02 03:19:14 +00:00
|
|
|
// Maintain scroll position of parent after initial load of context
|
2020-10-05 06:36:22 +00:00
|
|
|
if let contextParentID = collectionService.contextParentID {
|
2020-10-02 03:19:14 +00:00
|
|
|
let contextParentIdentifier = CollectionItemIdentifier(id: contextParentID, kind: .status, info: [:])
|
2020-09-02 09:07:09 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
if identifiers == [[], [contextParentIdentifier], []] || identifiers.isEmpty {
|
2020-10-02 03:19:14 +00:00
|
|
|
maintainScrollPositionOfItem = contextParentIdentifier
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 09:07:09 +00:00
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
}
|