From 88be267c8fb52fd84b5757e3d4395947931c3d74 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 7 Nov 2025 17:27:20 -0800 Subject: [PATCH] Convert to async/await. --- .../Feedbin/FeedbinAccountDelegate.swift | 68 +++++++------------ 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index be9487aa7..0780f07de 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -168,56 +168,32 @@ final class FeedbinAccountDelegate: AccountDelegate { } } - @MainActor func importOPML(for account: Account, opmlFile: URL) async throws { - try await withCheckedThrowingContinuation { continuation in - self.importOPML(for: account, opmlFile: opmlFile) { result in - continuation.resume(with: result) - } - } - } - - private func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result) -> Void) { - var fileData: Data? - - do { - fileData = try Data(contentsOf: opmlFile) - } catch { - completion(.failure(error)) - return - } - - guard let opmlData = fileData else { - completion(.success(())) + let opmlData = try Data(contentsOf: opmlFile) + guard !opmlData.isEmpty else { return } Self.logger.info("Feedbin: Did begin importing OPML") isOPMLImportInProgress = true refreshProgress.addToNumberOfTasksAndRemaining(1) + defer { + isOPMLImportInProgress = false + refreshProgress.completeTask() + } - caller.importOPML(opmlData: opmlData) { result in - Task { @MainActor in - switch result { - case .success(let importResult): - if importResult.complete { - Self.logger.info("Feedbin: Finished importing OPML") - self.refreshProgress.completeTask() - self.isOPMLImportInProgress = false - DispatchQueue.main.async { - completion(.success(())) - } - } else { - self.checkImportResult(opmlImportResultID: importResult.importResultID, completion: completion) - } - case .failure(let error): - Self.logger.info("Feedbin: OPML import failed: \(error.localizedDescription)") - self.refreshProgress.completeTask() - self.isOPMLImportInProgress = false - let wrappedError = AccountError.wrappedError(error: error, account: account) - completion(.failure(wrappedError)) - } + do { + let importResult = try await caller.importOPML(opmlData: opmlData) + if importResult.complete { + Self.logger.info("Feedbin: Finished importing OPML") + } else { + // This will retry until success or error. + try await self.checkImportResult(opmlImportResultID: importResult.importResultID) } + } catch { + Self.logger.info("Feedbin: OPML import failed: \(error.localizedDescription)") + let wrappedError = AccountError.wrappedError(error: error, account: account) + throw wrappedError } } @@ -604,7 +580,15 @@ final class FeedbinAccountDelegate: AccountDelegate { private extension FeedbinAccountDelegate { - @MainActor func checkImportResult(opmlImportResultID: Int, completion: @escaping (Result) -> Void) { + @MainActor func checkImportResult(opmlImportResultID: Int) async throws { + try await withCheckedThrowingContinuation { continuation in + self.checkImportResult(opmlImportResultID: opmlImportResultID) { result in + continuation.resume(with: result) + } + } + } + + @MainActor private func checkImportResult(opmlImportResultID: Int, completion: @escaping (Result) -> Void) { Task { @MainActor in while true { try? await Task.sleep(for: .seconds(15))