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

View File

@ -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() {

View File

@ -24,12 +24,9 @@ enum AppDelegateCallback {
}
class AppDelegate: NSObject, UIApplicationDelegate {
private(set) static var shared: AppDelegate!
let callbacks = PassthroughSubject<AppDelegateCallback, Never>()
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
}

View File

@ -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
}