Implemented expanding and collapsing by section header.

This commit is contained in:
Maurice Parker 2019-04-18 11:38:38 -05:00
parent 53077baa46
commit 1bec890875
1 changed files with 75 additions and 4 deletions

View File

@ -49,8 +49,9 @@ class MasterViewController: UITableViewController, UndoableCommandRunner {
refreshControl = UIRefreshControl() refreshControl = UIRefreshControl()
refreshControl!.addTarget(self, action: #selector(refreshAccounts(_:)), for: .valueChanged) refreshControl!.addTarget(self, action: #selector(refreshAccounts(_:)), for: .valueChanged)
// Set up the backing structures // Default the sections to expanded and set up the shadow table
for _ in treeController.rootNode.childNodes { for section in treeController.rootNode.childNodes {
expandedNodes.append(section)
shadowTable.append([Node]()) shadowTable.append([Node]())
} }
@ -191,6 +192,11 @@ class MasterViewController: UITableViewController, UndoableCommandRunner {
headerView.unreadCount = 0 headerView.unreadCount = 0
} }
headerView.tag = section
let tap = UITapGestureRecognizer(target: self, action:#selector(self.toggleSectionHeader(_:)))
headerView.addGestureRecognizer(tap)
return headerView return headerView
} }
@ -360,6 +366,22 @@ class MasterViewController: UITableViewController, UndoableCommandRunner {
self.present(feedViewController, animated: true) self.present(feedViewController, animated: true)
} }
@objc func toggleSectionHeader(_ sender: UITapGestureRecognizer) {
guard let sectionIndex = sender.view?.tag,
let sectionNode = treeController.rootNode.childAtIndex(sectionIndex)
else {
return
}
if expandedNodes.contains(sectionNode) {
collapse(section: sectionIndex)
} else {
expand(section: sectionIndex)
}
}
// MARK: API // MARK: API
func configure(_ cell: MasterTableViewCell, _ node: Node) { func configure(_ cell: MasterTableViewCell, _ node: Node) {
@ -572,6 +594,31 @@ private extension MasterViewController {
} }
func expand(section: Int) {
guard let expandNode = treeController.rootNode.childAtIndex(section) else {
return
}
expandedNodes.append(expandNode)
animatingChanges = true
var indexPathsToInsert = [IndexPath]()
for i in 0..<expandNode.childNodes.count {
if let child = expandNode.childAtIndex(i) {
indexPathsToInsert.append(IndexPath(row: i, section: section))
shadowTable[section].insert(child, at: i)
}
}
tableView.beginUpdates()
tableView.insertRows(at: indexPathsToInsert, with: .automatic)
tableView.endUpdates()
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
@ -581,11 +628,11 @@ private extension MasterViewController {
func expand(_ indexPath: IndexPath) { func expand(_ indexPath: IndexPath) {
animatingChanges = true
let expandNode = shadowTable[indexPath.section][indexPath.row] let expandNode = shadowTable[indexPath.section][indexPath.row]
expandedNodes.append(expandNode) expandedNodes.append(expandNode)
animatingChanges = true
var indexPathsToInsert = [IndexPath]() var indexPathsToInsert = [IndexPath]()
for i in 0..<expandNode.childNodes.count { for i in 0..<expandNode.childNodes.count {
if let child = expandNode.childAtIndex(i) { if let child = expandNode.childAtIndex(i) {
@ -603,6 +650,30 @@ private extension MasterViewController {
} }
func collapse(section: Int) {
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]()
tableView.beginUpdates()
tableView.deleteRows(at: indexPathsToRemove, with: .automatic)
tableView.endUpdates()
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