| 1 | import SwiftUI |
| 2 | import UIKit |
| 3 | |
| 4 | @MainActor |
| 5 | final class SceneDelegate: UIResponder, UIWindowSceneDelegate { |
| 6 | static let configurationName = "Default Configuration" |
| 7 | |
| 8 | var window: UIWindow? |
| 9 | |
| 10 | private let appState = AppState() |
| 11 | |
| 12 | func scene( |
| 13 | _ scene: UIScene, |
| 14 | willConnectTo session: UISceneSession, |
| 15 | options connectionOptions: UIScene.ConnectionOptions |
| 16 | ) { |
| 17 | guard let windowScene = scene as? UIWindowScene else { return } |
| 18 | |
| 19 | let launchURL = connectionOptions.urlContexts.first?.url |
| 20 | let tabBarController = KleponTabBarController(appState: appState) |
| 21 | let window = UIWindow(windowScene: windowScene) |
| 22 | window.rootViewController = tabBarController |
| 23 | window.makeKeyAndVisible() |
| 24 | self.window = window |
| 25 | |
| 26 | if let launchURL { |
| 27 | tabBarController.handle(url: launchURL) |
| 28 | } |
| 29 | |
| 30 | Task { |
| 31 | appState.guideEngine.refreshStorageUsage() |
| 32 | await appState.accountStore.restoreSessionIfNeeded() |
| 33 | if launchURL == nil, !appState.hasCompletedOnboarding { |
| 34 | await showOnboarding(from: tabBarController) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { |
| 40 | guard let url = URLContexts.first?.url, |
| 41 | let tabBarController = window?.rootViewController as? KleponTabBarController |
| 42 | else { |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | tabBarController.handle(url: url) |
| 47 | } |
| 48 | |
| 49 | private func showOnboarding(from presenter: UIViewController) async { |
| 50 | let onboardingView = OnboardingView( |
| 51 | onBrowseFirst: { [weak self] in self?.appState.completeOnboarding() }, |
| 52 | onComplete: { [weak self] in self?.appState.completeOnboarding() } |
| 53 | ) |
| 54 | .environmentObject(appState) |
| 55 | .environmentObject(appState.favoritesStore) |
| 56 | .environmentObject(appState.recentSearchStore) |
| 57 | .environmentObject(appState.recentlyViewedStore) |
| 58 | .environmentObject(appState.guideEngine) |
| 59 | .environmentObject(appState.accountStore) |
| 60 | |
| 61 | let hostingController = UIHostingController(rootView: onboardingView) |
| 62 | hostingController.modalPresentationStyle = .fullScreen |
| 63 | presenter.present(hostingController, animated: true) |
| 64 | } |
| 65 | } |