From 5c3ec45a9552410f46c7173c46157c730ad2746b Mon Sep 17 00:00:00 2001 From: Sasikanth Miriyampalli Date: Mon, 16 Oct 2023 15:59:07 +0530 Subject: [PATCH] Provide UI view controller via application component --- iosApp/iosApp/AppDelegate.swift | 39 ++++++++++++++++++- .../rss/reader/di/ApplicationComponent.kt | 6 ++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/iosApp/iosApp/AppDelegate.swift b/iosApp/iosApp/AppDelegate.swift index 44a09f726..88d2a40fb 100644 --- a/iosApp/iosApp/AppDelegate.swift +++ b/iosApp/iosApp/AppDelegate.swift @@ -14,7 +14,9 @@ import Sentry class AppDelegate: NSObject, UIApplicationDelegate { let rootHolder: RootHolder = RootHolder() - lazy var applicationComponent: InjectApplicationComponent = InjectApplicationComponent() + lazy var applicationComponent: InjectApplicationComponent = InjectApplicationComponent( + uiViewControllerProvider: { UIApplication.topViewController()! } + ) func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { @@ -67,3 +69,38 @@ class AppDelegate: NSObject, UIApplicationDelegate { } } } + +extension UIApplication { + + private class func keyWindowCompat() -> UIWindow? { + return UIApplication + .shared + .connectedScenes + .flatMap { ($0 as? UIWindowScene)?.windows ?? [] } + .last { $0.isKeyWindow } + } + + class func topViewController( + base: UIViewController? = UIApplication.keyWindowCompat()?.rootViewController + ) -> UIViewController? { + if let nav = base as? UINavigationController { + return topViewController(base: nav.visibleViewController) + } + + if let tab = base as? UITabBarController { + let moreNavigationController = tab.moreNavigationController + + if let top = moreNavigationController.topViewController, top.view.window != nil { + return topViewController(base: top) + } else if let selected = tab.selectedViewController { + return topViewController(base: selected) + } + } + + if let presented = base?.presentedViewController { + return topViewController(base: presented) + } + + return base + } +} diff --git a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/di/ApplicationComponent.kt b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/di/ApplicationComponent.kt index 67a87894b..7eac7fd62 100644 --- a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/di/ApplicationComponent.kt +++ b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/di/ApplicationComponent.kt @@ -18,10 +18,14 @@ package dev.sasikanth.rss.reader.di import dev.sasikanth.rss.reader.di.scopes.AppScope import dev.sasikanth.rss.reader.repository.RssRepository import me.tatarka.inject.annotations.Component +import me.tatarka.inject.annotations.Provides +import platform.UIKit.UIViewController @AppScope @Component -abstract class ApplicationComponent : SharedApplicationComponent() { +abstract class ApplicationComponent( + @get:Provides val uiViewControllerProvider: () -> UIViewController, +) : SharedApplicationComponent() { abstract val rssRepository: RssRepository }