From 01f08b9f27385262e014f69d2c7df71d3b3f7e5e Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 15 May 2024 09:31:16 -0700 Subject: [PATCH] =?UTF-8?q?Make=20FeedlyOAuthAccountAuthorizationOperation?= =?UTF-8?q?=20not=20a=20MainThreadOperation,=20since=20we=E2=80=99re=20mov?= =?UTF-8?q?ing=20away=20from=20MainThreadOperationQueue.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...lyOAuthAccountAuthorizationOperation.swift | 40 +++++++++---------- .../AccountsPreferencesViewController.swift | 8 +++- iOS/Settings/AddAccountViewController.swift | 17 ++++++-- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Account/Sources/Account/Feedly/FeedlyOAuthAccountAuthorizationOperation.swift b/Account/Sources/Account/Feedly/FeedlyOAuthAccountAuthorizationOperation.swift index d818a0044..cd9554fb4 100644 --- a/Account/Sources/Account/Feedly/FeedlyOAuthAccountAuthorizationOperation.swift +++ b/Account/Sources/Account/Feedly/FeedlyOAuthAccountAuthorizationOperation.swift @@ -25,7 +25,7 @@ public enum FeedlyOAuthAccountAuthorizationOperationError: LocalizedError { return NSLocalizedString("There is already a Feedly account with that username created.", comment: "Duplicate Error") } } -@MainActor @objc public final class FeedlyOAuthAccountAuthorizationOperation: NSObject, MainThreadOperation, ASWebAuthenticationPresentationContextProviding { +@MainActor @objc public final class FeedlyOAuthAccountAuthorizationOperation: NSObject { public var isCanceled: Bool = false { didSet { @@ -34,21 +34,17 @@ public enum FeedlyOAuthAccountAuthorizationOperationError: LocalizedError { } } } - public var id: Int? - public weak var operationDelegate: MainThreadOperationDelegate? - public var name: String? - public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock? + + public var completionBlock: ((FeedlyOAuthAccountAuthorizationOperation) -> Void)? public weak var presentationAnchor: ASPresentationAnchor? public weak var delegate: FeedlyOAuthAccountAuthorizationOperationDelegate? - private let accountType: AccountType private let oauthClient: OAuthAuthorizationClient private var session: ASWebAuthenticationSession? private let secretsProvider: SecretsProvider - public init(accountType: AccountType, secretsProvider: SecretsProvider) { - self.accountType = accountType + public init(secretsProvider: SecretsProvider) { self.secretsProvider = secretsProvider self.oauthClient = FeedlyAPICaller.API.cloud.oauthAuthorizationClient(secretsProvider: secretsProvider) } @@ -132,17 +128,6 @@ public enum FeedlyOAuthAccountAuthorizationOperationError: LocalizedError { } } - - nonisolated public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { - - MainActor.assumeIsolated { - guard let anchor = presentationAnchor else { - fatalError("\(self) has outlived presentation anchor.") - } - return anchor - } - } - @MainActor private func saveAccount(for grant: OAuthAuthorizationGrant) { guard !AccountManager.shared.duplicateServiceAccount(type: .feedly, username: grant.accessToken.username) else { didFinish(FeedlyOAuthAccountAuthorizationOperationError.duplicateAccount) @@ -172,7 +157,7 @@ public enum FeedlyOAuthAccountAuthorizationOperationError: LocalizedError { @MainActor private func didFinish() { assert(Thread.isMainThread) - operationDelegate?.operationDidComplete(self) +// operationDelegate?.operationDidComplete(self) } @MainActor private func didFinish(_ error: Error) { @@ -181,3 +166,18 @@ public enum FeedlyOAuthAccountAuthorizationOperationError: LocalizedError { didFinish() } } + +// MARK: - ASWebAuthenticationPresentationContextProviding + +extension FeedlyOAuthAccountAuthorizationOperation: ASWebAuthenticationPresentationContextProviding { + + nonisolated public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { + + MainActor.assumeIsolated { + guard let anchor = presentationAnchor else { + fatalError("\(self) has outlived presentation anchor.") + } + return anchor + } + } +} diff --git a/Mac/Preferences/Accounts/AccountsPreferencesViewController.swift b/Mac/Preferences/Accounts/AccountsPreferencesViewController.swift index fa42a2753..4d88f8171 100644 --- a/Mac/Preferences/Accounts/AccountsPreferencesViewController.swift +++ b/Mac/Preferences/Accounts/AccountsPreferencesViewController.swift @@ -185,11 +185,13 @@ extension AccountsPreferencesViewController: AccountsPreferencesAddAccountDelega addAccountWindowController = accountsReaderAPIWindowController case .feedly: - let addAccount = FeedlyOAuthAccountAuthorizationOperation(accountType: .feedly, secretsProvider: Secrets()) + let addAccount = FeedlyOAuthAccountAuthorizationOperation(secretsProvider: Secrets()) addAccount.delegate = self addAccount.presentationAnchor = self.view.window! runAwaitingFeedlyLoginAlertModal(forLifetimeOf: addAccount) - MainThreadOperationQueue.shared.add(addAccount) + Task { @MainActor in + addAccount.run() + } case .newsBlur: let accountsNewsBlurWindowController = AccountsNewsBlurWindowController() @@ -273,6 +275,8 @@ private extension AccountsPreferencesViewController { } +// MARK: - FeedlyOAuthAccountAuthorizationOperationDelegate + extension AccountsPreferencesViewController: FeedlyOAuthAccountAuthorizationOperationDelegate { func oauthAccountAuthorizationOperation(_ operation: FeedlyOAuthAccountAuthorizationOperation, didCreate account: Account) { diff --git a/iOS/Settings/AddAccountViewController.swift b/iOS/Settings/AddAccountViewController.swift index 655dbddcf..9f8949016 100644 --- a/iOS/Settings/AddAccountViewController.swift +++ b/iOS/Settings/AddAccountViewController.swift @@ -15,7 +15,7 @@ protocol AddAccountDismissDelegate: UIViewController { @MainActor func dismiss() } -class AddAccountViewController: UITableViewController, AddAccountDismissDelegate { +final class AddAccountViewController: UITableViewController, AddAccountDismissDelegate { private enum AddAccountSections: Int, CaseIterable { case local = 0 @@ -66,7 +66,9 @@ class AddAccountViewController: UITableViewController, AddAccountDismissDelegate } } } - + + private var feedlyAddAccountOperation: FeedlyOAuthAccountAuthorizationOperation? + override func viewDidLoad() { super.viewDidLoad() } @@ -198,10 +200,15 @@ class AddAccountViewController: UITableViewController, AddAccountDismissDelegate addViewController.delegate = self present(navController, animated: true) case .feedly: - let addAccount = FeedlyOAuthAccountAuthorizationOperation(accountType: .feedly, secretsProvider: Secrets()) + let addAccount = FeedlyOAuthAccountAuthorizationOperation(secretsProvider: Secrets()) addAccount.delegate = self addAccount.presentationAnchor = self.view.window! - MainThreadOperationQueue.shared.add(addAccount) + feedlyAddAccountOperation = addAccount + + Task { @MainActor in + addAccount.run() + } + case .newsBlur: let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "NewsBlurAccountNavigationViewController") as! UINavigationController navController.modalPresentationStyle = .currentContext @@ -224,6 +231,8 @@ class AddAccountViewController: UITableViewController, AddAccountDismissDelegate } +// MARK: - FeedlyOAuthAccountAuthorizationOperationDelegate + extension AddAccountViewController: FeedlyOAuthAccountAuthorizationOperationDelegate { func oauthAccountAuthorizationOperation(_ operation: FeedlyOAuthAccountAuthorizationOperation, didCreate account: Account) {