Slide 1

Slide 1 text

HOW TO DESIGN YOUR VIEW CONTROLLERS

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Server API integration Gorgeous animations Gestures Responsive interfaces Bussiness logic Local persistence Social networks integration Interface flow Localization App analysis

Slide 4

Slide 4 text

–Robert Martin “A class or module should have one, and only one, reason to change” SINGLE RESPONSIBILITY PRINCIPLE

Slide 5

Slide 5 text

Conflicts

Slide 6

Slide 6 text

CONSEQUENCES • Hard to read/find Code • Hard to test Code • Hard to change code • Conflicts with team mates

Slide 7

Slide 7 text

A BETTER WORLD IS POSSIBLE

Slide 8

Slide 8 text

THE VC RESPONSIBILITIES • Acts as an use case conductor • Is agnostic on how other components implement functionalities • Knows on how to react to events

Slide 9

Slide 9 text

THE VC RESPONSIBILITIES View Controller UIKit events User interaction Model changes Server side events Change model Send URL requests Show user feedback Change system state The VC needs to be aware of what is happening The VC needs to know what to do

Slide 10

Slide 10 text

COMPONENTS View Controller Model objects Data sources Handlers View Framework events UIKit User interaction User feedback Read state Interact with business logic Obtain objects from model State changes

Slide 11

Slide 11 text

SEPARATE VIEW AND VC • The VC only controls one View • The VC should know what to show but no how • The View implements all the details of the interface (hierarchy, layout, animations, ….) View Controller View User interaction User feedback

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

IMPLEMENTATION BENEFITS • Smaller VC • Simpler VC • The Interface can be refactored without changing the VC • Be able to work with team (S.R.P)

Slide 15

Slide 15 text

TESTING BENEFITS • When testing the VC change the View for a Fake View • The Fake View is synchronous • Use the delegate of the view to simulate events from user • Use the Fake View to spy the VC orders • Analyse View Models VC creates View Controller View

Slide 16

Slide 16 text

TESTING THE VIEW • The View is a boundary • The View is hard to test • You can use snapshot from Facebook to test layout • You can test your datasources • You can use KIF to do acceptance tests

Slide 17

Slide 17 text

EXAMPLE • Building Paper for iOS Facebook Talk to get more information • Find an extended article on how to separate View and VC at jpellat.com • Also find an article on how to test VC • Find an implemented example on my github @jpellat

Slide 18

Slide 18 text

DATA SOURCES • The VC has to be agnostic on how we obtain objects • The Datasource are used to obtain and save objects • DataSource interface should allow operations to be asynchronous • You should have different datasources for different kind of objects that can be stored/saved View Controller Data Source obtain objects Save objects

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

• The Storages implement how to store and retrieve data from local storages • The Services are interfaces that defines the API of our servers DATASOURCES IMPLEMENTATION • The Datasources components should not work on main thread • DS decides how to retrieve the data, the components implement it

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

• Inject the DataSources using the init • Fake your DataSources to return the objects you need for your test TESTING BENEFITS FOR VC View Controller DataSource

Slide 23

Slide 23 text

TESTING THE DATASOURCES • The DataSource is a good target for testing • Test that it is obtaining the data from the source you expect depending on the conditions • You can test your storages by saving and retrieving objects and compare they • You can do integration tests with your API for services but can be a blocker on continuous integration :(

Slide 24

Slide 24 text

MORE INFORMATION • See Martin Fowler’s article about DataSources • Coming soon on jpellat.com a complete implementation

Slide 25

Slide 25 text

HANDLERS • Implement complex operations in Business Logic • Coordinates elements of our system • Is not coupled to the interface View Controller Handler Interact with BL

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

• Inject the Handlers using the init • Fake your Handler to spy the interaction of the VC and the Handler TESTING BENEFITS FOR VC View Controller Handler

Slide 30

Slide 30 text

TEST HANDLERS • Handlers are a good target for testing • The handlers contain Business Logic we need to test • We can fake the Datasources and services for our tests

Slide 31

Slide 31 text

FINAL RESULT • Now we have small and readable classes • Decisions can be delayed • Most of our stack can be unit tested • Our system is easy to extend and change

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

@jordipellat jpellat.com github.com/jpellat Doubts?