Fix node duplication bug on rebuilding tree.

This commit is contained in:
Brent Simmons 2017-10-19 21:38:50 -07:00
parent 913a5f46a0
commit 1481d72ed1

View File

@ -28,23 +28,26 @@ final class SidebarTreeControllerDelegate: TreeControllerDelegate {
private extension SidebarTreeControllerDelegate { private extension SidebarTreeControllerDelegate {
func childNodesForRootNode(_ node: Node) -> [Node]? { func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
// The child nodes are the top-level items of the local Account. // The child nodes are the top-level items of the local Account.
// This will be expanded later to add synthetic feeds (All Unread, for instance). // This will be expanded later to add synthetic feeds (All Unread, for instance)
// and other accounts.
let localAccountChildren = AccountManager.shared.localAccount.children
var updatedChildNodes = [Node]() var updatedChildNodes = [Node]()
for oneRepresentedObject in AccountManager.shared.localAccount.children { localAccountChildren.forEach { (representedObject) in
if let existingNode = node.childNodeRepresentingObject(oneRepresentedObject) { if let existingNode = rootNode.childNodeRepresentingObject(representedObject) {
// Reuse nodes. // Reuse nodes.
if !updatedChildNodes.contains(existingNode) { if !updatedChildNodes.contains(existingNode) {
updatedChildNodes += [existingNode] updatedChildNodes += [existingNode]
return
} }
} }
if let newNode = createNode(representedObject: oneRepresentedObject as AnyObject, parent: node) { if let newNode = createNode(representedObject: representedObject as AnyObject, parent: rootNode) {
updatedChildNodes += [newNode] updatedChildNodes += [newNode]
} }
} }
@ -53,24 +56,25 @@ private extension SidebarTreeControllerDelegate {
return updatedChildNodes return updatedChildNodes
} }
func childNodesForFolderNode(_ node: Node) -> [Node]? { func childNodesForFolderNode(_ folderNode: Node) -> [Node]? {
var updatedChildNodes = [Node]() var updatedChildNodes = [Node]()
let folder = node.representedObject as! Folder let folder = folderNode.representedObject as! Folder
for oneRepresentedObject in folder.children { folder.children.forEach { (representedObject) in
if let existingNode = node.childNodeRepresentingObject(oneRepresentedObject) { if let existingNode = folderNode.childNodeRepresentingObject(representedObject) {
if !updatedChildNodes.contains(existingNode) { if !updatedChildNodes.contains(existingNode) {
updatedChildNodes += [existingNode] updatedChildNodes += [existingNode]
return
} }
} }
if let newNode = self.createNode(representedObject: oneRepresentedObject, parent: node) { if let newNode = self.createNode(representedObject: representedObject, parent: folderNode) {
updatedChildNodes += [newNode] updatedChildNodes += [newNode]
} }
} }
updatedChildNodes = Node.nodesSortedAlphabeticallyWithFoldersAtEnd(updatedChildNodes) updatedChildNodes = Node.nodesSortedAlphabeticallyWithFoldersAtEnd(updatedChildNodes)
return updatedChildNodes return updatedChildNodes
} }