Slide 1

Slide 1 text

Modern Ways to Architect your Android app: MVP and MVVM

Slide 2

Slide 2 text

Hello! I am Jorge Coca Mobile Engineer @ SPR Consulting. You can find me at: @jcocaramos

Slide 3

Slide 3 text

What does MVP stand for? Model View Presenter

Slide 4

Slide 4 text

What does MVVM stand for? Model View-View Model

Slide 5

Slide 5 text

Why do I need to learn both?

Slide 6

Slide 6 text

“ During the last years, the community seemed to be inclined to incorporate MVP patterns in their Android apps

Slide 7

Slide 7 text

“ … but then, during the last Google IO

Slide 8

Slide 8 text

DATA BINDING Connects data and UI elements -> Automates listener creation, message sending, setters...

Slide 9

Slide 9 text

What’s the goal of these two approaches?

Slide 10

Slide 10 text

We want to make our code... ■ Independent of frameworks ■ Testable ■ Independent of UI ■ Independent of storage mechanisms ■ Independent of external dependencies

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Model View Presenter

Slide 13

Slide 13 text

Model View Presenter (MVP) ■ Derivation of MVC pattern ■ Mostly used for UI interfaces ■ Main problem that solves is testing Model Presenter View

Slide 14

Slide 14 text

View Presenter Model 1 User does something 2 Gets data 3 Prepares and returns data 4 Updates view

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

M for Model ■ Domain object ■ Holds data, but not behaviors ■ It may contain validation, but never formatting ■ POJO objects

Slide 17

Slide 17 text

V for View ■ Displays the data ■ Routes commands to the presenter ■ It’s going to be abstracted using an interface implemented by Android components ■ It’s passive ○ Activities and Fragments

Slide 18

Slide 18 text

P for Presenter ■ Acts upon the model and the view ■ Receives commands from the view and responds accordingly ■ Responsible for formatting data ■ Responsible for presentation logic ■ Doesn’t have Android dependencies ○ GOOD: unit tests ○ BAD: attached to View lifecycle

Slide 19

Slide 19 text

GOOD Test all our UI code without framework code

Slide 20

Slide 20 text

BAD Attached to View lifecycle

Slide 21

Slide 21 text

Model View-View Model

Slide 22

Slide 22 text

Model View-View Model (MVVM) ■ Commonly used by Microsoft ■ Binding engine will avoid boilerplate code View Model State and operations View Model Data binding and commands Sends notification ViewModel updates the model Sends notification

Slide 23

Slide 23 text

VM for View-Model ■ ViewModel implementations will contain state logic of the view ■ Activities and Fragments lifecycle tied to VM (that’s why DataBinding is good!)

Slide 24

Slide 24 text

VM for View-Model ■ ViewModel won’t contain a view instance; will use events mechanisms ■ Decouple our Views from the Model by introducing a ViewModel layer ■ Don’t tie directly to Domain models; create a POJO for the binding

Slide 25

Slide 25 text

V for View ■ Views will keep an instance of the ViewModel and will register different listeners to know when the ViewModel has changed

Slide 26

Slide 26 text

… but what is really Data Binding?

Slide 27

Slide 27 text

“ Data Binding is the process that establishes a connection between the application UI and the Business logic. If the settings and notifications are correctly set, the data reflects changes when made. It can also mean then when the UI is changed, the underlying data will reflect that change.

Slide 28

Slide 28 text

“ Data Binding is the process that establishes a connection between the application UI and the Business logic. If the settings and notifications are correctly set, the data reflects changes when made. It can also mean then when the UI is changed, the underlying data will reflect that change.

Slide 29

Slide 29 text

MVVM with Data Binding - Layouts

Slide 30

Slide 30 text

MVVM with Data Binding - Layouts

Slide 31

Slide 31 text

MVVM with Data Binding - Layouts

Slide 32

Slide 32 text

MVVM with Data Binding - Layouts

Slide 33

Slide 33 text

MVVM with Data Binding - ViewModel public class ContactViewModel extends BaseObservable { private String mContactName; // Constructors and other things omitted for clarity. @Bindable public String getName() { return mContactName; } public void setName(String name) { mContactName = name; notifyPropertyChanged(BR.name); } }

Slide 34

Slide 34 text

BaseObservable ■ Provides the infrastructure for setting up the data binding process ■ BR class is generated: ○ Each POJO field adorned with @Bindable will have a constant declared in BR corresponding to the name

Slide 35

Slide 35 text

MVVMwith DataBiding - Establish the binding public class AddNewContactActivity extends Activity{ private ContactViewModel mViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mViewModel = createViewModel(); initializeViewModel(); } private void ContactViewModel createViewModel() { ... } private void initializeViewModel() { ActivityAddNewContactBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_add_new_contact); binding.setContact(mViewModel); } // Rest of the code omitted. }

Slide 36

Slide 36 text

MVVMwith DataBiding - Establish the binding public class AddNewContactActivity extends Activity{ private ContactViewModel mViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mViewModel = createViewModel(); initializeViewModel(); } private void ContactViewModel createViewModel() { ... } private void initializeViewModel() { ActivityAddNewContactBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_add_new_contact); binding.setContact(mViewModel); } // Rest of the code omitted. }

Slide 37

Slide 37 text

What’s the current status of Data Binding?

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Data Binding - Current Status https://developer.android.com/tools/data-binding/guide.html ■ Lots of bugs ■ Not even all the examples work ■ It can cause errors even in snippets where you don’t use data binding

Slide 40

Slide 40 text

Don’t use data binding in production!

Slide 41

Slide 41 text

Example

Slide 42

Slide 42 text

Rock Hall Of Fame MVP RecyclerView with a list of rock bands implemented using MVP

Slide 43

Slide 43 text

Band details Mix of MVP with data binding Example of how data binding works and its current status Place your screenshot here

Slide 44

Slide 44 text

Links ■ Github repo with the example ○ https://github.com/jorgecoca/RockHallOfFameMVP ■ Slides ○ https://speakerdeck.com/jorgecoca/modern-ways-to- architecture-your-android-app-mvp-and-mvvm

Slide 45

Slide 45 text

Thank you!