Created
August 19, 2021 03:16
-
-
Save zmcartor/817b08571e184a35b43af89f101b8850 to your computer and use it in GitHub Desktop.
MVVM+Coordinator Architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct FrontScreenViewmodel { | |
var hello = "there" | |
} | |
// Builds the ViewControllers, and injects the ViewModels | |
class Factory { | |
let viewModelFactory = ViewModelFactory() | |
let mainStack: UINavigationController | |
func createFrontScreen(withStack: UINavigationController? = nil) -> UIViewController { | |
// create a new Coordinator, pass in our factory | |
let coor = FrontScreenCoordinator(withStack: withStack ?? mainStack, factory: self) | |
let viewmodel = viewModelFactory.FrontScreenViewmodel() | |
return FrontScreenViewController(withViewModel: viewmodel, coordinator: coor) | |
} | |
init(withStack: UINavigationController) { | |
mainStack = withStack | |
} | |
} | |
// Handles Core Data, Networing, etc assembly of documentModel into ViewModel | |
class ViewModelFactory { | |
func FrontScreenViewmodel() -> FrontScreenViewmodel { | |
// pass in other things like interactors, etc. | |
return FrontScreenViewmodel() | |
} | |
} | |
class FrontScreenCoordinator { | |
// encapsulates the Routes this VC can trigger. Each VC has unique coordinator. | |
func gotoNext() {} | |
func goBack() {} | |
func showStore(withId: Int) { | |
let vc = factory.createFrontScreen() | |
stack.pushViewController(vc, animated: true) | |
} | |
private let factory: Factory | |
private let stack: UINavigationController | |
init(withStack: UINavigationController, factory: Factory) { | |
self.stack = withStack | |
self.factory = factory | |
} | |
} | |
class FrontScreenViewController: UIViewController { | |
private let coordinator: FrontScreenCoordinator | |
private let viewModel: FrontScreenViewmodel | |
init(withViewModel: FrontScreenViewmodel, coordinator: FrontScreenCoordinator) { | |
self.coordinator = coordinator | |
self.viewModel = withViewModel | |
super.init() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment