R.swift icon indicating copy to clipboard operation
R.swift copied to clipboard

Support for native dependency injection with storyboard

Open pmieszal opened this issue 4 years ago • 3 comments

iOS 13 introduces native dependency injection for storyboard view controllers. It would be truly awesome to add this feature for R.swift.

More here: https://www.hackingwithswift.com/example-code/uikit/how-to-use-dependency-injection-with-storyboards

pmieszal avatar Oct 17 '19 09:10 pmieszal

This would be a nice feature! I think we should generate an extra function on iOS 13 with a closure as last argument.

Something like this: R.storyboard.main.userController { coder in .init(coder: coder, user: myUser) }

tomlokhorst avatar Nov 27 '19 14:11 tomlokhorst

Note to self: I just found this branch I created over a year ago: https://github.com/mac-cain13/R.swift/tree/creator

I'm not sure what the state of this is. Maybe this works? Maybe it doesn't...

tomlokhorst avatar Mar 04 '21 17:03 tomlokhorst

@tomlokhorst How is the state of https://github.com/mac-cain13/R.swift.Library/tree/creator ? I created a similar extension in my project, and seems to be working.

extension UIStoryboard {
    func instantiateViewController<ViewControllerResource: StoryboardViewControllerResourceType>(
        withResource resource: ViewControllerResource,
        creator: @escaping (NSCoder) -> ViewControllerResource.ViewControllerType?
    ) -> ViewControllerResource.ViewControllerType?
    where ViewControllerResource.ViewControllerType: UIViewController {
        instantiateViewController(identifier: resource.identifier, creator: creator)
    }
}

extension _R.storyboard.launch {
    func launchViewController(creator: @escaping (NSCoder) -> LaunchViewController?) -> LaunchViewController? {
        UIStoryboard(resource: self)
            .instantiateViewController(
                withResource: launchViewController,
                creator: creator
            )
    }
}

let vc = R.storyboard.launch.launchViewController { .init(coder: $0, dependency: dependency) }

I think it should be merged at first, and fix StoryboardGenerator to use it in generated file.

417-72KI avatar Jun 28 '21 03:06 417-72KI