2017-10-13 15:50:33 +02:00
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
//
|
|
|
|
// Container.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 4/17/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import Data
|
|
|
|
|
|
|
|
extension NSNotification.Name {
|
|
|
|
|
|
|
|
public static let ChildrenDidChange = Notification.Name("ChildrenDidChange")
|
|
|
|
}
|
|
|
|
|
|
|
|
public protocol Container {
|
|
|
|
|
|
|
|
var children: [AnyObject] { get }
|
2017-10-19 03:45:09 +02:00
|
|
|
|
|
|
|
func hasAtLeastOneFeed() -> Bool
|
2017-10-13 15:50:33 +02:00
|
|
|
func objectIsChild(_ object: AnyObject) -> Bool
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
func hasChildFolder(with: String) -> Bool
|
|
|
|
func childFolder(with: String) -> Folder?
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
//Recursive
|
|
|
|
func flattenedFeeds() -> Set<Feed>
|
|
|
|
func hasFeed(with feedID: String) -> Bool
|
|
|
|
func hasFeed(withURL url: String) -> Bool
|
|
|
|
func existingFeed(with feedID: String) -> Feed?
|
|
|
|
func existingFeed(withURL url: String) -> Feed?
|
|
|
|
func existingFolder(with name: String) -> Folder?
|
2017-11-05 03:03:47 +01:00
|
|
|
func existingFolder(withID: Int) -> Folder?
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
|
|
func postChildrenDidChangeNotification()
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension Container {
|
|
|
|
|
2017-10-19 03:45:09 +02:00
|
|
|
func hasAtLeastOneFeed() -> Bool {
|
|
|
|
|
|
|
|
for child in children {
|
2017-10-19 04:14:40 +02:00
|
|
|
if child is Feed {
|
2017-10-19 03:45:09 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if let folder = child as? Folder {
|
|
|
|
if folder.hasAtLeastOneFeed() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
func hasChildFolder(with name: String) -> Bool {
|
|
|
|
|
|
|
|
return childFolder(with: name) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func childFolder(with name: String) -> Folder? {
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
if let folder = child as? Folder, folder.name == name {
|
|
|
|
return folder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-13 15:50:33 +02:00
|
|
|
func objectIsChild(_ object: AnyObject) -> Bool {
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
if object === child {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
func flattenedFeeds() -> Set<Feed> {
|
|
|
|
|
|
|
|
var feeds = Set<Feed>()
|
|
|
|
|
|
|
|
for object in children {
|
|
|
|
if let feed = object as? Feed {
|
|
|
|
feeds.insert(feed)
|
|
|
|
}
|
|
|
|
else if let container = object as? Container {
|
|
|
|
feeds.formUnion(container.flattenedFeeds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return feeds
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasFeed(with feedID: String) -> Bool {
|
|
|
|
|
|
|
|
return existingFeed(with: feedID) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasFeed(withURL url: String) -> Bool {
|
|
|
|
|
|
|
|
return existingFeed(withURL: url) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func existingFeed(with feedID: String) -> Feed? {
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
|
|
|
|
if let feed = child as? Feed, feed.feedID == feedID {
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
if let container = child as? Container, let feed = container.existingFeed(with: feedID) {
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func existingFeed(withURL url: String) -> Feed? {
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
|
|
|
|
if let feed = child as? Feed, feed.url == url {
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
if let container = child as? Container, let feed = container.existingFeed(withURL: url) {
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func existingFolder(with name: String) -> Folder? {
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
|
|
|
|
if let folder = child as? Folder {
|
|
|
|
if folder.name == name {
|
|
|
|
return folder
|
|
|
|
}
|
|
|
|
if let subFolder = folder.existingFolder(with: name) {
|
|
|
|
return subFolder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-05 03:03:47 +01:00
|
|
|
func existingFolder(withID folderID: Int) -> Folder? {
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
|
|
|
|
if let folder = child as? Folder {
|
|
|
|
if folder.folderID == folderID {
|
|
|
|
return folder
|
|
|
|
}
|
|
|
|
if let subFolder = folder.existingFolder(withID: folderID) {
|
|
|
|
return subFolder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
func postChildrenDidChangeNotification() {
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .ChildrenDidChange, object: self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|