From fba76983d3ac15dd87d32d024b9b6a5e7a8972be Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 20 Jan 2026 21:21:09 -0800 Subject: [PATCH] Use diffable data source APIs to update cells rather than direct cell access. --- .../MainFeedCollectionViewController.swift | 51 ++++++++++----- .../MainTimelineViewController.swift | 65 ++++++++++--------- 2 files changed, 68 insertions(+), 48 deletions(-) diff --git a/iOS/MainFeed/MainFeedCollectionViewController.swift b/iOS/MainFeed/MainFeedCollectionViewController.swift index 27f864a56..63af59ec0 100644 --- a/iOS/MainFeed/MainFeedCollectionViewController.swift +++ b/iOS/MainFeed/MainFeedCollectionViewController.swift @@ -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 diff --git a/iOS/MainTimeline/MainTimelineViewController.swift b/iOS/MainTimeline/MainTimelineViewController.swift index 51facb11a..8bd9c07ea 100644 --- a/iOS/MainTimeline/MainTimelineViewController.swift +++ b/iOS/MainTimeline/MainTimelineViewController.swift @@ -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) }