Skip to content

Commit

Permalink
Provide UI view controller via application component
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Oct 16, 2023
1 parent d4a2227 commit 5c3ec45
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
39 changes: 38 additions & 1 deletion iosApp/iosApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5c3ec45

Please sign in to comment.