Improve pagination

This commit is contained in:
Justin Mazzocchi 2021-01-15 21:54:38 -08:00
parent 418d7f354a
commit 76f6f7e86e
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
1 changed files with 14 additions and 8 deletions

View File

@ -88,6 +88,8 @@ class TableViewController: UITableViewController {
heightCache[item] = cell.frame.height
cellHeightCaches[tableView.frame.width] = heightCache
paginateIfLastIndexPathPresent(indexPaths: [indexPath])
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
@ -115,14 +117,7 @@ class TableViewController: UITableViewController {
extension TableViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView, prefetchRowsAt 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
else { return }
viewModel.request(maxId: maxId, minId: nil)
paginateIfLastIndexPathPresent(indexPaths: indexPaths)
}
}
@ -436,5 +431,16 @@ private extension TableViewController {
present(activityViewController, animated: true, completion: nil)
}
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
else { return }
viewModel.request(maxId: maxId, minId: nil)
}
}
// swiftlint:enable file_length