"ios: mvvmc" - aleksandr nikolajev from mooncascade

29
iOS: MVVMC MobileMonday 15 May 2017 Aleksandr Nikolajev iOS developer

Upload: mobilemonday-estonia

Post on 22-Jan-2018

943 views

Category:

Software


2 download

TRANSCRIPT

Page 1: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

iOS: MVVMC

MobileMonday 15 May 2017

Aleksandr Nikolajev iOS developer

Page 2: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

ME• Graduated as material scientist in University of

Tartu

• Joined Mooncascade in 2015

• FinTech, transportation, multimedia, sports, utilities

Page 3: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

AGENDA• Introduction to MVVMC and it’s pieces

• Some examples

• Unit testing

Page 4: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

• Tight coupling between View and Controller

• Hard to test interactions between View and Controller

• Model can be testable

THE PROBLEM WITH APPLE’s MVC

Page 5: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

• An architecture pattern

• Model - View - ViewModel - Coordinator

• MVVMC treats UIViewController as a dumb presenter - just a view

WHAT IS MVVMC ?

Page 6: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

HOW IT WORKS ?• Coordinator - owns View and/or another Coordinator(s), notifies Coordinator(s)

• View - owns ViewModel, notifies ViewModel (e.g button tap)

• ViewModel - owns Model, updates View, notifies Coordinator

Page 7: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

Model• Usually a protocol

• C.R.U.D - Create, Read, Update, Delete

• Uses functions, NOT properties

• Injected into ViewModel

Page 8: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 9: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

ViewModel• Usually a class

• Independent of UIKit

• Injected into View and has no idea what the View is

• Business logic

• Operate with Model

• Provide presentation data to view and notify if that data has changed

• Communicate states to Coordinator

Page 10: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 11: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

Coordinator• Declared as a protocol

• Handle navigation

• Create View and inject ViewModel into it

Page 12: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 13: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

COMPLEX FLOWS• Many UIViewControllers

• Many options to choose where to go

• Does the UIViewController has to know what to show next?

Page 14: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 15: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 16: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

View• UIViewController is also a View !!!

• Present human readable data

• Do the layout

• Inform ViewModel about interactions

Page 17: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 18: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

NOT ONLY UIViewController • There is no rule about using ViewModels only

with UIViewController

• You can use ViewModels on almost on every UI element

• UITextField, UIButton, …

Page 19: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

NOT ONLY UIViewController

• This will decrease your code lines in ViewModel

• Makes code more distributed and increases reusability

• Adds more flexibility to write unit tests

Page 20: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 21: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 22: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

HOW IT COMES TOGETHER?

• The GOD — AppCoordinator

• Only AppDelegate knows about AppCoordinator

• Desides what to show when app is finished launching

Page 23: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 24: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade
Page 25: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

UNIT TESTING• ViewModel gets notified about events happened

in the view - we can test what is the result

• Test if presentation data is formatted correctly (e.g convert Date object into formatted date string)

• Test if correct delegate method is called when it’s needed to move on with the flow

• Test if errors are shown to view

Page 26: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

• Well distributed code

• Good testability

• Easy of use

THE PROFIT

Page 27: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

• MVVM-C A simple way to navigate http://tech.trivago.com/2016/08/26/mvvm-c-a-simple-way-to-navigate

• iOS Architecture Patternshttps://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52

REFERENCES

Page 28: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

THANK YOU!

Page 29: "iOS: MVVMC" - Aleksandr Nikolajev from Mooncascade

If you wish to play with the code, clone this repo

https://github.com/sasnik/MoMoMVVMC