Use diffable data source APIs to update cells rather than direct cell access.

This commit is contained in:
Brent Simmons committed 2026-01-20 21:21:09 -08:00
1 parent fb1db82c4f
commit fba76983d3
2 files changed
+68 -48

No files matched your search

@@ -573,17 +573,24 @@ final class MainFeedCollectionViewController: UICollectionViewController, Undoab
as? MainFeedCollectionHeaderReusableView
}
private func reloadAllVisibleCells() {
private func reconfigureVisibleCells() {
let visibleIndexPaths = collectionView.indexPathsForVisibleItems
let itemIdentifiers = visibleIndexPaths.compactMap { dataSource.itemIdentifier(for: $0) }
guard !itemIdentifiers.isEmpty else {
reconfigureCells(itemIdentifiers) { [weak self] in
self?.restoreSelectionIfNecessary(adjustScroll: false)
}
}
private func reconfigureCells(_ items: [SidebarItemNode], completion: (() -> Void)? = nil) {
guard !items.isEmpty else {
completion?()
return
}
var snapshot = dataSource.snapshot()
snapshot.reconfigureItems(itemIdentifiers)
dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
self?.restoreSelectionIfNecessary(adjustScroll: false)
snapshot.reconfigureItems(items)
dataSource.apply(snapshot, animatingDifferences: false) {
completion?()
}
}
@@ -601,7 +608,7 @@ final class MainFeedCollectionViewController: UICollectionViewController, Undoab
@objc func preferredContentSizeCategoryDidChange() {
IconImageCache.shared.emptyCache()
reloadAllVisibleCells()
reconfigureVisibleCells()
}
@objc func unreadCountDidChange(_ note: Notification) {
@@ -618,19 +625,16 @@ final class MainFeedCollectionViewController: UICollectionViewController, Undoab
return
}
for cell in collectionView.visibleCells {
guard let indexPath = collectionView.indexPath(for: cell),
let sidebarItemNode = dataSource.itemIdentifier(for: indexPath),
let visibleIndexPaths = collectionView.indexPathsForVisibleItems
let itemsToReconfigure = visibleIndexPaths.compactMap { indexPath -> SidebarItemNode? in
guard let sidebarItemNode = dataSource.itemIdentifier(for: indexPath),
sidebarItemNode.node.representedObject === unreadCountProvider as AnyObject else {
continue
}
if let feedCell = cell as? MainFeedCollectionViewCell {
feedCell.unreadCount = unreadCountProvider.unreadCount
}
if let folderCell = cell as? MainFeedCollectionViewFolderCell {
folderCell.unreadCount = unreadCountProvider.unreadCount
return nil
}
return sidebarItemNode
}
reconfigureCells(itemsToReconfigure)
}
@objc func feedSettingDidChange(_ note: Notification) {
@@ -643,14 +647,25 @@ final class MainFeedCollectionViewController: UICollectionViewController, Undoab
}
@objc func faviconDidBecomeAvailable(_ note: Notification) {
applyToAvailableCells(configureIcon)
reconfigureVisibleCells()
}
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
guard let feed = note.userInfo?[UserInfoKey.feed] as? Feed else {
return
}
applyToCellsForRepresentedObject(feed, configureIcon(_:_:))
let visibleIndexPaths = collectionView.indexPathsForVisibleItems
let itemsToReconfigure = visibleIndexPaths.compactMap { indexPath -> SidebarItemNode? in
guard let sidebarItemNode = dataSource.itemIdentifier(for: indexPath),
let candidateSidebarItem = sidebarItemNode.node.representedObject as? SidebarItem,
feed.sidebarItemID == candidateSidebarItem.sidebarItemID else {
return nil
}
return sidebarItemNode
}
reconfigureCells(itemsToReconfigure)
}
// MARK: - Actions
@@ -620,20 +620,11 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu
let visibleArticles = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
let visibleUpdatedArticles = visibleArticles.filter { articleIDs.contains($0.articleID) }
for article in visibleUpdatedArticles {
if let indexPath = dataSource.indexPath(for: article) {
if let cell = tableView.cellForRow(at: indexPath) as? MainTimelineIconFeedCell {
let cellData = configure(article: article)
cell.cellData = cellData
cell.indexPathRow = indexPath.row
}
if let cell = tableView.cellForRow(at: indexPath) as? MainTimelineFeedCell {
let cellData = configure(article: article)
cell.cellData = cellData
cell.indexPathRow = indexPath.row
}
}
guard !visibleUpdatedArticles.isEmpty else {
return
}
reconfigureCells(visibleUpdatedArticles)
}
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
@@ -655,16 +646,25 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu
return
}
for indexPath in indexPaths {
guard let article = dataSource.itemIdentifier(for: indexPath), let authors = article.authors, !authors.isEmpty else {
continue
let articlesToReload = indexPaths.compactMap { indexPath -> Article? in
guard let article = dataSource.itemIdentifier(for: indexPath),
let authors = article.authors,
!authors.isEmpty else {
return nil
}
for author in authors {
if author.avatarURL == avatarURL, let cell = tableView.cellForRow(at: indexPath) as? MainTimelineIconFeedCell, let image = iconImageFor(article) {
cell.setIconImage(image)
if author.avatarURL == avatarURL {
return article
}
}
return nil
}
guard !articlesToReload.isEmpty else {
return
}
reconfigureCells(articlesToReload)
}
@objc func faviconDidBecomeAvailable(_ note: Notification) {
@@ -681,16 +681,21 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu
return
}
for indexPath in indexPaths {
let articlesToReload = indexPaths.compactMap { indexPath -> Article? in
guard let article = dataSource.itemIdentifier(for: indexPath) else {
continue
return nil
}
if feed == nil || feed == article.feed {
if let cell = tableView.cellForRow(at: indexPath) as? MainTimelineIconFeedCell, let image = iconImageFor(article) {
cell.setIconImage(image)
}
return article
}
return nil
}
guard !articlesToReload.isEmpty else {
return
}
reconfigureCells(articlesToReload)
}
func userDefaultsDidChange() {
@@ -699,14 +704,14 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu
if self.numberOfTextLines != AppDefaults.shared.timelineNumberOfLines || self.iconSize != AppDefaults.shared.timelineIconSize {
self.numberOfTextLines = AppDefaults.shared.timelineNumberOfLines
self.iconSize = AppDefaults.shared.timelineIconSize
self.reloadAllVisibleCells()
self.reconfigureVisibleCells()
}
self.updateToolbar()
}
@objc func contentSizeCategoryDidChange(_ note: Notification) {
Self.logger.debug("MainTimelineViewController: contentSizeCategoryDidChange")
reloadAllVisibleCells()
reconfigureVisibleCells()
}
@objc func displayNameDidChange(_ note: Notification) {
@@ -727,17 +732,17 @@ final class MainTimelineViewController: UITableViewController, UndoableCommandRu
// MARK: - Reloading
func queueReloadAvailableCells() {
CoalescingQueue.standard.add(self, #selector(reloadAllVisibleCells))
CoalescingQueue.standard.add(self, #selector(reconfigureVisibleCells))
}
@objc private func reloadAllVisibleCells() {
@objc private func reconfigureVisibleCells() {
let visibleArticles = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
reloadCells(visibleArticles)
reconfigureCells(visibleArticles)
}
private func reloadCells(_ articles: [Article]) {
private func reconfigureCells(_ articles: [Article]) {
var snapshot = dataSource.snapshot()
snapshot.reloadItems(articles)
snapshot.reconfigureItems(articles)
dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
self?.restoreSelectionIfNecessary(adjustScroll: false)
}