Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CocoaheadsNL August - M2Mobi - Pragmatic MVVM

CocoaheadsNL August - M2Mobi - Pragmatic MVVM

Menno Lovink talks about using MVVM at M2Mobi

CocoaHeadsNL

August 24, 2016
Tweet

More Decks by CocoaHeadsNL

Other Decks in Technology

Transcript

  1. MVVM EXAMPLE struct Flight { let flightCode: String let destination:

    String let origin: String let departureDate: NSDate let arrivalDate: NSDate } VM M VC V
  2. MVVM EXAMPLE class HomeViewController: UIViewController { var flightTileTitle:UILabel var flightTileSubtitle:

    UILabel var flightTileImage: UIImageView var viewModel: FlightViewModel func refresh() { flightTileTitle.text = viewModel.flightTileTitle flightTileSubtitle.text = viewModel.flightTileSubtitle flightTileImage.image = viewModel.flightTileImage } } VM M VC V
  3. MVVM EXAMPLE struct FlightViewModel { let model: Flight var flightTileTitle:

    String { return "naar \(model.destination)” } var flightTileSubtitle: String { let prettyDate = NSDateFormatter().stringFromDate(model.departureDate) return "\(model.flightCode) - \(prettyDate)" } var flightTileImage: UIImage { return UIImage(named:”IC_LeavingFlight”) } } VM M VC V
  4. VIEWCONTROLLER RE-USE VM M VC V class HomeViewController: UIViewController {

    var flightTileTitle:UILabel var flightTileSubtitle: UILabel var flightTileImage: UIImageView var viewModel: FlightViewModel func refresh() { flightTileTitle.text = viewModel.flightTileTitle flightTileSubtitle.text = viewModel.flightTileSubtitle flightTileImage.image = viewModel.flightTileImage } }
  5. VIEWCONTROLLER RE-USE class HomeViewController: UIViewController { var flightTileTitle:UILabel var flightTileSubtitle:

    UILabel var flightTileImage: UIImageView var viewModel: HomeViewControllerViewModel func refresh() { flightTileTitle.text = viewModel.flightTileTitle flightTileSubtitle.text = viewModel.flightTileSubtitle flightTileImage.image = viewModel.flightTileImage } } VM M VC V
  6. VIEWCONTROLLER RE-USE protocol HomeViewControllerViewModel { var flightTileTitle: String { get

    } var flightTileSubtitle: String { get } var flightTileImage: UIImage { get } } class HomeViewController: UIViewController { var flightTileTitle:UILabel var flightTileSubtitle: UILabel var flightTileImage: UIImageView var viewModel: HomeViewControllerViewModel func refresh() { flightTileTitle.text = viewModel.flightTileTitle flightTileSubtitle.text = viewModel.flightTileSubtitle flightTileImage.image = viewModel.flightTileImage } } VM M VC V
  7. VIEWCONTROLLER RE-USE struct DepartingFlightViewModel: HomeViewControllerViewModel { let model: Flight var

    flightTileTitle: String { return "naar \(model.destination)” } var flightTileSubtitle: String { let prettyDate = NSDateFormatter().stringFromDate(model.departureDate) return "\(model.flightCode) - \(prettyDate)" } var tileImage: UIImage { return UIImage(named:”IC_LeavingFlight”) } } VM M VC V
  8. VIEWCONTROLLER RE-USE struct ArrivingFlightViewModel: HomeViewControllerViewModel { let model: Flight var

    flightTileTitle: String { return "van \(model.origin)” } var flightTileSubtitle: String { let prettyDate = NSDateFormatter().stringFromDate(model.arrivalDate) return "\(model.flightCode) - \(prettyDate)" } var tileImage: UIImage { return UIImage(named:”IC_ArrivingFlight”) } } VM M VC V
  9. TILE RE-USE class HomeViewController: UIViewController { var flightTileTitle:UILabel var flightTileSubtitle:

    UILabel var flightTileImage: UIImageView var viewModel: HomeViewControllerViewModel func refresh() { flightTileTitle.text = viewModel.flightTileTitle flightTileSubtitle.text = viewModel.flightTileSubtitle flightTileImage.image = viewModel.flightTileImage } } VM M VC V
  10. TILE RE-USE class ImageTile: UIView { var title:UILabel var subTitle:

    UILabel var image: UIImageView var viewModel: ImageTileViewModel func refresh() { tileTitle.text = viewModel.tileTitle tileSubtitle.text = viewModel.tileSubtitle tileImage.image = viewModel.tileImage } } VM M V
  11. TILE RE-USE struct EmployeeViewModel: ImageTileViewModel { let model: Employee var

    tileTitle: String { return "\(model.firstName) \(model.lastName)” } var tileSubtitle: String { return "\(model.profession)" } var tileImage: UIImage { return UIImage(named:”IC_PERSON”) } } VM M V
  12. TILE RE-USE VM M VC V class HomeViewController: UIViewController {

    var flightTile: ImageTile var viewModel: HomeViewControllerViewModel func refresh() { flightTile.viewModel = viewModel.flightTileViewModel } }
  13. TILE RE-USE class HomeViewController: UIViewController { var flightTile: ImageTile var

    viewModel: HomeViewControllerViewModel func refresh() { flightTile.viewModel = viewModel.flightTileViewModel } } VM M VC V protocol HomeViewControllerViewModel { var flightTileViewModel: ImageTileViewModel { get } }