2017-10-13 15:50:33 +02:00
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
|
//
|
|
|
|
|
// Container.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
|
// NetNewsWire
|
2017-10-08 06:41:21 +02:00
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 4/17/16.
|
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import Articles
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2018-01-23 07:00:01 +01:00
|
|
|
|
extension Notification.Name {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
|
|
|
|
public static let ChildrenDidChange = Notification.Name("ChildrenDidChange")
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
|
public protocol Container: class, ContainerIdentifiable {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2019-05-30 03:47:52 +02:00
|
|
|
|
var account: Account? { get }
|
2019-11-15 03:11:41 +01:00
|
|
|
|
var topLevelWebFeeds: Set<WebFeed> { get set }
|
2018-09-17 02:54:42 +02:00
|
|
|
|
var folders: Set<Folder>? { get set }
|
2017-10-19 03:45:09 +02:00
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func hasAtLeastOneWebFeed() -> 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?
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func removeWebFeed(_ webFeed: WebFeed)
|
|
|
|
|
func addWebFeed(_ webFeed: WebFeed)
|
2019-02-06 06:48:32 +01:00
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
//Recursive — checks subfolders
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func flattenedWebFeeds() -> Set<WebFeed>
|
|
|
|
|
func has(_ webFeed: WebFeed) -> Bool
|
|
|
|
|
func hasWebFeed(with webFeedID: String) -> Bool
|
|
|
|
|
func hasWebFeed(withURL url: String) -> Bool
|
|
|
|
|
func existingWebFeed(withWebFeedID: String) -> WebFeed?
|
|
|
|
|
func existingWebFeed(withURL url: String) -> WebFeed?
|
2017-10-08 06:41:21 +02:00
|
|
|
|
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 {
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func hasAtLeastOneWebFeed() -> Bool {
|
|
|
|
|
return topLevelWebFeeds.count > 0
|
2017-10-19 03:45:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
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? {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
guard let folders = folders else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
for folder in folders {
|
|
|
|
|
if folder.name == name {
|
2017-10-22 06:00:21 +02:00
|
|
|
|
return folder
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 15:50:33 +02:00
|
|
|
|
func objectIsChild(_ object: AnyObject) -> Bool {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
if let feed = object as? WebFeed {
|
|
|
|
|
return topLevelWebFeeds.contains(feed)
|
2018-09-17 02:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
if let folder = object as? Folder {
|
|
|
|
|
return folders?.contains(folder) ?? false
|
2017-10-13 15:50:33 +02:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func flattenedWebFeeds() -> Set<WebFeed> {
|
|
|
|
|
var feeds = Set<WebFeed>()
|
|
|
|
|
feeds.formUnion(topLevelWebFeeds)
|
2018-09-17 02:54:42 +02:00
|
|
|
|
if let folders = folders {
|
|
|
|
|
for folder in folders {
|
2019-11-15 03:11:41 +01:00
|
|
|
|
feeds.formUnion(folder.flattenedWebFeeds())
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return feeds
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func hasWebFeed(with webFeedID: String) -> Bool {
|
|
|
|
|
return existingWebFeed(withWebFeedID: webFeedID) != nil
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func hasWebFeed(withURL url: String) -> Bool {
|
|
|
|
|
return existingWebFeed(withURL: url) != nil
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func has(_ webFeed: WebFeed) -> Bool {
|
|
|
|
|
return flattenedWebFeeds().contains(webFeed)
|
2019-08-22 06:07:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func existingWebFeed(withWebFeedID webFeedID: String) -> WebFeed? {
|
|
|
|
|
for feed in flattenedWebFeeds() {
|
|
|
|
|
if feed.webFeedID == webFeedID {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
return feed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
func existingWebFeed(withURL url: String) -> WebFeed? {
|
|
|
|
|
for feed in flattenedWebFeeds() {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
if feed.url == url {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
return feed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func existingFolder(with name: String) -> Folder? {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
guard let folders = folders else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
for folder in folders {
|
|
|
|
|
if folder.name == name {
|
|
|
|
|
return folder
|
|
|
|
|
}
|
|
|
|
|
if let subFolder = folder.existingFolder(with: name) {
|
|
|
|
|
return subFolder
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 03:03:47 +01:00
|
|
|
|
func existingFolder(withID folderID: Int) -> Folder? {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
guard let folders = folders else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-11-05 03:03:47 +01:00
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
for folder in folders {
|
|
|
|
|
if folder.folderID == folderID {
|
|
|
|
|
return folder
|
|
|
|
|
}
|
|
|
|
|
if let subFolder = folder.existingFolder(withID: folderID) {
|
|
|
|
|
return subFolder
|
2017-11-05 03:03:47 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
|
func postChildrenDidChangeNotification() {
|
|
|
|
|
NotificationCenter.default.post(name: .ChildrenDidChange, object: self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|