Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Protocol Oriented MVVM - Auckland iOS Meetup

1,726 views

Published on

Originally presented at Auckland iOS meetup http://www.meetup.com/auckland-ios-meetup/events/227283431/

Published in: Technology
  • Be the first to comment

Protocol Oriented MVVM - Auckland iOS Meetup

  1. 1. P!MVVM@NATASHATHEROBOT
  2. 2. "Swift Is a Protocol-Oriented Programming Language" — Dave Abrahams, Professor of Blowing-Your-Mind
  3. 3. UITableViewDelegate UITableViewDataSource UITextFieldDelegate NSURLSessionDelegate CLLocationManagerDelegate MCSessionDelegate
  4. 4. !
  5. 5. !
  6. 6. !
  7. 7. !
  8. 8. Artsy Engineering: MVVM in Swift
  9. 9. MODEL let amount = 6729383.99
  10. 10. VIEW Your balance is $6,729,383.99
  11. 11. VIEWMODEL struct AccountViewModel { let displayBalance: String init(model: BankAccount) { let formattedBalance = model.balance.currencyValue displayBalance = "Your balance is (formattedBalance)" } }
  12. 12. VIEWCONTROLLER var viewModel = ViewModel(model: Account)
  13. 13. VIEWCONTROLLER var viewModel = ViewModel(model: Account)
  14. 14. PROTOCOLS !!!
  15. 15. THE PROBLEM class SwitchWithTextTableViewCell: UITableViewCell { func configure( title: String, titleFont: UIFont, titleColor: UIColor, switchOn: Bool, switchColor: UIColor = .purpleColor(), onSwitchToggleHandler: onSwitchToggleHandlerType? = nil) { // configure views here } }
  16. 16. PROTOCOLS TO THE RESCUE !
  17. 17. 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) }
  18. 18. extension SwitchWithTextCellProtocol { var switchColor: UIColor { return .purpleColor() } }
  19. 19. class SwitchWithTextTableViewCell: UITableViewCell { func configure(withDelegate delegate: SwitchWithTextCellProtocol) { // configure views here } }
  20. 20. 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!") } } }
  21. 21. CELLFORROWATINDEXPATH // YourViewController.swift let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell // this is where the magic happens! cell.configure(withDelegate: MinionModeViewModel()) return cell
  22. 22. !
  23. 23.
  24. 24. 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 } }
  25. 25. // SwitchWithTextTableViewCell func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) { // configure views here }
  26. 26. struct MinionModeViewModel: SwitchWithTextCellDataSource { var title = "Minion Mode!!!" var switchOn = true }
  27. 27. 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!") } } }
  28. 28. // SettingsViewController let viewModel = MinionModeViewModel() cell.configure(withDataSource: viewModel, delegate: viewModel) return cell
  29. 29. !
  30. 30. @MHOLLEMANS: MIXINS AND TRAITS IN SWIFT 2.0
  31. 31. class AIPlayer: GameObject, AITrait, GunTrait, RenderTrait, HealthTrait { ... } class ZapMonster: GameObject, GunTrait, RenderTrait, HealthTrait, MovementTrait { ... }
  32. 32. ! "
  33. 33. 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) }
  34. 34. protocol ImagePresentable { var imageName: String { get } } protocol TextFieldPresentable { var placeholder: String { get } var text: String { get } func onTextFieldDidEndEditing(textField: UITextField) }
  35. 35. extension TextPresentable { var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17) } }
  36. 36. !!! class SwitchWithTextTableViewCell: UITableViewCell { func configure<T where T: TextPresentable, T: SwitchPresentable>(withPresenter presenter: T) // configure views here } }
  37. 37. !!!!!! typealias SwitchWithTextViewPresentable = protocol<TextPresentable, SwitchPresentable> class SwitchWithTextTableViewCell: UITableViewCell { func configure(withPresenter presenter: T) // configure views here } }
  38. 38. extension MinionModeViewModel: TextPresentable { var text: String { return "Minion Mode" } var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17.0) } }
  39. 39. 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!") } } }
  40. 40. let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell let viewModel = MinionModeViewModel() cell.configure(withPresenter: viewModel) return cell
  41. 41. !"#
  42. 42. "Change is the only constant." — Unknown
  43. 43. typealias SwitchWithTextViewPresentable = protocol<TextPresentable, SwitchPresentable, ImagePresentable> class SwitchWithTextTableViewCell: UITableViewCell { func configure(withPresenter presenter: T) // configure views here } }
  44. 44. extension MinionModeViewModel: ImagePresentable { var imageName: String { return "minionParty.png" } }
  45. 45. !"
  46. 46. > Use Protocols to Configure Your Views > Use Protocol Extensions for Defaults > Use ViewModels to Provide Data for the Protocols
  47. 47. HOW CAN WE MAKE THIS BETTER?

×