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")
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 06:51:14 +01:00
|
|
|
|
public protocol Container: class {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
var topLevelFeeds: Set<Feed> { get set }
|
|
|
|
|
var folders: Set<Folder>? { get set }
|
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-11-05 06:51:14 +01:00
|
|
|
|
func deleteFeed(_ feed: Feed)
|
2019-05-07 00:34:41 +02:00
|
|
|
|
func deleteFolder(_ folder: Folder, completion: @escaping (Result<Void, Error>) -> Void)
|
2019-02-06 06:48:32 +01:00
|
|
|
|
|
|
|
|
|
func addFeed(_ feed: Feed)
|
|
|
|
|
func addFeeds(_ feeds: Set<Feed>)
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
//Recursive — checks subfolders
|
2017-10-08 06:41:21 +02:00
|
|
|
|
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 {
|
|
|
|
|
|
2019-02-06 06:48:32 +01:00
|
|
|
|
func addFeed(_ feed: Feed) {
|
|
|
|
|
addFeeds(Set([feed]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addFeeds(_ feeds: Set<Feed>) {
|
|
|
|
|
let feedCount = topLevelFeeds.count
|
|
|
|
|
topLevelFeeds.formUnion(feeds)
|
|
|
|
|
if feedCount != topLevelFeeds.count {
|
|
|
|
|
postChildrenDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-19 03:45:09 +02:00
|
|
|
|
func hasAtLeastOneFeed() -> Bool {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
return topLevelFeeds.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 {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
if let feed = object as? Feed {
|
|
|
|
|
return topLevelFeeds.contains(feed)
|
|
|
|
|
}
|
|
|
|
|
if let folder = object as? Folder {
|
|
|
|
|
return folders?.contains(folder) ?? false
|
2017-10-13 15:50:33 +02:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
|
func flattenedFeeds() -> Set<Feed> {
|
|
|
|
|
|
|
|
|
|
var feeds = Set<Feed>()
|
2018-09-17 02:54:42 +02:00
|
|
|
|
feeds.formUnion(topLevelFeeds)
|
|
|
|
|
if let folders = folders {
|
|
|
|
|
for folder in folders {
|
|
|
|
|
feeds.formUnion(folder.flattenedFeeds())
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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? {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
for feed in flattenedFeeds() {
|
|
|
|
|
if feed.feedID == feedID {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
return feed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func existingFeed(withURL url: String) -> Feed? {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
for feed in flattenedFeeds() {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|