Fix pagination issues

This commit is contained in:
Justin Mazzocchi 2021-01-16 11:41:01 -08:00
parent 8831a6c8e6
commit 7bc3ce0de3
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
6 changed files with 22 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import Mastodon
public protocol CollectionService { public protocol CollectionService {
var sections: AnyPublisher<[[CollectionItem]], Error> { get } var sections: AnyPublisher<[[CollectionItem]], Error> { get }
var nextPageMaxId: AnyPublisher<String, Never> { get } var nextPageMaxId: AnyPublisher<String, Never> { get }
var preferLastPresentIdOverNextPageMaxId: Bool { get }
var title: AnyPublisher<String, Never> { get } var title: AnyPublisher<String, Never> { get }
var titleLocalizationComponents: AnyPublisher<[String], Never> { get } var titleLocalizationComponents: AnyPublisher<[String], Never> { get }
var navigationService: NavigationService { get } var navigationService: NavigationService { get }
@ -16,6 +17,8 @@ public protocol CollectionService {
extension CollectionService { extension CollectionService {
public var nextPageMaxId: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() } public var nextPageMaxId: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
public var preferLastPresentIdOverNextPageMaxId: Bool { false }
public var title: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() } public var title: AnyPublisher<String, Never> { Empty().eraseToAnyPublisher() }
public var titleLocalizationComponents: AnyPublisher<[String], Never> { Empty().eraseToAnyPublisher() } public var titleLocalizationComponents: AnyPublisher<[String], Never> { Empty().eraseToAnyPublisher() }

View File

@ -10,6 +10,7 @@ public struct TimelineService {
public let sections: AnyPublisher<[[CollectionItem]], Error> public let sections: AnyPublisher<[[CollectionItem]], Error>
public let navigationService: NavigationService public let navigationService: NavigationService
public let nextPageMaxId: AnyPublisher<String, Never> public let nextPageMaxId: AnyPublisher<String, Never>
public let preferLastPresentIdOverNextPageMaxId = true
public let title: AnyPublisher<String, Never> public let title: AnyPublisher<String, Never>
private let timeline: Timeline private let timeline: Timeline

View File

@ -16,6 +16,7 @@ class TableViewController: UITableViewController {
private let identification: Identification private let identification: Identification
private let loadingTableFooterView = LoadingTableFooterView() private let loadingTableFooterView = LoadingTableFooterView()
private let webfingerIndicatorView = WebfingerIndicatorView() private let webfingerIndicatorView = WebfingerIndicatorView()
@Published private var loading = false
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private var cellHeightCaches = [CGFloat: [CollectionItem: CGFloat]]() private var cellHeightCaches = [CGFloat: [CollectionItem: CGFloat]]()
private var shouldKeepPlayingVideoAfterDismissal = false private var shouldKeepPlayingVideoAfterDismissal = false
@ -234,7 +235,9 @@ private extension TableViewController {
.sink { [weak self] in self?.set(expandAllState: $0) } .sink { [weak self] in self?.set(expandAllState: $0) }
.store(in: &cancellables) .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 } guard let self = self else { return }
self.tableView.tableFooterView = $0 ? self.loadingTableFooterView : UIView() self.tableView.tableFooterView = $0 ? self.loadingTableFooterView : UIView()
@ -244,7 +247,6 @@ private extension TableViewController {
viewModel.alertItems viewModel.alertItems
.compactMap { $0 } .compactMap { $0 }
.handleEvents(receiveOutput: { print($0.error) })
.sink { [weak self] in self?.present(alertItem: $0) } .sink { [weak self] in self?.present(alertItem: $0) }
.store(in: &cancellables) .store(in: &cancellables)
@ -433,11 +435,13 @@ private extension TableViewController {
} }
func paginateIfLastIndexPathPresent(indexPaths: [IndexPath]) { func paginateIfLastIndexPathPresent(indexPaths: [IndexPath]) {
guard guard !loading,
let maxId = viewModel.nextPageMaxId, let indexPath = indexPaths.last,
let indexPath = indexPaths.last, indexPath.section == dataSource.numberOfSections(in: tableView) - 1,
indexPath.section == dataSource.numberOfSections(in: tableView) - 1, indexPath.row == dataSource.tableView(tableView, numberOfRowsInSection: indexPath.section) - 1,
indexPath.row == dataSource.tableView(tableView, numberOfRowsInSection: indexPath.section) - 1 let maxId = viewModel.preferLastPresentIdOverNextPageMaxId
? dataSource.itemIdentifier(for: indexPath)?.itemId
: viewModel.nextPageMaxId
else { return } else { return }
viewModel.request(maxId: maxId, minId: nil) viewModel.request(maxId: maxId, minId: nil)

View File

@ -82,6 +82,8 @@ extension CollectionItemsViewModel: CollectionViewModel {
public var shouldAdjustContentInset: Bool { collectionService is ContextService } public var shouldAdjustContentInset: Bool { collectionService is ContextService }
public var preferLastPresentIdOverNextPageMaxId: Bool { collectionService.preferLastPresentIdOverNextPageMaxId }
public func request(maxId: String? = nil, minId: String? = nil) { public func request(maxId: String? = nil, minId: String? = nil) {
let publisher: AnyPublisher<Never, Error> let publisher: AnyPublisher<Never, Error>

View File

@ -13,6 +13,7 @@ public protocol CollectionViewModel {
var events: AnyPublisher<CollectionItemEvent, Never> { get } var events: AnyPublisher<CollectionItemEvent, Never> { get }
var shouldAdjustContentInset: Bool { get } var shouldAdjustContentInset: Bool { get }
var nextPageMaxId: String? { get } var nextPageMaxId: String? { get }
var preferLastPresentIdOverNextPageMaxId: Bool { get }
func request(maxId: String?, minId: String?) func request(maxId: String?, minId: String?)
func viewedAtTop(indexPath: IndexPath) func viewedAtTop(indexPath: IndexPath)
func select(indexPath: IndexPath) func select(indexPath: IndexPath)

View File

@ -103,6 +103,10 @@ extension ProfileViewModel: CollectionViewModel {
collectionViewModel.value.nextPageMaxId collectionViewModel.value.nextPageMaxId
} }
public var preferLastPresentIdOverNextPageMaxId: Bool {
collectionViewModel.value.preferLastPresentIdOverNextPageMaxId
}
public func request(maxId: String?, minId: String?) { public func request(maxId: String?, minId: String?) {
if case .statuses = collection, maxId == nil { if case .statuses = collection, maxId == nil {
profileService.fetchPinnedStatuses() profileService.fetchPinnedStatuses()