Viewmodel and friends: DevFest Budapest 2017

Viewmodel and friends: DevFest Budapest 2017

Talk about new Architecture components: Viewmodel, Lifecycle, LiveData. The role of MVVM, RxJava...

A8b79d304b5184e5a5b0a109590f6683?s=128

Danny Preussler

October 10, 2017
Tweet

Transcript

  1. DEVFEST BUDAPEST ViewMODEL AND FRIENDS the new architecture components @PreusslerBerlin

  2. None
  3. The ”architecture components”

  4. The ”architecture components” •Room •Lifecycle Observer •LiveData •ViewModel •New: Pagination

  5. The ”architecture components” •Room •Lifecycle Observer •LiveData •ViewModel •New: Pagination

  6. The ”architecture components” ViewModel like in Model-View-ViewModel ViewModel?

  7. ViewModel in MVVM world

  8. ViewModel in data binding Rx XML ViewModel

  9. ViewModel in data binding <TextView … android:id="@+id/all_shows_item_title" android:text="@{viewModel.title}" /> <android.support.v7.widget.CardView

    … android:onClick="@{() -> viewModel.onClicked()}"> <data> <variable name="viewModel" type="com.vmn.playplex.main.allshows.SeriesViewModel"/> </data>
  10. in new suggested architecture

  11. in new suggested architecture

  12. Is MVP dead?

  13. Is MVP dead? It’s like Java and Kotlin: •MVP will

    stay for quite some time •There is a new cooler kid in town that won’t leave Put the ViewModel behind Presenter
  14. Is MVP dead? It’s like Java and Kotlin: •MVP will

    stay for quite some time •There is a new cooler kid in town that won’t leave Put the ViewModel behind Presenter
  15. Is MVP dead? It’s like Java and Kotlin: •MVP will

    stay for quite some time •There is a new cooler kid in town that won’t leave Put the ViewModel behind Presenter
  16. Is MVP dead? It’s like Java and Kotlin: •MVP will

    stay for quite some time •There is a new cooler kid in town that won’t leave Put the ViewModel behind Presenter
  17. In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  18. In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  19. In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  20. In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  21. In architecture components •Remember configuration change can be: •Rotation •Any

    other resize i.e. split screen •Language change
  22. life cycle: rotation onCreate onStart onResume onPause onStop onDestroy onCreate

    onStart onResume ViewModel
  23. life cycle: finish onCreate onStart onResume onPause onStop onDestroy ViewModel

  24. How to use compile 'android.arch.lifecycle:runtime:1.0.0-beta1' compile 'android.arch.lifecycle:extensions:1.0.0-beta1’ kapt 'android.arch.lifecycle:compiler:1.0.0-beta1

  25. How to use class MyViewModel() : ViewModel() {

  26. How to use class MyViewModel(app: Application) : AndroidViewModel(app) {

  27. How to use class MyViewModel() :ViewModelObservable() { Coming soon Jose

    Alcérreca, Google https://medium.com/@dpreussler/add-the-new-viewmodel-to-your-mvvm-36bfea86b159
  28. How to use override fun onStopped() { … } No

    more life cycle forwarding!
  29. How to use override fun onCreate(...) { model = ViewModelProviders

    .of(this) .get(MyViewModel::class.java) }
  30. What if… constructor arguments needed?

  31. How to use class MyViewModelFactory :ViewModelProvider.Factory(useCase: MyUseCase) { fun <T:

    ViewModel> create(aClass: Class<T>): T { return MyViewModel(useCase) as T } }
  32. How to use ViewModelProviders .of(this, MyViewModelFactory(usecase)) .get(MyShowsViewModel::class.java)

  33. How to use •Always try to build your own Factory

    •Default factory uses newInstance() which is some hundred times slower than new calls (reflection) https://speakerdeck.com/dpreussler/comparing-dependency-injection- frameworks-and-internals-for-android
  34. How to use •Always try to build your own Factory

    •Default factory uses newInstance() which is some hundred times slower than new calls (reflection) https://speakerdeck.com/dpreussler/comparing-dependency-injection- frameworks-and-internals-for-android
  35. What if… I need to clean something when destroyed?

  36. What if… class MyViewModel() : ViewModel() { override fun onCleared()

    { super.onCleared() cleanupSubscriptions() }
  37. How does it survive orientation change?

  38. How does it actually work? class HolderFragment extends Fragment {

    public HolderFragment() { setRetainInstance(true); } …
  39. How does it actually work? String HOLDER_TAG = "android.arch.lifecycle.state.StateProviderH olderFragment";

  40. How does it know the activity is finishing?

  41. How does it actually work? @Override public void onDestroy() {

    super.onDestroy(); mViewModelStore.clear(); }
  42. Can I do it differently? ViewModel is not life cycle

    aware?
  43. It just refuses to die

  44. remember Never hold View or Activity references in the ViewModel!

  45. What if Two Fragments of same Activity ask for same

    ViewModel.class via ViewModelProviders .of(this) .get(MyViewModel::class.java)
  46. result Different ViewModels

  47. What if Two Fragments of same Activity ask for same

    ViewModel.class via ViewModelProviders .of(this) .get(MyViewModel::class.java)
  48. result •Fragment and Activity share the same FragmentManager •But implementation

    uses Activity’s FragmentManager but ChildFragmentManager for Fragments
  49. result •Fragment and Activity share the same FragmentManager •But implementation

    uses Activity’s FragmentManager but ChildFragmentManager for Fragments
  50. What if Two Fragments of same Activity ask for same

    ViewModel.class via ViewModelProviders .of(getActivity()) .get(MyViewMode::class).java
  51. result Same ViewModel

  52. Other uses cases communication layer between activities and fragments or

    fragments and fragments
  53. So what about this LIFE CYCLE THING?

  54. LIFECYCLE is a class that holds the information about the

    lifecycle state of a component
  55. LIFECYCLE OWNER is a single method interface that denotes that

    the class has a Lifecycle. Fragments and Activities in Support Library 26.1.0+ already implement
  56. LIFECYCLE OBSERVER class MyListener() : LifecycleObserver { @OnLifecycleEvent(Event.ON_START) fun onStart()

    { … }
  57. LIFECYCLE OBSERVER activity.lifecycle.addObserver(listener)

  58. LIFECYCLE OBSERVER activity.lifecycle.addObserver(listener) An observer added with a Lifecycle will

    be automatically removed if the corresponding Lifecycle moves to Lifecycle.State#DESTROYED state.
  59. So SHOULD VIEWMODEL IMPLEMENT LIFE CYCLE OBSERVER?

  60. NOOOOOOOOOOOO

  61. ViewMODEL lives LOngER! Activity Activity ViewModel

  62. IntroduciNG LIVE DATA Activity Activity LiveData LiveData ViewModel

  63. IntroduciNG LIVE DATA •Observable similar to RxJava •Life cycle aware

    •Doesn’t emit when not needed •Memory leaks save
  64. IntroduciNG LIVE DATA myModel.message.observe( this, Observer { display(it) })

  65. What if I USE Rx Java?

  66. Rx Java FULL PICTURE Activity ViewModel (un)subscribe

  67. Rx Java FULL PICTURE Activity ViewModel Life cycle aware class

    (un)subscribe (un)subscribe
  68. What if I USE DATA BINDING?

  69. Data binding full picture XML ViewModel bind

  70. Data binding full picture XML Activity ViewModel (un)bind bind Life

    cycle aware class (un)bind
  71. WAYS TO OBSERVE DATA from VM? •Data binding Observable from

    xml or code, might need unregister •RxJava Observable from code, needs unregister •LiveData Observable, from code, no unregister, life cycle aware
  72. OR DON’T USE ARCH COMP VIEWMODEL Activity Life cycle ware

    ViewModel Repository Activity Life cycle ware ViewModel
  73. let`s sum up •Well designed API •Goal: common architecture language

    •Use the parts you need •Know about life cycle •In beta
  74. Want to know more •https://medium.com/@dpreussler/add-the-new- viewmodel-to-your-mvvm-36bfea86b159 •https://proandroiddev.com/customizing-the-new- viewmodel-cf28b8a7c5fc •http://hannesdorfmann.com/android/arch- components-purist

    •https://blog.stylingandroid.com/architecture- components-viewmodel/ •https://www.youtube.com/watch?v=QrbhPcbZv0I
  75. Southpark copyright Disclaimer

  76. viacom.tech

  77. View Model and FRIENDS @PreusslerBerlin