diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index c8ca42936..118ba22ae 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -722,26 +722,26 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr @objc func accountStateDidChange(_ note: Notification) { if representedObjectsContainsAnyPseudoFeed() { - fetchAndReplaceArticlesAsync() + fetchAndReplacePreservingSelectionAsync() } } @objc func accountsDidChange(_ note: Notification) { if representedObjectsContainsAnyPseudoFeed() { - fetchAndReplaceArticlesAsync() + fetchAndReplacePreservingSelectionAsync() } } @objc func userDidDeleteAccount(_ note: Notification) { undoManager?.removeAllActions() // Undo stack may contain actions for the deleted account. if representedObjectsContainsAnyPseudoFeed() { - fetchAndReplaceArticlesAsync() + fetchAndReplacePreservingSelectionAsync() } } @objc func containerChildrenDidChange(_ note: Notification) { if representedObjectsContainsAnyPseudoFeed() || representedObjectsContainAnyFolder() { - fetchAndReplaceArticlesAsync() + fetchAndReplacePreservingSelectionAsync() } } @@ -1043,6 +1043,19 @@ private extension TimelineViewController { } } + // The current article should stay in the timeline, and stay selected, + // when the timeline updates and the sidebar selection hasn't changed — + // even if the new fetch wouldn't otherwise include it. + func fetchAndReplacePreservingSelectionAsync() { + if let article = oneSelectedArticle, let account = article.account { + exceptionArticleFetcher = SingleArticleFetcher(account: account, articleID: article.articleID) + } + let savedSelection = selectedArticleIDs() + fetchAndReplaceArticlesAsync { [weak self] in + self?.restoreSelection(savedSelection) + } + } + func updateUnreadCount() { var count = 0 for article in articles { @@ -1190,7 +1203,7 @@ private extension TimelineViewController { replaceArticles(with: fetchedArticles) } - func fetchAndReplaceArticlesAsync() { + func fetchAndReplaceArticlesAsync(completion: (() -> Void)? = nil) { // 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() @@ -1206,6 +1219,7 @@ private extension TimelineViewController { fetchUnsortedArticlesAsync(for: representedObjects) { [weak self] (articles) in self?.replaceArticles(with: articles) + completion?() } }