NilStack / MVVMBaseProject

Base project to start :run: new app quickly, with MVVM architecture.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

screen shot 2016-04-30 at 17 52 16

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.

./install.sh YOUR_APP_NAME

tty

#Architecture

####MVVM

The structure of the base project is based on the MVVM (Model-View-ViewModel) architecture.

ic564167

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).

####AppCoordinator

The idea being that is to remove the UIViewController initalisation outside the application context.

  • 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()
  })
  
  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()
    }
 
    ...
}

License

MVVMBaseProject is released under an MIT License. See LICENSE for details.

Copyright © 2015 Rémi ROBERT.

Please provide attribution, it is greatly appreciated.

About

Base project to start :run: new app quickly, with MVVM architecture.

License:MIT License


Languages

Language:Swift 78.2%Language:Shell 19.8%Language:Ruby 2.0%