https://github.com/fastred/storyboarddependencyinjectiontemplates
Code generation of initializers for storyboard-based view controllers
https://github.com/fastred/storyboarddependencyinjectiontemplates
Last synced: 4 months ago
JSON representation
Code generation of initializers for storyboard-based view controllers
- Host: GitHub
- URL: https://github.com/fastred/storyboarddependencyinjectiontemplates
- Owner: fastred
- License: mit
- Created: 2017-12-05T15:12:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-06T14:24:36.000Z (over 8 years ago)
- Last Synced: 2025-03-10T19:33:28.133Z (over 1 year ago)
- Language: HTML
- Homepage: http://holko.pl/2017/12/06/future-proof-dependency-injection/
- Size: 8.79 KB
- Stars: 19
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StoryboardDependencyInjectionTemplates
This repository provides templates for [Sourcery](https://github.com/krzysztofzablocki/Sourcery). They allow you to code generate initializers for storyboard-based view controllers.
# StoryboardDependencyInjection.ejs
1. Add the following protocol to your project:
```swift
protocol StoryboardInitializable where Self: UIViewController {
static func instantiateFromStoryboard() -> Self
}
```
2. Conform view controllers to it.
3. Use this template with [Sourcery](https://github.com/krzysztofzablocki/Sourcery).
### Example
For the following view controller:
```swift
final class ProfileViewController: UIViewController, StoryboardInitializable {
var viewModel: ProfileViewModel!
static func instantiateFromStoryboard() -> ProfileViewController {
let storyboard = UIStoryboard(name: "Profile", bundle: Bundle.main)
return storyboard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
}
}
```
Sourcery will generate `StoryboardDependencyInjection.generated.swift` with:
```swift
// MARK: - ProfileViewController - StoryboardDependencyInjection
extension ProfileViewController {
static func makeFromStoryboard(
viewModel: ProfileViewModel
) -> ProfileViewController {
let viewController = StoryboardScene.Profile.instantiateProfileViewController()
viewController.setDependencies(
viewModel: viewModel
)
return viewController
}
func setDependencies(
viewModel: ProfileViewModel
) {
self.viewModel = viewModel
}
}
```
Now, use `makeFromStoryboard(viewModel:)` to create instances of `ProfileViewController`.
When you add a new implicitly unwrapped optional property to that view controller, the definition of this method will automatically update, and the Swift compiler will let you know about all call sites needing update.
# StoryboardDependencyInjectionWithSwiftGen.ejs
1. Add the following empty protocol to your project:
```swift
protocol StoryboardInitializable where Self: UIViewController { }
```
2. Conform view controllers to it.
3. Set up and run [SwiftGen](https://github.com/SwiftGen/SwiftGen).
4. Use this template with [Sourcery](https://github.com/krzysztofzablocki/Sourcery).
### Example
For the following view controller:
```swift
final class ProfileViewController: UIViewController, StoryboardInitializable {
var viewModel: ProfileViewModel!
}
```
Sourcery will analyze your code and code generated by SwiftGen (`enum StoryboardScene`) and generate `StoryboardDependencyInjectionWithSwiftGen.generated.swift` with:
```swift
// MARK: - ProfileViewController - StoryboardDependencyInjection
extension ProfileViewController {
static func makeFromStoryboard(
viewModel: ProfileViewModel
) -> ProfileViewController {
let viewController = StoryboardScene.Profile.instantiateProfileViewController()
viewController.setDependencies(
viewModel: viewModel
)
return viewController
}
func setDependencies(
viewModel: ProfileViewModel
) {
self.viewModel = viewModel
}
}
```
Now, use `makeFromStoryboard(viewModel:)` to create instances of `ProfileViewController`.
When you add a new implicitly unwrapped optional property to that view controller, the definition of this method will automatically update, and the Swift compiler will let you know about all call sites needing update.