diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 6134a34b2..71e6d67bd 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -396,10 +396,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } public func removeFeed(_ feed: Feed, from container: Container?, completion: @escaping (Result) -> Void) { - feedMetadata[feed.url] = nil delegate.removeFeed(for: self, with: feed, from: container, completion: completion) } + public func moveFeed(_ feed: Feed, from: Container, to: Container, completion: @escaping (Result) -> Void) { + delegate.moveFeed(for: self, with: feed, from: from, to: to, completion: completion) + } + public func renameFeed(_ feed: Feed, to name: String, completion: @escaping (Result) -> Void) { delegate.renameFeed(for: self, with: feed, to: name, completion: completion) } diff --git a/Frameworks/Account/AccountDelegate.swift b/Frameworks/Account/AccountDelegate.swift index 3bf3ff1c4..34b41db8f 100644 --- a/Frameworks/Account/AccountDelegate.swift +++ b/Frameworks/Account/AccountDelegate.swift @@ -36,7 +36,7 @@ protocol AccountDelegate { func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result) -> Void) func addFeed(for account: Account, with: Feed, to container: Container, completion: @escaping (Result) -> Void) func removeFeed(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result) -> Void) - + func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result) -> Void) func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result) -> Void) diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index 47d5cccb9..1ec25ca63 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -341,35 +341,28 @@ final class FeedbinAccountDelegate: AccountDelegate { } func removeFeed(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result) -> Void) { - - // This error should never happen - guard let subscriptionID = feed.subscriptionID else { - completion(.failure(FeedbinAccountDelegateError.invalidParameter)) - return + if feed.folderRelationship?.count ?? 0 > 1 { + deleteTagging(for: account, with: feed, from: container, completion: completion) + } else { + deleteSubscription(for: account, with: feed, from: container, completion: completion) } - - caller.deleteSubscription(subscriptionID: subscriptionID) { result in - switch result { - case .success: - DispatchQueue.main.async { - account.removeFeed(feed) - if let folders = account.folders { - for folder in folders { - folder.removeFeed(feed) - } - } - completion(.success(())) - } - case .failure(let error): - DispatchQueue.main.async { - let wrappedError = AccountError.wrappedError(error: error, account: account) - completion(.failure(wrappedError)) + } + + func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result) -> Void) { + if from is Account { + addFeed(for: account, with: feed, to: to, completion: completion) + } else { + deleteTagging(for: account, with: feed, from: from) { result in + switch result { + case .success: + self.addFeed(for: account, with: feed, to: to, completion: completion) + case .failure(let error): + completion(.failure(error)) } } } - } - + func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result) -> Void) { if let folder = container as? Folder, let feedID = Int(feed.feedID) { @@ -390,43 +383,16 @@ final class FeedbinAccountDelegate: AccountDelegate { } } } else { - if let account = container as? Account { - account.addFeedIfNotInAnyFolder(feed) - } DispatchQueue.main.async { + if let account = container as? Account { + account.addFeedIfNotInAnyFolder(feed) + } completion(.success(())) } } } - func removeFeed(for account: Account, from container: Container, with feed: Feed, completion: @escaping (Result) -> Void) { - - if let folder = container as? Folder, let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] { - caller.deleteTagging(taggingID: feedTaggingID) { result in - switch result { - case .success: - DispatchQueue.main.async { - folder.removeFeed(feed) - account.addFeedIfNotInAnyFolder(feed) - completion(.success(())) - } - case .failure(let error): - DispatchQueue.main.async { - let wrappedError = AccountError.wrappedError(error: error, account: account) - completion(.failure(wrappedError)) - } - } - } - } else { - if let account = container as? Account { - account.removeFeed(feed) - } - completion(.success(())) - } - - } - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { createFeed(for: account, url: feed.url, name: feed.editedName, container: container) { result in @@ -1148,5 +1114,63 @@ private extension FeedbinAccountDelegate { } } + + func deleteTagging(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result) -> Void) { + + if let folder = container as? Folder, let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] { + caller.deleteTagging(taggingID: feedTaggingID) { result in + switch result { + case .success: + DispatchQueue.main.async { + feed.folderRelationship?.removeValue(forKey: folder.name ?? "") + folder.removeFeed(feed) + account.addFeedIfNotInAnyFolder(feed) + completion(.success(())) + } + case .failure(let error): + DispatchQueue.main.async { + let wrappedError = AccountError.wrappedError(error: error, account: account) + completion(.failure(wrappedError)) + } + } + } + } else { + if let account = container as? Account { + account.removeFeed(feed) + } + completion(.success(())) + } + + } + + func deleteSubscription(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result) -> Void) { + + // This error should never happen + guard let subscriptionID = feed.subscriptionID else { + completion(.failure(FeedbinAccountDelegateError.invalidParameter)) + return + } + + caller.deleteSubscription(subscriptionID: subscriptionID) { result in + switch result { + case .success: + DispatchQueue.main.async { + account.removeFeed(feed) + if let folders = account.folders { + for folder in folders { + folder.removeFeed(feed) + } + } + completion(.success(())) + } + case .failure(let error): + DispatchQueue.main.async { + let wrappedError = AccountError.wrappedError(error: error, account: account) + completion(.failure(wrappedError)) + } + } + } + + } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index d4169bfb1..759c411b0 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -127,6 +127,12 @@ final class LocalAccountDelegate: AccountDelegate { completion(.success(())) } + func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result) -> Void) { + from.removeFeed(feed) + to.addFeed(feed) + completion(.success(())) + } + func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result) -> Void) { container.addFeed(feed) completion(.success(())) diff --git a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift index dafd0d01b..00eb7711a 100644 --- a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift +++ b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift @@ -307,18 +307,10 @@ private extension SidebarOutlineDataSource { } BatchUpdate.shared.start() - source.account?.removeFeed(feed, from: source) { result in + source.account?.moveFeed(feed, from: source, to: destination) { result in switch result { case .success: - destination.account?.addFeed(feed, to: destination) { result in - BatchUpdate.shared.end() - switch result { - case .success: - break - case .failure(let error): - NSApplication.shared.presentError(error) - } - } + BatchUpdate.shared.end() case .failure(let error): NSApplication.shared.presentError(error) }