Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Experimenting Architecture for Android

Experimenting Architecture for Android

Recently I've been learning about architecture for Android, I find the journey of learning this topic pretty tough, so I hope to share my finding in this talk!

JunRong Tan

April 27, 2017
Tweet

Other Decks in Technology

Transcript

  1. EXPERIMENTING WITH ARCHITECTURE FOR ANDROID 1

  2. Tan JunRong Android Engineer 

  3. 

  4. Let’s Talk About Architecture ” “ 4

  5. Why? Do We Really Need It? 5

  6. Let’s Consider… Login Activity 6

  7. 7

  8. 8

  9. 9

  10. 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
  11. NO ARCH = MVC 11

  12. CHOOSING ARCHITECTURE 12

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

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

  15. 15

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

  17. Need an example to compare… 17

  18. 18

  19. No Architecture 19

  20. 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
  21. Testing Strategy: No Arch 21

  22. MVP 22

  23. MVP: Activity nameET.addTextChangedListener({s ->
 mvpPresenter.onNameChange(s)
 })
 
 favoriteAnimalET.addTextChangedListener({ s ->


    mvpPresenter.onAnimalNameChange(s)
 }) override fun onDisplayStringUpdate(displayString: String) {
 displayTextView.text = displayString
 } Activity { } 23
  24. 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
  25. Testing Strategy: MVP 1 2 X 25

  26. Testing Strategy: MVP practical 26

  27. TEST - Test: states of the screen - Junit test:

    lightweight Code Wise - Decoupled - Readability / Consistency - Swap module (eg. view) easily MVP: advantage 27
  28. MVVM 28

  29. MVP MVVM 29 ObservableField

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

  31. MVP MVVM ViewModel has the same responsibility as Presenter 31

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

  33. 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
  34. ViewModel => displayString.set(“JR likes dog”) Presenter => view.updateDisplayString(“JR likes dog”)

    Key Difference MVP MVVM BOUND! 34
  35. OPERATIONS: same responsibility as Presenter in MVP View Model STATES:

    holding the state of views, bound with view + 35
  36. Testing Strategy: MVVM Not too different from MVP, instead of

    testing View.methods(), we test on ViewModel’s state 36
  37. RxJava 2 ways of doing MVVM Data Binding library from

    Google 37
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. MVVM DATABINDING MVP MVVM RXJAVA Today's Discussion MVP-VM 43

  44. state + operations 44

  45. state + operations what if we place operations back to

    Presenter? ViewModel only hold states now 45
  46. MVVM MVP-VM 46

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

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

  49. what does it mean? persist MVP-VM 49

  50. 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
  51. Save state of screen to DB Example: Cookpad recipe editor

    page 51
  52. Beyond that? 52

  53. Since we have all the states of view… if we

    store them can we replay it? 
 yes, but not quite yet… Hmmm…. 53
  54. REDUX Contract: Action No side effect: Pure reducer https://egghead.io/courses/getting-started-with-redux 54

  55. Android Studio Plugin 55

  56. 56

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

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

  59. 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
  60. tanjunrong@cookpad.com @worker8 Survey
 https://goo.gl/GU8vK2 60