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