From 76f6f7e86e14d62325e6b34fd2b14521613909f6 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Fri, 15 Jan 2021 21:54:38 -0800 Subject: [PATCH] Improve pagination --- View Controllers/TableViewController.swift | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/View Controllers/TableViewController.swift b/View Controllers/TableViewController.swift index cc02779..992a47a 100644 --- a/View Controllers/TableViewController.swift +++ b/View Controllers/TableViewController.swift @@ -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