From 19988a872a56686482930ead9f6f062db9be52b4 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 19 Mar 2018 19:16:04 -0400 Subject: [PATCH] Improve scroll-jank on Gallery Tile View - Don't fetch data unnecessarily - Use CATransaction to avoid insertion animations. They are off screen anyway. // FREEBIE --- .../MediaTileViewController.swift | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Signal/src/ViewControllers/MediaTileViewController.swift b/Signal/src/ViewControllers/MediaTileViewController.swift index e8f7aeb2c..0bc220592 100644 --- a/Signal/src/ViewControllers/MediaTileViewController.swift +++ b/Signal/src/ViewControllers/MediaTileViewController.swift @@ -340,15 +340,20 @@ public class MediaTileViewController: UICollectionViewController, MediaGalleryCe return } + guard !mediaGalleryDataSource.hasFetchedOldest else { + return + } + guard !isFetchingMoreData else { Logger.debug("\(logTag) in \(#function) already fetching more data") return } - isFetchingMoreData = true let scrollDistanceToBottom = oldContentHeight - contentOffsetY + CATransaction.begin() + CATransaction.setDisableActions(true) collectionView.performBatchUpdates({ mediaGalleryDataSource.ensureGalleryItemsLoaded(.before, item: oldestLoadedItem, amount: self.kMediaTileViewLoadBatchSize) { addedSections, addedItems in Logger.debug("\(self.logTag) in \(#function) insertingSections: \(addedSections) items: \(addedItems)") @@ -365,7 +370,9 @@ public class MediaTileViewController: UICollectionViewController, MediaGalleryCe Logger.debug("\(self.logTag) in \(#function) performBatchUpdates finished: \(finished)") self.isFetchingMoreData = false + CATransaction.commit() }) + } else if oldContentHeight - contentOffsetY < kEdgeThreshold { // Near the bottom, load newer content @@ -374,22 +381,31 @@ public class MediaTileViewController: UICollectionViewController, MediaGalleryCe return } + guard !mediaGalleryDataSource.hasFetchedMostRecent else { + return + } + guard !isFetchingMoreData else { Logger.debug("\(logTag) in \(#function) already fetching more data") return } - isFetchingMoreData = true - collectionView.performBatchUpdates({ - mediaGalleryDataSource.ensureGalleryItemsLoaded(.after, item: mostRecentLoadedItem, amount: self.kMediaTileViewLoadBatchSize) { addedSections, addedItems in - Logger.debug("\(self.logTag) in \(#function) insertingSections: \(addedSections), items: \(addedItems)") - collectionView.insertSections(addedSections) - collectionView.insertItems(at: addedItems) - } - }, completion: { finished in - Logger.debug("\(self.logTag) in \(#function) performBatchUpdates finished: \(finished)") - self.isFetchingMoreData = false - }) + + CATransaction.begin() + CATransaction.setDisableActions(true) + UIView.performWithoutAnimation { + collectionView.performBatchUpdates({ + mediaGalleryDataSource.ensureGalleryItemsLoaded(.after, item: mostRecentLoadedItem, amount: self.kMediaTileViewLoadBatchSize) { addedSections, addedItems in + Logger.debug("\(self.logTag) in \(#function) insertingSections: \(addedSections), items: \(addedItems)") + collectionView.insertSections(addedSections) + collectionView.insertItems(at: addedItems) + } + }, completion: { finished in + Logger.debug("\(self.logTag) in \(#function) performBatchUpdates finished: \(finished)") + self.isFetchingMoreData = false + CATransaction.commit() + }) + } } }