Protocol-Oriented MVVM (extended edition)
-
Upload
natasha-murashev -
Category
Technology
-
view
3.544 -
download
0
Transcript of Protocol-Oriented MVVM (extended edition)
![Page 1: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/1.jpg)
POMVVM@NATASHATHEROBOT
![Page 2: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/2.jpg)
![Page 3: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/3.jpg)
![Page 4: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/4.jpg)
"Swift Is a Protocol-Oriented Programming Language"
— Dave Abrahams, Professor of Blowing-Your-Mind
![Page 5: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/5.jpg)
UITableViewDelegate UITableViewDataSourceUITextFieldDelegateNSURLSessionDelegateCLLocationManagerDelegateMCSessionDelegate
![Page 6: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/6.jpg)
!
![Page 7: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/7.jpg)
!
![Page 8: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/8.jpg)
!
![Page 9: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/9.jpg)
!
![Page 10: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/10.jpg)
Artsy Engineering: MVVM in Swift
![Page 11: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/11.jpg)
MODELlet amount = 6729383.99
![Page 12: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/12.jpg)
VIEWYour balance is $6,729,383.99
![Page 13: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/13.jpg)
VIEWMODELstruct AccountViewModel { let displayBalance: String
init(model: BankAccount) { let formattedBalance = model.balance.currencyValue displayBalance = "Your balance is \(formattedBalance)" }}
![Page 14: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/14.jpg)
VIEWCONTROLLERvar viewModel = ViewModel(model: Account)
![Page 15: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/15.jpg)
![Page 16: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/16.jpg)
![Page 17: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/17.jpg)
![Page 18: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/18.jpg)
VIEWCONTROLLERvar viewModel = ViewModel(model: Account)
![Page 19: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/19.jpg)
PROTOCOLS!!!
![Page 20: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/20.jpg)
![Page 21: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/21.jpg)
THE PROBLEMclass SwitchWithTextTableViewCell: UITableViewCell {
func configure( title: String, titleFont: UIFont, titleColor: UIColor, switchOn: Bool, switchColor: UIColor = .purpleColor(), onSwitchToggleHandler: onSwitchToggleHandlerType? = nil) { // configure views here }
}
![Page 22: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/22.jpg)
PROTOCOLS TO THE RESCUE !
![Page 23: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/23.jpg)
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)}
![Page 24: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/24.jpg)
extension SwitchWithTextCellProtocol {
var switchColor: UIColor { return .purpleColor() }
}
![Page 25: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/25.jpg)
class SwitchWithTextTableViewCell: UITableViewCell {
func configure(withDelegate delegate: SwitchWithTextCellProtocol) { // configure views here }
}
![Page 26: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/26.jpg)
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!") } }}
![Page 27: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/27.jpg)
CELLFORROWATINDEXPATH// YourViewController.swiftlet cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell
// this is where the magic happens!cell.configure(withDelegate: MinionModeViewModel())
return cell
![Page 28: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/28.jpg)
!
![Page 29: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/29.jpg)
✍
![Page 30: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/30.jpg)
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 }}
![Page 31: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/31.jpg)
// SwitchWithTextTableViewCell
func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) { // configure views here }
![Page 32: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/32.jpg)
struct MinionModeViewModel: SwitchWithTextCellDataSource { var title = "Minion Mode!!!" var switchOn = true}
![Page 33: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/33.jpg)
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!") } }}
![Page 34: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/34.jpg)
// SettingsViewController
let viewModel = MinionModeViewModel()cell.configure(withDataSource: viewModel, delegate: viewModel)return cell
![Page 35: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/35.jpg)
!
![Page 36: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/36.jpg)
@MHOLLEMANS: MIXINS AND TRAITS IN SWIFT 2.0
![Page 37: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/37.jpg)
![Page 38: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/38.jpg)
![Page 39: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/39.jpg)
class AIPlayer: GameObject, AITrait, GunTrait, RenderTrait, HealthTrait { ...}
class ZapMonster: GameObject, GunTrait, RenderTrait, HealthTrait, MovementTrait { ...}
![Page 40: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/40.jpg)
! "
![Page 41: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/41.jpg)
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)}
![Page 42: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/42.jpg)
protocol ImagePresentable { var imageName: String { get }}
protocol TextFieldPresentable { var placeholder: String { get } var text: String { get }
func onTextFieldDidEndEditing(textField: UITextField)}
![Page 43: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/43.jpg)
extension TextPresentable {
var textColor: UIColor { return .blackColor() }
var font: UIFont { return .systemFontOfSize(17) }}
![Page 44: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/44.jpg)
!!!class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell { private var delegate: T?
func configure(withDelegate delegate: T) { // configure views here }
}
![Page 45: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/45.jpg)
extension MinionModeViewModel: TextPresentable { var text: String { return "Minion Mode" } var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17.0) }}
![Page 46: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/46.jpg)
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!") } }}
![Page 47: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/47.jpg)
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell<MinionModeViewModel>
let viewModel = MinionModeViewModel()cell.configure(withDelegate: viewModel)return cell
![Page 48: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/48.jpg)
!"#
![Page 49: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/49.jpg)
"Change is the only constant."— Unknown
![Page 50: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/50.jpg)
![Page 51: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/51.jpg)
class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable, T: ImagePresentable>: UITableViewCell {
}
![Page 52: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/52.jpg)
extension MinionModeViewModel: ImagePresentable { var imageName: String { return "minionParty.png" }}
![Page 53: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/53.jpg)
!"
![Page 54: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/54.jpg)
> Use Protocols to Configure Your Views> Use Protocol Extensions for Defaults
> Use ViewModels to Provide Data for the Protocols
![Page 55: Protocol-Oriented MVVM (extended edition)](https://reader034.fdocuments.in/reader034/viewer/2022050613/589f83e81a28abf0638b5e7b/html5/thumbnails/55.jpg)
HOW CAN WE MAKE THIS BETTER?