From 4884aebbfd48a8fb61415c13f0302f5df1689bea Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 6 Jul 2019 11:32:19 -0500 Subject: [PATCH] Encapsulate more of the AppCoordinate API --- iOS/AppCoordinator.swift | 64 ++++++++++++------- iOS/Detail/DetailViewController.swift | 4 +- iOS/MasterFeed/MasterFeedViewController.swift | 2 +- .../MasterTimelineViewController.swift | 2 +- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/iOS/AppCoordinator.swift b/iOS/AppCoordinator.swift index ba268d2a1..e2461ce73 100644 --- a/iOS/AppCoordinator.swift +++ b/iOS/AppCoordinator.swift @@ -45,7 +45,7 @@ class AppCoordinator { } private let treeControllerDelegate = FeedTreeControllerDelegate() - lazy var treeController: TreeController = { + private(set) lazy var treeController: TreeController = { return TreeController(delegate: treeControllerDelegate) }() @@ -57,7 +57,7 @@ class AppCoordinator { return shadowTable.count } - var currentMasterIndexPath: IndexPath? { + private(set) var currentMasterIndexPath: IndexPath? { didSet { guard let ip = currentMasterIndexPath, let node = nodeFor(ip) else { assertionFailure() @@ -88,8 +88,8 @@ class AppCoordinator { } - var showFeedNames = false - var showAvatars = false + private(set) var showFeedNames = false + private(set) var showAvatars = false var isPrevArticleAvailable: Bool { guard let indexPath = currentArticleIndexPath else { @@ -135,7 +135,7 @@ class AppCoordinator { return nil } - var currentArticleIndexPath: IndexPath? { + private(set) var currentArticleIndexPath: IndexPath? { didSet { if currentArticleIndexPath != oldValue { NotificationCenter.default.post(name: .ArticleSelectionDidChange, object: self, userInfo: nil) @@ -143,7 +143,7 @@ class AppCoordinator { } } - var articles = ArticleArray() { + private(set) var articles = ArticleArray() { didSet { if articles == oldValue { return @@ -246,24 +246,6 @@ class AppCoordinator { // MARK: API - func didSelectFeed(_ indexPath: IndexPath) { - masterTimelineViewController = UIStoryboard.main.instantiateController(ofType: MasterTimelineViewController.self) - masterTimelineViewController!.coordinator = self - currentMasterIndexPath = indexPath - masterNavigationController.pushViewController(masterTimelineViewController!, animated: true) - } - - func didSelectArticle(_ indexPath: IndexPath) { - let detailViewController = UIStoryboard.main.instantiateController(ofType: DetailViewController.self) - detailViewController.coordinator = self - detailViewController.navigationItem.leftBarButtonItem = rootSplitViewController.displayModeButtonItem - detailViewController.navigationItem.leftItemsSupplementBackButton = true - currentArticleIndexPath = indexPath -// rootSplitViewController.toggleMasterView() - rootSplitViewController.showDetailViewController(detailViewController, sender: self) - - } - func beginUpdates() { animatingChanges = true } @@ -445,6 +427,40 @@ class AppCoordinator { return indexes } + func selectFeed(_ indexPath: IndexPath) { + masterTimelineViewController = UIStoryboard.main.instantiateController(ofType: MasterTimelineViewController.self) + masterTimelineViewController!.coordinator = self + currentMasterIndexPath = indexPath + masterNavigationController.pushViewController(masterTimelineViewController!, animated: true) + } + + func selectArticle(_ indexPath: IndexPath) { + if let detailNavController = rootSplitViewController.viewControllers.last as? UINavigationController, + let _ = detailNavController.topViewController as? DetailViewController { + currentArticleIndexPath = indexPath + } else { + let detailViewController = UIStoryboard.main.instantiateController(ofType: DetailViewController.self) + detailViewController.coordinator = self + detailViewController.navigationItem.leftBarButtonItem = rootSplitViewController.displayModeButtonItem + detailViewController.navigationItem.leftItemsSupplementBackButton = true + currentArticleIndexPath = indexPath + // rootSplitViewController.toggleMasterView() + rootSplitViewController.showDetailViewController(detailViewController, sender: self) + } + } + + func selectPrevArticle() { + if let indexPath = prevArticleIndexPath { + selectArticle(indexPath) + } + } + + func selectNextArticle() { + if let indexPath = nextArticleIndexPath { + selectArticle(indexPath) + } + } + func selectNextUnread() { // This should never happen, but I don't want to risk throwing us diff --git a/iOS/Detail/DetailViewController.swift b/iOS/Detail/DetailViewController.swift index 5b3c3b1ba..714aad741 100644 --- a/iOS/Detail/DetailViewController.swift +++ b/iOS/Detail/DetailViewController.swift @@ -136,11 +136,11 @@ class DetailViewController: UIViewController { } @IBAction func prevArticle(_ sender: Any) { - coordinator.currentArticleIndexPath = coordinator.prevArticleIndexPath + coordinator.selectPrevArticle() } @IBAction func nextArticle(_ sender: Any) { - coordinator.currentArticleIndexPath = coordinator.nextArticleIndexPath + coordinator.selectNextArticle() } @IBAction func toggleRead(_ sender: Any) { diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index a06601ba0..4d76c2eaf 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -276,7 +276,7 @@ class MasterFeedViewController: ProgressTableViewController, UndoableCommandRunn } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - coordinator.didSelectFeed(indexPath) + coordinator.selectFeed(indexPath) } override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { diff --git a/iOS/MasterTimeline/MasterTimelineViewController.swift b/iOS/MasterTimeline/MasterTimelineViewController.swift index 0fc7e769e..1bdd67d59 100644 --- a/iOS/MasterTimeline/MasterTimelineViewController.swift +++ b/iOS/MasterTimeline/MasterTimelineViewController.swift @@ -175,7 +175,7 @@ class MasterTimelineViewController: ProgressTableViewController, UndoableCommand } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - coordinator.didSelectArticle(indexPath) + coordinator.selectArticle(indexPath) } // MARK: Notifications