Simplify code by moving the expanded indicator to Node

This commit is contained in:
Maurice Parker 2019-09-11 15:24:38 -05:00
parent ab38e755d3
commit 1b97aad79c
3 changed files with 24 additions and 51 deletions

View File

@ -168,7 +168,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
}
headerView.tag = section
headerView.disclosureExpanded = coordinator.isExpanded(sectionNode)
headerView.disclosureExpanded = sectionNode.isExpanded
let tap = UITapGestureRecognizer(target: self, action:#selector(self.toggleSectionHeader(_:)))
headerView.addGestureRecognizer(tap)
@ -281,7 +281,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
}
// If this is a folder and isn't expanded or doesn't have any entries, let the users drop on it
if destNode.representedObject is Folder && (destNode.numberOfChildNodes == 0 || !coordinator.isExpanded(destNode)) {
if destNode.representedObject is Folder && (destNode.numberOfChildNodes == 0 || !destNode.isExpanded) {
let movementAdjustment = sourceIndexPath > destIndexPath ? 1 : 0
return IndexPath(row: destIndexPath.row + movementAdjustment, section: destIndexPath.section)
}
@ -362,7 +362,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
return
}
if coordinator.isExpanded(sectionNode) {
if sectionNode.isExpanded {
headerView.disclosureExpanded = false
coordinator.collapseSection(sectionIndex)
self.applyChanges(animate: true)
@ -477,7 +477,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
return
}
if !coordinator.isExpanded(sectionNode) {
if !sectionNode.isExpanded {
coordinator.expandSection(sectionIndex)
self.applyChanges(animate: true) {
completion?()
@ -588,7 +588,7 @@ private extension MasterFeedViewController {
} else {
cell.indentationLevel = 0
}
cell.disclosureExpanded = coordinator.isExpanded(node)
cell.disclosureExpanded = node.isExpanded
cell.allowDisclosureSelection = node.canHaveChildNodes
cell.name = nameFor(node)

View File

@ -57,7 +57,6 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
private let fetchRequestQueue = FetchRequestQueue()
private var animatingChanges = false
private var expandedNodes = [Node]()
private var shadowTable = [[Node]]()
private var lastSearchString = ""
private var lastSearchScope: SearchScope? = nil
@ -246,7 +245,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
super.init()
for section in treeController.rootNode.childNodes {
expandedNodes.append(section)
section.isExpanded = true
shadowTable.append([Node]())
}
@ -370,17 +369,10 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
return
}
// If we are deactivating an account, clean up the expandedNodes table
if !account.isActive, let node = self.treeController.rootNode.childNodeRepresentingObject(account) {
if let nodeIndex = self.expandedNodes.firstIndex(of: node) {
self.expandedNodes.remove(at: nodeIndex)
}
}
rebuildBackingStores() {
// If we are activating an account, then automatically expand it
if account.isActive, let node = self.treeController.rootNode.childNodeRepresentingObject(account) {
self.expandedNodes.append(node)
node.isExpanded = true
}
}
}
@ -394,7 +386,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
// Automatically expand any new accounts
if let account = note.userInfo?[Account.UserInfoKey.account] as? Account,
let node = self.treeController.rootNode.childNodeRepresentingObject(account) {
self.expandedNodes.append(node)
node.isExpanded = true
}
}
}
@ -403,15 +395,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
if timelineFetcherContainsAnyPseudoFeed() {
fetchAndReplaceArticlesSync()
}
rebuildBackingStores() {
// Clean up the expandedNodes table for any deleted accounts
if let account = note.userInfo?[Account.UserInfoKey.account] as? Account,
let node = self.treeController.rootNode.childNodeRepresentingObject(account),
let nodeIndex = self.expandedNodes.firstIndex(of: node) {
self.expandedNodes.remove(at: nodeIndex)
}
}
rebuildBackingStores()
}
@objc func userDefaultsDidChange(_ note: Notification) {
@ -431,10 +415,6 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
// MARK: API
func isExpanded(_ node: Node) -> Bool {
return expandedNodes.contains(node)
}
func shadowNodesFor(section: Int) -> [Node] {
return shadowTable[section]
}
@ -458,11 +438,11 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
}
func expandSection(_ section: Int) {
guard let expandNode = treeController.rootNode.childAtIndex(section), !expandedNodes.contains(expandNode) else {
guard let expandNode = treeController.rootNode.childAtIndex(section), !expandNode.isExpanded else {
return
}
expandedNodes.append(expandNode)
expandNode.isExpanded = true
animatingChanges = true
@ -475,7 +455,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
for child in expandNode.childNodes {
addNode(child)
if expandedNodes.contains(child) {
if child.isExpanded {
for gChild in child.childNodes {
addNode(gChild)
}
@ -500,11 +480,11 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
}
func expandFolder(_ indexPath: IndexPath) {
guard let expandNode = nodeFor(indexPath), !expandedNodes.contains(expandNode) && expandNode.representedObject is Folder else {
guard let expandNode = nodeFor(indexPath), !expandNode.isExpanded && expandNode.representedObject is Folder else {
return
}
expandedNodes.append(expandNode)
expandNode.isExpanded = true
animatingChanges = true
@ -519,18 +499,13 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
}
func collapseSection(_ section: Int) {
guard let collapseNode = treeController.rootNode.childAtIndex(section), expandedNodes.contains(collapseNode) else {
guard let collapseNode = treeController.rootNode.childAtIndex(section), collapseNode.isExpanded else {
return
}
animatingChanges = true
if let removeNode = expandedNodes.firstIndex(of: collapseNode) {
expandedNodes.remove(at: removeNode)
}
collapseNode.isExpanded = false
shadowTable[section] = [Node]()
animatingChanges = false
}
@ -545,15 +520,13 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
}
func collapseFolder(_ indexPath: IndexPath) {
guard let collapseNode = nodeFor(indexPath), expandedNodes.contains(collapseNode) && collapseNode.representedObject is Folder else {
guard let collapseNode = nodeFor(indexPath), collapseNode.isExpanded && collapseNode.representedObject is Folder else {
return
}
animatingChanges = true
if let removeNode = expandedNodes.firstIndex(of: collapseNode) {
expandedNodes.remove(at: removeNode)
}
collapseNode.isExpanded = false
for child in collapseNode.childNodes {
if let index = shadowTable[indexPath.section].firstIndex(of: child) {
@ -1033,10 +1006,10 @@ private extension SceneCoordinator {
var result = [Node]()
let sectionNode = treeController.rootNode.childAtIndex(i)!
if expandedNodes.contains(sectionNode) {
if sectionNode.isExpanded {
for node in sectionNode.childNodes {
result.append(node)
if expandedNodes.contains(node) {
if node.isExpanded {
for child in node.childNodes {
result.append(child)
}
@ -1178,7 +1151,7 @@ private extension SceneCoordinator {
return true
}
if expandedNodes.contains(node) {
if node.isExpanded {
continue
}
@ -1284,7 +1257,7 @@ private extension SceneCoordinator {
return true
}
if expandedNodes.contains(node) {
if node.isExpanded {
continue
}

@ -1 +1 @@
Subproject commit db208e17bdf4f5e7e643c580acbc339191693537
Subproject commit d333739a776236aae32b3868415729499021cec3