Refactor expand/collapse shadow table code into NavigationModelController.

This commit is contained in:
Maurice Parker 2019-04-21 15:18:09 -05:00
parent 9f6ce38535
commit 6468f2c333
2 changed files with 134 additions and 122 deletions

View File

@ -122,7 +122,11 @@ class MasterViewController: UITableViewController, UndoableCommandRunner {
return 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) { if let indexPath = nmc.indexPathFor(node) {
tableView.scrollToRow(at: indexPath, at: .middle, animated: true) tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
@ -133,7 +137,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner {
// MARK: Table View // MARK: Table View
override func numberOfSections(in tableView: UITableView) -> Int { 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 { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
@ -456,10 +460,18 @@ class MasterViewController: UITableViewController, UndoableCommandRunner {
if nmc.expandedNodes.contains(sectionNode) { if nmc.expandedNodes.contains(sectionNode) {
headerView.disclosureExpanded = false 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 { } else {
headerView.disclosureExpanded = true 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 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) { func expand(_ cell: MasterTableViewCell) {
guard let indexPath = tableView.indexPath(for: cell) else { guard let indexPath = tableView.indexPath(for: cell) else {
return return
} }
expand(indexPath) nmc.expand(indexPath) { [weak self] indexPaths in
} self?.tableView.beginUpdates()
self?.tableView.insertRows(at: indexPaths, with: .automatic)
func expand(_ indexPath: IndexPath) { self?.tableView.endUpdates()
let expandNode = nmc.shadowTable[indexPath.section][indexPath.row]
nmc.expandedNodes.append(expandNode)
nmc.animatingChanges = true
var indexPathsToInsert = [IndexPath]()
for i in 0..<expandNode.childNodes.count {
if let child = expandNode.childAtIndex(i) {
let nextIndex = indexPath.row + i + 1
indexPathsToInsert.append(IndexPath(row: nextIndex, section: indexPath.section))
nmc.shadowTable[indexPath.section].insert(child, at: nextIndex)
}
} }
tableView.beginUpdates()
tableView.insertRows(at: indexPathsToInsert, with: .automatic)
tableView.endUpdates()
nmc.animatingChanges = false
} }
func collapse(section: Int) {
nmc.animatingChanges = true
guard let collapseNode = nmc.treeController.rootNode.childAtIndex(section) else {
return
}
if let removeNode = nmc.expandedNodes.firstIndex(of: collapseNode) {
nmc.expandedNodes.remove(at: removeNode)
}
var indexPathsToRemove = [IndexPath]()
for i in 0..<nmc.shadowTable[section].count {
indexPathsToRemove.append(IndexPath(row: i, section: section))
}
nmc.shadowTable[section] = [Node]()
tableView.beginUpdates()
tableView.deleteRows(at: indexPathsToRemove, with: .automatic)
tableView.endUpdates()
nmc.animatingChanges = false
}
func collapse(_ cell: MasterTableViewCell) { func collapse(_ cell: MasterTableViewCell) {
guard let indexPath = tableView.indexPath(for: cell) else { guard let indexPath = tableView.indexPath(for: cell) else {
return return
} }
collapse(indexPath) nmc.collapse(indexPath) { [weak self] indexPaths in
self?.tableView.beginUpdates()
self?.tableView.deleteRows(at: indexPaths, with: .automatic)
self?.tableView.endUpdates()
}
} }
func collapse(_ indexPath: IndexPath) {
nmc.animatingChanges = true
let collapseNode = nmc.shadowTable[indexPath.section][indexPath.row]
if let removeNode = nmc.expandedNodes.firstIndex(of: collapseNode) {
nmc.expandedNodes.remove(at: removeNode)
}
var indexPathsToRemove = [IndexPath]()
for child in collapseNode.childNodes {
if let index = nmc.shadowTable[indexPath.section].firstIndex(of: child) {
indexPathsToRemove.append(IndexPath(row: index, section: indexPath.section))
}
}
for child in collapseNode.childNodes {
if let index = nmc.shadowTable[indexPath.section].firstIndex(of: child) {
nmc.shadowTable[indexPath.section].remove(at: index)
}
}
tableView.beginUpdates()
tableView.deleteRows(at: indexPathsToRemove, with: .automatic)
tableView.endUpdates()
nmc.animatingChanges = false
}
} }

View File

@ -173,6 +173,114 @@ class NavigationModelController {
return nil return nil
} }
func expand(section: Int, completion: ([IndexPath]) -> ()) {
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..<expandNode.childNodes.count {
if let child = expandNode.childAtIndex(i) {
let nextIndex = indexPath.row + i + 1
indexPathsToInsert.append(IndexPath(row: nextIndex, section: indexPath.section))
shadowTable[indexPath.section].insert(child, at: nextIndex)
}
}
completion(indexPathsToInsert)
animatingChanges = false
}
func collapse(section: Int, completion: ([IndexPath]) -> ()) {
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..<shadowTable[section].count {
indexPathsToRemove.append(IndexPath(row: i, section: section))
}
shadowTable[section] = [Node]()
completion(indexPathsToRemove)
animatingChanges = false
}
func collapse(_ indexPath: IndexPath, completion: ([IndexPath]) -> ()) {
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<String>) -> IndexSet { func indexesForArticleIDs(_ articleIDs: Set<String>) -> IndexSet {
var indexes = IndexSet() var indexes = IndexSet()