Date post: | 22-Jan-2018 |
Category: |
Software |
Upload: | mobilemonday-estonia |
View: | 943 times |
Download: | 2 times |
iOS: MVVMC
MobileMonday 15 May 2017
Aleksandr Nikolajev iOS developer
ME• Graduated as material scientist in University of
Tartu
• Joined Mooncascade in 2015
• FinTech, transportation, multimedia, sports, utilities
AGENDA• Introduction to MVVMC and it’s pieces
• Some examples
• Unit testing
• Tight coupling between View and Controller
• Hard to test interactions between View and Controller
• Model can be testable
THE PROBLEM WITH APPLE’s MVC
• An architecture pattern
• Model - View - ViewModel - Coordinator
• MVVMC treats UIViewController as a dumb presenter - just a view
WHAT IS MVVMC ?
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
Model• Usually a protocol
• C.R.U.D - Create, Read, Update, Delete
• Uses functions, NOT properties
• Injected into ViewModel
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
Coordinator• Declared as a protocol
• Handle navigation
• Create View and inject ViewModel into it
COMPLEX FLOWS• Many UIViewControllers
• Many options to choose where to go
• Does the UIViewController has to know what to show next?
View• UIViewController is also a View !!!
• Present human readable data
• Do the layout
• Inform ViewModel about interactions
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, …
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
HOW IT COMES TOGETHER?
• The GOD — AppCoordinator
• Only AppDelegate knows about AppCoordinator
• Desides what to show when app is finished launching
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
• Well distributed code
• Good testability
• Easy of use
THE PROFIT
• 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
THANK YOU!
If you wish to play with the code, clone this repo
https://github.com/sasnik/MoMoMVVMC