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
|
|
|
|
2021-01-25 07:42:39 +00:00
|
|
|
// swiftlint:disable file_length
|
2021-01-23 03:48:33 +00:00
|
|
|
public class CollectionItemsViewModel: ObservableObject {
|
2021-01-26 00:45:18 +00:00
|
|
|
public let identityContext: IdentityContext
|
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
|
|
|
|
2021-01-31 00:38:56 +00:00
|
|
|
@Published private var lastUpdate = CollectionUpdate.empty
|
2020-10-05 06:36:22 +00:00
|
|
|
private let collectionService: CollectionService
|
2021-02-04 22:24:27 +00:00
|
|
|
private var viewModelCache = [CollectionItem: Any]()
|
|
|
|
private let eventsSubject = PassthroughSubject<AnyPublisher<CollectionItemEvent, Error>, 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-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
|
2021-02-08 19:07:59 +00:00
|
|
|
private var markerScrollPositionItemId: CollectionItem.Id?
|
2020-08-18 05:13:37 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-01-26 00:06:35 +00:00
|
|
|
public init(collectionService: CollectionService, identityContext: IdentityContext) {
|
2020-10-05 06:36:22 +00:00
|
|
|
self.collectionService = collectionService
|
2021-01-26 00:06:35 +00:00
|
|
|
self.identityContext = identityContext
|
2020-10-14 00:03:01 +00:00
|
|
|
expandAllSubject = CurrentValueSubject(
|
2021-01-26 00:06:35 +00:00
|
|
|
collectionService is ContextService && !identityContext.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
|
2021-01-23 06:15:52 +00:00
|
|
|
.handleEvents(receiveOutput: { [weak self] in self?.process(sections: $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
|
|
|
|
2021-02-08 01:46:51 +00:00
|
|
|
collectionService.accountIdsForRelationships
|
|
|
|
.filter { !$0.isEmpty }
|
|
|
|
.flatMap(identityContext.service.requestRelationships(ids:))
|
|
|
|
.catch { _ in Empty().setFailureType(to: Never.self) }
|
|
|
|
.sink { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-02-08 20:33:51 +00:00
|
|
|
if let timeline = collectionService.positionTimeline {
|
|
|
|
if identityContext.appPreferences.positionBehavior(timeline: timeline) == .localRememberPosition {
|
|
|
|
markerScrollPositionItemId = identityContext.service.getLocalLastReadId(timeline: timeline)
|
2021-02-08 19:07:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 03:01:12 +00:00
|
|
|
lastReadId.compactMap { $0 }
|
|
|
|
.removeDuplicates()
|
2021-01-17 22:52:49 +00:00
|
|
|
.debounce(for: .seconds(Self.lastReadIdDebounceInterval), scheduler: DispatchQueue.global())
|
2021-02-08 20:33:51 +00:00
|
|
|
.flatMap { identityContext.service.setLocalLastReadId($0, timeline: timeline) }
|
2020-10-27 03:01:12 +00:00
|
|
|
.sink { _ in } receiveValue: { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
2020-09-18 00:16:41 +00:00
|
|
|
|
2020-10-07 21:06:26 +00:00
|
|
|
public var updates: AnyPublisher<CollectionUpdate, Never> {
|
2021-01-17 02:16:43 +00:00
|
|
|
$lastUpdate.eraseToAnyPublisher()
|
2020-10-05 07:50:59 +00:00
|
|
|
}
|
2021-01-25 02:10:41 +00:00
|
|
|
|
|
|
|
public func requestNextPage(fromIndexPath indexPath: IndexPath) {
|
|
|
|
guard let maxId = collectionService.preferLastPresentIdOverNextPageMaxId
|
|
|
|
? lastUpdate.sections[indexPath.section].items[indexPath.item].itemId
|
|
|
|
: nextPageMaxId
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
request(maxId: maxId, minId: nil, search: nil)
|
|
|
|
}
|
2021-01-24 22:14:35 +00:00
|
|
|
}
|
2020-09-27 01:23:56 +00:00
|
|
|
|
2021-01-24 22:14:35 +00:00
|
|
|
extension CollectionItemsViewModel: CollectionViewModel {
|
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() }
|
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
public var events: AnyPublisher<CollectionItemEvent, Never> {
|
|
|
|
eventsSubject.flatMap { [weak self] eventPublisher -> AnyPublisher<CollectionItemEvent, Never> in
|
|
|
|
guard let self = self else { return Empty().eraseToAnyPublisher() }
|
|
|
|
|
|
|
|
return eventPublisher.assignErrorsToAlertItem(to: \.alertItem, on: self).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-10-01 02:35:06 +00:00
|
|
|
|
2021-01-16 20:06:35 +00:00
|
|
|
public var canRefresh: Bool { collectionService.canRefresh }
|
|
|
|
|
2021-01-23 03:48:33 +00:00
|
|
|
public func request(maxId: String? = nil, minId: String? = nil, search: Search?) {
|
2021-02-08 19:07:59 +00:00
|
|
|
collectionService.request(maxId: realMaxId(maxId: maxId), minId: minId, search: search)
|
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) {
|
2021-01-23 06:15:52 +00:00
|
|
|
let item = lastUpdate.sections[indexPath.section].items[indexPath.item]
|
2020-09-23 01:00:56 +00:00
|
|
|
|
2020-10-05 06:36:22 +00:00
|
|
|
switch item {
|
2021-02-08 05:24:06 +00:00
|
|
|
case let .status(status, _, _):
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .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()
|
2021-02-08 01:46:51 +00:00
|
|
|
case let .account(account, _, relationship):
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .navigation(.profile(collectionService
|
|
|
|
.navigationService
|
2021-02-08 05:24:06 +00:00
|
|
|
.profileService(account: account, relationship: relationship))))
|
2020-10-30 07:11:24 +00:00
|
|
|
case let .notification(notification, _):
|
|
|
|
if let status = notification.status {
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .navigation(.collection(collectionService
|
|
|
|
.navigationService
|
|
|
|
.contextService(id: status.displayStatus.id))))
|
2020-10-30 07:11:24 +00:00
|
|
|
} else {
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .navigation(.profile(collectionService
|
|
|
|
.navigationService
|
|
|
|
.profileService(account: notification.account))))
|
2020-10-30 07:11:24 +00:00
|
|
|
}
|
2020-10-29 06:03:45 +00:00
|
|
|
case let .conversation(conversation):
|
|
|
|
guard let status = conversation.lastStatus else { break }
|
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .navigation(.collection(collectionService
|
|
|
|
.navigationService
|
|
|
|
.contextService(id: status.displayStatus.id))))
|
2021-01-24 03:12:30 +00:00
|
|
|
case let .tag(tag):
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .navigation(.collection(collectionService
|
|
|
|
.navigationService
|
|
|
|
.timelineService(timeline: .tag(tag.name)))))
|
2021-01-25 07:42:39 +00:00
|
|
|
case let .moreResults(moreResults):
|
2021-02-04 22:24:27 +00:00
|
|
|
send(event: .navigation(.searchScope(moreResults.scope)))
|
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
|
|
|
|
2021-02-08 19:07:59 +00:00
|
|
|
if lastUpdate.sections.count > indexPath.section,
|
2021-01-23 06:15:52 +00:00
|
|
|
lastUpdate.sections[indexPath.section].items.count > indexPath.item {
|
|
|
|
lastReadId.send(lastUpdate.sections[indexPath.section].items[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 {
|
2021-01-23 06:15:52 +00:00
|
|
|
switch lastUpdate.sections[indexPath.section].items[indexPath.item] {
|
2021-02-08 01:46:51 +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
|
2021-02-04 22:24:27 +00:00
|
|
|
public func viewModel(indexPath: IndexPath) -> Any {
|
2021-01-23 06:15:52 +00:00
|
|
|
let item = lastUpdate.sections[indexPath.section].items[indexPath.item]
|
2021-02-04 22:24:27 +00:00
|
|
|
let cachedViewModel = viewModelCache[item]
|
2020-09-15 01:39:35 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
switch item {
|
2021-02-08 01:46:51 +00:00
|
|
|
case let .status(status, configuration, relationship):
|
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),
|
2021-02-04 22:24:27 +00:00
|
|
|
identityContext: identityContext,
|
|
|
|
eventsSubject: eventsSubject)
|
|
|
|
viewModelCache[item] = viewModel
|
2020-10-05 07:50:59 +00:00
|
|
|
}
|
2020-09-15 01:39:35 +00:00
|
|
|
|
2020-10-06 00:33:58 +00:00
|
|
|
viewModel.configuration = configuration
|
2021-02-08 01:46:51 +00:00
|
|
|
viewModel.accountViewModel.relationship = relationship
|
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(
|
2021-02-04 22:24:27 +00:00
|
|
|
loadMoreService: collectionService.navigationService.loadMoreService(loadMore: loadMore),
|
|
|
|
eventsSubject: eventsSubject)
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
viewModelCache[item] = viewModel
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2020-10-05 07:50:59 +00:00
|
|
|
return viewModel
|
2021-02-08 01:46:51 +00:00
|
|
|
case let .account(account, configuration, relationship):
|
2021-01-26 06:57:44 +00:00
|
|
|
let viewModel: AccountViewModel
|
2020-10-04 08:39:54 +00:00
|
|
|
|
2021-01-26 06:57:44 +00:00
|
|
|
if let cachedViewModel = cachedViewModel as? AccountViewModel {
|
|
|
|
viewModel = cachedViewModel
|
|
|
|
} else {
|
|
|
|
viewModel = AccountViewModel(
|
|
|
|
accountService: collectionService.navigationService.accountService(account: account),
|
2021-02-04 22:24:27 +00:00
|
|
|
identityContext: identityContext,
|
|
|
|
eventsSubject: eventsSubject)
|
|
|
|
viewModelCache[item] = viewModel
|
2021-01-26 06:57:44 +00:00
|
|
|
}
|
2020-10-05 06:36:22 +00:00
|
|
|
|
2021-01-26 06:57:44 +00:00
|
|
|
viewModel.configuration = configuration
|
2021-02-08 01:46:51 +00:00
|
|
|
viewModel.relationship = relationship
|
2020-10-05 06:36:22 +00:00
|
|
|
|
2020-10-30 07:11:24 +00:00
|
|
|
return viewModel
|
|
|
|
case let .notification(notification, statusConfiguration):
|
2021-02-04 22:24:27 +00:00
|
|
|
let viewModel: Any
|
2020-10-30 07:11:24 +00:00
|
|
|
|
|
|
|
if let cachedViewModel = cachedViewModel {
|
|
|
|
viewModel = cachedViewModel
|
|
|
|
} else if let status = notification.status, let statusConfiguration = statusConfiguration {
|
|
|
|
let statusViewModel = StatusViewModel(
|
|
|
|
statusService: collectionService.navigationService.statusService(status: status),
|
2021-02-04 22:24:27 +00:00
|
|
|
identityContext: identityContext,
|
|
|
|
eventsSubject: eventsSubject)
|
2020-10-30 07:11:24 +00:00
|
|
|
statusViewModel.configuration = statusConfiguration
|
|
|
|
viewModel = statusViewModel
|
2021-02-04 22:24:27 +00:00
|
|
|
viewModelCache[item] = viewModel
|
2020-10-30 07:11:24 +00:00
|
|
|
} else {
|
|
|
|
viewModel = NotificationViewModel(
|
|
|
|
notificationService: collectionService.navigationService.notificationService(
|
|
|
|
notification: notification),
|
2021-02-04 22:24:27 +00:00
|
|
|
identityContext: identityContext,
|
|
|
|
eventsSubject: eventsSubject)
|
|
|
|
viewModelCache[item] = viewModel
|
2020-10-30 07:11:24 +00:00
|
|
|
}
|
|
|
|
|
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),
|
2021-01-26 00:06:35 +00:00
|
|
|
identityContext: identityContext)
|
2020-10-29 06:03:45 +00:00
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
viewModelCache[item] = viewModel
|
2020-10-29 06:03:45 +00:00
|
|
|
|
2021-01-24 03:12:30 +00:00
|
|
|
return viewModel
|
|
|
|
case let .tag(tag):
|
|
|
|
if let cachedViewModel = cachedViewModel {
|
|
|
|
return cachedViewModel
|
|
|
|
}
|
|
|
|
|
2021-02-02 03:39:32 +00:00
|
|
|
let viewModel = TagViewModel(tag: tag, identityContext: identityContext)
|
2021-01-24 03:12:30 +00:00
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
viewModelCache[item] = viewModel
|
2021-01-24 03:12:30 +00:00
|
|
|
|
2021-01-25 07:42:39 +00:00
|
|
|
return viewModel
|
|
|
|
case let .moreResults(moreResults):
|
|
|
|
if let cachedViewModel = cachedViewModel {
|
|
|
|
return cachedViewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
let viewModel = MoreResultsViewModel(moreResults: moreResults)
|
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
viewModelCache[item] = viewModel
|
2021-01-25 07:42:39 +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
|
|
|
|
2020-10-14 00:03:01 +00:00
|
|
|
public func toggleExpandAll() {
|
2021-01-23 06:15:52 +00:00
|
|
|
let statusIds = Set(lastUpdate.sections.map(\.items).reduce([], +).compactMap { item -> Status.Id? in
|
2021-02-08 01:46:51 +00:00
|
|
|
guard case let .status(status, _, _) = item else { return nil }
|
2020-10-07 21:06:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 00:15:52 +00:00
|
|
|
|
|
|
|
public func applyAccountListEdit(viewModel: AccountViewModel, edit: CollectionItemEvent.AccountListEdit) {
|
|
|
|
(collectionService as? AccountListService)?.remove(id: viewModel.id)
|
2021-02-08 04:38:06 +00:00
|
|
|
.sink { _ in } receiveValue: { _ in }
|
|
|
|
.store(in: &cancellables)
|
2021-01-27 00:15:52 +00:00
|
|
|
|
|
|
|
switch edit {
|
|
|
|
case .acceptFollowRequest, .rejectFollowRequest:
|
|
|
|
identityContext.service.verifyCredentials()
|
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
|
|
.sink { _ in }
|
|
|
|
.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 {
|
2021-01-17 22:52:49 +00:00
|
|
|
private static let lastReadIdDebounceInterval: TimeInterval = 0.5
|
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
func send(event: CollectionItemEvent) {
|
|
|
|
eventsSubject.send(Just(event).setFailureType(to: Error.self).eraseToAnyPublisher())
|
2021-01-17 02:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 22:24:27 +00:00
|
|
|
var lastUpdateWasContextParentOnly: Bool {
|
|
|
|
collectionService is ContextService && lastUpdate.sections.map(\.items).map(\.count) == [0, 1, 0]
|
2020-10-05 06:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 06:15:52 +00:00
|
|
|
func process(sections: [CollectionSection]) {
|
|
|
|
let items = sections.map(\.items).reduce([], +)
|
|
|
|
let itemsSet = Set(items)
|
2020-08-24 02:50:54 +00:00
|
|
|
|
2021-01-17 02:16:43 +00:00
|
|
|
self.lastUpdate = .init(
|
2021-01-23 06:15:52 +00:00
|
|
|
sections: sections,
|
|
|
|
maintainScrollPositionItemId: idForScrollPositionMaintenance(newSections: sections),
|
|
|
|
shouldAdjustContentInset: lastUpdateWasContextParentOnly && items.count > 1)
|
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 }
|
|
|
|
|
2021-02-08 20:33:51 +00:00
|
|
|
guard let timeline = collectionService.positionTimeline,
|
|
|
|
identityContext.appPreferences.positionBehavior(timeline: timeline) == .localRememberPosition,
|
2021-01-23 06:15:52 +00:00
|
|
|
let lastItemId = lastUpdate.sections.last?.items.last?.itemId
|
2020-12-01 03:07:38 +00:00
|
|
|
else { return maxId }
|
|
|
|
|
|
|
|
return min(maxId, lastItemId)
|
|
|
|
}
|
|
|
|
|
2021-01-23 06:15:52 +00:00
|
|
|
func idForScrollPositionMaintenance(newSections: [CollectionSection]) -> CollectionItem.Id? {
|
|
|
|
let items = lastUpdate.sections.map(\.items).reduce([], +)
|
|
|
|
let newItems = newSections.map(\.items).reduce([], +)
|
2020-10-07 21:06:26 +00:00
|
|
|
|
2021-02-08 19:07:59 +00:00
|
|
|
if let itemId = markerScrollPositionItemId,
|
|
|
|
newItems.contains(where: { $0.itemId == itemId }) {
|
|
|
|
markerScrollPositionItemId = nil
|
2020-10-27 03:01:12 +00:00
|
|
|
|
2021-02-08 19:07:59 +00:00
|
|
|
return itemId
|
2020-10-27 03:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 23:44:15 +00:00
|
|
|
if collectionService is ContextService,
|
2021-01-23 06:15:52 +00:00
|
|
|
lastUpdate.sections.isEmpty || lastUpdate.sections.map(\.items.count) == [0, 1, 0],
|
|
|
|
let contextParent = newItems.first(where: {
|
2021-02-08 01:46:51 +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 {
|
2021-01-23 06:15:52 +00:00
|
|
|
let difference = newItems.difference(from: items)
|
2020-10-06 06:40:06 +00:00
|
|
|
|
|
|
|
if let lastSelectedLoadMore = lastSelectedLoadMore {
|
|
|
|
for removal in difference.removals {
|
|
|
|
if case let .remove(_, item, _) = removal,
|
|
|
|
case let .loadMore(loadMore) = item,
|
|
|
|
loadMore == lastSelectedLoadMore,
|
2021-02-04 22:24:27 +00:00
|
|
|
let direction = (viewModelCache[item] as? LoadMoreViewModel)?.direction,
|
2020-10-06 06:40:06 +00:00
|
|
|
direction == .up,
|
2021-01-23 06:15:52 +00:00
|
|
|
let statusAfterLoadMore = items.first(where: {
|
2021-02-08 01:46:51 +00:00
|
|
|
guard case let .status(status, _, _) = $0 else { return false }
|
2020-10-06 06:40:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-23 06:15:52 +00:00
|
|
|
if lastUpdate.sections.count > topVisibleIndexPath.section,
|
|
|
|
lastUpdate.sections[topVisibleIndexPath.section].items.count > topVisibleIndexPath.item {
|
|
|
|
let topVisibleItem = lastUpdate.sections[topVisibleIndexPath.section].items[topVisibleIndexPath.item]
|
2020-10-06 23:12:11 +00:00
|
|
|
|
2021-01-23 06:15:52 +00:00
|
|
|
if newSections.count > topVisibleIndexPath.section,
|
|
|
|
let newIndex = newSections[topVisibleIndexPath.section]
|
|
|
|
.items.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
|
|
|
}
|
2021-01-25 07:42:39 +00:00
|
|
|
// swiftlint:enable file_length
|