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

Natasha Murashev: Practical Protocol-Oriented Programming

Natasha Murashev: Practical Protocol-Oriented Programming

Realm

May 10, 2016
Tweet

More Decks by Realm

Other Decks in Programming

Transcript

  1. // FoodImageView.swift import UIKit class FoodImageView: UIImageView { func shake()

    { let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y)) layer.addAnimation(animation, forKey: "position") } }
  2. // ViewController.swift import UIKit class ViewController: UIViewController { @IBOutlet weak

    var foodImageView: FoodImageView! @IBAction func onShakeButtonTap(sender: AnyObject) { foodImageView.shake() } }
  3. // ShakeableButton.swift import UIKit class ActionButton: UIButton { func shake()

    { let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y)) layer.addAnimation(animation, forKey: "position") } }
  4. // ViewController.swift class ViewController: UIViewController { @IBOutlet weak var foodImageView:

    FoodImageView! @IBOutlet weak var actionButton: ActionButton! @IBAction func onShakeButtonTap(sender: AnyObject) { foodImageView.shake() actionButton.shake() } }
  5. // UIViewExtension.swift import UIKit extension UIView { func shake() {

    let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y)) layer.addAnimation(animation, forKey: "position") } }
  6. class FoodImageView: UIImageView { // other customization here } class

    ActionButton: UIButton { // other customization here } class ViewController: UIViewController { @IBOutlet weak var foodImageView: FoodImageView! @IBOutlet weak var actionButton: ActionButton! @IBAction func onShakeButtonTap(sender: AnyObject) { foodImageView.shake() actionButton.shake() } }
  7. // Shakeable.swift import UIKit protocol Shakeable { } extension Shakeable

    where Self: UIView { func shake() { // implementation code } }
  8. // FoodLaLaViewController override func viewDidLoad() { super.viewDidLoad() let foodCellNib =

    UINib(nibName: "FoodTableViewCell", bundle: nil) tableView.registerNib(foodCellNib, forCellReuseIdentifier: "FoodTableViewCell") }
  9. protocol ReusableView: class {} extension ReusableView where Self: UIView {

    static var reuseIdentifier: String { return String(self) } }
  10. protocol NibLoadableView: class { } extension NibLoadableView where Self: UIView

    { static var nibName: String { return String(self) } }
  11. extension UITableView { func register<T: UITableViewCell where T: ReusableView, T:

    NibLoadableView>(_: T.Type) { let nib = UINib(nibName: T.nibName, bundle: nil) registerNib(nib, forCellReuseIdentifier: T.reuseIdentifier) } }
  12. extension UITableView { func dequeueReusableCell<T: UITableViewCell where T: ReusableView>(forIndexPath indexPath:

    NSIndexPath) -> T { guard let cell = dequeueReusableCellWithIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as? T else { fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)") } return cell } }
  13. if indexPath.row == 0 { return tableView.dequeueReusableCell(forIndexPath: indexPath) as DesertTableViewCell

    } return tableView.dequeueReusableCell(forIndexPath: indexPath) as FoodTableViewCell
  14. struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { //

    make asynchronous API call // and return appropriate result } }
  15. struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { //

    make asynchronous API call // and return appropriate result } }
  16. // FoodLaLaViewController var dataSource = [Food]() { didSet { tableView.reloadData()

    } } override func viewDidLoad() { super.viewDidLoad() getFood() } private func getFood() { FoodService().getFood() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }
  17. // FoodLaLaViewController var dataSource = [Food]() { didSet { tableView.reloadData()

    } } override func viewDidLoad() { super.viewDidLoad() getFood() } private func getFood() { FoodService().getFood() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }
  18. struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { //

    make asynchronous API call // and return appropriate result } }
  19. struct FoodService: Gettable { func get(completionHandler: Result<[Food]> -> Void) {

    // make asynchronous API call // and return appropriate result } }
  20. // FoodLaLaViewController override func viewDidLoad() { super.viewDidLoad() getFood(fromService: FoodService()) }

    func getFood<S: Gettable where S.T == [Food]>(fromService service: S) { service.get() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }
  21. // FoodLaLaViewControllerTests class Fake_FoodService: Gettable { var getWasCalled = false

    func get(completionHandler: Result<[Food]> -> Void) { getWasCalled = true completionHandler(Result.Success(food)) } }
  22. // FoodLaLaViewControllerTests func testFetchFood() { let fakeFoodService = Fake_FoodService() viewController.getFood(fromService:

    fakeFoodService) XCTAssertTrue(fakeFoodService.getWasCalled) XCTAssertEqual(viewController.dataSource.count, food.count) XCTAssertEqual(viewController.dataSource, food) }