A handful of Xcode file templates for projects that use RXSwift and MVVM
A handful of Xcode file templates for projects that use RxSwift and my own variation of MVVM
These file templates are meant to be used for the following app architecture setup:
Let's call each "screen" or "view" in the app a Scene. So if you have an app that has a list view, a detail view and a login view you have 3 Scenes.
Each Scene consists of the following elements:
RouteRequest
for Scenes that should be presented by the main AppRouter
viewController()
method)When you want to show a new scene you never initialize any of its components yourself. Always ask the Scene's Builder to provide the Scene's ViewController via its static viewController()
method:
let nextViewController = NextBuilder.viewController()
If the new Scene needs some initial data you inject it into the new Scene's Builder.
A simple example:
You have a CarListScene
that shows a list of cars. When the user taps on a car in the list you want to navigate to a CarDetailsScene
that shows the details of the selected car. To hand the carID
to the Details Scene you inject it into the CarDetailsBuilder
:
class CarListRouter {
weak var viewController: CarListViewController?
func navigateToDetails(withCarID carID: String) {
let detailsViewController = CarDetailsBuilder.viewController(withCarID: carID)
viewController?.navigationController?.pushViewController(detailsViewController, animated: true)
}
}
The CarDetailsBuilder
then injects the data into the CarDetailsViewModel
:
struct CarDetailsBuilder {
static func viewController(withCarID carID: String) -> UIViewController {
let viewModel = CarDetailsViewModel(withCarID: carID)
let router = CarDetailsRouter()
let viewController = CarDetailsViewController(withViewModel: viewModel, router: router)
router.viewController = viewController
return viewController
}
}
And then the CarDetailsViewModel
prepares displayable data for the CarDetailsViewController
as it always does.
Download the RxSwift folder and add it to the following folder on your machine: ~/Library/Developer/Xcode/Templates
(You might need to create that folder if does not exist yet)
When you want to add a new scene to your app, open the New File dialog and choose RxSwift in the left column
Select Scene and click Next
Enter a name for the Scene. As you can see that name will be used as a prefix for all the 4 classes that will be needed for the scene (see above)
Click Next and save the scene. Xcode now creates the 4 classes needed for the scene.