From aac3c865618085c564d1a0080fa259e37ef017a7 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 10 Sep 2019 16:38:59 -0500 Subject: [PATCH] Ensure that selected feed is visible when transitioning to three panel mode --- iOS/MasterFeed/MasterFeedViewController.swift | 27 ++++++++++--------- iOS/SceneCoordinator.swift | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index fdbb03302..f5e38726e 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -441,6 +441,16 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner { // MARK: API + func restoreSelectionIfNecessary(adjustScroll: Bool) { + if let indexPath = coordinator.masterFeedIndexPathForCurrentTimeline() { + if adjustScroll { + tableView.selectRowAndScrollIfNotVisible(at: indexPath, animated: false, deselect: coordinator.isRootSplitCollapsed) + } else { + tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) + } + } + } + func updateFeedSelection() { if let indexPath = coordinator.currentFeedIndexPath { if tableView.indexPathForSelectedRow != indexPath { @@ -500,9 +510,8 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner { coordinator.expandFolder(indexPath) reloadNode(parent) - self.applyChanges(animate: true) { [weak self] in + self.applyChanges(animate: true, adjustScroll: true) { [weak self] in if let indexPath = self?.coordinator.indexPathFor(node) { - self?.tableView.scrollToRow(at: indexPath, at: .middle, animated: true) self?.coordinator.selectFeed(indexPath) completion?() } @@ -543,11 +552,11 @@ private extension MasterFeedViewController { var snapshot = dataSource.snapshot() snapshot.reloadItems([node]) dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in - self?.restoreSelectionIfNecessary() + self?.restoreSelectionIfNecessary(adjustScroll: false) } } - func applyChanges(animate: Bool, completion: (() -> Void)? = nil) { + func applyChanges(animate: Bool, adjustScroll: Bool = false, completion: (() -> Void)? = nil) { var snapshot = NSDiffableDataSourceSnapshot() let sections = coordinator.allSections snapshot.appendSections(sections) @@ -557,7 +566,7 @@ private extension MasterFeedViewController { } dataSource.apply(snapshot, animatingDifferences: animate) { [weak self] in - self?.restoreSelectionIfNecessary() + self?.restoreSelectionIfNecessary(adjustScroll: adjustScroll) completion?() } } @@ -636,7 +645,7 @@ private extension MasterFeedViewController { var snapshot = dataSource.snapshot() snapshot.reloadItems(nodes) dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in - self?.restoreSelectionIfNecessary() + self?.restoreSelectionIfNecessary(adjustScroll: false) } } @@ -669,12 +678,6 @@ private extension MasterFeedViewController { self.applyChanges(animate: true) } - func restoreSelectionIfNecessary() { - if let indexPath = coordinator.masterFeedIndexPathForCurrentTimeline() { - tableView.selectRowAndScrollIfNotVisible(at: indexPath, animated: false, deselect: coordinator.isRootSplitCollapsed) - } - } - 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 7b33a202a..09cfff8d8 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -1620,6 +1620,7 @@ private extension SceneCoordinator { let navController = addNavControllerIfNecessary(controller, showButton: false) subSplitViewController!.showDetailViewController(navController, sender: self) + masterFeedViewController.restoreSelectionIfNecessary(adjustScroll: true) masterTimelineViewController!.restoreSelectionIfNecessary() // Return the shim controller