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-11-09 06:22:20 +00:00
|
|
|
public final 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-14 00:03:01 +00:00
|
|
|
private let expandAllSubject: CurrentValueSubject<ExpandAllState, Never>
|
2020-10-31 00:53:57 +00:00
|
|
|
private var maintainScrollPositionItemId: CollectionItem.Id?
|
2020-10-06 23:12:11 +00:00
|
|
|
private var topVisibleIndexPath = IndexPath(item: 0, section: 0)
|
2020-10-27 03:01:12 +00:00
|
|
|
private let lastReadId = CurrentValueSubject<String?, Never>(nil)
|
2020-10-06 06:40:06 +00:00
|
|
|
private var lastSelectedLoadMore: LoadMore?
|
2020-10-27 03:01:12 +00:00
|
|
|
private var hasRequestedUsingMarker = false
|
2020-11-03 04:24:56 +00:00
|
|
|
private var shouldRestorePositionOfLocalLastReadId = false
|
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-14 00:03:01 +00:00
|
|
|
expandAllSubject = CurrentValueSubject(
|
2020-10-07 21:06:26 +00:00
|
|
|
collectionService is ContextService && !identification.identity.preferences.readingExpandSpoilers
|
2020-10-14 00:03:01 +00:00
|
|
|
? .expand : .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-10-27 03:01:12 +00:00
|
|
|
|
|
|
|
if let markerTimeline = collectionService.markerTimeline {
|
2020-11-03 04:24:56 +00:00
|
|
|
shouldRestorePositionOfLocalLastReadId =
|
|
|
|
identification.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .rememberPosition
|
2020-10-27 03:01:12 +00:00
|
|
|
lastReadId.compactMap { $0 }
|
|
|
|
.removeDuplicates()
|
|
|
|
.debounce(for: 0.5, scheduler: DispatchQueue.global())
|
|
|
|
.flatMap { identification.service.setLastReadId($0, forMarker: markerTimeline) }
|
|
|
|
.sink { _ in } receiveValue: { _ in }
|
|
|
|
.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
|
2020-10-15 07:44:01 +00:00
|
|
|
CollectionUpdate(items: $0,
|
2020-10-31 00:53:57 +00:00
|
|
|
maintainScrollPositionItemId: self?.maintainScrollPositionItemId)
|
2020-10-07 21:06:26 +00:00
|
|
|
}
|
|
|
|
.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-12-03 01:41:22 +00:00
|
|
|
public var titleLocalizationComponents: AnyPublisher<[String], Never> {
|
|
|
|
collectionService.titleLocalizationComponents
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
public var expandAll: AnyPublisher<ExpandAllState, Never> {
|
|
|
|
expandAllSubject.eraseToAnyPublisher()
|
2020-10-07 21:06:26 +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() }
|
|
|
|
|
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-27 03:01:12 +00:00
|
|
|
public var shouldAdjustContentInset: Bool { collectionService is ContextService }
|
|
|
|
|
2021-01-16 19:41:01 +00:00
|
|
|
public var preferLastPresentIdOverNextPageMaxId: Bool { collectionService.preferLastPresentIdOverNextPageMaxId }
|
|
|
|
|
2021-01-16 20:06:35 +00:00
|
|
|
public var canRefresh: Bool { collectionService.canRefresh }
|
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
public func request(maxId: String? = nil, minId: String? = nil) {
|
2020-10-27 03:01:12 +00:00
|
|
|
let publisher: AnyPublisher<Never, Error>
|
|
|
|
|
|
|
|
if let markerTimeline = collectionService.markerTimeline,
|
|
|
|
identification.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .syncPosition,
|
|
|
|
!hasRequestedUsingMarker {
|
|
|
|
publisher = identification.service.getMarker(markerTimeline)
|
|
|
|
.flatMap { [weak self] in
|
|
|
|
self?.collectionService.request(maxId: $0.lastReadId, minId: nil) ?? Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.catch { [weak self] _ in
|
|
|
|
self?.collectionService.request(maxId: nil, minId: nil) ?? Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.collect()
|
|
|
|
.flatMap { [weak self] _ in
|
|
|
|
self?.collectionService.request(maxId: nil, minId: nil) ?? Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
self.hasRequestedUsingMarker = true
|
|
|
|
} else {
|
2020-12-01 03:07:38 +00:00
|
|
|
publisher = collectionService.request(maxId: realMaxId(maxId: maxId), minId: minId)
|
2020-10-27 03:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
publisher
|
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-10-30 07:11:24 +00:00
|
|
|
case let .notification(notification, _):
|
|
|
|
if let status = notification.status {
|
|
|
|
eventsSubject.send(
|
|
|
|
.navigation(.collection(collectionService
|
|
|
|
.navigationService
|
|
|
|
.contextService(id: status.displayStatus.id))))
|
|
|
|
} else {
|
|
|
|
eventsSubject.send(
|
|
|
|
.navigation(.profile(collectionService
|
|
|
|
.navigationService
|
|
|
|
.profileService(account: notification.account))))
|
|
|
|
}
|
2020-10-29 06:03:45 +00:00
|
|
|
case let .conversation(conversation):
|
|
|
|
guard let status = conversation.lastStatus else { break }
|
|
|
|
|
|
|
|
eventsSubject.send(
|
|
|
|
.navigation(.collection(collectionService
|
|
|
|
.navigationService
|
|
|
|
.contextService(id: status.displayStatus.id))))
|
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-27 03:01:12 +00:00
|
|
|
|
2020-11-03 04:24:56 +00:00
|
|
|
if !shouldRestorePositionOfLocalLastReadId,
|
|
|
|
items.value.count > indexPath.section,
|
|
|
|
items.value[indexPath.section].count > indexPath.item {
|
|
|
|
lastReadId.send(items.value[indexPath.section][indexPath.item].itemId)
|
2020-10-27 03:01:12 +00:00
|
|
|
}
|
2020-10-06 23:12:11 +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-29 06:03:45 +00:00
|
|
|
// swiftlint:disable:next function_body_length cyclomatic_complexity
|
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-25 02:31:44 +00:00
|
|
|
let viewModel: StatusViewModel
|
2020-10-05 07:50:59 +00:00
|
|
|
|
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(
|
2020-10-15 07:44:01 +00:00
|
|
|
accountService: collectionService.navigationService.accountService(account: account),
|
|
|
|
identification: identification)
|
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-30 07:11:24 +00:00
|
|
|
return viewModel
|
|
|
|
case let .notification(notification, statusConfiguration):
|
|
|
|
let viewModel: CollectionItemViewModel
|
|
|
|
|
|
|
|
if let cachedViewModel = cachedViewModel {
|
|
|
|
viewModel = cachedViewModel
|
|
|
|
} else if let status = notification.status, let statusConfiguration = statusConfiguration {
|
|
|
|
let statusViewModel = StatusViewModel(
|
|
|
|
statusService: collectionService.navigationService.statusService(status: status),
|
|
|
|
identification: identification)
|
|
|
|
statusViewModel.configuration = statusConfiguration
|
|
|
|
viewModel = statusViewModel
|
|
|
|
cache(viewModel: viewModel, forItem: item)
|
|
|
|
} else {
|
|
|
|
viewModel = NotificationViewModel(
|
|
|
|
notificationService: collectionService.navigationService.notificationService(
|
|
|
|
notification: notification),
|
|
|
|
identification: identification)
|
|
|
|
cache(viewModel: viewModel, forItem: item)
|
|
|
|
}
|
|
|
|
|
2020-10-29 06:03:45 +00:00
|
|
|
return viewModel
|
|
|
|
case let .conversation(conversation):
|
|
|
|
if let cachedViewModel = cachedViewModel {
|
|
|
|
return cachedViewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
let viewModel = ConversationViewModel(
|
|
|
|
conversationService: collectionService.navigationService.conversationService(
|
|
|
|
conversation: conversation),
|
|
|
|
identification: identification)
|
|
|
|
|
|
|
|
cache(viewModel: viewModel, forItem: item)
|
|
|
|
|
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
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
public func toggleExpandAll() {
|
2020-10-07 21:06:26 +00:00
|
|
|
let statusIds = Set(items.value.reduce([], +).compactMap { item -> Status.Id? in
|
|
|
|
guard case let .status(status, _) = item else { return nil }
|
|
|
|
|
|
|
|
return status.id
|
|
|
|
})
|
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
switch expandAllSubject.value {
|
2020-10-07 21:06:26 +00:00
|
|
|
case .hidden:
|
|
|
|
break
|
2020-10-14 00:03:01 +00:00
|
|
|
case .expand:
|
|
|
|
(collectionService as? ContextService)?.expand(ids: statusIds)
|
2020-10-07 21:06:26 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.collect()
|
2020-10-14 00:03:01 +00:00
|
|
|
.sink { [weak self] _ in self?.expandAllSubject.send(.collapse) }
|
2020-10-07 21:06:26 +00:00
|
|
|
.store(in: &cancellables)
|
2020-10-14 00:03:01 +00:00
|
|
|
case .collapse:
|
|
|
|
(collectionService as? ContextService)?.collapse(ids: statusIds)
|
2020-10-07 21:06:26 +00:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.collect()
|
2020-10-14 00:03:01 +00:00
|
|
|
.sink { [weak self] _ in self?.expandAllSubject.send(.expand) }
|
2020-10-07 21:06:26 +00:00
|
|
|
.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) {
|
2021-01-04 01:28:52 +00:00
|
|
|
viewModelCache[item] = (viewModel, viewModel.events
|
|
|
|
.flatMap { [weak self] events -> AnyPublisher<CollectionItemEvent, Never> in
|
|
|
|
guard let self = self else { return Empty().eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
return events.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
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-11-02 23:05:09 +00:00
|
|
|
maintainScrollPositionItemId = idForScrollPositionMaintenance(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-12-01 03:07:38 +00:00
|
|
|
func realMaxId(maxId: String?) -> String? {
|
|
|
|
guard let maxId = maxId else { return nil }
|
|
|
|
|
|
|
|
guard let markerTimeline = collectionService.markerTimeline,
|
|
|
|
identification.appPreferences.positionBehavior(markerTimeline: markerTimeline) == .rememberPosition,
|
|
|
|
let lastItemId = items.value.last?.last?.itemId
|
|
|
|
else { return maxId }
|
|
|
|
|
|
|
|
return min(maxId, lastItemId)
|
|
|
|
}
|
|
|
|
|
2020-11-02 23:05:09 +00:00
|
|
|
func idForScrollPositionMaintenance(newItems: [[CollectionItem]]) -> CollectionItem.Id? {
|
|
|
|
let flatItems = items.value.reduce([], +)
|
2020-10-06 06:40:06 +00:00
|
|
|
let flatNewItems = newItems.reduce([], +)
|
2020-10-07 21:06:26 +00:00
|
|
|
|
2020-11-03 04:24:56 +00:00
|
|
|
if shouldRestorePositionOfLocalLastReadId,
|
|
|
|
let markerTimeline = collectionService.markerTimeline,
|
2020-10-27 03:01:12 +00:00
|
|
|
let localLastReadId = identification.service.getLocalLastReadId(markerTimeline),
|
2020-11-03 04:24:56 +00:00
|
|
|
flatNewItems.contains(where: { $0.itemId == localLastReadId }) {
|
|
|
|
shouldRestorePositionOfLocalLastReadId = false
|
2020-10-27 03:01:12 +00:00
|
|
|
|
2020-11-02 23:05:09 +00:00
|
|
|
return localLastReadId
|
2020-10-27 03:01:12 +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-11-02 23:05:09 +00:00
|
|
|
return contextParent.itemId
|
2020-10-06 06:40:06 +00:00
|
|
|
} else if collectionService is TimelineService {
|
|
|
|
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
|
|
|
|
}) {
|
2020-11-02 23:05:09 +00:00
|
|
|
return statusAfterLoadMore.itemId
|
2020-10-06 06:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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,
|
2020-11-02 23:05:09 +00:00
|
|
|
let newIndex = newItems[topVisibleIndexPath.section]
|
|
|
|
.firstIndex(where: { $0.itemId == topVisibleItem.itemId }),
|
2020-10-06 23:12:11 +00:00
|
|
|
newIndex > topVisibleIndexPath.item {
|
2020-11-02 23:05:09 +00:00
|
|
|
return topVisibleItem.itemId
|
2020-10-06 23:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|