From a703b7cc0a6f8c8c2e2eeef4f7347b46ba9d5fbe Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 9 Oct 2022 20:11:00 -0400 Subject: [PATCH] Prune offscreen rows on new timeline --- .../Timeline/TimelineViewController.swift | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Tusker/Screens/Timeline/TimelineViewController.swift b/Tusker/Screens/Timeline/TimelineViewController.swift index 73e4d711..5d5d62e1 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()