This project is inherited from https://github.com/duyduong/DTMvvm
DTMvvm is a library for who wants to start writing iOS application using MVVM (Model-View-ViewModel), written in Swift.
- Base classes for UIViewController, UIView, UITableViewCell and UICollectionCell
- Base classes for ViewModel, ListViewModel and CellViewModel
- Services injection
- Custom transitioning for UINavigationController and UIViewController
- iOS 9.0+
- Xcode 9.0+
- Swift 4.0+
The library heavily depends on RxSwift for data-binding and events. For who does not familiar with Reactive Programming, I suggest to start reading about it first. Beside that, here are the list of dependencies:
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate DTMvvm into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'DTMvvm'
end
Then, run the following command:
$ pod install
To run the example project, clone the repo, and run pod install
from the Example directory first.
The library is mainly written using Generic, so please familiar yourself with Swift Generic, and very important point, we can’t use Generic UIViewController to associate with UIViewController on InterfaceBuilder or Storyboard. So programmatically is prefer, but we can still use XIBs to instantiate our view (check example for more details)
I prefer Page over ViewController in term of MVVM
open class Page<VM: IViewModel>: UIViewController, IView, ITransionView
open class ListPage<VM: IListViewModel>: Page<VM>
open class CollectionPage<VM: IListViewModel>: Page<VM>
The idea is that each Page will contain a ViewModel property with type to be determined by generic VM
Same as Page, View is all also a generic UIView, while TableCell and CollectionCell are generic cell that can be used in ListPage and CollectionPage
open class View<VM: IGenericViewModel>: UIView, IView
open class CollectionCell<VM: IGenericViewModel>: UICollectionViewCell, IView
open class TableCell<VM: IGenericViewModel>: UITableViewCell, IView
They all have generic type VM to determine its own ViewModel
By inheriting View or Page, and implementing 2 main methods:
open func initialize() {}
open func bindViewAndViewModel() {}
Then we have a full set of a view that can bind with ViewModel
Base classes for our ViewModel
open class ViewModel<M: Model>: NSObject, IViewModel
open class ListViewModel<M: Model, CVM: IGenericViewModel>: ViewModel<M>, IListViewModel
open class CellViewModel<M: Model>: NSObject, IGenericViewModel
As we can see, ViewModel and CellViewModel use one generic type M (which is based type is Model). This generic type is for us to determine the model type for each ViewModel. The difference between ViewModel and CellViewModel is ViewModel contains navigation service that can help use to navigate between our pages in apllication, while CellViewModel does not. ListViewModel is a bit different. It uses one more generic type CVM, which represented for ViewModel type of a cell in side a page. In the other hand, it contains an items source array that can be bind with a list page or collection page
Please check examples for details usages of these base classes.
The library also supports services injection (for Unit Test) and some built-in services, especially navigation service, that helps us to navigate between our pages. Navigation service, by default, is injected to Page and ViewModel By calling
DependencyManager.shared.registerDefaults()
to register for all built-in services (NavigationService, StorageService and AlertService) in the library Or you can create your own navigation service and override the default injection. See examples for detail steps to setup services injection.
The library also supports for page transitions, including pages inside a navigation page and pages that presents modally. See examples for how to implement page transitions
DTMvvm is available under the MIT license. See the LICENSE file for more info.