From 6468f2c33386543dfebaf67d803dbeadd95b9fe2 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 21 Apr 2019 15:18:09 -0500 Subject: [PATCH] Refactor expand/collapse shadow table code into NavigationModelController. --- iOS/Master/MasterViewController.swift | 148 +++++--------------------- iOS/NavigationModelController.swift | 108 +++++++++++++++++++ 2 files changed, 134 insertions(+), 122 deletions(-) diff --git a/iOS/Master/MasterViewController.swift b/iOS/Master/MasterViewController.swift index 455fcb4df..59af27fe1 100644 --- a/iOS/Master/MasterViewController.swift +++ b/iOS/Master/MasterViewController.swift @@ -122,7 +122,11 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { return } - expand(indexPath) + nmc.expand(indexPath) { [weak self] indexPaths in + self?.tableView.beginUpdates() + self?.tableView.insertRows(at: indexPaths, with: .automatic) + self?.tableView.endUpdates() + } if let indexPath = nmc.indexPathFor(node) { tableView.scrollToRow(at: indexPath, at: .middle, animated: true) @@ -133,7 +137,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { // MARK: Table View override func numberOfSections(in tableView: UITableView) -> Int { - return nmc.treeController.rootNode.numberOfChildNodes + return nmc.shadowTable.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { @@ -456,10 +460,18 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { if nmc.expandedNodes.contains(sectionNode) { headerView.disclosureExpanded = false - collapse(section: sectionIndex) + nmc.collapse(section: sectionIndex) { [weak self] indexPaths in + self?.tableView.beginUpdates() + self?.tableView.deleteRows(at: indexPaths, with: .automatic) + self?.tableView.endUpdates() + } } else { headerView.disclosureExpanded = true - expand(section: sectionIndex) + nmc.expand(section: sectionIndex) { [weak self] indexPaths in + self?.tableView.beginUpdates() + self?.tableView.insertRows(at: indexPaths, with: .automatic) + self?.tableView.endUpdates() + } } } @@ -653,134 +665,26 @@ private extension MasterViewController { return nil } - func expand(section: Int) { - - guard let expandNode = nmc.treeController.rootNode.childAtIndex(section) else { - return - } - nmc.expandedNodes.append(expandNode) - - nmc.animatingChanges = true - - var indexPathsToInsert = [IndexPath]() - var i = 0 - - func addNode(_ node: Node) { - indexPathsToInsert.append(IndexPath(row: i, section: section)) - nmc.shadowTable[section].insert(node, at: i) - i = i + 1 - } - - for child in expandNode.childNodes { - addNode(child) - if nmc.expandedNodes.contains(child) { - for gChild in child.childNodes { - addNode(gChild) - } - } - } - - tableView.beginUpdates() - tableView.insertRows(at: indexPathsToInsert, with: .automatic) - tableView.endUpdates() - - nmc.animatingChanges = false - - } - func expand(_ cell: MasterTableViewCell) { guard let indexPath = tableView.indexPath(for: cell) else { return } - expand(indexPath) - } - - func expand(_ indexPath: IndexPath) { - - let expandNode = nmc.shadowTable[indexPath.section][indexPath.row] - nmc.expandedNodes.append(expandNode) - - nmc.animatingChanges = true - - var indexPathsToInsert = [IndexPath]() - for i in 0.. ()) { + + guard let expandNode = treeController.rootNode.childAtIndex(section) else { + return + } + expandedNodes.append(expandNode) + + animatingChanges = true + + var indexPathsToInsert = [IndexPath]() + var i = 0 + + func addNode(_ node: Node) { + indexPathsToInsert.append(IndexPath(row: i, section: section)) + shadowTable[section].insert(node, at: i) + i = i + 1 + } + + for child in expandNode.childNodes { + addNode(child) + if expandedNodes.contains(child) { + for gChild in child.childNodes { + addNode(gChild) + } + } + } + + completion(indexPathsToInsert) + + animatingChanges = false + + } + + func expand(_ indexPath: IndexPath, completion: ([IndexPath]) -> ()) { + + let expandNode = shadowTable[indexPath.section][indexPath.row] + expandedNodes.append(expandNode) + + animatingChanges = true + + var indexPathsToInsert = [IndexPath]() + for i in 0.. ()) { + + animatingChanges = true + + guard let collapseNode = treeController.rootNode.childAtIndex(section) else { + return + } + + if let removeNode = expandedNodes.firstIndex(of: collapseNode) { + expandedNodes.remove(at: removeNode) + } + + var indexPathsToRemove = [IndexPath]() + for i in 0.. ()) { + + animatingChanges = true + + let collapseNode = shadowTable[indexPath.section][indexPath.row] + if let removeNode = expandedNodes.firstIndex(of: collapseNode) { + expandedNodes.remove(at: removeNode) + } + + var indexPathsToRemove = [IndexPath]() + + for child in collapseNode.childNodes { + if let index = shadowTable[indexPath.section].firstIndex(of: child) { + indexPathsToRemove.append(IndexPath(row: index, section: indexPath.section)) + } + } + + for child in collapseNode.childNodes { + if let index = shadowTable[indexPath.section].firstIndex(of: child) { + shadowTable[indexPath.section].remove(at: index) + } + } + + completion(indexPathsToRemove) + + animatingChanges = false + + } + func indexesForArticleIDs(_ articleIDs: Set) -> IndexSet { var indexes = IndexSet()