NetNewsWire/Evergreen/MainWindow/Sidebar/SidebarTreeControllerDelega...

101 lines
2.6 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// SidebarTreeControllerDelegate.swift
// Evergreen
//
// Created by Brent Simmons on 7/24/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSTree
import Data
2017-09-24 21:24:44 +02:00
import Account
2017-05-27 19:43:27 +02:00
final class SidebarTreeControllerDelegate: TreeControllerDelegate {
func treeController(treeController: TreeController, childNodesFor node: Node) -> [Node]? {
if node.isRoot {
return childNodesForRootNode(node)
}
if node.representedObject is Folder {
return childNodesForFolderNode(node)
}
return nil
}
}
private extension SidebarTreeControllerDelegate {
func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
2017-05-27 19:43:27 +02:00
// 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)
// and other accounts.
return childNodesForContainerNode(rootNode, AccountManager.shared.localAccount.children as! [AnyHashable])
}
func childNodesForFolderNode(_ folderNode: Node) -> [Node]? {
2017-05-27 19:43:27 +02:00
let folder = folderNode.representedObject as! Folder
return childNodesForContainerNode(folderNode, folder.children as! [AnyHashable])
2017-05-27 19:43:27 +02:00
}
func childNodesForContainerNode(_ containerNode: Node, _ children: [AnyHashable]) -> [Node]? {
2017-05-27 19:43:27 +02:00
var updatedChildNodes = [Node]()
children.forEach { (representedObject) in
if let existingNode = containerNode.childNodeRepresentingObject(representedObject) {
2017-05-27 19:43:27 +02:00
if !updatedChildNodes.contains(existingNode) {
updatedChildNodes += [existingNode]
return
2017-05-27 19:43:27 +02:00
}
}
if let newNode = self.createNode(representedObject: representedObject, parent: containerNode) {
2017-05-27 19:43:27 +02:00
updatedChildNodes += [newNode]
}
}
2017-05-27 19:43:27 +02:00
updatedChildNodes = Node.nodesSortedAlphabeticallyWithFoldersAtEnd(updatedChildNodes)
return updatedChildNodes
}
func createNode(representedObject: Any, parent: Node) -> Node? {
2017-05-27 19:43:27 +02:00
if let feed = representedObject as? Feed {
return createNode(feed: feed, parent: parent)
}
if let folder = representedObject as? Folder {
return createNode(folder: folder, parent: parent)
}
return nil
}
func createNode(feed: Feed, parent: Node) -> Node {
return Node(representedObject: feed, parent: parent)
}
func createNode(folder: Folder, parent: Node) -> Node {
let node = Node(representedObject: folder, parent: parent)
2017-05-27 19:43:27 +02:00
node.canHaveChildNodes = true
return node
}
func nodeInArrayRepresentingObject(_ nodes: [Node], _ representedObject: AnyHashable) -> Node? {
2017-05-27 19:43:27 +02:00
for oneNode in nodes {
if oneNode.representedObject == representedObject {
2017-05-27 19:43:27 +02:00
return oneNode
}
}
return nil
}
}