From 082f5e6ce3346a28e1a5a58ef80077125d0a7272 Mon Sep 17 00:00:00 2001 From: shannon Date: Tue, 19 Nov 2024 11:30:01 -0500 Subject: [PATCH] Fully initialize AuthenticationServiceProvider from within init contributes to iOS-319 --- Mastodon/Coordinator/SceneCoordinator.swift | 2 -- Mastodon/Supporting Files/AppDelegate.swift | 1 - MastodonIntent/IntentHandler.swift | 1 - .../AuthenticationServiceProvider.swift | 18 ++++++++++++------ .../Scene/ShareViewController.swift | 1 - .../FollowersCount/FollowersCountWidget.swift | 2 -- .../Variants/Hashtag/HashtagWidget.swift | 2 -- .../LatestFollowersWidget.swift | 2 -- .../MultiFollowersCountWidget.swift | 2 -- 9 files changed, 12 insertions(+), 19 deletions(-) diff --git a/Mastodon/Coordinator/SceneCoordinator.swift b/Mastodon/Coordinator/SceneCoordinator.swift index a5fe7505a..36e41dd4c 100644 --- a/Mastodon/Coordinator/SceneCoordinator.swift +++ b/Mastodon/Coordinator/SceneCoordinator.swift @@ -250,8 +250,6 @@ extension SceneCoordinator { func setup() { let rootViewController: UIViewController - AuthenticationServiceProvider.shared.prepareForUse() - switch UIDevice.current.userInterfaceIdiom { case .phone: let viewController = MainTabBarController(context: appContext, coordinator: self, authenticationBox: authenticationBox) diff --git a/Mastodon/Supporting Files/AppDelegate.swift b/Mastodon/Supporting Files/AppDelegate.swift index b712c18df..42fcd1cd1 100644 --- a/Mastodon/Supporting Files/AppDelegate.swift +++ b/Mastodon/Supporting Files/AppDelegate.swift @@ -17,7 +17,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var appContext: AppContext { return AppContext.shared } func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - AuthenticationServiceProvider.shared.prepareForUse() AppSecret.default.register() diff --git a/MastodonIntent/IntentHandler.swift b/MastodonIntent/IntentHandler.swift index 7e4ddfdcf..73264495f 100644 --- a/MastodonIntent/IntentHandler.swift +++ b/MastodonIntent/IntentHandler.swift @@ -12,7 +12,6 @@ class IntentHandler: INExtension { @MainActor override func handler(for intent: INIntent) -> Any { - AuthenticationServiceProvider.shared.prepareForUse() switch intent { case is SendPostIntent: diff --git a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift index 86430d807..a107ee7c9 100644 --- a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift +++ b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift @@ -43,11 +43,7 @@ public class AuthenticationServiceProvider: ObservableObject { $authentications .map { authentications -> [MastodonAuthenticationBox] in - return authentications - .sorted(by: { $0.activedAt > $1.activedAt }) - .compactMap { authentication -> MastodonAuthenticationBox? in - return MastodonAuthenticationBox(authentication: authentication) - } + return self.authenticationBoxes(authentications) } .assign(to: &$mastodonAuthenticationBoxes) @@ -60,6 +56,14 @@ public class AuthenticationServiceProvider: ObservableObject { } } + private func authenticationBoxes(_ authentications: [MastodonAuthentication]) -> [MastodonAuthenticationBox] { + return authentications + .sorted(by: { $0.activedAt > $1.activedAt }) + .compactMap { authentication -> MastodonAuthenticationBox? in + return MastodonAuthenticationBox(authentication: authentication) + } + } + @Published private var authentications: [MastodonAuthentication] = [] { didSet { persist(authentications) @@ -156,10 +160,12 @@ public extension AuthenticationServiceProvider { } @MainActor - func prepareForUse() { + private func prepareForUse() { if authentications.isEmpty { restoreFromKeychain() } + mastodonAuthenticationBoxes = authenticationBoxes(authentications) + currentActiveUser.send(mastodonAuthenticationBoxes.first) } @MainActor diff --git a/ShareActionExtension/Scene/ShareViewController.swift b/ShareActionExtension/Scene/ShareViewController.swift index 311af1147..1010204ce 100644 --- a/ShareActionExtension/Scene/ShareViewController.swift +++ b/ShareActionExtension/Scene/ShareViewController.swift @@ -160,7 +160,6 @@ extension ShareViewController { extension ShareViewController { private func setupAuthContext() throws -> MastodonAuthenticationBox? { - AuthenticationServiceProvider.shared.prepareForUse() return AuthenticationServiceProvider.shared.currentActiveUser.value } diff --git a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift index edbaf6c37..fdc78dbee 100644 --- a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift +++ b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift @@ -73,8 +73,6 @@ private extension FollowersCountWidgetProvider { func loadCurrentEntry(for configuration: FollowersCountIntent, in context: Context, completion: @escaping (FollowersCountEntry) -> Void) { Task { @MainActor in - AuthenticationServiceProvider.shared.prepareForUse() - guard let authBox = AuthenticationServiceProvider.shared.currentActiveUser.value else { diff --git a/WidgetExtension/Variants/Hashtag/HashtagWidget.swift b/WidgetExtension/Variants/Hashtag/HashtagWidget.swift index a72b75fd7..7e78e1fcb 100644 --- a/WidgetExtension/Variants/Hashtag/HashtagWidget.swift +++ b/WidgetExtension/Variants/Hashtag/HashtagWidget.swift @@ -28,8 +28,6 @@ extension HashtagWidgetProvider { Task { await MainActor.run { - AuthenticationServiceProvider.shared.prepareForUse() - guard let authBox = AuthenticationServiceProvider.shared.currentActiveUser.value else { diff --git a/WidgetExtension/Variants/LatestFollowers/LatestFollowersWidget.swift b/WidgetExtension/Variants/LatestFollowers/LatestFollowersWidget.swift index 71c78e9b1..dd679e3ea 100644 --- a/WidgetExtension/Variants/LatestFollowers/LatestFollowersWidget.swift +++ b/WidgetExtension/Variants/LatestFollowers/LatestFollowersWidget.swift @@ -80,8 +80,6 @@ private extension LatestFollowersWidgetProvider { func loadCurrentEntry(for configuration: LatestFollowersIntent, in context: Context, completion: @escaping (LatestFollowersEntry) -> Void) { Task { @MainActor in - AuthenticationServiceProvider.shared.prepareForUse() - guard let authBox = AuthenticationServiceProvider.shared.currentActiveUser.value else { diff --git a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift index 2cd78be4e..64e176c79 100644 --- a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift +++ b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift @@ -72,8 +72,6 @@ struct MultiFollowersCountWidget: Widget { private extension MultiFollowersCountWidgetProvider { func loadCurrentEntry(for configuration: MultiFollowersCountIntent, in context: Context, completion: @escaping (MultiFollowersCountEntry) -> Void) { Task { @MainActor in - - AuthenticationServiceProvider.shared.prepareForUse() guard let authBox = AuthenticationServiceProvider.shared.currentActiveUser.value