MVVM and
RxJava –
the perfect
mix
Droidcon Zagreb 2016
Florina Muntenescu
1
What and who upday is
2
When did we
decide to go
the RxJava &
MVVM way?
3
When did we decide to go the RxJava & MVVM way?
The old app
4
When did we decide to go the RxJava & MVVM way?
The old app
99 little bugs in the code.
99 little bugs in the code.
Take one down, patch it around.
127 little bugs in the code…
5
When did we decide to go the RxJava & MVVM way?
From the old to the new
6
Crash course
in RxJava
7
Crash course in RxJava
Sum it up!
A1 0 1
A2 0 2
combineLatest( (A1,A2) => A1+A2)
C1 0 1 3
8
What is MVVM
and how do
you apply it?
9
What is MVVM and how do you apply it?
Model-View-Presenter
IView IPresenter
1…1
View Presenter Model
10
What is MVVM and how do you apply it?
Model-View-ViewModel
1…*
View ViewModel DataModel
11
What is MVVM and how do you apply it?
12
What is MVVM and how do you apply it?
Model-View-ViewModel
1…*
View ViewModel DataModel
13
What is MVVM and how do you apply it?
Testing
14
What is MVVM and how do you apply it?
Model-View-ViewModel
1…*
View ViewModel
1…*
View
View ViewModel DataModel
1…*
View ViewModel
15
What is MVVM and how do you apply it?
Function Function
Spreadsheet 1…* Spreadsheet
View ViewModel
View ViewModel
Observable<String>
getFunctionText()
16
What is MVVM and how do you apply it?
implements
IFunctionView
Function Function
Spreadsheet
View 1…1 Spreadsheet
Presenter
View
implements Presenter
IFunctionView
void
textUpdated(String)
17
18
We made
mistakes but
we recovered!
19
We made mistakes but we recovered!
20
We made mistakes but we recovered!
View
21
We made mistakes but we recovered!
ViewModel
22
We made mistakes but we recovered!
View
23
We made mistakes but we recovered!
What about Views that don’t have have lifecycle
events?
24
We made mistakes but we recovered!
Views and subscriptions
25
We made mistakes but we recovered!
Views and subscriptions
26
27
RxJava – the
good and the
bad
28
RxJava – the good and the bad
RxJava vs Threads/AsyncTask/Event bus frameworks
29
RxJava – the good and the bad
Stream
composability FTW!
30
RxJava – the good and the bad
Split a string using a regular expression and append a
new line to each item in the resulting list
“RxJava – the good and the bad”
[“RxJava\n”, “the\n”, “good\n”, ”and\n”, “the\n”, ”bad\n”]
31
RxJava – the good and the bad
“RxJava“ “the“ “good“
…
map(line -> line.trim() + NEW_LINE)
“RxJava\n “ “the\n“ “good\n“
…
toList()
["RxJava\n“, “the\n“, “good\n“
...]
32
RxJava – the good and the bad
Split a string using a regular expression and append a
new line to each item in the resulting list
33
34
Exclusive to Samsung
Simple MVVM example: https://github.com/florina-muntenescu/DroidconMVVM
MVP vs MVVM example: https://github.com/florina-muntenescu/MVPvsMVVM
35