From 6d24ea642a969ac098ebff3065f848eff4309af2 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 5 Sep 2019 16:38:33 -0500 Subject: [PATCH] Add expand and collapse all folders shortcuts --- iOS/MasterFeed/MasterFeedViewController.swift | 10 ++++++ iOS/SceneCoordinator.swift | 33 ++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index b80dbe217..e0c34774d 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -418,6 +418,16 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner { } } + @objc func expandAll(_ sender: Any?) { + coordinator.expandAllSectionsAndFolders() + self.applyChanges(animate: true) + } + + @objc func collapseAllExceptForGroupItems(_ sender: Any?) { + coordinator.collapseAllFolders() + self.applyChanges(animate: true) + } + // MARK: API func updateFeedSelection() { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 70c418056..7111f79c4 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -437,9 +437,10 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func expandSection(_ section: Int) { - guard let expandNode = treeController.rootNode.childAtIndex(section) else { + guard let expandNode = treeController.rootNode.childAtIndex(section), !expandedNodes.contains(expandNode) else { return } + expandedNodes.append(expandNode) animatingChanges = true @@ -463,6 +464,20 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { animatingChanges = false } + func expandAllSectionsAndFolders() { + for (sectionIndex, sectionNode) in treeController.rootNode.childNodes.enumerated() { + + expandSection(sectionIndex) + + for topLevelNode in sectionNode.childNodes { + if topLevelNode.representedObject is Folder, let indexPath = indexPathFor(topLevelNode) { + expandFolder(indexPath) + } + } + + } + } + func expandFolder(_ indexPath: IndexPath) { let expandNode = shadowTable[indexPath.section][indexPath.row] guard !expandedNodes.contains(expandNode) else { return } @@ -481,12 +496,12 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } func collapseSection(_ section: Int) { - animatingChanges = true - - guard let collapseNode = treeController.rootNode.childAtIndex(section) else { + guard let collapseNode = treeController.rootNode.childAtIndex(section), expandedNodes.contains(collapseNode) else { return } + animatingChanges = true + if let removeNode = expandedNodes.firstIndex(of: collapseNode) { expandedNodes.remove(at: removeNode) } @@ -496,6 +511,16 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { animatingChanges = false } + func collapseAllFolders() { + for sectionNode in treeController.rootNode.childNodes { + for topLevelNode in sectionNode.childNodes { + if topLevelNode.representedObject is Folder, let indexPath = indexPathFor(topLevelNode) { + collapseFolder(indexPath) + } + } + } + } + func collapseFolder(_ indexPath: IndexPath) { animatingChanges = true