2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// SidebarTreeControllerDelegate.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/24/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSTree
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2017-09-24 21:24:44 +02:00
|
|
|
import Account
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2019-04-18 11:56:15 +02:00
|
|
|
final class FeedTreeControllerDelegate: TreeControllerDelegate {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
func treeController(treeController: TreeController, childNodesFor node: Node) -> [Node]? {
|
|
|
|
|
|
|
|
if node.isRoot {
|
|
|
|
return childNodesForRootNode(node)
|
|
|
|
}
|
2017-11-19 21:59:37 +01:00
|
|
|
if node.representedObject is Container {
|
|
|
|
return childNodesForContainerNode(node)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2017-12-16 20:16:32 +01:00
|
|
|
if node.representedObject is SmartFeedsController {
|
|
|
|
return childNodesForSmartFeeds(node)
|
|
|
|
}
|
2017-11-19 21:59:37 +01:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 11:56:15 +02:00
|
|
|
private extension FeedTreeControllerDelegate {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-10-20 06:38:50 +02:00
|
|
|
func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
2017-12-16 20:16:32 +01:00
|
|
|
// The top-level nodes are Smart Feeds and accounts.
|
|
|
|
|
|
|
|
let smartFeedsNode = rootNode.existingOrNewChildNode(with: SmartFeedsController.shared)
|
|
|
|
smartFeedsNode.canHaveChildNodes = true
|
|
|
|
smartFeedsNode.isGroupItem = true
|
2017-11-19 01:56:36 +01:00
|
|
|
|
2017-12-16 20:16:32 +01:00
|
|
|
return [smartFeedsNode] + sortedAccountNodes(rootNode)
|
2017-11-19 01:56:36 +01:00
|
|
|
}
|
|
|
|
|
2017-12-16 20:16:32 +01:00
|
|
|
func childNodesForSmartFeeds(_ parentNode: Node) -> [Node] {
|
2017-11-19 01:56:36 +01:00
|
|
|
|
2017-12-16 20:16:32 +01:00
|
|
|
return SmartFeedsController.shared.smartFeeds.map { parentNode.existingOrNewChildNode(with: $0) }
|
2017-10-20 06:52:45 +02:00
|
|
|
}
|
2017-10-20 06:38:50 +02:00
|
|
|
|
2017-11-19 21:59:37 +01:00
|
|
|
func childNodesForContainerNode(_ containerNode: Node) -> [Node]? {
|
2017-10-20 06:52:45 +02:00
|
|
|
|
2017-11-19 21:59:37 +01:00
|
|
|
let container = containerNode.representedObject as! Container
|
2017-10-20 06:52:45 +02:00
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
var children = [AnyObject]()
|
|
|
|
children.append(contentsOf: Array(container.topLevelFeeds))
|
|
|
|
if let folders = container.folders {
|
|
|
|
children.append(contentsOf: Array(folders))
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
var updatedChildNodes = [Node]()
|
2017-10-08 06:41:21 +02:00
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
children.forEach { (representedObject) in
|
2017-10-08 06:41:21 +02:00
|
|
|
|
2017-10-20 06:52:45 +02:00
|
|
|
if let existingNode = containerNode.childNodeRepresentingObject(representedObject) {
|
2017-05-27 19:43:27 +02:00
|
|
|
if !updatedChildNodes.contains(existingNode) {
|
|
|
|
updatedChildNodes += [existingNode]
|
2017-10-20 06:38:50 +02:00
|
|
|
return
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-20 06:38:50 +02:00
|
|
|
|
2017-10-20 06:52:45 +02:00
|
|
|
if let newNode = self.createNode(representedObject: representedObject, parent: containerNode) {
|
2017-05-27 19:43:27 +02:00
|
|
|
updatedChildNodes += [newNode]
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 06:38:50 +02:00
|
|
|
|
2017-11-19 01:56:36 +01:00
|
|
|
return updatedChildNodes.sortedAlphabeticallyWithFoldersAtEnd()
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2017-10-20 06:52:45 +02:00
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
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)
|
|
|
|
}
|
2017-11-19 01:56:36 +01:00
|
|
|
if let account = representedObject as? Account {
|
|
|
|
return createNode(account: account, parent: parent)
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNode(feed: Feed, parent: Node) -> Node {
|
2017-11-19 01:56:36 +01:00
|
|
|
|
|
|
|
return parent.createChildNode(feed)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createNode(folder: Folder, parent: Node) -> Node {
|
2017-11-19 01:56:36 +01:00
|
|
|
|
|
|
|
let node = parent.createChildNode(folder)
|
2017-05-27 19:43:27 +02:00
|
|
|
node.canHaveChildNodes = true
|
|
|
|
return node
|
|
|
|
}
|
2017-11-19 01:56:36 +01: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] {
|
|
|
|
|
2019-05-02 13:01:30 +02:00
|
|
|
let nodes = AccountManager.shared.sortedActiveAccounts.map { (account) -> Node in
|
2017-12-18 21:43:18 +01:00
|
|
|
let accountNode = parent.existingOrNewChildNode(with: account)
|
|
|
|
accountNode.canHaveChildNodes = true
|
|
|
|
accountNode.isGroupItem = true
|
|
|
|
return accountNode
|
|
|
|
}
|
2019-05-02 02:07:44 +02:00
|
|
|
return nodes
|
2017-11-19 01:56:36 +01:00
|
|
|
}
|
|
|
|
|
2017-11-05 07:05:20 +01:00
|
|
|
func nodeInArrayRepresentingObject(_ nodes: [Node], _ representedObject: AnyObject) -> Node? {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
for oneNode in nodes {
|
2017-11-05 07:05:20 +01:00
|
|
|
if oneNode.representedObject === representedObject {
|
2017-05-27 19:43:27 +02:00
|
|
|
return oneNode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|