NetNewsWire/Shared/Tree/WebFeedTreeControllerDelega...

154 lines
4.1 KiB
Swift
Raw Normal View History

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
import Articles
2017-09-24 21:24:44 +02:00
import Account
2017-05-27 19:43:27 +02:00
final class WebFeedTreeControllerDelegate: TreeControllerDelegate {
2017-05-27 19:43:27 +02:00
private var filterExceptions = Set<FeedIdentifier>()
2019-11-22 17:55:54 +01:00
var isReadFiltered = false
func addFilterException(_ feedID: FeedIdentifier) {
filterExceptions.insert(feedID)
}
func resetFilterExceptions() {
filterExceptions = Set<FeedIdentifier>()
}
2017-05-27 19:43:27 +02:00
func treeController(treeController: TreeController, childNodesFor node: Node) -> [Node]? {
if node.isRoot {
return childNodesForRootNode(node)
}
if node.representedObject is Container {
return childNodesForContainerNode(node)
2017-05-27 19:43:27 +02:00
}
if node.representedObject is SmartFeedsController {
return childNodesForSmartFeeds(node)
}
2017-05-27 19:43:27 +02:00
return nil
}
}
private extension WebFeedTreeControllerDelegate {
2017-05-27 19:43:27 +02:00
func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
var topLevelNodes = [Node]()
let smartFeedsNode = rootNode.existingOrNewChildNode(with: SmartFeedsController.shared)
smartFeedsNode.canHaveChildNodes = true
smartFeedsNode.isGroupItem = true
topLevelNodes.append(smartFeedsNode)
topLevelNodes.append(contentsOf: sortedAccountNodes(rootNode))
return topLevelNodes
2017-11-19 01:56:36 +01:00
}
func childNodesForSmartFeeds(_ parentNode: Node) -> [Node] {
return SmartFeedsController.shared.smartFeeds.compactMap { (feed) -> Node? in
// All Smart Feeds should remain visible despite the Hide Read Feeds setting
return parentNode.existingOrNewChildNode(with: feed as AnyObject)
}
}
func childNodesForContainerNode(_ containerNode: Node) -> [Node]? {
let container = containerNode.representedObject as! Container
var children = [AnyObject]()
for webFeed in container.topLevelWebFeeds {
if let feedID = webFeed.feedID, !(!filterExceptions.contains(feedID) && isReadFiltered && webFeed.unreadCount == 0) {
children.append(webFeed)
}
}
if let folders = container.folders {
for folder in folders {
if let feedID = folder.feedID, !(!filterExceptions.contains(feedID) && isReadFiltered && folder.unreadCount == 0) {
children.append(folder)
}
}
}
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-11-19 01:56:36 +01:00
return updatedChildNodes.sortedAlphabeticallyWithFoldersAtEnd()
2017-05-27 19:43:27 +02:00
}
func createNode(representedObject: Any, parent: Node) -> Node? {
if let webFeed = representedObject as? WebFeed {
return createNode(webFeed: webFeed, parent: parent)
2017-05-27 19:43:27 +02:00
}
2017-05-27 19:43:27 +02:00
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(webFeed: WebFeed, parent: Node) -> Node {
return parent.createChildNode(webFeed)
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] {
let nodes = AccountManager.shared.sortedActiveAccounts.compactMap { (account) -> Node? in
let accountNode = parent.existingOrNewChildNode(with: account)
accountNode.canHaveChildNodes = true
accountNode.isGroupItem = true
return accountNode
}
return nodes
2017-11-19 01:56:36 +01:00
}
func nodeInArrayRepresentingObject(_ nodes: [Node], _ representedObject: AnyObject) -> 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
}
}