From a7ba7e3b4afeb7ec76b8df2232dcbd2663513e4b Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 27 Mar 2024 17:18:17 -0700 Subject: [PATCH] Convert restoreFeed to async/await. --- Account/Sources/Account/Account.swift | 6 ++-- Account/Sources/Account/AccountDelegate.swift | 2 +- .../CloudKit/CloudKitAccountDelegate.swift | 34 ++++++++++--------- .../Feedbin/FeedbinAccountDelegate.swift | 17 +++++++++- .../Feedly/FeedlyAccountDelegate.swift | 17 +++++++++- .../LocalAccount/LocalAccountDelegate.swift | 6 ++-- .../NewsBlur/NewsBlurAccountDelegate.swift | 17 +++++++++- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 17 +++++++++- Shared/Commands/DeleteCommand.swift | 14 +++++--- 9 files changed, 100 insertions(+), 30 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 32413938b..9f18ec702 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -635,11 +635,13 @@ public enum FetchType { delegate.renameFeed(for: self, with: feed, to: name, completion: completion) } - public func restoreFeed(_ feed: Feed, container: Container, completion: @escaping (Result) -> Void) { - delegate.restoreFeed(for: self, feed: feed, container: container, completion: completion) + public func restoreFeed(_ feed: Feed, container: Container) async throws { + + try await delegate.restoreFeed(for: self, feed: feed, container: container) } public func addFolder(_ name: String) async throws -> Folder { + try await delegate.createFolder(for: self, name: name) } diff --git a/Account/Sources/Account/AccountDelegate.swift b/Account/Sources/Account/AccountDelegate.swift index b1f962741..35c938bbb 100644 --- a/Account/Sources/Account/AccountDelegate.swift +++ b/Account/Sources/Account/AccountDelegate.swift @@ -42,7 +42,7 @@ import Secrets 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 restoreFeed(for account: Account, feed: Feed, container: Container) async throws func restoreFolder(for account: Account, folder: Folder) async throws func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index e00c4abbb..0f119e90c 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -321,17 +321,21 @@ enum CloudKitAccountDelegateError: LocalizedError { } } - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { - createFeed(for: account, url: feed.url, name: feed.editedName, container: container, validateFeed: true) { result in - switch result { - case .success: - completion(.success(())) - case .failure(let error): - completion(.failure(error)) + func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.createFeed(for: account, url: feed.url, name: feed.editedName, container: container, validateFeed: true) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } } } } - + func createFolder(for account: Account, name: String) async throws -> Folder { try await withCheckedThrowingContinuation { continuation in @@ -471,17 +475,15 @@ enum CloudKitAccountDelegateError: LocalizedError { folder.topLevelFeeds.remove(feed) group.enter() - self.restoreFeed(for: account, feed: feed, container: folder) { result in - self.refreshProgress.completeTask() - group.leave() - switch result { - case .success: - break - case .failure(let error): + + Task { @MainActor in + do { + try await self.restoreFeed(for: account, feed: feed, container: folder) + } catch { os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription) } + group.leave() } - } group.notify(queue: DispatchQueue.main) { diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 7119b594c..5342ac2c3 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -551,7 +551,22 @@ final class FeedbinAccountDelegate: AccountDelegate { } - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { + func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.restoreFeed(for: account, feed: feed, container: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { if let existingFeed = account.existingFeed(withURL: feed.url) { account.addFeed(existingFeed, to: container) { result in diff --git a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index ca807d6d0..a776adc61 100644 --- a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -533,7 +533,22 @@ final class FeedlyAccountDelegate: AccountDelegate { to.addFeed(feed) } - @MainActor func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { + func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.restoreFeed(for: account, feed: feed, container: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { if let existingFeed = account.existingFeed(withURL: feed.url) { account.addFeed(existingFeed, to: container) { result in switch result { diff --git a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index 9c81dbc8a..0bb6271bc 100644 --- a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -112,11 +112,11 @@ final class LocalAccountDelegate: AccountDelegate { completion(.success(())) } - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { + func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { + container.addFeed(feed) - completion(.success(())) } - + func createFolder(for account: Account, name: String) async throws -> Folder { guard let folder = account.ensureFolder(with: name) else { diff --git a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index a677e65cf..1926ae612 100644 --- a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -581,7 +581,22 @@ final class NewsBlurAccountDelegate: AccountDelegate { } } - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> ()) { + func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.restoreFeed(for: account, feed: feed, container: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> ()) { if let existingFeed = account.existingFeed(withURL: feed.url) { account.addFeed(existingFeed, to: container) { result in switch result { diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 1420a94f5..8853d07cf 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -603,7 +603,22 @@ final class ReaderAPIAccountDelegate: AccountDelegate { } } - func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { + func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { + + try await withCheckedThrowingContinuation { continuation in + + self.restoreFeed(for: account, feed: feed, container: container) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + + private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result) -> Void) { if let existingFeed = account.existingFeed(withURL: feed.url) { account.addFeed(existingFeed, to: container) { result in diff --git a/Shared/Commands/DeleteCommand.swift b/Shared/Commands/DeleteCommand.swift index 7a9fee033..a151a2db5 100644 --- a/Shared/Commands/DeleteCommand.swift +++ b/Shared/Commands/DeleteCommand.swift @@ -187,11 +187,17 @@ import Core } BatchUpdate.shared.start() - account.restoreFeed(feed, container: container) { result in - BatchUpdate.shared.end() - self.checkResult(result) - } + Task { @MainActor in + + do { + try await account.restoreFeed(feed, container: container) + BatchUpdate.shared.end() + } catch { + BatchUpdate.shared.end() + self.errorHandler(error) + } + } } private func restoreFolder() {