diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index 03d662476..09ed73d65 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -25,7 +25,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, private let navigationRootCoordinator: NavigationRootCoordinator private let userSessionStore: UserSessionStoreProtocol private let appSettings: AppSettings - + private let appDelegate: AppDelegate + /// Common background task to continue long-running tasks in the background. private var backgroundTask: BackgroundTaskProtocol? @@ -57,7 +58,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, private let appRouteURLParser: AppRouteURLParser @Consumable private var storedAppRoute: AppRoute? - init() { + init(appDelegate: AppDelegate) { Self.setupEnvironmentVariables() let appSettings = AppSettings() @@ -74,6 +75,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, AppSettings.reset() } + self.appDelegate = appDelegate self.appSettings = appSettings appRouteURLParser = AppRouteURLParser(appSettings: appSettings) @@ -493,20 +495,16 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, notificationManager.setUserSession(userSession) notificationManager.requestAuthorization() - if let appDelegate = AppDelegate.shared { - appDelegateObserver = appDelegate.callbacks - .receive(on: DispatchQueue.main) - .sink { [weak self] callback in - switch callback { - case .registeredNotifications(let deviceToken): - Task { await self?.notificationManager.register(with: deviceToken) } - case .failedToRegisteredNotifications(let error): - self?.notificationManager.registrationFailed(with: error) - } + appDelegateObserver = appDelegate.callbacks + .receive(on: DispatchQueue.main) + .sink { [weak self] callback in + switch callback { + case .registeredNotifications(let deviceToken): + Task { await self?.notificationManager.register(with: deviceToken) } + case .failedToRegisteredNotifications(let error): + self?.notificationManager.registrationFailed(with: error) } - } else { - MXLog.error("Couldn't register to AppDelegate callbacks") - } + } } private func observeUserSessionChanges() { diff --git a/ElementX/Sources/Application/AppDelegate.swift b/ElementX/Sources/Application/AppDelegate.swift index 4dcb3517a..8be8ee0a4 100644 --- a/ElementX/Sources/Application/AppDelegate.swift +++ b/ElementX/Sources/Application/AppDelegate.swift @@ -24,12 +24,9 @@ enum AppDelegateCallback { } class AppDelegate: NSObject, UIApplicationDelegate { - private(set) static var shared: AppDelegate! let callbacks = PassthroughSubject() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { - // worst singleton ever - Self.shared = self NSTextAttachment.registerViewProviderClass(PillAttachmentViewProvider.self, forFileType: InfoPlistReader.main.pillsUTType) return true } diff --git a/ElementX/Sources/Application/Application.swift b/ElementX/Sources/Application/Application.swift index 2e1df3e20..daaaeac8b 100644 --- a/ElementX/Sources/Application/Application.swift +++ b/ElementX/Sources/Application/Application.swift @@ -20,7 +20,7 @@ import SwiftUI struct Application: App { @UIApplicationDelegateAdaptor(AppDelegate.self) private var applicationDelegate @Environment(\.openURL) private var openURL - private let appCoordinator: AppCoordinatorProtocol + private var appCoordinator: AppCoordinatorProtocol! init() { if ProcessInfo.isRunningUITests { @@ -28,7 +28,7 @@ struct Application: App { } else if ProcessInfo.isRunningUnitTests { appCoordinator = UnitTestsAppCoordinator() } else { - appCoordinator = AppCoordinator() + appCoordinator = AppCoordinator(appDelegate: applicationDelegate) } } @@ -57,7 +57,7 @@ struct Application: App { } } } - + private var shouldHideStatusBar: Bool { ProcessInfo.isRunningUITests }