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

4,634 views

Published on

Originally presented at Swift Summit: https://www.swiftsummit.com/

Published in: Software, Mobile, Technology
  • DOWNLOAD FULL eBOOK INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookeBOOK Crime, eeBOOK Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • The Scrambler Unlock Her Legs | 95% Off by Bobby Rio-Rob Judge?  https://tinyurl.com/unlockherlegss
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice !! Download 100 % Free Ebooks, PPts, Study Notes, Novels, etc @ https://www.ThesisScientist.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hey guys! Who wants to chat with me? More photos with me here 👉 http://www.bit.ly/katekoxx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice !! Download 100 % Free Ebooks, PPts, Study Notes, Novels, etc @ https://www.ThesisScientist.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Protocol-Oriented MVVM

  1. 1. POMVVM@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. THE PROBLEM class SwitchWithTextTableViewCell: UITableViewCell { func configure( title: String, titleFont: UIFont, titleColor: UIColor, switchOn: Bool, switchColor: UIColor = .purpleColor(), onSwitchToggleHandler: onSwitchToggleHandlerType? = nil) { // configure views here } }
  10. 10. PROTOCOLS TO THE RESCUE !
  11. 11. 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) }
  12. 12. extension SwitchWithTextCellProtocol { var switchColor: UIColor { return .purpleColor() } }
  13. 13. class SwitchWithTextTableViewCell: UITableViewCell { func configure(withDelegate delegate: SwitchWithTextCellProtocol) { // configure views here } }
  14. 14. 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!") } } }
  15. 15. CELLFORROWATINDEXPATH // YourViewController.swift let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell // this is where the magic happens! cell.configure(withDelegate: MinionModeViewModel()) return cell
  16. 16. !
  17. 17. 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 } }
  18. 18. // SwitchWithTextTableViewCell func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) { // configure views here }
  19. 19. struct MinionModeViewModel: SwitchWithTextCellDataSource { var title = "Minion Mode!!!" var switchOn = true }
  20. 20. 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!") } } }
  21. 21. // SettingsViewController let viewModel = MinionModeViewModel() cell.configure(withDataSource: viewModel, delegate: viewModel) return cell
  22. 22. !
  23. 23. @MHOLLEMANS: MIXINS AND TRAITS IN SWIFT 2.0
  24. 24. class AIPlayer: GameObject, AITrait, GunTrait, RenderTrait, HealthTrait { ... } class ZapMonster: GameObject, GunTrait, RenderTrait, HealthTrait, MovementTrait { ... }
  25. 25. ! "
  26. 26. 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) }
  27. 27. protocol ImagePresentable { var imageName: String { get } } protocol TextFieldPresentable { var placeholder: String { get } var text: String { get } func onTextFieldDidEndEditing(textField: UITextField) }
  28. 28. extension TextPresentable { var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17) } }
  29. 29. !!! class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell { private var delegate: T? func configure(withDelegate delegate: T) { // configure views here } }
  30. 30. extension MinionModeViewModel: TextPresentable { var text: String { return "Minion Mode" } var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17.0) } }
  31. 31. 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!") } } }
  32. 32. let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell<MinionModeViewModel> let viewModel = MinionModeViewModel() cell.configure(withDelegate: viewModel) return cell
  33. 33. !"#
  34. 34. "Change is the only constant." — Unknown
  35. 35. !"
  36. 36. > Use Protocols to Configure Your Views > Use Protocol Extensions for Defaults > Use ViewModels to Provide Data for the Protocols
  37. 37. HOW CAN WE MAKE THIS BETTER?

×