SlideShare a Scribd company logo
POMVVM@NATASHATHEROBOT
Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)
"Swift Is a Protocol-Oriented
Programming Language"
— Dave Abrahams, Professor of Blowing-Your-Mind
UITableViewDelegate
UITableViewDataSource
UITextFieldDelegate
NSURLSessionDelegate
CLLocationManagerDelegate
MCSessionDelegate
!
!
!
!
Artsy Engineering: MVVM in Swift
MODEL
let amount = 6729383.99
VIEW
Your balance is $6,729,383.99
VIEWMODEL
struct AccountViewModel {
let displayBalance: String
init(model: BankAccount) {
let formattedBalance = model.balance.currencyValue
displayBalance = "Your balance is (formattedBalance)"
}
}
VIEWCONTROLLER
var viewModel = ViewModel(model: Account)
Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)
VIEWCONTROLLER
var viewModel = ViewModel(model: Account)
PROTOCOLS
!!!
Protocol-Oriented MVVM (extended edition)
THE PROBLEM
class SwitchWithTextTableViewCell: UITableViewCell {
func configure(
title: String,
titleFont: UIFont,
titleColor: UIColor,
switchOn: Bool,
switchColor: UIColor = .purpleColor(),
onSwitchToggleHandler: onSwitchToggleHandlerType? = nil)
{
// configure views here
}
}
PROTOCOLS TO
THE RESCUE !
protocol SwitchWithTextCellProtocol {
var title: String { get }
var titleFont: UIFont { get }
var titleColor: UIColor { get }
var switchOn: Bool { get }
var switchColor: UIColor { get }
func onSwitchTogleOn(on: Bool)
}
extension SwitchWithTextCellProtocol {
var switchColor: UIColor {
return .purpleColor()
}
}
class SwitchWithTextTableViewCell: UITableViewCell {
func configure(withDelegate delegate: SwitchWithTextCellProtocol)
{
// configure views here
}
}
struct MinionModeViewModel: SwitchWithTextCellProtocol {
var title = "Minion Mode!!!"
var switchOn = true
var switchColor: UIColor {
return .yellowColor()
}
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
CELLFORROWATINDEXPATH
// YourViewController.swift
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell",
forIndexPath: indexPath) as! SwitchWithTextTableViewCell
// this is where the magic happens!
cell.configure(withDelegate: MinionModeViewModel())
return cell
!
✍
protocol SwitchWithTextCellDataSource {
var title: String { get }
var switchOn: Bool { get }
}
protocol SwitchWithTextCellDelegate {
func onSwitchTogleOn(on: Bool)
var switchColor: UIColor { get }
var textColor: UIColor { get }
var font: UIFont { get }
}
// SwitchWithTextTableViewCell
func configure(withDataSource dataSource: SwitchWithTextCellDataSource,
delegate: SwitchWithTextCellDelegate?)
{
// configure views here
}
struct MinionModeViewModel: SwitchWithTextCellDataSource {
var title = "Minion Mode!!!"
var switchOn = true
}
extension MinionModeViewModel: SwitchWithTextCellDelegate {
var switchColor: UIColor {
return .yellowColor()
}
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
// SettingsViewController
let viewModel = MinionModeViewModel()
cell.configure(withDataSource: viewModel, delegate: viewModel)
return cell
!
@MHOLLEMANS: MIXINS
AND TRAITS IN SWIFT 2.0
Protocol-Oriented MVVM (extended edition)
Protocol-Oriented MVVM (extended edition)
class AIPlayer: GameObject, AITrait, GunTrait, RenderTrait, HealthTrait {
...
}
class ZapMonster: GameObject, GunTrait, RenderTrait, HealthTrait, MovementTrait {
...
}
! "
protocol TextPresentable {
var text: String { get }
var textColor: UIColor { get }
var font: UIFont { get }
}
protocol SwitchPresentable {
var switchOn: Bool { get }
var switchColor: UIColor { get }
func onSwitchTogleOn(on: Bool)
}
protocol ImagePresentable {
var imageName: String { get }
}
protocol TextFieldPresentable {
var placeholder: String { get }
var text: String { get }
func onTextFieldDidEndEditing(textField: UITextField)
}
extension TextPresentable {
var textColor: UIColor {
return .blackColor()
}
var font: UIFont {
return .systemFontOfSize(17)
}
}
!!!
class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell {
private var delegate: T?
func configure(withDelegate delegate: T) {
// configure views here
}
}
extension MinionModeViewModel: TextPresentable {
var text: String { return "Minion Mode" }
var textColor: UIColor { return .blackColor() }
var font: UIFont { return .systemFontOfSize(17.0) }
}
extension MinionModeViewModel: SwitchPresentable {
var switchOn: Bool { return false }
var switchColor: UIColor { return .yellowColor() }
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell",
forIndexPath: indexPath) as! SwitchWithTextTableViewCell<MinionModeViewModel>
let viewModel = MinionModeViewModel()
cell.configure(withDelegate: viewModel)
return cell
!"#
"Change is the only constant."
— Unknown
Protocol-Oriented MVVM (extended edition)
class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable, T: ImagePresentable>: UITableViewCell {
}
extension MinionModeViewModel: ImagePresentable {
var imageName: String { return "minionParty.png" }
}
!"
> Use Protocols to Configure Your Views
> Use Protocol Extensions for Defaults
> Use ViewModels to Provide Data for the Protocols
HOW CAN WE
MAKE THIS
BETTER?

More Related Content

What's hot (19)

PDF
Workshop 5: JavaScript testing
Visual Engineering
 
PDF
Workshop 10: ECMAScript 6
Visual Engineering
 
PDF
Everything You (N)ever Wanted to Know about Testing View Controllers
Brian Gesiak
 
PDF
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
PDF
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
PDF
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
PDF
Viking academy backbone.js
Bert Wijnants
 
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
PDF
Rails on Oracle 2011
Raimonds Simanovskis
 
PDF
Integrating React.js with PHP projects
Ignacio Martín
 
PDF
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
PPTX
Vue next
Vitalii Ratyshnyi
 
KEY
Gwt and Xtend
Sven Efftinge
 
PDF
Why Every Tester Should Learn Ruby
Raimonds Simanovskis
 
KEY
Why ruby
rstankov
 
PDF
Frontin like-a-backer
Frank de Jonge
 
KEY
Backbone js
rstankov
 
KEY
Ruby/Rails
rstankov
 
PDF
React, Redux and es6/7
Dongho Cho
 
Workshop 5: JavaScript testing
Visual Engineering
 
Workshop 10: ECMAScript 6
Visual Engineering
 
Everything You (N)ever Wanted to Know about Testing View Controllers
Brian Gesiak
 
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
Viking academy backbone.js
Bert Wijnants
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
Rails on Oracle 2011
Raimonds Simanovskis
 
Integrating React.js with PHP projects
Ignacio Martín
 
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
Gwt and Xtend
Sven Efftinge
 
Why Every Tester Should Learn Ruby
Raimonds Simanovskis
 
Why ruby
rstankov
 
Frontin like-a-backer
Frank de Jonge
 
Backbone js
rstankov
 
Ruby/Rails
rstankov
 
React, Redux and es6/7
Dongho Cho
 

Similar to Protocol-Oriented MVVM (extended edition) (20)

PDF
Building mobile web apps with Mobello
Jeong-Geun Kim
 
PDF
[22]Efficient and Testable MVVM pattern
NAVER Engineering
 
PPTX
Asp.NET MVC
vrluckyin
 
PPTX
Knockout.js
Vivek Rajan
 
PPTX
Developing ASP.NET Applications Using the Model View Controller Pattern
goodfriday
 
PDF
Migrating Objective-C to Swift
Elmar Kretzer
 
PDF
Swift - One step forward from Obj-C
Nissan Tsafrir
 
PDF
Intro to Laravel 4
Singapore PHP User Group
 
PDF
Integrating SAP the Java EE Way - JBoss One Day talk 2012
hwilming
 
PPTX
Asp.net mvc training
icubesystem
 
PDF
Wt unit 2 ppts client side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
Wt unit 2 ppts client sied technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPT
Sqlapi0.1
jitendral
 
PDF
Say bye to Fragments with Conductor & Kotlin
Miquel Beltran Febrer
 
PPTX
Fundaments of Knockout js
Flavius-Radu Demian
 
PDF
Backbone.js — Introduction to client-side JavaScript MVC
pootsbook
 
PDF
Red Hat Agile integration Workshop Labs
Judy Breedlove
 
PDF
DataFX 8 (JavaOne 2014)
Hendrik Ebbers
 
PDF
Patterns Are Good For Managers
AgileThought
 
PDF
Jsf intro
vantinhkhuc
 
Building mobile web apps with Mobello
Jeong-Geun Kim
 
[22]Efficient and Testable MVVM pattern
NAVER Engineering
 
Asp.NET MVC
vrluckyin
 
Knockout.js
Vivek Rajan
 
Developing ASP.NET Applications Using the Model View Controller Pattern
goodfriday
 
Migrating Objective-C to Swift
Elmar Kretzer
 
Swift - One step forward from Obj-C
Nissan Tsafrir
 
Intro to Laravel 4
Singapore PHP User Group
 
Integrating SAP the Java EE Way - JBoss One Day talk 2012
hwilming
 
Asp.net mvc training
icubesystem
 
Wt unit 2 ppts client side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 2 ppts client sied technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Sqlapi0.1
jitendral
 
Say bye to Fragments with Conductor & Kotlin
Miquel Beltran Febrer
 
Fundaments of Knockout js
Flavius-Radu Demian
 
Backbone.js — Introduction to client-side JavaScript MVC
pootsbook
 
Red Hat Agile integration Workshop Labs
Judy Breedlove
 
DataFX 8 (JavaOne 2014)
Hendrik Ebbers
 
Patterns Are Good For Managers
AgileThought
 
Jsf intro
vantinhkhuc
 
Ad

More from Natasha Murashev (20)

PDF
Digital Nomad: The New Normal
Natasha Murashev
 
PDF
Build Features, Not Apps
Natasha Murashev
 
PDF
Build Features Not Apps
Natasha Murashev
 
PDF
Build Features Not Apps
Natasha Murashev
 
PDF
The Secret Life of a Digital Nomad
Natasha Murashev
 
PDF
How to Win on the Apple Watch
Natasha Murashev
 
PDF
Hello watchOS2
Natasha Murashev
 
PDF
The Swift Architect
Natasha Murashev
 
PDF
The Zen Guide to WatchOS 2
Natasha Murashev
 
PDF
HealthKit Deep Dive
Natasha Murashev
 
PDF
Using Parse in Hackathons
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
Unleash the Power of Playgrounds
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
AltConf 2015: Swift Thinking
Natasha Murashev
 
PDF
Swift Thinking
Natasha Murashev
 
PDF
Funcitonal Swift Conference: The Functional Way
Natasha Murashev
 
PDF
Intro To Swift
Natasha Murashev
 
PDF
HealthKit: Getting Ready for the New Year
Natasha Murashev
 
Digital Nomad: The New Normal
Natasha Murashev
 
Build Features, Not Apps
Natasha Murashev
 
Build Features Not Apps
Natasha Murashev
 
Build Features Not Apps
Natasha Murashev
 
The Secret Life of a Digital Nomad
Natasha Murashev
 
How to Win on the Apple Watch
Natasha Murashev
 
Hello watchOS2
Natasha Murashev
 
The Swift Architect
Natasha Murashev
 
The Zen Guide to WatchOS 2
Natasha Murashev
 
HealthKit Deep Dive
Natasha Murashev
 
Using Parse in Hackathons
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Unleash the Power of Playgrounds
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
AltConf 2015: Swift Thinking
Natasha Murashev
 
Swift Thinking
Natasha Murashev
 
Funcitonal Swift Conference: The Functional Way
Natasha Murashev
 
Intro To Swift
Natasha Murashev
 
HealthKit: Getting Ready for the New Year
Natasha Murashev
 
Ad

Recently uploaded (20)

PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
July Patch Tuesday
Ivanti
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 

Protocol-Oriented MVVM (extended edition)