mac-cain13 / R.swift

Strong typed, autocompleted resources like images, fonts and segues in Swift projects
MIT License
9.45k stars 752 forks source link

Support for native dependency injection with storyboard #569

Closed pmieszal closed 1 year ago

pmieszal commented 4 years ago

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

tomlokhorst commented 4 years ago

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 commented 3 years ago

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...

417-72KI commented 3 years ago

@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.