Fix pagination issues
This commit is contained in:
parent
8831a6c8e6
commit
7bc3ce0de3
|
@ -6,6 +6,7 @@ import Mastodon
|
|||
public protocol CollectionService {
|
||||
var sections: AnyPublisher<[[CollectionItem]], Error> { get }
|
||||
var nextPageMaxId: AnyPublisher<String, Never> { get }
|
||||
var preferLastPresentIdOverNextPageMaxId: Bool { get }
|
||||
var title: AnyPublisher<String, Never> { get }
|
||||
var titleLocalizationComponents: AnyPublisher<[String], Never> { get }
|
||||
var navigationService: NavigationService { get }
|
||||
|
@ -16,6 +17,8 @@ public protocol CollectionService {
|
|||
extension CollectionService {
|
||||
public var nextPageMaxId: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
|
||||
|
||||
public var preferLastPresentIdOverNextPageMaxId: Bool { false }
|
||||
|
||||
public var title: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
|
||||
|
||||
public var titleLocalizationComponents: AnyPublisher<[String], Never> { Empty().eraseToAnyPublisher() }
|
||||
|
|
|
@ -10,6 +10,7 @@ public struct TimelineService {
|
|||
public let sections: AnyPublisher<[[CollectionItem]], Error>
|
||||
public let navigationService: NavigationService
|
||||
public let nextPageMaxId: AnyPublisher<String, Never>
|
||||
public let preferLastPresentIdOverNextPageMaxId = true
|
||||
public let title: AnyPublisher<String, Never>
|
||||
|
||||
private let timeline: Timeline
|
||||
|
|
|
@ -16,6 +16,7 @@ class TableViewController: UITableViewController {
|
|||
private let identification: Identification
|
||||
private let loadingTableFooterView = LoadingTableFooterView()
|
||||
private let webfingerIndicatorView = WebfingerIndicatorView()
|
||||
@Published private var loading = false
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private var cellHeightCaches = [CGFloat: [CollectionItem: CGFloat]]()
|
||||
private var shouldKeepPlayingVideoAfterDismissal = false
|
||||
|
@ -234,7 +235,9 @@ private extension TableViewController {
|
|||
.sink { [weak self] in self?.set(expandAllState: $0) }
|
||||
.store(in: &cancellables)
|
||||
|
||||
viewModel.loading.receive(on: RunLoop.main).sink { [weak self] in
|
||||
viewModel.loading.receive(on: DispatchQueue.main).assign(to: &$loading)
|
||||
|
||||
$loading.sink { [weak self] in
|
||||
guard let self = self else { return }
|
||||
|
||||
self.tableView.tableFooterView = $0 ? self.loadingTableFooterView : UIView()
|
||||
|
@ -244,7 +247,6 @@ private extension TableViewController {
|
|||
|
||||
viewModel.alertItems
|
||||
.compactMap { $0 }
|
||||
.handleEvents(receiveOutput: { print($0.error) })
|
||||
.sink { [weak self] in self?.present(alertItem: $0) }
|
||||
.store(in: &cancellables)
|
||||
|
||||
|
@ -433,11 +435,13 @@ private extension TableViewController {
|
|||
}
|
||||
|
||||
func paginateIfLastIndexPathPresent(indexPaths: [IndexPath]) {
|
||||
guard
|
||||
let maxId = viewModel.nextPageMaxId,
|
||||
let indexPath = indexPaths.last,
|
||||
indexPath.section == dataSource.numberOfSections(in: tableView) - 1,
|
||||
indexPath.row == dataSource.tableView(tableView, numberOfRowsInSection: indexPath.section) - 1
|
||||
guard !loading,
|
||||
let indexPath = indexPaths.last,
|
||||
indexPath.section == dataSource.numberOfSections(in: tableView) - 1,
|
||||
indexPath.row == dataSource.tableView(tableView, numberOfRowsInSection: indexPath.section) - 1,
|
||||
let maxId = viewModel.preferLastPresentIdOverNextPageMaxId
|
||||
? dataSource.itemIdentifier(for: indexPath)?.itemId
|
||||
: viewModel.nextPageMaxId
|
||||
else { return }
|
||||
|
||||
viewModel.request(maxId: maxId, minId: nil)
|
||||
|
|
|
@ -82,6 +82,8 @@ extension CollectionItemsViewModel: CollectionViewModel {
|
|||
|
||||
public var shouldAdjustContentInset: Bool { collectionService is ContextService }
|
||||
|
||||
public var preferLastPresentIdOverNextPageMaxId: Bool { collectionService.preferLastPresentIdOverNextPageMaxId }
|
||||
|
||||
public func request(maxId: String? = nil, minId: String? = nil) {
|
||||
let publisher: AnyPublisher<Never, Error>
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ public protocol CollectionViewModel {
|
|||
var events: AnyPublisher<CollectionItemEvent, Never> { get }
|
||||
var shouldAdjustContentInset: Bool { get }
|
||||
var nextPageMaxId: String? { get }
|
||||
var preferLastPresentIdOverNextPageMaxId: Bool { get }
|
||||
func request(maxId: String?, minId: String?)
|
||||
func viewedAtTop(indexPath: IndexPath)
|
||||
func select(indexPath: IndexPath)
|
||||
|
|
|
@ -103,6 +103,10 @@ extension ProfileViewModel: CollectionViewModel {
|
|||
collectionViewModel.value.nextPageMaxId
|
||||
}
|
||||
|
||||
public var preferLastPresentIdOverNextPageMaxId: Bool {
|
||||
collectionViewModel.value.preferLastPresentIdOverNextPageMaxId
|
||||
}
|
||||
|
||||
public func request(maxId: String?, minId: String?) {
|
||||
if case .statuses = collection, maxId == nil {
|
||||
profileService.fetchPinnedStatuses()
|
||||
|
|
Loading…
Reference in New Issue