R.swift
R.swift copied to clipboard
Support for native dependency injection with storyboard
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
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) }
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 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.