Slide 1

Slide 1 text

EXPERIMENTING WITH ARCHITECTURE FOR ANDROID 1

Slide 2

Slide 2 text

Tan JunRong Android Engineer

Slide 3

Slide 3 text

Slide 4

Slide 4 text

Let’s Talk About Architecture ” “ 4

Slide 5

Slide 5 text

Why? Do We Really Need It? 5

Slide 6

Slide 6 text

Let’s Consider… Login Activity 6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9

Slide 10

Slide 10 text

NO ARCHITECTURE - Too coupled, hard to swap view/data source/ main logic - Activity becomes really long, hard to read - Hard to test, only with Espresso - etc 10

Slide 11

Slide 11 text

NO ARCH = MVC 11

Slide 12

Slide 12 text

CHOOSING ARCHITECTURE 12

Slide 13

Slide 13 text

WHICH!? MVVM DATABINDING MVP UNCLE BOB’S CLEAN ARCH MVVM RXJAVA 13

Slide 14

Slide 14 text

https://github.com/googlesamples/ android-architecture Official Google example on Github 14

Slide 15

Slide 15 text

15

Slide 16

Slide 16 text

MVVM DATABINDING MVP MVVM RXJAVA Today's Discussion MVP-VM 16

Slide 17

Slide 17 text

Need an example to compare… 17

Slide 18

Slide 18 text

18

Slide 19

Slide 19 text

No Architecture 19

Slide 20

Slide 20 text

No Arch nameEditText.addTextChangedListener(TextWatcherImp { s, _, _, _ ->
 name = s.toString()
 updateDisplayString()
 })
 
 favoriteAnimalEditText.addTextChangedListener(TextWatcherImp { s, _, _, _ ->
 animalName = s.toString()
 updateDisplayString()
 }) fun updateDisplayString() {
 displayTextView.text = name + " likes " + animalName
 } 20

Slide 21

Slide 21 text

Testing Strategy: No Arch 21

Slide 22

Slide 22 text

MVP 22

Slide 23

Slide 23 text

MVP: Activity nameET.addTextChangedListener({s ->
 mvpPresenter.onNameChange(s)
 })
 
 favoriteAnimalET.addTextChangedListener({ s ->
 mvpPresenter.onAnimalNameChange(s)
 }) override fun onDisplayStringUpdate(displayString: String) {
 displayTextView.text = displayString
 } Activity { } 23

Slide 24

Slide 24 text

MVP: Presenter override fun onNameChange(s: String) {
 name = s displayStringUpdate()
 }
 
 override fun onAnimalNameChange(s: String) {
 animalName = s displayStringUpdate()
 } fun displayStringUpdate() {
 view.onDisplayStringUpdate(name + " likes " + animalName)
 } Presenter { } 24

Slide 25

Slide 25 text

Testing Strategy: MVP 1 2 X 25

Slide 26

Slide 26 text

Testing Strategy: MVP practical 26

Slide 27

Slide 27 text

TEST - Test: states of the screen - Junit test: lightweight Code Wise - Decoupled - Readability / Consistency - Swap module (eg. view) easily MVP: advantage 27

Slide 28

Slide 28 text

MVVM 28

Slide 29

Slide 29 text

MVP MVVM 29 ObservableField

Slide 30

Slide 30 text

https://news.realm.io/news/eric-maxwell-mvc-mvp-and-mvvm-on-android/ This MVVM Example 30

Slide 31

Slide 31 text

MVP MVVM ViewModel has the same responsibility as Presenter 31

Slide 32

Slide 32 text

https://msdn.microsoft.com/en-us/library/hh848246.aspx Microsoft MVVM Diagram 32

Slide 33

Slide 33 text

The ViewModel and Presenter looks the same!? but wait, ViewModel has something different consider this… setting displayString ViewModel => displayString.set(“JR likes dog”) Presenter => view.updateDisplayString(“JR likes dog”) MVP vs MVVM 33

Slide 34

Slide 34 text

ViewModel => displayString.set(“JR likes dog”) Presenter => view.updateDisplayString(“JR likes dog”) Key Difference MVP MVVM BOUND! 34

Slide 35

Slide 35 text

OPERATIONS: same responsibility as Presenter in MVP View Model STATES: holding the state of views, bound with view + 35

Slide 36

Slide 36 text

Testing Strategy: MVVM Not too different from MVP, instead of testing View.methods(), we test on ViewModel’s state 36

Slide 37

Slide 37 text

RxJava 2 ways of doing MVVM Data Binding library from Google 37

Slide 38

Slide 38 text

Data Binding library from Google I use RxJava before trying this lib I find it hard to use logic inside XML, cannot be tested android:visibility = "@{viewModel.diplayString != null ? View.VISIBLE : View.GONE}" also, RxJava provides more, manipulating streams https://medium.com/@Miqubel/4-reasons-im-not-using-android-data-binding-e62127c2650c google this term: android data binding library bad idea ” “ Observable is not compatible with RxJava 38

Slide 39

Slide 39 text

RxJava Easier to use (if you’re already using RxJava) no spaghetti code inside xml you can do more, manipulating streams viewModel.displayStringObservable.subscribe { 
 displayTextView.text = it
 } https://github.com/worker8/learning-architecture-for-android 39

Slide 40

Slide 40 text

MVVM: advantage (SAME AS MVP) Key Difference - Deal with ViewModel, not View - (VM is bound to View automatically) TEST - Test: states of the screen - Junit test: lightweight Code Wise - Decoupled - Readability - Swap module (eg. view) easily 40

Slide 41

Slide 41 text

instead of checking view.action() My Opinion I like MVVM more I can test on view state directly from ViewModel disadvantage of mvvm: data binding library, remedied by RxJava 41

Slide 42

Slide 42 text

Test out yourself before choosing How to Choose? Remember the problem you’re trying to solve with arch Don’t focus on the solution! For trivial app, use no-architecture If project uses RxJava, use MVVM otherwise: use MVP personal advice… 42

Slide 43

Slide 43 text

MVVM DATABINDING MVP MVVM RXJAVA Today's Discussion MVP-VM 43

Slide 44

Slide 44 text

state + operations 44

Slide 45

Slide 45 text

state + operations what if we place operations back to Presenter? ViewModel only hold states now 45

Slide 46

Slide 46 text

MVVM MVP-VM 46

Slide 47

Slide 47 text

Since VM is separated, I’ll call it MVP-VM MVP-VM? 47

Slide 48

Slide 48 text

What difference does it make? persist MVP-VM VM 48

Slide 49

Slide 49 text

what does it mean? persist MVP-VM 49

Slide 50

Slide 50 text

Observable.combineLatest(input.name, input.favoriteAnimal, { name, favoriteAnimal -> MvpVmViewModel(name, favoriteAnimal, name + " likes " + favoriteAnimal)}) .subscribe(outputViewModel) I’m making new MvpVmViewModel in every mutation MvpVmViewModel MvpVmViewModel MvpVmViewModel MvpVmViewModel MVP-VM 50

Slide 51

Slide 51 text

Save state of screen to DB Example: Cookpad recipe editor page 51

Slide 52

Slide 52 text

Beyond that? 52

Slide 53

Slide 53 text

Since we have all the states of view… if we store them can we replay it? 
 yes, but not quite yet… Hmmm…. 53

Slide 54

Slide 54 text

REDUX Contract: Action No side effect: Pure reducer https://egghead.io/courses/getting-started-with-redux 54

Slide 55

Slide 55 text

Android Studio Plugin 55

Slide 56

Slide 56 text

56

Slide 57

Slide 57 text

https://github.com/worker8/learning-architecture-for-android/pull/1 Example done using Redux style 57

Slide 58

Slide 58 text

https://github.com/worker8/learning-architecture-for-android Github example https://github.com/kittinunf/remote-redux-devtools-android 58

Slide 59

Slide 59 text

Uncle Bob’s clean architecture What else is Out there? Redux : Front End Development VIPER: iOS https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52 59

Slide 60

Slide 60 text

[email protected] @worker8 Survey
 https://goo.gl/GU8vK2 60