Slide 1

Slide 1 text

Android MVP (Model View Presenter) pattern and multi-Presenter Activities. Chryssa Aliferi Android Software Engineer ChryssaAliferi

Slide 2

Slide 2 text

Our beloved Android

Slide 3

Slide 3 text

Our beloved Android: The business logic and UI logic coexist in the Activity classes:

Slide 4

Slide 4 text

Our beloved Android: Everything is connected with everything. UI + Business Logic + Data + Multiple Threads = Android GOD Activity

Slide 5

Slide 5 text

What is MVP?

Slide 6

Slide 6 text

MVP stands for? Most Valuable Player

Slide 7

Slide 7 text

MVP stands for? Minimum Viable Product

Slide 8

Slide 8 text

What is MVP? Model View Presenter: Separates the presentation layer from the logic. MVP stands for:

Slide 9

Slide 9 text

What is MVP? Model View Presenter: MVP stands for:

Slide 10

Slide 10 text

What is MVP? Model: is a data layer such as database or a set of classes that provide the data we want to display in the view. View: represents the UI components and transforms the information contained by the model into the UI. Presenter: is a layer that provides View with data from Model, according to some rules.

Slide 11

Slide 11 text

MVP Model Model: • Set of classes that define the data. • Business rules for data and data transformations • Data Retrieval

Slide 12

Slide 12 text

View: • Activity, Fragment, View, Dialog • Reference to the Presenter • Exposes methods and Events to the presenter • Transforms the information contained by the model into the UI. • Animations • Navigating to other screens MVP View

Slide 13

Slide 13 text

Presenter: • Middle man between View and Model • Controls and updates UI • Handles all UI events on behalf of the view • Retrieves data from the Model layer and displays them in the View MVP Presenter

Slide 14

Slide 14 text

How to implement MVP?

Slide 15

Slide 15 text

• View interface - “Screen” • all “Screen” methods refer to view behaviour • Android Activity • “screen” methods implemented • Presenter class & Presenter “children” flows • accesses view through screen MVP Implementation

Slide 16

Slide 16 text

MVP Example A new screen concerning the Payment feature, with two modes:
 • Select Payment • Edit Payment

Slide 17

Slide 17 text

PaymentActivity Without MVP:

Slide 18

Slide 18 text

PaymentScreen.java interface:

Slide 19

Slide 19 text

PaymentActivity With MVP:

Slide 20

Slide 20 text

PaymentPresenter.java:

Slide 21

Slide 21 text

MVP Example What about multi- Presenter Activities? Let’s try this again…

Slide 22

Slide 22 text

PaymentScreen.java interface:

Slide 23

Slide 23 text

PaymentActivity with multiple presenters:

Slide 24

Slide 24 text

PaymentPresenter with multiple presenters:

Slide 25

Slide 25 text

EditPaymentFlow.java :

Slide 26

Slide 26 text

SelectPaymentFlow.java :

Slide 27

Slide 27 text

Taxibeat MVP Example The Taxibeat PaymentMeans Activity Case.

Slide 28

Slide 28 text

Taxibeat PaymentActivity.java :

Slide 29

Slide 29 text

Taxibeat MVP Example Another Taxibeat case: Multi-Presenter Activities

Slide 30

Slide 30 text

Taxibeat LocateActivity.java: • LocateActivity.java is the Main Taxibeat Activity • LocateActivity.java contains 2 basic Presenters, the LocatePresenter.java and the MenuPresenter.java • LocatePresenter.java has 3 presenter flow descendants, the PickupFlow.java, the DropOffFlow.java and the CallingFlow.java

Slide 31

Slide 31 text

PickupFlow.java • LocatePresenter.java descendant • PickupFlow.java presenter contains all the logic behind the process of setting the correct pickup location. • onAddressAnimationEnd(), PickupFlow calls “destroys” itself and gives control to DropOffFlow. • onBackButton() press we terminate the app.

Slide 32

Slide 32 text

DropOffFlow.java • LocatePresenter.java descendant • DropOffFlow.java presenter contains all the logic behind the process of setting the dropoff location and all the hailing options. • onClick() of the green button, DropOffFlow “destroys” itself and gives control to CallingFlow. • onBackButton() press we handle back control to the PickupFlow.

Slide 33

Slide 33 text

CallingFlow.java • LocatePresenter.java descendant • CallingFlow presenter contains all the logic behind the process of calling a taxi. • onClick() of the red button or on onBackButton() press, CallingFlow “destroys” itself • Then gives control back to DropOffFlow.

Slide 34

Slide 34 text

MenuPresenter.java • MenuPresenter presenter contains all the logic behind the menu initialisation and menu onClick actions. • MenuPresenter is an autonomous presenter that handles only menu actions.

Slide 35

Slide 35 text

LocateActivity contains: LocatePresenter MenuPresenter PickupFlow CallingFlow DropOffFlow MenuScreen LocateScreen

Slide 36

Slide 36 text

MVP Recap

Slide 37

Slide 37 text

MVP Pros vs Cons Pros: • Separation of concerns • Maintainability • Re-usability of views, presenters and screens • Less time spending on view control • Information hiding • Testable! Cons: • Redundancy • Learning curve • Takes time, not suitable for small projects.

Slide 38

Slide 38 text

MVP Do not forget: • Use your logic, to distinguish the main business flows • Create your “Screen”, your View interface • Activities, Fragments or custom views implement the View interface • Separate your app business logic by moving it to a plain java Presenter object • Make Presenter “children” if needed to make your code even cleaner • Spend time writing a clean interface between your Presenter and your View • Test your Presenter. • Retain the state of the Presenters, “initialise” and “destroy” them.

Slide 39

Slide 39 text

§ GitHub Project https://github.com/ChryssaAliferi/MultiPresenterMVP

Slide 40

Slide 40 text

The End! Any Questions?

Slide 41

Slide 41 text

www.taxibeat.com/careers We are hiring!

Slide 42

Slide 42 text

Resources: • Is Activity GOD? by Kenju Wagatsuma on SlideShare • MVP design pattern for Android Apps by Infinum on SlideShare • Android Code That Scales, With MVP by Nathan Barraille • MVP for Android: how to organize the presentation layer by Antonio Leiva • Introduction to Model View Presenter on Android by Konstantin Mikheev

Slide 43

Slide 43 text

§ Thank you! Chryssa Aliferi Android Software Engineer ChryssaAliferi