Application: pass AppDelegate in initializer, instead of using singleton

This commit is contained in:
Andrew Breckenridge
2023-09-27 11:35:41 -07:00
committed by Stefan Ceriu
parent ee0539f260
commit 718e01f3bc
3 changed files with 16 additions and 21 deletions
@@ -25,6 +25,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
private let navigationRootCoordinator: NavigationRootCoordinator private let navigationRootCoordinator: NavigationRootCoordinator
private let userSessionStore: UserSessionStoreProtocol private let userSessionStore: UserSessionStoreProtocol
private let appSettings: AppSettings private let appSettings: AppSettings
private let appDelegate: AppDelegate
/// Common background task to continue long-running tasks in the background. /// Common background task to continue long-running tasks in the background.
private var backgroundTask: BackgroundTaskProtocol? private var backgroundTask: BackgroundTaskProtocol?
@@ -57,7 +58,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
private let appRouteURLParser: AppRouteURLParser private let appRouteURLParser: AppRouteURLParser
@Consumable private var storedAppRoute: AppRoute? @Consumable private var storedAppRoute: AppRoute?
init() { init(appDelegate: AppDelegate) {
Self.setupEnvironmentVariables() Self.setupEnvironmentVariables()
let appSettings = AppSettings() let appSettings = AppSettings()
@@ -74,6 +75,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
AppSettings.reset() AppSettings.reset()
} }
self.appDelegate = appDelegate
self.appSettings = appSettings self.appSettings = appSettings
appRouteURLParser = AppRouteURLParser(appSettings: appSettings) appRouteURLParser = AppRouteURLParser(appSettings: appSettings)
@@ -493,7 +495,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
notificationManager.setUserSession(userSession) notificationManager.setUserSession(userSession)
notificationManager.requestAuthorization() notificationManager.requestAuthorization()
if let appDelegate = AppDelegate.shared {
appDelegateObserver = appDelegate.callbacks appDelegateObserver = appDelegate.callbacks
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] callback in .sink { [weak self] callback in
@@ -504,9 +505,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
self?.notificationManager.registrationFailed(with: error) self?.notificationManager.registrationFailed(with: error)
} }
} }
} else {
MXLog.error("Couldn't register to AppDelegate callbacks")
}
} }
private func observeUserSessionChanges() { private func observeUserSessionChanges() {
@@ -24,12 +24,9 @@ enum AppDelegateCallback {
} }
class AppDelegate: NSObject, UIApplicationDelegate { class AppDelegate: NSObject, UIApplicationDelegate {
private(set) static var shared: AppDelegate!
let callbacks = PassthroughSubject<AppDelegateCallback, Never>() let callbacks = PassthroughSubject<AppDelegateCallback, Never>()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { 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) NSTextAttachment.registerViewProviderClass(PillAttachmentViewProvider.self, forFileType: InfoPlistReader.main.pillsUTType)
return true return true
} }
@@ -20,7 +20,7 @@ import SwiftUI
struct Application: App { struct Application: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) private var applicationDelegate @UIApplicationDelegateAdaptor(AppDelegate.self) private var applicationDelegate
@Environment(\.openURL) private var openURL @Environment(\.openURL) private var openURL
private let appCoordinator: AppCoordinatorProtocol private var appCoordinator: AppCoordinatorProtocol!
init() { init() {
if ProcessInfo.isRunningUITests { if ProcessInfo.isRunningUITests {
@@ -28,7 +28,7 @@ struct Application: App {
} else if ProcessInfo.isRunningUnitTests { } else if ProcessInfo.isRunningUnitTests {
appCoordinator = UnitTestsAppCoordinator() appCoordinator = UnitTestsAppCoordinator()
} else { } else {
appCoordinator = AppCoordinator() appCoordinator = AppCoordinator(appDelegate: applicationDelegate)
} }
} }