remirobert / Mvvmbaseproject
Programming Languages
项目(使用MVVM结构)基于App极速启动
As is common in every app development cycle, there comes a time when we must start setting up a project. And this is pretty much always the same steps. Setting up folders, architecture, Podfile, etc.
This is a base project in Swift, for iOS application. This base project is based on the MVVM architeture. It's totally automated thanks to a script, for fast production.
#Installation
To install this base project, for your new app, just run the script install.sh. This will rename the base project with your app name, and install pods. After that, you will be ready to go.
git clone https://github.com/remirobert/MVVMBaseProject.git
cd MVVMBaseProject
./install.sh YOUR_APP_NAME
#Architecture
####📱 production / 🛠 development
The project has two different targets, prod and dev. And with the marco DEV, you can configure some global variables like the URL of your server, etc. Also two different LaunchScreens are provided to avoid confusion when you are developing your app.
This is how you can setup some variables depending on your developement environement. You can switch easily between two modes with the schemes.
#if DEV
let SERVER_URL = "http://dev.server.com/api/"
#else
let SERVER_URL = "http://prod.server.com/api/"
#endif
####MVVM
The structure of the base project is based on the MVVM (Model-View-ViewModel) architecture.
There are three core components in the MVVM pattern: the model, the view, and the view model. Each serves a distinct and separate role. The following illustration shows the relationships between the three components. So, in the iOS development the UIViewController has the UIView, and the controller. Each UIViewController has its one ViewModel.
To handle the communication between the UIViewController and the ViewModel, I use RxSwift, to observe the data, and bind it with the UIView (Any Reactive programing framework can be use here. You can event use some delegate pattern).
This design pattern will avoid you the MEGA UIViewController (thanks god 🙏).
####AppCoordinator
The idea being that is to move the UIViewController initalisation outside the application context by :
- manage the transitions between UIViewController, and the way to present them (present, push, show, etc)
- initialise the ViewModel for each UIViewController
- handle the data flow between UIViewController
At this point each UIViewController, are less or more very generic, and can be reused in an another application very easily.
example for a LoginViewController:
AppCoordinator.swift:
private func instanceLoginController() {
guard let vc = LoginViewController.instanceController(StoryBoards.Login) as? LoginViewController else {
return
}
vc.transitions = LoginTransitions(
didLogin: {
self.instanceFeedController()
})
//init ViewModel, can passe some data from other controllers
vc.viewModel = LoginViewModel()
self.rootController = UINavigationController(rootViewController: vc)
vc.start()
}
LoginViewController.swift:
struct LoginTransitions {
var didLogin: (Void -> Void)
}
class LoginViewController: UIViewController, Coordinable {
var transitions: LoginTransitions?
typealias ViewModel = LoginViewModel
var viewModel: ViewModel?
func didLoginUser() {
...
self.transitions?.didLogin()
}
...
}
made in 同济 with ❤️
If you have any suggestion to improve this architecture, or some reckon, please be free to tell me. All the pull request are welcome And also don't forget to ⭐️ if you like it 😉.
License
MVVMBaseProject
is released under an MIT License. See LICENSE
for details.
Copyright © 2016 Rémi ROBERT.
Please provide attribution, it is greatly appreciated.