YokanApp.swift
iosApp/YokanApp.swift
1import SwiftUI2import UIKit3import YokanShared45final class ReaderSystemUiViewController: UIViewController {6 private let contentController: UIViewController7 private var readerFullscreen = false8 private var appIsDark = false910 init(contentController: UIViewController) {11 self.contentController = contentController12 super.init(nibName: nil, bundle: nil)13 IosReaderSystemUiBridge.shared.register { [weak self] enabled in14 self?.setReaderFullscreen(enabled.boolValue)15 }16 IosSystemBarThemeBridge.shared.register { [weak self] isDark in17 self?.setAppTheme(isDark.boolValue)18 }19 }2021 @available(*, unavailable)22 required init?(coder: NSCoder) {23 fatalError("ReaderSystemUiViewController must be created programmatically")24 }2526 override var prefersStatusBarHidden: Bool {27 readerFullscreen28 }2930 override var preferredStatusBarStyle: UIStatusBarStyle {31 appIsDark ? .lightContent : .darkContent32 }3334 override var prefersHomeIndicatorAutoHidden: Bool {35 readerFullscreen36 }3738 override func viewDidLoad() {39 super.viewDidLoad()40 addChild(contentController)41 view.addSubview(contentController.view)42 contentController.didMove(toParent: self)43 }4445 override func viewDidLayoutSubviews() {46 super.viewDidLayoutSubviews()47 contentController.view.frame = view.bounds48 }4950 deinit {51 IosReaderSystemUiBridge.shared.unregister()52 IosSystemBarThemeBridge.shared.unregister()53 }5455 private func setReaderFullscreen(_ enabled: Bool) {56 guard readerFullscreen != enabled else { return }57 readerFullscreen = enabled58 setNeedsStatusBarAppearanceUpdate()59 setNeedsUpdateOfHomeIndicatorAutoHidden()60 }6162 private func setAppTheme(_ isDark: Bool) {63 let update = { [weak self] in64 guard let self, self.appIsDark != isDark else { return }65 self.appIsDark = isDark66 self.setNeedsStatusBarAppearanceUpdate()67 }68 if Thread.isMainThread { update() } else { DispatchQueue.main.async(execute: update) }69 }70}7172final class AppDelegate: NSObject, UIApplicationDelegate {73 func application(74 _ application: UIApplication,75 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil76 ) -> Bool {77 IosBackgroundTaskRegistration.shared.register()78 return true79 }8081 func applicationDidEnterBackground(_ application: UIApplication) {82 YokanEmbeddedMihonPause { _ in }83 YokanRuntime.shared.reschedulePeriodicTasks()84 }8586 func applicationDidBecomeActive(_ application: UIApplication) {87 YokanEmbeddedMihonResume { _ in }88 }8990 // Home-screen quick actions (Info.plist UIApplicationShortcutItems) carry a yokan:// route as their type.91 func application(92 _ application: UIApplication,93 configurationForConnecting connectingSceneSession: UISceneSession,94 options: UIScene.ConnectionOptions95 ) -> UISceneConfiguration {96 if let item = options.shortcutItem {97 AppDeepLinks.shared.dispatch(link: item.type)98 }99 let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)100 configuration.delegateClass = QuickActionSceneDelegate.self101 return configuration102 }103}104105final class QuickActionSceneDelegate: NSObject, UIWindowSceneDelegate {106 func windowScene(107 _ windowScene: UIWindowScene,108 performActionFor shortcutItem: UIApplicationShortcutItem,109 completionHandler: @escaping (Bool) -> Void110 ) {111 AppDeepLinks.shared.dispatch(link: shortcutItem.type)112 completionHandler(true)113 }114}115116struct ComposeRootView: UIViewControllerRepresentable {117 func makeUIViewController(context: Context) -> UIViewController {118 return ReaderSystemUiViewController(contentController: MainViewControllerKt.MainViewController())119 }120121 func updateUIViewController(_ uiViewController: UIViewController, context: Context) {122 }123}124125@main126struct YokanIOSApp: App {127 @UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate128129 var body: some Scene {130 WindowGroup {131 ComposeRootView()132 .ignoresSafeArea()133 .onOpenURL { url in134 AppDeepLinks.shared.dispatch(link: url.absoluteString)135 }136 }137 }138}139