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. 

  2. 7

  3. 8

  4. 9

  5. 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
  6. 15

  7. 18

  8. 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
  9. MVP: Activity nameET.addTextChangedListener({s ->
 mvpPresenter.onNameChange(s)
 })
 
 favoriteAnimalET.addTextChangedListener({ s ->


    mvpPresenter.onAnimalNameChange(s)
 }) override fun onDisplayStringUpdate(displayString: String) {
 displayTextView.text = displayString
 } Activity { } 23
  10. 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
  11. TEST - Test: states of the screen - Junit test:

    lightweight Code Wise - Decoupled - Readability / Consistency - Swap module (eg. view) easily MVP: advantage 27
  12. 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
  13. OPERATIONS: same responsibility as Presenter in MVP View Model STATES:

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

    testing View.methods(), we test on ViewModel’s state 36
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. state + operations what if we place operations back to

    Presenter? ViewModel only hold states now 45
  21. 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
  22. Since we have all the states of view… if we

    store them can we replay it? 
 yes, but not quite yet… Hmmm…. 53
  23. 56

  24. 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