metatext/ViewModels/Sources/ViewModels/CollectionItemsViewModel.swift

169 lines
6.7 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-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?
public private(set) var maintainScrollPositionOfItem: CollectionItemIdentifier?
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
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-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-05 07:50:59 +00:00
public var sections: AnyPublisher<[[CollectionItemIdentifier]], Never> {
items.map { $0.map { $0.map(CollectionItemIdentifier.init(item:)) } }.eraseToAnyPublisher()
}
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-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-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-09-23 01:00:56 +00:00
navigationEventsSubject.send(
.collectionNavigation(
2020-10-05 20:06:50 +00:00
CollectionItemsViewModel(
2020-10-05 06:36:22 +00:00
collectionService: collectionService
2020-09-25 05:39:06 +00:00
.navigationService
2020-10-05 23:54:45 +00:00
.contextService(id: status.displayStatus.id))))
2020-10-04 08:39:54 +00:00
case .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):
navigationEventsSubject.send(
.profileNavigation(
ProfileViewModel(
profileService: collectionService.navigationService.profileService(account: account))))
2020-09-23 01:00:56 +00:00
}
}
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-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
if let cachedViewModel = viewModelCache[item]?.0 as? StatusViewModel {
viewModel = cachedViewModel
} else {
2020-10-05 23:54:45 +00:00
viewModel = .init(statusService: collectionService.navigationService.statusService(status: status))
2020-10-05 07:50:59 +00:00
cache(viewModel: viewModel, forItem: item)
}
2020-09-15 01:39:35 +00:00
2020-10-05 23:44:15 +00:00
viewModel.isContextParent = configuration.isContextParent
viewModel.isPinned = configuration.isPinned
2020-10-05 07:50:59 +00:00
viewModel.isReplyInContext = configuration.isReplyInContext
viewModel.hasReplyFollowing = configuration.hasReplyFollowing
2020-10-04 08:39:54 +00:00
2020-10-05 07:50:59 +00:00
return viewModel
case let .loadMore(loadMore):
if let cachedViewModel = viewModelCache[item]?.0 as? LoadMoreViewModel {
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):
if let cachedViewModel = viewModelCache[item]?.0 as? AccountViewModel {
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-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) {
viewModelCache[item] = (viewModel, viewModel.events.flatMap { $0.compactMap(NavigationEvent.init) }
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { [weak self] in self?.navigationEventsSubject.send($0) })
}
2020-10-05 07:50:59 +00:00
func process(items: [[CollectionItem]]) {
determineIfScrollPositionShouldBeMaintained(newItems: items)
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-05 07:50:59 +00:00
func determineIfScrollPositionShouldBeMaintained(newItems: [[CollectionItem]]) {
maintainScrollPositionOfItem = nil // clear old value
2020-09-02 09:07:09 +00:00
// Maintain scroll position of parent after initial load of context
2020-10-05 23:44:15 +00:00
if collectionService is ContextService,
items.value.isEmpty || items.value.map(\.count) == [0, 1, 0],
let contextParent = newItems.reduce([], +).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
return configuration.isContextParent
}) {
maintainScrollPositionOfItem = .init(item: contextParent)
}
2020-09-02 09:07:09 +00:00
}
2020-08-21 02:29:01 +00:00
}