From e099390025f8c5d008767cdb6efd1e1a9ffc9588 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 6 Nov 2025 20:15:22 -0800 Subject: [PATCH] Convert to async/await. --- .../FolderInspectorViewController.swift | 22 ++++++++++--------- .../AddFolder/AddFolderWindowController.swift | 9 ++++---- .../Sidebar/SidebarOutlineDataSource.swift | 9 ++++---- Mac/Scriptability/Folder+Scriptability.swift | 14 ++++++------ Modules/Account/Sources/Account/Account.swift | 9 ++++---- .../Sources/Account/AccountDelegate.swift | 6 ++--- .../Sources/Account/AccountError.swift | 3 +++ .../CloudKit/CloudKitAccountDelegate.swift | 22 ++++++++++++++++--- .../Feedbin/FeedbinAccountDelegate.swift | 19 +++++++++++----- .../Feedly/FeedlyAccountDelegate.swift | 16 ++++++++++++++ Modules/Account/Sources/Account/Folder.swift | 13 +++++++++-- .../LocalAccount/LocalAccountDelegate.swift | 15 +++++-------- .../NewsBlur/NewsBlurAccountDelegate.swift | 18 ++++++++++++++- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 19 +++++++++++----- iOS/Add/AddFolderViewController.swift | 16 ++++++++------ 15 files changed, 143 insertions(+), 67 deletions(-) diff --git a/Mac/Inspector/FolderInspectorViewController.swift b/Mac/Inspector/FolderInspectorViewController.swift index b88bf7732..b173fa166 100644 --- a/Mac/Inspector/FolderInspectorViewController.swift +++ b/Mac/Inspector/FolderInspectorViewController.swift @@ -106,20 +106,22 @@ private extension FolderInspectorViewController { } func renameFolderIfNecessary() { - guard let folder = folder, - let account = folder.account, - let nameTextField = nameTextField, - folder.nameForDisplay != nameTextField.stringValue else { + guard let folder, let account = folder.account, let nameTextField else { return } - account.renameFolder(folder, to: nameTextField.stringValue) { [weak self] result in - if case .failure(let error) = result { - self?.presentError(error) - } else { - self?.windowTitle = folder.nameForDisplay + let newFolderName = nameTextField.stringValue + guard !newFolderName.isEmpty, folder.nameForDisplay != newFolderName else { + return + } + + Task { @MainActor in + do { + try await account.renameFolder(folder, to: newFolderName) + windowTitle = folder.nameForDisplay + } catch { + presentError(error) } } } - } diff --git a/Mac/MainWindow/AddFolder/AddFolderWindowController.swift b/Mac/MainWindow/AddFolder/AddFolderWindowController.swift index b77581ea5..d67783f62 100644 --- a/Mac/MainWindow/AddFolder/AddFolderWindowController.swift +++ b/Mac/MainWindow/AddFolder/AddFolderWindowController.swift @@ -111,11 +111,10 @@ private extension AddFolderWindowController { return } - account.addFolder(folderName) { result in - switch result { - case .success: - break - case .failure(let error): + Task { @MainActor in + do { + try await account.addFolder(folderName) + } catch { NSApplication.shared.presentError(error) } } diff --git a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift index 7966c923b..5e73078ae 100644 --- a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift +++ b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift @@ -440,9 +440,10 @@ private extension SidebarOutlineDataSource { return } - destinationAccount.addFolder(folder.name ?? "") { result in - switch result { - case .success(let destinationFolder): + Task { @MainActor in + do { + let destinationFolder = try await destinationAccount.addFolder(folder.name ?? "") + for feed in folder.topLevelFeeds { if let existingFeed = destinationAccount.existingFeed(withURL: feed.url) { destinationAccount.addFeed(existingFeed, to: destinationFolder) { result in @@ -464,7 +465,7 @@ private extension SidebarOutlineDataSource { } } } - case .failure(let error): + } catch { NSApplication.shared.presentError(error) } } diff --git a/Mac/Scriptability/Folder+Scriptability.swift b/Mac/Scriptability/Folder+Scriptability.swift index 9c2793a27..be33bcdf7 100644 --- a/Mac/Scriptability/Folder+Scriptability.swift +++ b/Mac/Scriptability/Folder+Scriptability.swift @@ -66,8 +66,8 @@ final class ScriptableFolder: NSObject, UniqueIdScriptingObject, ScriptingObject tell account X to make new folder at end with properties {name:"new folder name"} */ class func handleCreateElement(command:NSCreateCommand) -> Any? { - guard command.isCreateCommand(forClass:"fold") else { return nil } - let name = command.property(forKey:"name") as? String ?? "" + guard command.isCreateCommand(forClass: "fold") else { return nil } + let name = command.property(forKey: "name") as? String ?? "" // some combination of the tell target and the location specifier ("in" or "at") // identifies where the new folder should be created @@ -79,13 +79,13 @@ final class ScriptableFolder: NSObject, UniqueIdScriptingObject, ScriptingObject command.suspendExecution() - account.addFolder(name) { result in - switch result { - case .success(let folder): + Task { @MainActor in + do { + let folder = try await account.addFolder(name) let scriptableAccount = ScriptableAccount(account) - let scriptableFolder = ScriptableFolder(folder, container:scriptableAccount) + let scriptableFolder = ScriptableFolder(folder, container: scriptableAccount) command.resumeExecution(withResult:scriptableFolder.objectSpecifier) - case .failure: + } catch { command.resumeExecution(withResult:nil) } } diff --git a/Modules/Account/Sources/Account/Account.swift b/Modules/Account/Sources/Account/Account.swift index ddbac1eb4..82ffb48ab 100644 --- a/Modules/Account/Sources/Account/Account.swift +++ b/Modules/Account/Sources/Account/Account.swift @@ -595,16 +595,17 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, delegate.restoreFeed(for: self, feed: feed, container: container, completion: completion) } - public func addFolder(_ name: String, completion: @escaping (Result) -> Void) { - delegate.createFolder(for: self, name: name, completion: completion) + @discardableResult + @MainActor public func addFolder(_ name: String) async throws -> Folder { + try await delegate.createFolder(for: self, name: name) } public func removeFolder(_ folder: Folder, completion: @escaping (Result) -> Void) { delegate.removeFolder(for: self, with: folder, completion: completion) } - public func renameFolder(_ folder: Folder, to name: String, completion: @escaping (Result) -> Void) { - delegate.renameFolder(for: self, with: folder, to: name, completion: completion) + public func renameFolder(_ folder: Folder, to name: String) async throws { + try await delegate.renameFolder(for: self, with: folder, to: name) } public func restoreFolder(_ folder: Folder, completion: @escaping (Result) -> Void) { diff --git a/Modules/Account/Sources/Account/AccountDelegate.swift b/Modules/Account/Sources/Account/AccountDelegate.swift index 02c0be542..bf6c6bd0d 100644 --- a/Modules/Account/Sources/Account/AccountDelegate.swift +++ b/Modules/Account/Sources/Account/AccountDelegate.swift @@ -28,12 +28,12 @@ protocol AccountDelegate { @MainActor func refreshAll(for account: Account) async throws @MainActor func syncArticleStatus(for account: Account) async throws @MainActor func sendArticleStatus(for account: Account) async throws - func refreshArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) + @MainActor func refreshArticleStatus(for account: Account) async throws func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result) -> Void) - func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) - func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result) -> Void) func createFeed(for account: Account, url: String, name: String?, container: Container, validateFeed: Bool, completion: @escaping (Result) -> Void) diff --git a/Modules/Account/Sources/Account/AccountError.swift b/Modules/Account/Sources/Account/AccountError.swift index 38f2b164c..080152c70 100644 --- a/Modules/Account/Sources/Account/AccountError.swift +++ b/Modules/Account/Sources/Account/AccountError.swift @@ -14,6 +14,7 @@ public enum AccountError: LocalizedError { case createErrorNotFound case createErrorAlreadySubscribed case opmlImportInProgress + case invalidParameter case wrappedError(error: Error, account: Account) public var account: Account? { @@ -41,6 +42,8 @@ public enum AccountError: LocalizedError { return NSLocalizedString("You are already subscribed to this feed and can’t add it again.", comment: "Already subscribed") case .opmlImportInProgress: return NSLocalizedString("An OPML import for this account is already running.", comment: "Import running") + case .invalidParameter: + return NSLocalizedString("Couldn’t fulfill the request due to an invalid parameter.", comment: "Invalid parameter") case .wrappedError(let error, let account): switch error { case TransportError.httpError(let status): diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index fddcd6ca8..fbfaef918 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -288,7 +288,15 @@ final class CloudKitAccountDelegate: AccountDelegate { } } - func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) { + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { + try await withCheckedThrowingContinuation { continuation in + createFolder(for: account, name: name) { result in + continuation.resume(with: result) + } + } + } + + private func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) { syncProgress.addToNumberOfTasksAndRemaining(1) accountZone.createFolder(name: name) { result in self.syncProgress.completeTask() @@ -298,7 +306,7 @@ final class CloudKitAccountDelegate: AccountDelegate { folder.externalID = externalID completion(.success(folder)) } else { - completion(.failure(FeedbinAccountDelegateError.invalidParameter)) + completion(.failure(AccountError.invalidParameter)) } case .failure(let error): self.processAccountError(account, error) @@ -307,7 +315,15 @@ final class CloudKitAccountDelegate: AccountDelegate { } } - func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { + try await withCheckedThrowingContinuation { continuation in + renameFolder(for: account, with: folder, to: name) { result in + continuation.resume(with: result) + } + } + } + + private func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { syncProgress.addToNumberOfTasksAndRemaining(1) accountZone.renameFolder(folder, to: name) { result in self.syncProgress.completeTask() diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 247cfa6af..920fe061f 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -214,15 +214,22 @@ final class FeedbinAccountDelegate: AccountDelegate { } - func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) { - if let folder = account.ensureFolder(with: name) { - completion(.success(folder)) - } else { - completion(.failure(FeedbinAccountDelegateError.invalidParameter)) + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { + guard let folder = account.ensureFolder(with: name) else { + throw AccountError.invalidParameter + } + return folder + } + + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { + try await withCheckedThrowingContinuation { continuation in + renameFolder(for: account, with: folder, to: name) { result in + continuation.resume(with: result) + } } } - func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { + private func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { guard folder.hasAtLeastOneFeed() else { folder.name = name diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index a6d5d71c2..6be27ee97 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -260,6 +260,14 @@ final class FeedlyAccountDelegate: AccountDelegate { } } + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { + try await withCheckedThrowingContinuation { continuation in + createFolder(for: account, name: name) { result in + continuation.resume(with: result) + } + } + } + func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) { let progress = refreshProgress @@ -283,6 +291,14 @@ final class FeedlyAccountDelegate: AccountDelegate { } } + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { + try await withCheckedThrowingContinuation { continuation in + renameFolder(for: account, with: folder, to: name) { result in + continuation.resume(with: result) + } + } + } + func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { guard let id = folder.externalID else { return DispatchQueue.main.async { diff --git a/Modules/Account/Sources/Account/Folder.swift b/Modules/Account/Sources/Account/Folder.swift index 080c53ac3..e0b7c63ce 100644 --- a/Modules/Account/Sources/Account/Folder.swift +++ b/Modules/Account/Sources/Account/Folder.swift @@ -66,8 +66,17 @@ public final class Folder: SidebarItem, Renamable, Container, Hashable { // MARK: - Renamable public func rename(to name: String, completion: @escaping (Result) -> Void) { - guard let account = account else { return } - account.renameFolder(self, to: name, completion: completion) + guard let account else { + return + } + Task { @MainActor in + do { + try await account.renameFolder(self, to: name) + completion(.success(())) + } catch { + completion(.failure(error)) + } + } } // MARK: - Init diff --git a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index b47459cd1..f939f2ae7 100644 --- a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -64,8 +64,7 @@ final class LocalAccountDelegate: AccountDelegate { @MainActor func sendArticleStatus(for account: Account) async throws { } - func refreshArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { - completion(.success(())) + @MainActor func refreshArticleStatus(for account: Account) async throws { } func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result) -> Void) { @@ -144,17 +143,15 @@ final class LocalAccountDelegate: AccountDelegate { completion(.success(())) } - func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) { - if let folder = account.ensureFolder(with: name) { - completion(.success(folder)) - } else { - completion(.failure(FeedbinAccountDelegateError.invalidParameter)) + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { + guard let folder = account.ensureFolder(with: name) else { + throw AccountError.invalidParameter } + return folder } - func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { folder.name = name - completion(.success(())) } func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result) -> Void) { diff --git a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index c372b2b1b..d6ee240b5 100644 --- a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -360,7 +360,15 @@ final class NewsBlurAccountDelegate: AccountDelegate { completion(.success(())) } - func createFolder(for account: Account, name: String, completion: @escaping (Result) -> ()) { + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { + try await withCheckedThrowingContinuation { continuation in + self.createFolder(for: account, name: name) { result in + continuation.resume(with: result) + } + } + } + + private func createFolder(for account: Account, name: String, completion: @escaping (Result) -> ()) { self.refreshProgress.addToNumberOfTasksAndRemaining(1) caller.addFolder(named: name) { result in @@ -379,6 +387,14 @@ final class NewsBlurAccountDelegate: AccountDelegate { } } + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { + try await withCheckedThrowingContinuation { continuation in + renameFolder(for: account, with: folder, to: name) { result in + continuation.resume(with: result) + } + } + } + func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> ()) { guard let folderToRename = folder.name else { completion(.failure(NewsBlurError.invalidParameter)) diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index d62150168..5ce86dbd0 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -284,15 +284,22 @@ final class ReaderAPIAccountDelegate: AccountDelegate { func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result) -> Void) { } - func createFolder(for account: Account, name: String, completion: @escaping (Result) -> Void) { - if let folder = account.ensureFolder(with: name) { - completion(.success(folder)) - } else { - completion(.failure(ReaderAPIAccountDelegateError.invalidParameter)) + @MainActor func createFolder(for account: Account, name: String) async throws -> Folder { + guard let folder = account.ensureFolder(with: name) else { + throw AccountError.invalidParameter + } + return folder + } + + @MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws { + try await withCheckedThrowingContinuation { continuation in + renameFolder(for: account, with: folder, to: name) { result in + continuation.resume(with: result) + } } } - func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { + private func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result) -> Void) { refreshProgress.addToNumberOfTasksAndRemaining(1) caller.renameTag(oldName: folder.name ?? "", newName: name) { result in diff --git a/iOS/Add/AddFolderViewController.swift b/iOS/Add/AddFolderViewController.swift index 7fbef5dba..daec8ef3d 100644 --- a/iOS/Add/AddFolderViewController.swift +++ b/iOS/Add/AddFolderViewController.swift @@ -79,13 +79,15 @@ final class AddFolderViewController: UITableViewController { guard let folderName = nameTextField.text else { return } - selectedAccount.addFolder(folderName) { result in - switch result { - case .success: - self.dismiss(animated: true) - case .failure(let error): - self.presentError(error) - self.dismiss(animated: true) + + Task { @MainActor in + defer { + dismiss(animated: true) + } + do { + try await selectedAccount.addFolder(folderName) + } catch { + presentError(error) } } }