From f6719d8b4f2f5fc3b1e4cbb91cb18c800807eb18 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 26 Mar 2024 16:50:11 -0700 Subject: [PATCH] Convert AccountDelegate.syncArticleStatus to async/await. --- Account/Sources/Account/Account.swift | 17 ++--------- Account/Sources/Account/AccountDelegate.swift | 2 +- .../CloudKit/CloudKitAccountDelegate.swift | 29 ++++++++++--------- .../Feedbin/FeedbinAccountDelegate.swift | 27 +++++++++-------- .../Feedly/FeedlyAccountDelegate.swift | 27 +++++++++-------- .../LocalAccount/LocalAccountDelegate.swift | 3 +- .../NewsBlur/NewsBlurAccountDelegate.swift | 27 +++++++++-------- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 27 ++++++++--------- iOS/AppDefaults.swift | 2 +- iOS/AppDelegate.swift | 28 +++++++++--------- 10 files changed, 93 insertions(+), 96 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index c711e1fa6..8d958c369 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -464,24 +464,11 @@ public enum FetchType { } } - public func syncArticleStatus(completion: ((Result) -> Void)? = nil) { - delegate.syncArticleStatus(for: self, completion: completion) - } - public func syncArticleStatus() async throws { - try await withCheckedThrowingContinuation { continuation in - self.syncArticleStatus { result in - switch result { - case .success: - continuation.resume() - case .failure(let error): - continuation.resume(throwing: error) - } - } - } + try await delegate.syncArticleStatus(for: self) } - + public func importOPML(_ opmlFile: URL, completion: @escaping (Result) -> Void) { guard !delegate.isOPMLImportInProgress else { completion(.failure(AccountError.opmlImportInProgress)) diff --git a/Account/Sources/Account/AccountDelegate.swift b/Account/Sources/Account/AccountDelegate.swift index c1cef58d7..5de6ca9cf 100644 --- a/Account/Sources/Account/AccountDelegate.swift +++ b/Account/Sources/Account/AccountDelegate.swift @@ -26,7 +26,7 @@ import Secrets func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) func refreshAll(for account: Account, completion: @escaping (Result) -> Void) - func syncArticleStatus(for account: Account, completion: ((Result) -> Void)?) + func syncArticleStatus(for account: Account) async throws func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) func refreshArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 65937c92d..3a2e4c203 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -95,24 +95,27 @@ enum CloudKitAccountDelegateError: LocalizedError { standardRefreshAll(for: account, completion: completion) } - 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)) + func syncArticleStatus(for account: Account) async throws { + + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + switch result { + case .success: + self.refreshArticleStatus(for: account) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } } + case .failure(let error): + continuation.resume(throwing: error) } - case .failure(let error): - completion?(.failure(error)) } } } - + func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { sendArticleStatus(for: account, showProgress: false, completion: completion) } diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 1700b5e93..8ad324c16 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -112,20 +112,23 @@ 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)) + func syncArticleStatus(for account: Account) async throws { + + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + switch result { + case .success: + self.refreshArticleStatus(for: account) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } } + case .failure(let error): + continuation.resume(throwing: error) } - case .failure(let error): - completion?(.failure(error)) } } } diff --git a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index b1e15228a..a328a381c 100644 --- a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -145,20 +145,23 @@ final class FeedlyAccountDelegate: AccountDelegate { operationQueue.add(syncAllOperation) } - @MainActor 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)) + @MainActor func syncArticleStatus(for account: Account) async throws { + + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + switch result { + case .success: + self.refreshArticleStatus(for: account) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } } + case .failure(let error): + continuation.resume(throwing: error) } - case .failure(let error): - completion?(.failure(error)) } } } diff --git a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index 93ec83db1..a108b5f40 100644 --- a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -68,8 +68,7 @@ final class LocalAccountDelegate: AccountDelegate { } - func syncArticleStatus(for account: Account, completion: ((Result) -> Void)? = nil) { - completion?(.success(())) + func syncArticleStatus(for account: Account) async throws { } func sendArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { diff --git a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 5f91b8166..4e56f490a 100644 --- a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -113,20 +113,23 @@ final class NewsBlurAccountDelegate: 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)) + func syncArticleStatus(for account: Account) async throws { + + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + switch result { + case .success: + self.refreshArticleStatus(for: account) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } } + case .failure(let error): + continuation.resume(throwing: error) } - case .failure(let error): - completion?(.failure(error)) } } } diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 2ecb4a0a5..2e387de34 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -175,25 +175,26 @@ final class ReaderAPIAccountDelegate: AccountDelegate { } - func syncArticleStatus(for account: Account, completion: ((Result) -> Void)? = nil) { + func syncArticleStatus(for account: Account) async throws { guard variant != .inoreader else { - completion?(.success(())) return } - 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)) + try await withCheckedThrowingContinuation { continuation in + sendArticleStatus(for: account) { result in + switch result { + case .success: + self.refreshArticleStatus(for: account) { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } } + case .failure(let error): + continuation.resume(throwing: error) } - case .failure(let error): - completion?(.failure(error)) } } } diff --git a/iOS/AppDefaults.swift b/iOS/AppDefaults.swift index d29c150c1..3f065810a 100644 --- a/iOS/AppDefaults.swift +++ b/iOS/AppDefaults.swift @@ -33,7 +33,7 @@ final class AppDefaults { static let shared = AppDefaults() private init() {} - static var store: UserDefaults = { + static let store: UserDefaults = { let appIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "AppIdentifierPrefix") as! String let suiteName = "\(appIdentifierPrefix)group.\(Bundle.main.bundleIdentifier!)" return UserDefaults.init(suiteName: suiteName)! diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index f4ff43079..edb649c5c 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -453,16 +453,15 @@ private extension AppDelegate { self.prepareAccountsForBackground() - account.syncArticleStatus(completion: { _ in - if !self.accountManager.isSuspended { - try? WidgetDataEncoder.shared.encodeWidgetData() - self.prepareAccountsForBackground() - self.suspendApplication() - } - }) + try? await account.syncArticleStatus + if !self.accountManager.isSuspended { + try? WidgetDataEncoder.shared.encodeWidgetData() + self.prepareAccountsForBackground() + self.suspendApplication() + } } } - + @MainActor func handleMarkAsStarred(userInfo: [AnyHashable: Any]) { guard let articlePathInfo = ArticlePathInfo(userInfo: userInfo) else { @@ -489,13 +488,12 @@ private extension AppDelegate { account.markArticles(articles, statusKey: .starred, flag: true) { _ in } - account.syncArticleStatus(completion: { _ in - if !self.accountManager.isSuspended { - try? WidgetDataEncoder.shared.encodeWidgetData() - self.prepareAccountsForBackground() - self.suspendApplication() - } - }) + try? await account.syncArticleStatus() + if !self.accountManager.isSuspended { + try? WidgetDataEncoder.shared.encodeWidgetData() + self.prepareAccountsForBackground() + self.suspendApplication() + } } } }