From c5625635de85fe18b3ffe77e88e3cb7ca848b8d0 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 26 Jan 2026 12:20:58 -0800 Subject: [PATCH] Fix restore-selection bug on refreshing the timeline. --- .../MainTimelineModernViewController.swift | 10 ++++----- iOS/SceneCoordinator.swift | 21 ++++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/iOS/MainTimeline/MainTimelineModernViewController.swift b/iOS/MainTimeline/MainTimelineModernViewController.swift index dd1d6feea..f2b2e0064 100644 --- a/iOS/MainTimeline/MainTimelineModernViewController.swift +++ b/iOS/MainTimeline/MainTimelineModernViewController.swift @@ -226,11 +226,10 @@ final class MainTimelineModernViewController: UIViewController, UndoableCommandR Self.logger.debug("MainTimelineModernViewController: restoreSelectionIfNecessary selecting item and adjusting scroll") collectionView.selectItemAndScrollIfNotVisible(at: indexPath, animations: []) } else { - if let indexPaths = collectionView.indexPathsForSelectedItems { - if !indexPaths.contains(indexPath) { - Self.logger.debug("MainTimelineModernViewController: restoreSelectionIfNecessary does not contain selected index path") - collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically) - } + let indexPaths = collectionView.indexPathsForSelectedItems ?? [] + if !indexPaths.contains(indexPath) { + Self.logger.debug("MainTimelineModernViewController: restoreSelectionIfNecessary does not contain selected index path") + collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically) } } } @@ -256,6 +255,7 @@ final class MainTimelineModernViewController: UIViewController, UndoableCommandR func reinitializeArticles(resetScroll: Bool) { Self.logger.debug("MainTimelineModernViewController: reinitializeArticles") resetUI(resetScroll: resetScroll) + restoreSelectionIfNecessary(adjustScroll: false) } func reloadArticles(animated: Bool) { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index acf0dc35f..8e41363ee 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -816,7 +816,7 @@ struct SidebarItemNode: Hashable, Sendable { if let article = self.currentArticle, let account = article.account { exceptionArticleFetcher = SingleArticleFetcher(account: account, articleID: article.articleID) } - fetchAndReplaceArticlesAsync(animated: true) { + fetchAndReplaceArticlesAsync(animated: true, emptyFirst: false) { self.mainTimelineViewController?.reinitializeArticles(resetScroll: resetScroll) } } @@ -2081,12 +2081,17 @@ private extension SceneCoordinator { if articles != sortedArticles { articles = sortedArticles - // Clear current article if it's no longer in the timeline. + // Update currentArticle to the new instance if it's still in the timeline, + // or clear it if it's no longer there. // Don't call selectArticle(nil) because that triggers navigation on iPhone. // Skip during state restoration so the restored article stays open. - if !isRestoringState, let currentArticle, !sortedArticles.contains(where: { $0.articleID == currentArticle.articleID && $0.accountID == currentArticle.accountID }) { - self.currentArticle = nil - articleViewController?.article = nil + if !isRestoringState, let currentArticle { + if let newArticle = sortedArticles.first(where: { $0.articleID == currentArticle.articleID && $0.accountID == currentArticle.accountID }) { + self.currentArticle = newArticle + } else { + self.currentArticle = nil + articleViewController?.article = nil + } } updateShowNamesAndIcons() @@ -2139,11 +2144,13 @@ private extension SceneCoordinator { fetchRequestQueue.cancelAllRequests() } - func fetchAndReplaceArticlesAsync(animated: Bool, completion: @escaping () -> Void) { + func fetchAndReplaceArticlesAsync(animated: Bool, emptyFirst: Bool = true, completion: @escaping () -> Void) { // To be called when we need to do an entire fetch, but an async delay is okay. // Example: we have the Today feed selected, and the calendar day just changed. cancelPendingAsyncFetches() - emptyTheTimeline() + if emptyFirst { + emptyTheTimeline() + } guard let timelineFeed = timelineFeed else { completion() return