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 20:06:50 +00:00
|
|
|
final public class CollectionItemsViewModel: ObservableObject {
|
2020-09-01 07:33:49 +00:00
|
|
|
@Published public var alertItem: AlertItem?
|
2020-10-05 22:50:05 +00:00
|
|
|
public private(set) var nextPageMaxId: String?
|
2020-08-30 05:31:30 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
private let items = CurrentValueSubject<[[CollectionItem]], Never>([])
|
2020-10-05 06:36:22 +00:00
|
|
|
private let collectionService: CollectionService
|
2020-10-07 00:31:29 +00:00
|
|
|
private let identification: Identification
|
2020-10-06 23:26:11 +00:00
|
|
|
private var viewModelCache = [CollectionItem: (viewModel: CollectionItemViewModel, events: AnyCancellable)]()
|
2020-10-07 00:31:29 +00:00
|
|
|
private let eventsSubject = PassthroughSubject<CollectionItemEvent, Never>()
|
2020-09-23 01:00:56 +00:00
|
|
|
private let loadingSubject = PassthroughSubject<Bool, Never>()
|
2020-10-07 21:06:26 +00:00
|
|
|
private let showMoreForAllSubject: CurrentValueSubject<ShowMoreForAllState, Never>
|
|
|
|
private var maintainScrollPosition: CollectionItemIdentifier?
|
2020-10-06 23:12:11 +00:00
|
|
|
private var topVisibleIndexPath = IndexPath(item: 0, section: 0)
|
2020-10-06 06:40:06 +00:00
|
|
|
private var lastSelectedLoadMore: LoadMore?
|
2020-08-18 05:13:37 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2020-10-07 00:31:29 +00:00
|
|
|
public init(collectionService: CollectionService, identification: Identification) {
|
2020-10-05 06:36:22 +00:00
|
|
|
self.collectionService = collectionService
|
2020-10-07 00:31:29 +00:00
|
|
|
self.identification = identification
|
2020-10-07 21:06:26 +00:00
|
|
|
showMoreForAllSubject = CurrentValueSubject(
|
|
|
|
collectionService is ContextService && !identification.identity.preferences.readingExpandSpoilers
|
|
|
|
? .showMore : .hidden)
|
2020-08-18 05:13:37 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
collectionService.sections
|
2020-10-05 07:50:59 +00:00
|
|
|
.handleEvents(receiveOutput: { [weak self] in self?.process(items: $0) })
|
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 07:50:59 +00:00
|
|
|
.sink { _ in }
|
|
|
|
.store(in: &cancellables)
|
2020-09-24 01:33:13 +00:00
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
collectionService.nextPageMaxId
|
|
|
|
.sink { [weak self] in self?.nextPageMaxId = $0 }
|
2020-09-24 01:33:13 +00:00
|
|
|
.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 20:06:50 +00:00
|
|
|
extension CollectionItemsViewModel: CollectionViewModel {
|
2020-10-07 21:06:26 +00:00
|
|
|
public var updates: AnyPublisher<CollectionUpdate, Never> {
|
|
|
|
items.map { [weak self] in
|
|
|
|
CollectionUpdate(items: $0.map { $0.map(CollectionItemIdentifier.init(item:)) },
|
|
|
|
maintainScrollPosition: self?.maintainScrollPosition)
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2020-10-05 07:50:59 +00:00
|
|
|
}
|
2020-09-27 01:23:56 +00:00
|
|
|
|
2020-10-05 20:21:06 +00:00
|
|
|
public var title: AnyPublisher<String, Never> { collectionService.title }
|
2020-09-23 01:43:06 +00:00
|
|
|
|
2020-10-07 21:06:26 +00:00
|
|
|
public var showMoreForAll: AnyPublisher<ShowMoreForAllState, Never> {
|
|
|
|
showMoreForAllSubject.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() }
|
|
|
|
|
2020-10-07 00:31:29 +00:00
|
|
|
public var events: AnyPublisher<CollectionItemEvent, Never> { eventsSubject.eraseToAnyPublisher() }
|
2020-10-01 02:35:06 +00:00
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
public func request(maxId: String? = nil, minId: String? = nil) {
|
|
|
|
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 07:50:59 +00:00
|
|
|
public func select(indexPath: IndexPath) {
|
|
|
|
let item = items.value[indexPath.section][indexPath.item]
|
2020-09-23 01:00:56 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
switch item {
|
2020-10-05 23:54:45 +00:00
|
|
|
case let .status(status, _):
|
2020-10-07 00:31:29 +00:00
|
|
|
eventsSubject.send(
|
|
|
|
.navigation(.collection(collectionService
|
|
|
|
.navigationService
|
|
|
|
.contextService(id: status.displayStatus.id))))
|
2020-10-06 06:40:06 +00:00
|
|
|
case let .loadMore(loadMore):
|
|
|
|
lastSelectedLoadMore = loadMore
|
2020-10-05 07:50:59 +00:00
|
|
|
(viewModel(indexPath: indexPath) as? LoadMoreViewModel)?.loadMore()
|
2020-10-05 06:36:22 +00:00
|
|
|
case let .account(account):
|
2020-10-07 00:31:29 +00:00
|
|
|
eventsSubject.send(
|
|
|
|
.navigation(.profile(collectionService
|
|
|
|
.navigationService
|
|
|
|
.profileService(account: account))))
|
2020-09-23 01:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 23:12:11 +00:00
|
|
|
public func viewedAtTop(indexPath: IndexPath) {
|
|
|
|
topVisibleIndexPath = indexPath
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
public func canSelect(indexPath: IndexPath) -> Bool {
|
2020-10-05 23:24:58 +00:00
|
|
|
switch items.value[indexPath.section][indexPath.item] {
|
2020-10-05 23:54:45 +00:00
|
|
|
case let .status(_, configuration):
|
2020-10-05 23:44:15 +00:00
|
|
|
return !configuration.isContextParent
|
2020-10-05 23:24:58 +00:00
|
|
|
case .loadMore:
|
|
|
|
return !((viewModel(indexPath: indexPath) as? LoadMoreViewModel)?.loading ?? false)
|
|
|
|
default:
|
|
|
|
return true
|
2020-09-23 01:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
public func viewModel(indexPath: IndexPath) -> CollectionItemViewModel {
|
|
|
|
let item = items.value[indexPath.section][indexPath.item]
|
2020-10-06 23:26:11 +00:00
|
|
|
let cachedViewModel = viewModelCache[item]?.viewModel
|
2020-09-15 01:39:35 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
switch item {
|
2020-10-05 23:54:45 +00:00
|
|
|
case let .status(status, configuration):
|
2020-10-05 07:50:59 +00:00
|
|
|
var viewModel: StatusViewModel
|
|
|
|
|
2020-10-06 23:26:11 +00:00
|
|
|
if let cachedViewModel = cachedViewModel as? StatusViewModel {
|
2020-10-05 07:50:59 +00:00
|
|
|
viewModel = cachedViewModel
|
|
|
|
} else {
|
2020-10-07 21:06:26 +00:00
|
|
|
viewModel = .init(
|
|
|
|
statusService: collectionService.navigationService.statusService(status: status),
|
|
|
|
identification: identification)
|
2020-10-05 07:50:59 +00:00
|
|
|
cache(viewModel: viewModel, forItem: item)
|
|
|
|
}
|
2020-09-15 01:39:35 +00:00
|
|
|
|
2020-10-06 00:33:58 +00:00
|
|
|
viewModel.configuration = configuration
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
return viewModel
|
|
|
|
case let .loadMore(loadMore):
|
2020-10-06 23:26:11 +00:00
|
|
|
if let cachedViewModel = cachedViewModel {
|
2020-10-05 07:50:59 +00:00
|
|
|
return cachedViewModel
|
|
|
|
}
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
let viewModel = LoadMoreViewModel(
|
|
|
|
loadMoreService: collectionService.navigationService.loadMoreService(loadMore: loadMore))
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
cache(viewModel: viewModel, forItem: item)
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
return viewModel
|
|
|
|
case let .account(account):
|
2020-10-06 23:26:11 +00:00
|
|
|
if let cachedViewModel = cachedViewModel {
|
2020-10-05 07:50:59 +00:00
|
|
|
return cachedViewModel
|
|
|
|
}
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
let viewModel = AccountViewModel(
|
|
|
|
accountService: collectionService.navigationService.accountService(account: account))
|
2020-10-05 06:36:22 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
cache(viewModel: viewModel, forItem: item)
|
2020-10-05 06:36:22 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
return viewModel
|
2020-10-05 06:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-07 21:06:26 +00:00
|
|
|
|
|
|
|
public func toggleShowMoreForAll() {
|
|
|
|
let statusIds = Set(items.value.reduce([], +).compactMap { item -> Status.Id? in
|
|
|
|
guard case let .status(status, _) = item else { return nil }
|
|
|
|
|
|
|
|
return status.id
|
|
|
|
})
|
|
|
|
|
|
|
|
switch showMoreForAllSubject.value {
|
|
|
|
case .hidden:
|
|
|
|
break
|
|
|
|
case .showMore:
|
|
|
|
(collectionService as? ContextService)?.showMore(ids: statusIds)
|
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.collect()
|
|
|
|
.sink { [weak self] _ in self?.showMoreForAllSubject.send(.showLess) }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
case .showLess:
|
|
|
|
(collectionService as? ContextService)?.showLess(ids: statusIds)
|
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.collect()
|
|
|
|
.sink { [weak self] _ in self?.showMoreForAllSubject.send(.showMore) }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 07:50:59 +00:00
|
|
|
}
|
2020-10-05 06:36:22 +00:00
|
|
|
|
2020-10-05 20:06:50 +00:00
|
|
|
private extension CollectionItemsViewModel {
|
2020-10-05 06:36:22 +00:00
|
|
|
func cache(viewModel: CollectionItemViewModel, forItem item: CollectionItem) {
|
2020-10-07 00:31:29 +00:00
|
|
|
viewModelCache[item] = (viewModel, viewModel.events.flatMap { $0 }
|
2020-10-05 06:36:22 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-10-07 00:31:29 +00:00
|
|
|
.sink { [weak self] in self?.eventsSubject.send($0) })
|
2020-10-05 06:36:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
func process(items: [[CollectionItem]]) {
|
2020-10-07 21:06:26 +00:00
|
|
|
maintainScrollPosition = identifierForScrollPositionMaintenance(newItems: items)
|
2020-10-05 07:50:59 +00:00
|
|
|
self.items.send(items)
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
let itemsSet = Set(items.reduce([], +))
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
viewModelCache = viewModelCache.filter { itemsSet.contains($0.key) }
|
2020-08-24 02:50:54 +00:00
|
|
|
}
|
2020-09-02 09:07:09 +00:00
|
|
|
|
2020-10-06 06:40:06 +00:00
|
|
|
func identifierForScrollPositionMaintenance(newItems: [[CollectionItem]]) -> CollectionItemIdentifier? {
|
|
|
|
let flatNewItems = newItems.reduce([], +)
|
2020-10-07 21:06:26 +00:00
|
|
|
|
2020-10-05 23:44:15 +00:00
|
|
|
if collectionService is ContextService,
|
|
|
|
items.value.isEmpty || items.value.map(\.count) == [0, 1, 0],
|
2020-10-06 06:40:06 +00:00
|
|
|
let contextParent = flatNewItems.first(where: {
|
2020-10-05 23:54:45 +00:00
|
|
|
guard case let .status(_, configuration) = $0 else { return false }
|
2020-10-05 23:44:15 +00:00
|
|
|
|
2020-10-06 06:40:06 +00:00
|
|
|
return configuration.isContextParent // Maintain scroll position of parent after initial load of context
|
2020-10-05 23:44:15 +00:00
|
|
|
}) {
|
2020-10-06 06:40:06 +00:00
|
|
|
return .init(item: contextParent)
|
|
|
|
} else if collectionService is TimelineService {
|
|
|
|
let flatItems = items.value.reduce([], +)
|
|
|
|
let difference = flatNewItems.difference(from: flatItems)
|
|
|
|
|
|
|
|
if let lastSelectedLoadMore = lastSelectedLoadMore {
|
|
|
|
for removal in difference.removals {
|
|
|
|
if case let .remove(_, item, _) = removal,
|
|
|
|
case let .loadMore(loadMore) = item,
|
|
|
|
loadMore == lastSelectedLoadMore,
|
2020-10-06 23:26:11 +00:00
|
|
|
let direction = (viewModelCache[item]?.viewModel as? LoadMoreViewModel)?.direction,
|
2020-10-06 06:40:06 +00:00
|
|
|
direction == .up,
|
|
|
|
let statusAfterLoadMore = flatItems.first(where: {
|
|
|
|
guard case let .status(status, _) = $0 else { return false }
|
|
|
|
|
|
|
|
return status.id == loadMore.beforeStatusId
|
|
|
|
}) {
|
|
|
|
return .init(item: statusAfterLoadMore)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 23:12:11 +00:00
|
|
|
|
|
|
|
if items.value.count > topVisibleIndexPath.section,
|
|
|
|
items.value[topVisibleIndexPath.section].count > topVisibleIndexPath.item {
|
|
|
|
let topVisibleItem = items.value[topVisibleIndexPath.section][topVisibleIndexPath.item]
|
|
|
|
|
|
|
|
if newItems.count > topVisibleIndexPath.section,
|
|
|
|
let newIndex = newItems[topVisibleIndexPath.section].firstIndex(of: topVisibleItem),
|
|
|
|
newIndex > topVisibleIndexPath.item {
|
|
|
|
return .init(item: topVisibleItem)
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 03:19:14 +00:00
|
|
|
}
|
2020-10-06 06:40:06 +00:00
|
|
|
|
|
|
|
return nil
|
2020-09-02 09:07:09 +00:00
|
|
|
}
|
2020-08-21 02:29:01 +00:00
|
|
|
}
|