2017-05-27 10:43:27 -07:00
|
|
|
|
//
|
|
|
|
|
// SidebarTreeControllerDelegate.swift
|
|
|
|
|
// Evergreen
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/24/16.
|
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSTree
|
2017-09-17 12:22:15 -07:00
|
|
|
|
import Data
|
2017-09-24 12:24:44 -07:00
|
|
|
|
import Account
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
final class SidebarTreeControllerDelegate: TreeControllerDelegate {
|
|
|
|
|
|
|
|
|
|
func treeController(treeController: TreeController, childNodesFor node: Node) -> [Node]? {
|
|
|
|
|
|
|
|
|
|
if node.isRoot {
|
|
|
|
|
return childNodesForRootNode(node)
|
|
|
|
|
}
|
2017-11-19 12:59:37 -08:00
|
|
|
|
if node.representedObject is Container {
|
|
|
|
|
return childNodesForContainerNode(node)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2017-11-19 12:59:37 -08:00
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension SidebarTreeControllerDelegate {
|
|
|
|
|
|
2017-10-19 21:38:50 -07:00
|
|
|
|
func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
|
2017-10-07 21:41:21 -07:00
|
|
|
|
|
2017-11-18 16:56:36 -08:00
|
|
|
|
// The top-level nodes are pseudo-feeds (All Unread, Starred, etc.) and accounts.
|
|
|
|
|
|
2017-11-19 12:59:37 -08:00
|
|
|
|
return pseudoFeedNodes(rootNode) + sortedAccountNodes(rootNode)
|
2017-11-18 16:56:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 12:59:37 -08:00
|
|
|
|
func pseudoFeedNodes(_ rootNode: Node) -> [Node] {
|
2017-11-18 16:56:36 -08:00
|
|
|
|
|
2017-11-19 12:59:37 -08:00
|
|
|
|
// The appDelegate’s pseudoFeeds are already sorted properly.
|
|
|
|
|
return appDelegate.pseudoFeeds.map { rootNode.createChildNode($0) }
|
2017-10-19 21:52:45 -07:00
|
|
|
|
}
|
2017-10-19 21:38:50 -07:00
|
|
|
|
|
2017-11-19 12:59:37 -08:00
|
|
|
|
func childNodesForContainerNode(_ containerNode: Node) -> [Node]? {
|
2017-10-19 21:52:45 -07:00
|
|
|
|
|
2017-11-19 12:59:37 -08:00
|
|
|
|
let container = containerNode.representedObject as! Container
|
2017-10-19 21:52:45 -07:00
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
var updatedChildNodes = [Node]()
|
2017-10-07 21:41:21 -07:00
|
|
|
|
|
2017-11-19 12:59:37 -08:00
|
|
|
|
container.children.forEach { (representedObject) in
|
2017-10-07 21:41:21 -07:00
|
|
|
|
|
2017-10-19 21:52:45 -07:00
|
|
|
|
if let existingNode = containerNode.childNodeRepresentingObject(representedObject) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
if !updatedChildNodes.contains(existingNode) {
|
|
|
|
|
updatedChildNodes += [existingNode]
|
2017-10-19 21:38:50 -07:00
|
|
|
|
return
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-19 21:38:50 -07:00
|
|
|
|
|
2017-10-19 21:52:45 -07:00
|
|
|
|
if let newNode = self.createNode(representedObject: representedObject, parent: containerNode) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
updatedChildNodes += [newNode]
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-19 21:38:50 -07:00
|
|
|
|
|
2017-11-18 16:56:36 -08:00
|
|
|
|
return updatedChildNodes.sortedAlphabeticallyWithFoldersAtEnd()
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2017-10-19 21:52:45 -07:00
|
|
|
|
|
2017-10-07 21:41:21 -07:00
|
|
|
|
func createNode(representedObject: Any, parent: Node) -> Node? {
|
2017-05-27 10:43:27 -07: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)
|
|
|
|
|
}
|
2017-11-18 16:56:36 -08:00
|
|
|
|
if let account = representedObject as? Account {
|
|
|
|
|
return createNode(account: account, parent: parent)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createNode(feed: Feed, parent: Node) -> Node {
|
2017-11-18 16:56:36 -08:00
|
|
|
|
|
|
|
|
|
return parent.createChildNode(feed)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createNode(folder: Folder, parent: Node) -> Node {
|
2017-11-18 16:56:36 -08:00
|
|
|
|
|
|
|
|
|
let node = parent.createChildNode(folder)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
node.canHaveChildNodes = true
|
|
|
|
|
return node
|
|
|
|
|
}
|
2017-11-18 16:56:36 -08:00
|
|
|
|
|
|
|
|
|
func createNode(account: Account, parent: Node) -> Node {
|
|
|
|
|
|
|
|
|
|
let node = parent.createChildNode(account)
|
|
|
|
|
node.canHaveChildNodes = true
|
|
|
|
|
node.isGroupItem = true
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sortedAccountNodes(_ parent: Node) -> [Node] {
|
|
|
|
|
|
|
|
|
|
let nodes = AccountManager.shared.accounts.map { createNode(account: $0, parent: parent) }
|
|
|
|
|
return nodes.sortedAlphabetically()
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-04 23:05:20 -07:00
|
|
|
|
func nodeInArrayRepresentingObject(_ nodes: [Node], _ representedObject: AnyObject) -> Node? {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
for oneNode in nodes {
|
2017-11-04 23:05:20 -07:00
|
|
|
|
if oneNode.representedObject === representedObject {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return oneNode
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|