From 28282a40c58f56dfe9731441dad7a40ef3cab3db Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 6 Nov 2025 19:20:08 -0800 Subject: [PATCH] Convert to async/await. --- Modules/Account/Sources/Account/Account.swift | 11 +- .../Sources/Account/AccountDelegate.swift | 2 +- .../Sources/Account/AccountManager.swift | 12 +- .../CloudKit/CloudKitAccountDelegate.swift | 10 +- .../Feedbin/FeedbinAccountDelegate.swift | 181 ++++++------------ .../Feedly/FeedlyAccountDelegate.swift | 10 +- .../LocalAccount/LocalAccountDelegate.swift | 3 +- .../NewsBlur/NewsBlurAccountDelegate.swift | 10 +- 8 files changed, 94 insertions(+), 145 deletions(-) diff --git a/Modules/Account/Sources/Account/Account.swift b/Modules/Account/Sources/Account/Account.swift index 0f37e761e..2ebf9eeb8 100644 --- a/Modules/Account/Sources/Account/Account.swift +++ b/Modules/Account/Sources/Account/Account.swift @@ -398,15 +398,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, try await delegate.refreshAll(for: self) } - public func sendArticleStatus(completion: ((Result) -> Void)? = nil) { - delegate.sendArticleStatus(for: self) { result in - switch result { - case .success: - completion?(.success(())) - case .failure(let error): - completion?(.failure(error)) - } - } + @MainActor public func sendArticleStatus() async throws { + try await delegate.sendArticleStatus(for: self) } public func syncArticleStatus(completion: ((Result) -> Void)? = nil) { diff --git a/Modules/Account/Sources/Account/AccountDelegate.swift b/Modules/Account/Sources/Account/AccountDelegate.swift index 248762d09..5ec36e9d2 100644 --- a/Modules/Account/Sources/Account/AccountDelegate.swift +++ b/Modules/Account/Sources/Account/AccountDelegate.swift @@ -27,7 +27,7 @@ protocol AccountDelegate { @MainActor func refreshAll(for account: Account) async throws func syncArticleStatus(for account: Account, completion: ((Result) -> Void)?) - func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) + @MainActor func sendArticleStatus(for account: Account) async throws func refreshArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result) -> Void) diff --git a/Modules/Account/Sources/Account/AccountManager.swift b/Modules/Account/Sources/Account/AccountManager.swift index 95b6fa8bf..4ec94fd28 100644 --- a/Modules/Account/Sources/Account/AccountManager.swift +++ b/Modules/Account/Sources/Account/AccountManager.swift @@ -296,16 +296,10 @@ public final class AccountManager: UnreadCountProvider { } public func sendArticleStatusAll(completion: (() -> Void)? = nil) { - let group = DispatchGroup() - - for account in activeAccounts { - group.enter() - account.sendArticleStatus() { _ in - group.leave() + Task { @MainActor in + for account in activeAccounts { + try? await account.sendArticleStatus() } - } - - group.notify(queue: DispatchQueue.global(qos: .background)) { completion?() } } diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 6ae0f2bcd..da69c5c35 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -131,7 +131,15 @@ final class CloudKitAccountDelegate: AccountDelegate { } } - func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { + @MainActor func sendArticleStatus(for account: Account) async throws { + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + continuation.resume(with: result) + } + } + } + + private func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { sendArticleStatus(for: account, showProgress: false, completion: completion) } diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 2f6d2d33d..bca9edb91 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -84,86 +84,44 @@ final class FeedbinAccountDelegate: AccountDelegate { } func syncArticleStatus(for account: Account, completion: ((Result) -> Void)? = nil) { - sendArticleStatus(for: account) { result in - switch result { - case .success: - self.refreshArticleStatus(for: account) { result in - switch result { - case .success: - completion?(.success(())) - case .failure(let error): - completion?(.failure(error)) - } - } - case .failure(let error): + Task { @MainActor in + do { + try await sendArticleStatus(for: account) + try await refreshArticleStatus(for: account) + completion?(.success(())) + } catch { completion?(.failure(error)) } } } - func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { - Task { @MainActor in - Self.logger.info("Feedbin: Sending article statuses") - - func processStatuses(_ syncStatuses: [SyncStatus]) { - let createUnreadStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == false } - let deleteUnreadStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == true } - let createStarredStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == true } - let deleteStarredStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == false } + @MainActor func sendArticleStatus(for account: Account) async throws { + Self.logger.info("Feedbin: Sending article statuses") + defer { + Self.logger.info("Feedbin: Finished sending article statuses") + } - let group = DispatchGroup() - var errorOccurred = false + guard let syncStatuses = try await syncDatabase.selectForProcessing() else { + return + } - group.enter() - self.sendArticleStatuses(createUnreadStatuses, apiCall: self.caller.createUnreadEntries) { result in - group.leave() - if case .failure = result { - errorOccurred = true - } - } + let createUnreadStatuses = Array(syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == false }) + try await self.sendArticleStatuses(createUnreadStatuses, apiCall: self.caller.createUnreadEntries) - group.enter() - self.sendArticleStatuses(deleteUnreadStatuses, apiCall: self.caller.deleteUnreadEntries) { result in - group.leave() - if case .failure = result { - errorOccurred = true - } - } + let deleteUnreadStatuses = Array(syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == true }) + try await self.sendArticleStatuses(deleteUnreadStatuses, apiCall: self.caller.deleteUnreadEntries) - group.enter() - self.sendArticleStatuses(createStarredStatuses, apiCall: self.caller.createStarredEntries) { result in - group.leave() - if case .failure = result { - errorOccurred = true - } - } + let createStarredStatuses = Array(syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == true }) + try await self.sendArticleStatuses(createStarredStatuses, apiCall: self.caller.createStarredEntries) - group.enter() - self.sendArticleStatuses(deleteStarredStatuses, apiCall: self.caller.deleteStarredEntries) { result in - group.leave() - if case .failure = result { - errorOccurred = true - } - } + let deleteStarredStatuses = Array(syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == false }) + try await self.sendArticleStatuses(deleteStarredStatuses, apiCall: self.caller.deleteStarredEntries) + } - group.notify(queue: DispatchQueue.main) { - Self.logger.info("Feedbin: Finished sending article statuses") - if errorOccurred { - completion(.failure(FeedbinAccountDelegateError.unknown)) - } else { - completion(.success(())) - } - } - } - - do { - guard let syncStatuses = try await syncDatabase.selectForProcessing() else { - completion(.success(())) - return - } - processStatuses(Array(syncStatuses)) - } catch { - completion(.failure(error)) + @MainActor func refreshArticleStatus(for account: Account) async throws { + try await withCheckedThrowingContinuation { continuation in + self.refreshArticleStatus(for: account) { result in + continuation.resume(with: result) } } } @@ -537,7 +495,7 @@ final class FeedbinAccountDelegate: AccountDelegate { try await syncDatabase.insertStatuses(syncStatuses) if let count = try? await syncDatabase.selectPendingCount(), count > 100 { - sendArticleStatus(for: account) { _ in } + try await sendArticleStatus(for: account) } completion(.success(())) } catch { @@ -673,55 +631,12 @@ private extension FeedbinAccountDelegate { } - func refreshArticlesAndStatuses(_ account: Account) async throws { - try await withCheckedThrowingContinuation { continuation in - refreshArticlesAndStatuses(account) { result in - continuation.resume(with: result) - } - } - } - - func refreshArticlesAndStatuses(_ account: Account, completion: @escaping (Result) -> Void) { - self.sendArticleStatus(for: account) { result in - switch result { - case .success: - - self.refreshArticleStatus(for: account) { result in - switch result { - case .success: - - self.refreshArticles(account) { result in - switch result { - case .success: - - self.refreshMissingArticles(account) { result in - switch result { - case .success: - - DispatchQueue.main.async { - self.refreshProgress.reset() - completion(.success(())) - } - - case .failure(let error): - completion(.failure(error)) - } - } - - case .failure(let error): - completion(.failure(error)) - } - } - - case .failure(let error): - completion(.failure(error)) - } - } - - case .failure(let error): - completion(.failure(error)) - } - } + @MainActor func refreshArticlesAndStatuses(_ account: Account) async throws { + try await sendArticleStatus(for: account) + try await refreshArticleStatus(for: account) + try await refreshArticles(account) + try await refreshMissingArticles(account) + refreshProgress.reset() } // This function can be deleted if Feedbin updates their taggings.json service to @@ -917,6 +832,14 @@ private extension FeedbinAccountDelegate { return d } + @MainActor func sendArticleStatuses(_ statuses: [SyncStatus], apiCall: ([Int], @escaping (Result) -> Void) -> Void) async throws { + try await withCheckedThrowingContinuation { continuation in + self.sendArticleStatuses(statuses, apiCall: apiCall) { result in + continuation.resume(with: result) + } + } + } + func sendArticleStatuses(_ statuses: [SyncStatus], apiCall: ([Int], @escaping (Result) -> Void) -> Void, completion: @escaping ((Result) -> Void)) { @@ -1103,7 +1026,15 @@ private extension FeedbinAccountDelegate { } - func refreshArticles(_ account: Account, completion: @escaping VoidResultCompletionBlock) { + @MainActor func refreshArticles(_ account: Account) async throws { + try await withCheckedThrowingContinuation { continuation in + self.refreshArticles(account) { result in + continuation.resume(with: result) + } + } + } + + private func refreshArticles(_ account: Account, completion: @escaping VoidResultCompletionBlock) { Self.logger.info("Feedbin: Refreshing articles") @@ -1142,7 +1073,15 @@ private extension FeedbinAccountDelegate { } } - func refreshMissingArticles(_ account: Account, completion: @escaping ((Result) -> Void)) { + @MainActor func refreshMissingArticles(_ account: Account) async throws { + try await withCheckedThrowingContinuation { continuation in + self.refreshMissingArticles(account) { result in + continuation.resume(with: result) + } + } + } + + private func refreshMissingArticles(_ account: Account, completion: @escaping ((Result) -> Void)) { Self.logger.info("Feedbin: Refreshing missing articles") account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { result in diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 05be3ebe8..4e86cbba5 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -176,7 +176,15 @@ final class FeedlyAccountDelegate: AccountDelegate { } } - func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { + @MainActor func sendArticleStatus(for account: Account) async throws { + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + continuation.resume(with: result) + } + } + } + + private func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { // Ensure remote articles have the same status as they do locally. let send = FeedlySendArticleStatusesOperation(database: syncDatabase, service: caller) send.completionBlock = { operation in diff --git a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index cafd0f58c..e2ef4c93f 100644 --- a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -62,8 +62,7 @@ final class LocalAccountDelegate: AccountDelegate { completion?(.success(())) } - func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { - completion(.success(())) + func sendArticleStatus(for account: Account) async throws { } func refreshArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { diff --git a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index a51b958d3..78c43bf9d 100644 --- a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -142,7 +142,15 @@ final class NewsBlurAccountDelegate: AccountDelegate { } } - func sendArticleStatus(for account: Account, completion: @escaping (Result) -> ()) { + @MainActor func sendArticleStatus(for account: Account) async throws { + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + continuation.resume(with: result) + } + } + } + + private func sendArticleStatus(for account: Account, completion: @escaping (Result) -> ()) { Task { @MainActor in Self.logger.info("NewsBlur: Sending story statuses")