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

Protocol-Oriented MVVM

Protocol-Oriented MVVM

With Swift 2.0 came Protocol Extensions, which make Swift a Protocol-Oriented Programming Language. Learn what that means and how to apply protocol extensions to create beautiful and robust code without ever defaulting to subclassing.

By Natasha the Robot http://natashatherobot.com

Powered by http://xebia.com

do{iOS} conference

November 09, 2015
Tweet

More Decks by do{iOS} conference

Other Decks in Programming

Transcript

  1. !

  2. !

  3. !

  4. !

  5. VIEWMODEL struct AccountViewModel { let displayBalance: String init(model: BankAccount) {

    let formattedBalance = model.balance.currencyValue displayBalance = "Your balance is \(formattedBalance)" } }
  6. THE PROBLEM class SwitchWithTextTableViewCell: UITableViewCell { func configure( title: String,

    titleFont: UIFont, titleColor: UIColor, switchOn: Bool, switchColor: UIColor = .purpleColor(), onSwitchToggleHandler: onSwitchToggleHandlerType? = nil) { // configure views here } }
  7. 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) }
  8. 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!") } } }
  9. CELLFORROWATINDEXPATH // YourViewController.swift let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as!

    SwitchWithTextTableViewCell // this is where the magic happens! cell.configure(withDelegate: MinionModeViewModel()) return cell
  10. !

  11. 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 } }
  12. 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!") } } }
  13. !

  14. class AIPlayer: GameObject, AITrait, GunTrait, RenderTrait, HealthTrait { ... }

    class ZapMonster: GameObject, GunTrait, RenderTrait, HealthTrait, MovementTrait { ... }
  15. ! "

  16. 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) }
  17. protocol ImagePresentable { var imageName: String { get } }

    protocol TextFieldPresentable { var placeholder: String { get } var text: String { get } func onTextFieldDidEndEditing(textField: UITextField) }
  18. extension TextPresentable { var textColor: UIColor { return .blackColor() }

    var font: UIFont { return .systemFontOfSize(17) } }
  19. !!! class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell {

    private var delegate: T? func configure(withDelegate delegate: T) { // configure views here } }
  20. extension MinionModeViewModel: TextPresentable { var text: String { return "Minion

    Mode" } var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17.0) } }
  21. 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!") } } }
  22. !"#

  23. !"

  24. > Use Protocols to Configure Your Views > Use Protocol

    Extensions for Defaults > Use ViewModels to Provide Data for the Protocols