2017-05-27 10:43:27 -07:00
|
|
|
//
|
|
|
|
// SidebarTreeControllerDelegate.swift
|
2018-08-28 22:18:24 -07:00
|
|
|
// NetNewsWire
|
2017-05-27 10:43:27 -07:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/24/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSTree
|
2018-07-23 18:29:08 -07:00
|
|
|
import Articles
|
2017-09-24 12:24:44 -07:00
|
|
|
import Account
|
2017-05-27 10:43:27 -07:00
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
final class WebFeedTreeControllerDelegate: TreeControllerDelegate {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
2019-11-22 10:55:54 -06:00
|
|
|
var isReadFiltered = false
|
2019-11-21 15:55:50 -06:00
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
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-12-16 11:16:32 -08:00
|
|
|
if node.representedObject is SmartFeedsController {
|
|
|
|
return childNodesForSmartFeeds(node)
|
|
|
|
}
|
2017-11-19 12:59:37 -08:00
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
private extension WebFeedTreeControllerDelegate {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
2017-10-19 21:38:50 -07:00
|
|
|
func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
|
2019-11-21 15:55:50 -06:00
|
|
|
var topLevelNodes = [Node]()
|
|
|
|
|
|
|
|
// Check to see if we should show the SmartFeeds top level by checking the unreadFeed
|
2019-11-22 10:55:54 -06:00
|
|
|
if !(isReadFiltered && SmartFeedsController.shared.unreadFeed.unreadCount == 0) {
|
2019-11-21 15:55:50 -06:00
|
|
|
let smartFeedsNode = rootNode.existingOrNewChildNode(with: SmartFeedsController.shared)
|
|
|
|
smartFeedsNode.canHaveChildNodes = true
|
|
|
|
smartFeedsNode.isGroupItem = true
|
2019-11-21 16:25:00 -06:00
|
|
|
smartFeedsNode.isExpanded = true
|
2019-11-21 15:55:50 -06:00
|
|
|
topLevelNodes.append(smartFeedsNode)
|
|
|
|
}
|
2017-10-07 21:41:21 -07:00
|
|
|
|
2019-11-21 15:55:50 -06:00
|
|
|
topLevelNodes.append(contentsOf: sortedAccountNodes(rootNode))
|
|
|
|
|
|
|
|
return topLevelNodes
|
2017-11-18 16:56:36 -08:00
|
|
|
}
|
|
|
|
|
2017-12-16 11:16:32 -08:00
|
|
|
func childNodesForSmartFeeds(_ parentNode: Node) -> [Node] {
|
2019-11-21 15:55:50 -06:00
|
|
|
return SmartFeedsController.shared.smartFeeds.compactMap { (feed) -> Node? in
|
2019-11-22 10:55:54 -06:00
|
|
|
if isReadFiltered && feed.unreadCount == 0 {
|
2019-11-21 15:55:50 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return parentNode.existingOrNewChildNode(with: feed as AnyObject)
|
|
|
|
}
|
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]? {
|
|
|
|
let container = containerNode.representedObject as! Container
|
2017-10-19 21:52:45 -07:00
|
|
|
|
2018-09-16 17:54:42 -07:00
|
|
|
var children = [AnyObject]()
|
2019-11-21 15:55:50 -06:00
|
|
|
|
|
|
|
for webFeed in container.topLevelWebFeeds {
|
2019-11-22 10:55:54 -06:00
|
|
|
if !(isReadFiltered && webFeed.unreadCount == 0) {
|
2019-11-21 15:55:50 -06:00
|
|
|
children.append(webFeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 17:54:42 -07:00
|
|
|
if let folders = container.folders {
|
2019-11-21 15:55:50 -06:00
|
|
|
for folder in folders {
|
2019-11-22 10:55:54 -06:00
|
|
|
if !(isReadFiltered && folder.unreadCount == 0) {
|
2019-11-21 15:55:50 -06:00
|
|
|
children.append(folder)
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 17:54:42 -07:00
|
|
|
}
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
var updatedChildNodes = [Node]()
|
2017-10-07 21:41:21 -07:00
|
|
|
|
2018-09-16 17:54:42 -07:00
|
|
|
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? {
|
2019-11-14 20:11:41 -06:00
|
|
|
if let webFeed = representedObject as? WebFeed {
|
|
|
|
return createNode(webFeed: webFeed, parent: parent)
|
2017-05-27 10:43:27 -07:00
|
|
|
}
|
2019-11-21 15:55:50 -06:00
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
if let folder = representedObject as? Folder {
|
|
|
|
return createNode(folder: folder, parent: parent)
|
|
|
|
}
|
2019-11-21 15:55:50 -06:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-14 20:11:41 -06:00
|
|
|
func createNode(webFeed: WebFeed, parent: Node) -> Node {
|
|
|
|
return parent.createChildNode(webFeed)
|
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] {
|
2019-11-21 15:55:50 -06:00
|
|
|
let nodes = AccountManager.shared.sortedActiveAccounts.compactMap { (account) -> Node? in
|
2019-11-22 10:55:54 -06:00
|
|
|
if isReadFiltered && account.unreadCount == 0 {
|
2019-11-21 15:55:50 -06:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-18 12:43:18 -08:00
|
|
|
let accountNode = parent.existingOrNewChildNode(with: account)
|
|
|
|
accountNode.canHaveChildNodes = true
|
|
|
|
accountNode.isGroupItem = true
|
2019-11-21 16:25:00 -06:00
|
|
|
accountNode.isExpanded = true
|
2017-12-18 12:43:18 -08:00
|
|
|
return accountNode
|
|
|
|
}
|
2019-05-01 19:07:44 -05:00
|
|
|
return nodes
|
2017-11-18 16:56:36 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|