From f4bc17c8f1b96f159462e5b37bba7c2675d09468 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Wed, 29 May 2019 20:47:52 -0500 Subject: [PATCH] Refactor addFeed and removeFeed usages to be more consistent --- Frameworks/Account/Account.swift | 19 ++++----- Frameworks/Account/Container.swift | 5 ++- .../Feedbin/FeedbinAccountDelegate.swift | 2 +- Frameworks/Account/Folder.swift | 12 +----- .../LocalAccount/LocalAccountDelegate.swift | 42 ++++++------------- .../Sidebar/SidebarOutlineDataSource.swift | 21 ++++------ 6 files changed, 35 insertions(+), 66 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 1014d0d8f..d1313beb3 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -61,6 +61,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, return defaultName }() + public var account: Account? { + return self + } public let accountID: String public let type: AccountType public var nameForDisplay: String { @@ -373,11 +376,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, return feed } - func addFeed(container: Container, feed: Feed, completion: @escaping (Result) -> Void) { + public func addFeed(_ feed: Feed, to container: Container, completion: @escaping (Result) -> Void) { delegate.addFeed(for: self, to: container, with: feed, completion: completion) } - func removeFeed(_ feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { + public func removeFeed(_ feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { delegate.removeFeed(for: self, from: container, with: feed, completion: completion) } @@ -659,21 +662,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, return _flattenedFeeds } - public func removeFeed(_ feed: Feed, completion: @escaping (Result) -> Void) { - delegate.removeFeed(for: self, from: self, with: feed, completion: completion) - } - - public func addFeed(_ feed: Feed, completion: @escaping (Result) -> Void) { - delegate.addFeed(for: self, to: self, with: feed, completion: completion) - } - - func removeFeed(_ feed: Feed) { + public func removeFeed(_ feed: Feed) { topLevelFeeds.remove(feed) structureDidChange() postChildrenDidChangeNotification() } - func addFeed(_ feed: Feed) { + public func addFeed(_ feed: Feed) { topLevelFeeds.insert(feed) structureDidChange() postChildrenDidChangeNotification() diff --git a/Frameworks/Account/Container.swift b/Frameworks/Account/Container.swift index 78afa189d..bd3092894 100644 --- a/Frameworks/Account/Container.swift +++ b/Frameworks/Account/Container.swift @@ -18,6 +18,7 @@ extension Notification.Name { public protocol Container: class { + var account: Account? { get } var topLevelFeeds: Set { get set } var folders: Set? { get set } @@ -27,8 +28,8 @@ public protocol Container: class { func hasChildFolder(with: String) -> Bool func childFolder(with: String) -> Folder? - func removeFeed(_ feed: Feed, completion: @escaping (Result) -> Void) - func addFeed(_ feed: Feed, completion: @escaping (Result) -> Void) + func removeFeed(_ feed: Feed) + func addFeed(_ feed: Feed) //Recursive — checks subfolders func flattenedFeeds() -> Set diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index 8866ff7bf..58f5b5ebb 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -870,7 +870,7 @@ private extension FeedbinAccountDelegate { let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL) feed.subscriptionID = String(sub.subscriptionID) - container.addFeed(feed) { result in + account.addFeed(feed, to: container) { result in switch result { case .success: if let name = name { diff --git a/Frameworks/Account/Folder.swift b/Frameworks/Account/Folder.swift index 5bf961fb1..f2ee0551f 100644 --- a/Frameworks/Account/Folder.swift +++ b/Frameworks/Account/Folder.swift @@ -95,20 +95,12 @@ public final class Folder: DisplayNameProvider, Renamable, Container, UnreadCoun return topLevelFeeds.contains(feed) } - public func addFeed(_ feed: Feed, completion: @escaping (Result) -> Void) { - account?.addFeed(container: self, feed: feed, completion: completion) - } - - public func removeFeed(_ feed: Feed, completion: @escaping (Result) -> Void) { - account?.removeFeed(feed, from: self, completion: completion) - } - - func addFeed(_ feed: Feed) { + public func addFeed(_ feed: Feed) { topLevelFeeds.insert(feed) postChildrenDidChangeNotification() } - func removeFeed(_ feed: Feed) { + public func removeFeed(_ feed: Feed) { topLevelFeeds.remove(feed) postChildrenDidChangeNotification() } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 2cbfe6d67..8ce2848aa 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -123,36 +123,26 @@ final class LocalAccountDelegate: AccountDelegate { } func deleteFeed(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result) -> Void) { - if let account = container as? Account { - account.removeFeed(feed) - } - if let folder = container as? Folder { - folder.removeFeed(feed) - } + container?.removeFeed(feed) completion(.success(())) } func addFeed(for account: Account, to container: Container, with feed: Feed, completion: @escaping (Result) -> Void) { + container.addFeed(feed) + completion(.success(())) + } + + func removeFeed(for account: Account, from container: Container, with feed: Feed, completion: @escaping (Result) -> Void) { + container.removeFeed(feed) + completion(.success(())) + } + + func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { if let folder = container as? Folder { folder.addFeed(feed) } else if let account = container as? Account { account.addFeed(feed) } - completion(.success(())) - } - - func removeFeed(for account: Account, from container: Container, with feed: Feed, completion: @escaping (Result) -> Void) { - if let account = container as? Account { - account.removeFeed(feed) - } - if let folder = container as? Folder { - folder.removeFeed(feed) - } - completion(.success(())) - } - - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { - container.addFeed(feed, completion: completion) } func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result) -> Void) { @@ -210,14 +200,8 @@ extension LocalAccountDelegate: FeedFinderDelegate { feed.editedName = self.createFeedName - self.createFeedContainer?.addFeed(feed) { result in - switch result { - case .success: - self.createFeedCompletion?(.success(feed)) - case .failure(let error): - self.createFeedCompletion?(.failure(error)) - } - } + self.createFeedContainer?.addFeed(feed) + self.createFeedCompletion?(.success(feed)) } diff --git a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift index 214f8bd7d..8c3ecea0e 100644 --- a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift +++ b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift @@ -285,13 +285,11 @@ private extension SidebarOutlineDataSource { } func copyInAccount(node: Node, to parentNode: Node) { - guard let feed = node.representedObject as? Feed else { + guard let feed = node.representedObject as? Feed, let destination = parentNode.representedObject as? Container else { return } - let destination = parentNode.representedObject as? Container - - destination?.addFeed(feed) { result in + destination.account?.addFeed(feed, to: destination) { result in switch result { case .success: break @@ -302,18 +300,17 @@ private extension SidebarOutlineDataSource { } func moveInAccount(node: Node, to parentNode: Node) { - guard let feed = node.representedObject as? Feed else { + guard let feed = node.representedObject as? Feed, + let source = node.parent?.representedObject as? Container, + let destination = parentNode.representedObject as? Container else { return } - let source = node.parent?.representedObject as? Container - let destination = parentNode.representedObject as? Container - BatchUpdate.shared.start() - source?.removeFeed(feed) { result in + source.account?.removeFeed(feed, from: source) { result in switch result { case .success: - destination?.addFeed(feed) { result in + destination.account?.addFeed(feed, to: destination) { result in BatchUpdate.shared.end() switch result { case .success: @@ -336,7 +333,7 @@ private extension SidebarOutlineDataSource { } if let existingFeed = destinationAccount.existingFeed(withURL: feed.url) { - destinationContainer.addFeed(existingFeed) { result in + destinationAccount.addFeed(existingFeed, to: destinationContainer) { result in switch result { case .success: break @@ -368,7 +365,7 @@ private extension SidebarOutlineDataSource { if let existingFeed = destinationAccount.existingFeed(withURL: feed.url) { BatchUpdate.shared.start() - destinationContainer.addFeed(existingFeed) { result in + destinationAccount.addFeed(existingFeed, to: destinationContainer) { result in switch result { case .success: sourceAccount.deleteFeed(feed, from: sourceContainer) { result in