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.tag = section
headerView.disclosureExpanded = coordinator.isExpanded(sectionNode) headerView.disclosureExpanded = sectionNode.isExpanded
let tap = UITapGestureRecognizer(target: self, action:#selector(self.toggleSectionHeader(_:))) let tap = UITapGestureRecognizer(target: self, action:#selector(self.toggleSectionHeader(_:)))
headerView.addGestureRecognizer(tap) 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 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 let movementAdjustment = sourceIndexPath > destIndexPath ? 1 : 0
return IndexPath(row: destIndexPath.row + movementAdjustment, section: destIndexPath.section) return IndexPath(row: destIndexPath.row + movementAdjustment, section: destIndexPath.section)
} }
@ -362,7 +362,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
return return
} }
if coordinator.isExpanded(sectionNode) { if sectionNode.isExpanded {
headerView.disclosureExpanded = false headerView.disclosureExpanded = false
coordinator.collapseSection(sectionIndex) coordinator.collapseSection(sectionIndex)
self.applyChanges(animate: true) self.applyChanges(animate: true)
@ -477,7 +477,7 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
return return
} }
if !coordinator.isExpanded(sectionNode) { if !sectionNode.isExpanded {
coordinator.expandSection(sectionIndex) coordinator.expandSection(sectionIndex)
self.applyChanges(animate: true) { self.applyChanges(animate: true) {
completion?() completion?()
@ -588,7 +588,7 @@ private extension MasterFeedViewController {
} else { } else {
cell.indentationLevel = 0 cell.indentationLevel = 0
} }
cell.disclosureExpanded = coordinator.isExpanded(node) cell.disclosureExpanded = node.isExpanded
cell.allowDisclosureSelection = node.canHaveChildNodes cell.allowDisclosureSelection = node.canHaveChildNodes
cell.name = nameFor(node) cell.name = nameFor(node)

View File

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

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