diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index 22cda2500..aee0f7ae7 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -469,8 +469,6 @@ private extension MasterFeedViewController { } func applyChanges(animate: Bool, completion: (() -> Void)? = nil) { - let savedNode = selectedNode() - var snapshot = NSDiffableDataSourceSnapshot() let sections = coordinator.allSections snapshot.appendSections(sections) @@ -480,7 +478,7 @@ private extension MasterFeedViewController { } dataSource.apply(snapshot, animatingDifferences: animate) { [weak self] in - self?.selectRow(node: savedNode) + self?.restoreSelectionIfNecessary() completion?() } } @@ -605,6 +603,15 @@ private extension MasterFeedViewController { self.applyChanges(animate: true) } + func restoreSelectionIfNecessary() { + guard traitCollection.userInterfaceIdiom == .pad else { + return + } + if let indexPath = coordinator.masterFeedIndexPathForCurrentTimeline(), indexPath != tableView.indexPathForSelectedRow { + tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) + } + } + func makeFeedContextMenu(indexPath: IndexPath, includeDeleteRename: Bool) -> UIContextMenuConfiguration { return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { [ weak self] suggestedActions in diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index f3697a68f..c34f225df 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -467,6 +467,13 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { animatingChanges = false } + func masterFeedIndexPathForCurrentTimeline() -> IndexPath? { + guard let node = treeController.rootNode.descendantNode(where: { return $0.representedObject === timelineFetcher as AnyObject }) else { + return nil + } + return indexPathFor(node) + } + func indexForArticleID(_ articleID: String?) -> Int? { guard let articleID = articleID else { return nil } updateArticleRowMapIfNeeded()