Swiftpack.co -  Package - duyduong/DDMvvm
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
duyduong/DDMvvm
iOS swift MVVM library
.package(url: "https://github.com/duyduong/DDMvvm.git", from: "1.9.0")

DDMvvm

CI Status Version License Platform

DDMvvm is a library for who wants to start writing iOS application using MVVM (Model-View-ViewModel), written in Swift.

Features

  • ☑ Base classes for UIViewController, UIView, UITableViewCell and UICollectionCell
  • ☑ Base classes for ViewModel, ListViewModel and CellViewModel
  • ☑ Services injection
  • ☑ Custom transitioning for UINavigationController and UIViewController

Requirements

  • iOS 9.0+
  • Xcode 9.0+
  • Swift 4.0+

Dependencies

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:

Installation

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate DDMvvm 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 'DDMvvm'
end

Then, run the following command:

$ pod install

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

At the glance

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)

Libray Components

Page, ListPage and CollectionPage

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

View, TableCell and CollectionCell

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

ViewModel, ListViewModel and CellViewModel

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.

Services

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.

Page Transitions

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

License

DDMvvm is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 16
Last commit: 4 weeks ago

Release Notes

Suport for Carthage, Swift PM and Cocoapods
4 weeks ago

Added support for Carthage, Swift PM and Cocoapods

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco