From 9b17293f4efee320e07b2a91144ae7b4d55106c7 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Fri, 6 Sep 2019 07:29:36 -0500 Subject: [PATCH] Fix bugs in automated routing on application launch --- iOS/MasterFeed/MasterFeedViewController.swift | 7 +++- iOS/SceneCoordinator.swift | 40 +++++++++++-------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index e0c34774d..1c2c10c2e 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -259,7 +259,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { becomeFirstResponder() - coordinator.selectFeed(indexPath) + coordinator.selectFeed(indexPath, automated: false) } override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath { @@ -469,17 +469,20 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner { func discloseFeed(_ feed: Feed, completion: (() -> Void)? = nil) { guard let node = coordinator.rootNode.descendantNodeRepresentingObject(feed as AnyObject) else { - return + completion?() + return } if let indexPath = coordinator.indexPathFor(node) { tableView.scrollToRow(at: indexPath, at: .middle, animated: true) coordinator.selectFeed(indexPath) + completion?() return } // It wasn't already visable, so expand its folder and try again guard let parent = node.parent, let indexPath = coordinator.indexPathFor(parent) else { + completion?() return } diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index ae584deef..27aaacbd6 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -108,7 +108,6 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { var timelineFetcher: ArticleFetcher? { didSet { - selectArticle(nil) if timelineFetcher is Feed { showFeedNames = false } else { @@ -300,6 +299,8 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func handle(_ activity: NSUserActivity) { + selectFeed(nil) + guard let activityType = ActivityType(rawValue: activity.activityType) else { return } switch activityType { case .selectToday: @@ -567,24 +568,28 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { return indexes } - func selectFeed(_ indexPath: IndexPath?) { - if navControllerForTimeline().viewControllers.filter({ $0 is MasterTimelineViewController }).count < 1 { - masterTimelineViewController = UIStoryboard.main.instantiateController(ofType: MasterTimelineViewController.self) - masterTimelineViewController!.coordinator = self - navControllerForTimeline().pushViewController(masterTimelineViewController!, animated: true) - } - + func selectFeed(_ indexPath: IndexPath?, automated: Bool = true) { + selectArticle(nil) currentFeedIndexPath = indexPath if let ip = indexPath, let node = nodeFor(ip), let fetcher = node.representedObject as? ArticleFetcher { timelineFetcher = fetcher updateSelectingActivity(with: node) + + if navControllerForTimeline().viewControllers.filter({ $0 is MasterTimelineViewController }).count < 1 { + masterTimelineViewController = UIStoryboard.main.instantiateController(ofType: MasterTimelineViewController.self) + masterTimelineViewController!.coordinator = self + navControllerForTimeline().pushViewController(masterTimelineViewController!, animated: !automated) + } } else { timelineFetcher = nil + + if rootSplitViewController.isCollapsed && navControllerForTimeline().viewControllers.last is MasterTimelineViewController { + navControllerForTimeline().popViewController(animated: !automated) + } } masterFeedViewController.updateFeedSelection() - selectArticle(nil) } func selectPrevFeed() { @@ -626,9 +631,13 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } if indexPath == nil { - if !rootSplitViewController.isCollapsed { + if rootSplitViewController.isCollapsed { + if masterNavigationController.children.last is DetailViewController { + masterNavigationController.popViewController(animated: false) + } + } else { let systemMessageViewController = UIStoryboard.main.instantiateController(ofType: SystemMessageViewController.self) - installDetailController(systemMessageViewController) + installDetailController(systemMessageViewController, automated: automated) } masterTimelineViewController?.updateArticleSelection(animate: true) return @@ -637,7 +646,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { if detailViewController == nil { let detailViewController = UIStoryboard.main.instantiateController(ofType: DetailViewController.self) detailViewController.coordinator = self - installDetailController(detailViewController) + installDetailController(detailViewController, automated: automated) } // Automatically hide the overlay @@ -649,7 +658,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } if automated { - masterTimelineViewController?.updateArticleSelection(animate: true) + masterTimelineViewController?.updateArticleSelection(animate: false) } detailViewController?.updateArticleSelection() @@ -839,7 +848,6 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func discloseFeed(_ feed: Feed, completion: (() -> Void)? = nil) { - masterNavigationController.popViewController(animated: true) masterFeedViewController.discloseFeed(feed) { completion?() } @@ -1463,7 +1471,7 @@ private extension SceneCoordinator { // during the display mode change callback (in the split view controller delegate). To fool the // system, we leave the same controller, the shim, in place and change its child controllers instead. - func installDetailController(_ detailController: UIViewController) { + func installDetailController(_ detailController: UIViewController, automated: Bool) { let showButton = rootSplitViewController.displayMode != .allVisible let controller = addNavControllerIfNecessary(detailController, showButton: showButton) @@ -1471,7 +1479,7 @@ private extension SceneCoordinator { let targetSplit = ensureDoubleSplit().children.first as! UISplitViewController targetSplit.showDetailViewController(controller, sender: self) } else if rootSplitViewController.isCollapsed { - masterNavigationController.pushViewController(controller, animated: true) + masterNavigationController.pushViewController(controller, animated: !automated) } else { if let shimController = rootSplitViewController.viewControllers.last { shimController.replaceChildAndPinView(controller)