diff --git a/Tusker/Screens/Timeline/TimelineViewController.swift b/Tusker/Screens/Timeline/TimelineViewController.swift index 73e4d71186..5d5d62e17b 100644 --- a/Tusker/Screens/Timeline/TimelineViewController.swift +++ b/Tusker/Screens/Timeline/TimelineViewController.swift @@ -160,7 +160,7 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) - // TODO: prune offscreen rows + pruneOffscreenRows() } private func removeTimelineDescriptionCell() { @@ -170,6 +170,25 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro isShowingTimelineDescription = false } + private func pruneOffscreenRows() { + guard let lastVisibleIndexPath = collectionView.indexPathsForVisibleItems.last else { + return + } + var snapshot = dataSource.snapshot() + let items = snapshot.itemIdentifiers(inSection: .statuses) + let pageSize = 20 + let numberOfPagesToPrune = (items.count - lastVisibleIndexPath.row - 1) / pageSize + if numberOfPagesToPrune > 0 { + let itemsToRemove = Array(items.suffix(numberOfPagesToPrune * pageSize)) + snapshot.deleteItems(itemsToRemove) + } + dataSource.apply(snapshot, animatingDifferences: false) + + if case .status(id: let id, state: _) = snapshot.itemIdentifiers(inSection: .statuses).last { + older = .before(id: id, count: nil) + } + } + @objc func refresh() { Task { await controller.loadNewer()