AppMain.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import SwiftUI
  2. import Firebase
  3. // TODO: Errors are not shown to the user, but instead just logged
  4. @main
  5. struct AppMain: App {
  6. private let tag = "main"
  7. @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate: AppDelegate
  8. @StateObject private var store = Store.shared
  9. init() {
  10. Log.d(tag, "Launching ntfy 🥳. Welcome!")
  11. Log.d(tag, "Base URL is \(Config.appBaseUrl), user agent is \(ApiService.userAgent)")
  12. // We must configure Firebase here, and not in the AppDelegate. For some reason
  13. // configuring it there did not work.
  14. FirebaseApp.configure()
  15. FirebaseConfiguration.shared.setLoggerLevel(.max)
  16. }
  17. var body: some Scene {
  18. WindowGroup {
  19. MainView()
  20. .environmentObject(store)
  21. .environmentObject(delegate)
  22. .environment(\.managedObjectContext, store.context)
  23. .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
  24. // Use this hook instead of applicationDidBecomeActive, see https://stackoverflow.com/a/68888509/1440785
  25. // That post also explains how to start SwiftUI from AppDelegate if that's ever needed.
  26. Log.d(tag, "App became active, refreshing objects")
  27. store.hardRefresh()
  28. }
  29. }
  30. }
  31. }