'

Protocol-Oriented MVVM

Понравилась презентация – покажи это...





Слайд 0

POMVVM @NATASHATHEROBOT


Слайд 1


Слайд 2


Слайд 3

"Swift Is a Protocol-Oriented Programming Language" — Dave Abrahams, Professor of Blowing-Your-Mind


Слайд 4

UITableViewDelegate UITableViewDataSource UITextFieldDelegate NSURLSessionDelegate CLLocationManagerDelegate MCSessionDelegate


Слайд 5

!


Слайд 6

!


Слайд 7

!


Слайд 8

!


Слайд 9

Artsy Engineering: MVVM in Swift


Слайд 10


Слайд 11

THE PROBLEM class SwitchWithTextTableViewCell: UITableViewCell { func configure( title: String, titleFont: UIFont, titleColor: UIColor, switchOn: Bool, switchColor: UIColor = .purpleColor(), onSwitchToggleHandler: onSwitchToggleHandlerType? = nil) { // configure views here } }


Слайд 12

PROTOCOLS TO THE RESCUE !


Слайд 13

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) }


Слайд 14

extension SwitchWithTextCellProtocol { var switchColor: UIColor { return .purpleColor() } }


Слайд 15

class SwitchWithTextTableViewCell: UITableViewCell { func configure(withDelegate delegate: SwitchWithTextCellProtocol) { // configure views here } }


Слайд 16

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!") } } }


Слайд 17

CELLFORROWATINDEXPATH // YourViewController.swift let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell // this is where the magic happens! cell.configure(withDelegate: MinionModeViewModel()) return cell


Слайд 18

!


Слайд 19

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 } }


Слайд 20

// SwitchWithTextTableViewCell func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) { // configure views here }


Слайд 21

struct MinionModeViewModel: SwitchWithTextCellDataSource { var title = "Minion Mode!!!" var switchOn = true }


Слайд 22

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!") } } }


Слайд 23

// SettingsViewController let viewModel = MinionModeViewModel() cell.configure(withDataSource: viewModel, delegate: viewModel) return cell


Слайд 24

!


Слайд 25

@MHOLLEMANS: MIXINS AND TRAITS IN SWIFT 2.0


Слайд 26


Слайд 27


Слайд 28

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


Слайд 29

!"


Слайд 30

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) }


Слайд 31

protocol ImagePresentable { var imageName: String { get } } protocol TextFieldPresentable { var placeholder: String { get } var text: String { get } func onTextFieldDidEndEditing(textField: UITextField) }


Слайд 32

extension TextPresentable { var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17) } }


Слайд 33

!!! class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell { private var delegate: T? func configure(withDelegate delegate: T) { // configure views here } }


Слайд 34

extension MinionModeViewModel: TextPresentable { var text: String { return "Minion Mode" } var textColor: UIColor { return .blackColor() } var font: UIFont { return .systemFontOfSize(17.0) } }


Слайд 35

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!") } } }


Слайд 36

let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell", forIndexPath: indexPath) as! SwitchWithTextTableViewCell<MinionModeViewModel> let viewModel = MinionModeViewModel() cell.configure(withDelegate: viewModel) return cell


Слайд 37

!"#


Слайд 38

"Change is the only constant." — Unknown


Слайд 39

!"


Слайд 40

> Use Protocols to Configure Your Views > Use Protocol Extensions for Defaults > Use ViewModels to Provide Data for the Protocols


Слайд 41

HOW CAN WE MAKE THIS BETTER?


Слайд 42


×

HTML:





Ссылка: