NetNewsWire/Shared/Tree/FolderTreeControllerDelegate.swift

54 lines
1.3 KiB
Swift
Raw Normal View History

2017-05-27 10:43:27 -07:00
//
// FolderTreeControllerDelegate.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
2017-05-27 10:43:27 -07:00
//
// Created by Brent Simmons on 8/10/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import RSTree
import Articles
2017-09-24 12:24:44 -07:00
import Account
2017-05-27 10:43:27 -07:00
final class FolderTreeControllerDelegate: TreeControllerDelegate {
func treeController(treeController: TreeController, childNodesFor node: Node) -> [Node]? {
2019-05-01 14:03:59 -05:00
return node.isRoot ? childNodesForRootNode(node) : childNodes(node)
2017-05-27 10:43:27 -07:00
}
}
private extension FolderTreeControllerDelegate {
func childNodesForRootNode(_ node: Node) -> [Node]? {
let accountNodes: [Node] = AccountManager.shared.sortedActiveAccounts.map { account in
2019-05-01 14:03:59 -05:00
let accountNode = Node(representedObject: account, parent: node)
accountNode.canHaveChildNodes = true
return accountNode
}
return accountNodes
2019-05-01 14:03:59 -05:00
}
func childNodes(_ node: Node) -> [Node]? {
guard let account = node.representedObject as? Account, let folders = account.folders else {
return nil
2017-05-27 10:43:27 -07:00
}
2019-05-01 14:03:59 -05:00
let folderNodes: [Node] = folders.map { createNode($0, parent: node) }
2017-11-18 16:56:36 -08:00
return folderNodes.sortedAlphabetically()
2019-05-01 14:03:59 -05:00
2017-05-27 10:43:27 -07:00
}
func createNode(_ folder: Folder, parent: Node) -> Node {
let node = Node(representedObject: folder, parent: parent)
2017-05-27 10:43:27 -07:00
node.canHaveChildNodes = false
return node
}
2019-05-01 14:03:59 -05:00
2017-05-27 10:43:27 -07:00
}