From 4a4ece71f9ee3e8395e39430132a83011706a0b2 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 28 Mar 2024 09:28:16 -0700 Subject: [PATCH] Convert removeFeed to async await. --- Account/Sources/Account/Account.swift | 5 ++- Account/Sources/Account/AccountDelegate.swift | 2 +- .../CloudKit/CloudKitAccountDelegate.swift | 17 +++++++- .../Feedbin/FeedbinAccountDelegate.swift | 17 +++++++- .../Feedly/FeedlyAccountDelegate.swift | 17 +++++++- .../LocalAccount/LocalAccountDelegate.swift | 4 +- .../NewsBlur/NewsBlurAccountDelegate.swift | 17 +++++++- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 17 +++++++- Mac/Scriptability/Folder+Scriptability.swift | 11 +++--- Shared/Commands/DeleteCommand.swift | 17 +++++--- iOS/Feeds/FeedsViewController+Drop.swift | 39 +++++++------------ 11 files changed, 119 insertions(+), 44 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 47530ffb0..8f63b795c 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -624,8 +624,9 @@ public enum FetchType { return feed } - public func removeFeed(_ feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { - delegate.removeFeed(for: self, with: feed, from: container, completion: completion) + public func removeFeed(_ feed: Feed, from container: Container) async throws { + + try await delegate.removeFeed(for: self, with: feed, from: container) } public func moveFeed(_ feed: Feed, from: Container, to: Container, completion: @escaping (Result) -> Void) { diff --git a/Account/Sources/Account/AccountDelegate.swift b/Account/Sources/Account/AccountDelegate.swift index 0f5d5a073..8533c132b 100644 --- a/Account/Sources/Account/AccountDelegate.swift +++ b/Account/Sources/Account/AccountDelegate.swift @@ -39,7 +39,7 @@ import Secrets func createFeed(for account: Account, url: String, name: String?, container: Container, validateFeed: Bool, completion: @escaping (Result) -> Void) func renameFeed(for account: Account, with feed: Feed, to name: String) async throws func addFeed(for account: Account, with: Feed, to container: Container) async throws - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) + func removeFeed(for account: Account, with feed: Feed, from container: Container) async throws 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) async throws diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 60de2cd5a..d33201ece 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -285,7 +285,22 @@ enum CloudKitAccountDelegateError: LocalizedError { } } - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { + func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.removeFeed(for: account, with: feed, from: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { removeFeedFromCloud(for: account, with: feed, from: container) { result in switch result { case .success: diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 7e1cd57eb..fbd4c4184 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -541,7 +541,22 @@ final class FeedbinAccountDelegate: AccountDelegate { } - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { + func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.removeFeed(for: account, with: feed, from: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { if feed.folderRelationship?.count ?? 0 > 1 { deleteTagging(for: account, with: feed, from: container, completion: completion) } else { diff --git a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 36efd4ceb..abd02792e 100644 --- a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -540,7 +540,22 @@ final class FeedlyAccountDelegate: AccountDelegate { } } - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { + func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.removeFeed(for: account, with: feed, from: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { guard let folder = container as? Folder, let collectionId = folder.externalID else { return DispatchQueue.main.async { completion(.failure(FeedlyAccountDelegateError.unableToRemoveFeed(feed.nameForDisplay))) diff --git a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index 6e44f4e40..f3a27b070 100644 --- a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -96,9 +96,9 @@ final class LocalAccountDelegate: AccountDelegate { feed.editedName = name } - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { + func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { + container.removeFeed(feed) - completion(.success(())) } func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result) -> Void) { diff --git a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index b62a0d8cc..329567cf2 100644 --- a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -610,7 +610,22 @@ final class NewsBlurAccountDelegate: AccountDelegate { completion(.success(())) } - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> ()) { + func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.removeFeed(for: account, with: feed, from: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> ()) { deleteFeed(for: account, with: feed, from: container, completion: completion) } diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 37f27d701..115c02c6e 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -560,7 +560,22 @@ final class ReaderAPIAccountDelegate: AccountDelegate { } - func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { + func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.removeFeed(for: account, with: feed, from: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result) -> Void) { guard let subscriptionID = feed.externalID else { completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) return diff --git a/Mac/Scriptability/Folder+Scriptability.swift b/Mac/Scriptability/Folder+Scriptability.swift index 0def7becb..1ce320f88 100644 --- a/Mac/Scriptability/Folder+Scriptability.swift +++ b/Mac/Scriptability/Folder+Scriptability.swift @@ -51,11 +51,12 @@ import Core } @MainActor func deleteElement(_ element:ScriptingObject) { - if let scriptableFeed = element as? ScriptableFeed { - BatchUpdate.shared.perform { - folder.account?.removeFeed(scriptableFeed.feed, from: folder) { result in } - } - } + // TODO: fix this +// if let scriptableFeed = element as? ScriptableFeed { +// BatchUpdate.shared.perform { +// folder.account?.removeFeed(scriptableFeed.feed, from: folder) { result in } +// } +// } } // MARK: --- handle NSCreateCommand --- diff --git a/Shared/Commands/DeleteCommand.swift b/Shared/Commands/DeleteCommand.swift index 8057d3351..09de26a71 100644 --- a/Shared/Commands/DeleteCommand.swift +++ b/Shared/Commands/DeleteCommand.swift @@ -152,12 +152,19 @@ import Core } BatchUpdate.shared.start() - account?.removeFeed(feed, from: container) { result in - BatchUpdate.shared.end() - completion() - self.checkResult(result) + + Task { @MainActor in + do { + try await account?.removeFeed(feed, from: container) + BatchUpdate.shared.end() + completion() + } catch { + BatchUpdate.shared.end() + completion() + self.errorHandler(error) + } } - + } else if let folder = folder { BatchUpdate.shared.start() diff --git a/iOS/Feeds/FeedsViewController+Drop.swift b/iOS/Feeds/FeedsViewController+Drop.swift index 3cc91c5dc..11e022fc5 100644 --- a/iOS/Feeds/FeedsViewController+Drop.swift +++ b/iOS/Feeds/FeedsViewController+Drop.swift @@ -125,17 +125,8 @@ extension SidebarViewController: UITableViewDropDelegate { do { try await destinationContainer.account?.addFeed(existingFeed, to: destinationContainer) - - sourceContainer.account?.removeFeed(feed, from: sourceContainer) { result in - BatchUpdate.shared.end() - switch result { - case .success: - break - case .failure(let error): - self.presentError(error) - } - } - + try await sourceContainer.account?.removeFeed(feed, from: sourceContainer) + BatchUpdate.shared.end() } catch { BatchUpdate.shared.end() self.presentError(error) @@ -148,23 +139,23 @@ extension SidebarViewController: UITableViewDropDelegate { destinationContainer.account?.createFeed(url: feed.url, name: feed.editedName, container: destinationContainer, validateFeed: false) { result in switch result { case .success: - sourceContainer.account?.removeFeed(feed, from: sourceContainer) { result in - BatchUpdate.shared.end() - switch result { - case .success: - break - case .failure(let error): + + Task { @MainActor in + do { + try await sourceContainer.account?.removeFeed(feed, from: sourceContainer) + BatchUpdate.shared.end() + } catch { + BatchUpdate.shared.end() self.presentError(error) } } - case .failure(let error): - BatchUpdate.shared.end() - self.presentError(error) + + case .failure(let error): + BatchUpdate.shared.end() + self.presentError(error) + } } + } - } - } - - }