ViewModel: A deep dive (GDG Heraklion)

ViewModel: A deep dive (GDG Heraklion)

At Google I/O we got the Android architecture components.
One of its most interesting concepts is the new ViewModel.
How does it work? How do I plug it into my app? Can I use it without any of the other components?
Can I plug it into an existing MVVM or how would I build MVVM with Google's ViewModel?

A8b79d304b5184e5a5b0a109590f6683?s=128

Danny Preussler

July 27, 2017
Tweet

Transcript

  1. 2.
  2. 8.

    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>
  3. 11.

    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
  4. 12.

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

    In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  8. 16.

    In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  9. 17.

    In architecture components •A ViewModel provides the data for a

    specific UI •The ViewModel does not know about the View! •Survives configuration change
  10. 24.

    How to use class MyViewModel extends ObservableViewModel { Coming soon

    Jose Alcérreca, Google https://medium.com/@dpreussler/add-the-new-viewmodel-to-your-mvvm-36bfea86b159
  11. 25.

    How to use public void onStopped() { … } No

    more life cycle forwarding!
  12. 28.

    How to use class MyViewModelFactory implements ViewModelProvider.Factory { @Inject MyUseCase

    useCase; @Override public MyViewModel create(Class modelClass) { return new MyViewModel(useCase); } }
  13. 30.

    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
  14. 31.

    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
  15. 33.

    What if… class MyViewModel extends ViewModel { @Override protected void

    onCleared() { super.onCleared(); cleanupSubscriptions(); }
  16. 35.

    How does it actually work? class HolderFragment extends Fragment {

    public HolderFragment() { setRetainInstance(true); } …
  17. 38.

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

    super.onDestroy(); mViewModelStore.clear(); }
  18. 43.

    Could you write that? •What if asked for ViewModel but

    fragment transaction not done yet? •You might end up with duplicates
  19. 44.

    Could you write that? •What if asked for ViewModel but

    fragment transaction not done yet? •You might end up with duplicates
  20. 45.

    Could you write that? static class HolderFragmentManager { private Map<Activity,

    HolderFragment> mNotCommittedActivityHolders = new HashMap<>(); private Map<Fragment, HolderFragment> mNotCommittedFragmentHolders = new HashMap<>(); private ActivityLifecycleCallbacks mActivityCallbacks = new EmptyActivityLifecycleCallbacks() { @Override public void onActivityDestroyed(Activity activity) { HolderFragment fragment = mNotCommittedActivityHolders.remove(activity); …
  21. 46.

    Could you write that? •What if activity dies before fragment

    transaction? •You might end up with memory leaks
  22. 47.

    Could you write that? private ActivityLifecycleCallbacks mActivityCallbacks = new EmptyActivityLifecycleCallbacks()

    { @Override public void onActivityDestroyed(Activity activity) { HolderFragment fragment = mNotCommittedActivityHolders.remove(activity); if (fragment != null) { Log.e(LOG_TAG, "Failed to save a ViewModel for " + activity); } } }; …
  23. 48.

    What if Two Fragments of same Activity ask for same

    ViewModel.class via ViewModelProviders .of(this) .get(MyViewModel.class);
  24. 50.

    What if Two Fragments of same Activity ask for same

    ViewModel.class via ViewModelProviders .of(this) .get(MyViewModel.class);
  25. 51.

    result •Fragment and Activity share the same FragmentManager •But implementation

    uses Activity’s FragmentManager but ChildFragmentManager for Fragments
  26. 52.

    result •Fragment and Activity share the same FragmentManager •But implementation

    uses Activity’s FragmentManager but ChildFragmentManager for Fragments
  27. 53.

    What if Two Fragments of same Activity ask for same

    ViewModel.class via ViewModelProviders .of(getActivity()) .get(MyViewModel.class);
  28. 57.

    all problems solved? ViewModels provide a convenient way to retain

    data across configuration changes but they are not persisted if the application is killed by the operating system https://developer.android.com/topic/libraries/architecture/viewmodel.html#viewm odel_vs_savedinstancestate
  29. 59.

    Why? The data saved via onSaveInstanceState is kept in the

    system process memory and the Android OS allows you to keep only a very small amount of data so it is not a good place to keep actual data for your app. TransactionTooLargeException anyone?
  30. 61.

    after The truth •Keep non-UI states in non-UI layer Not

    in bundle! •Use real caching strategies •Allows updating cache in background
  31. 62.

    after The truth •Keep non-UI states in non-UI layer Not

    in bundle! •Use real caching strategies •Allows updating cache in background
  32. 63.

    after The truth •Keep non-UI states in non-UI layer Not

    in bundle! •Use real caching strategies •Allows updating cache in background
  33. 64.
  34. 70.

    lets tweak it class MyModelFactory implements ViewModelProvider.Factory { … public

    MyModelFactory(Bundle bundle) { this.bundle = bundle; } @Override public MyViewModel create(Class modelClass) { MyViewModel viewModel = new MyViewModel(); viewModel.readFrom(bundle); return viewModel; }
  35. 77.

    list item ViewModels In MVVM: •each item in RecyclerView should

    be backed by a ViewModel •ListViewModel and ItemViewModels •ItemViewModels normally don’t retrieve their own state so no need to extend Googles ViewModel
  36. 78.

    list item ViewModels In MVVM: •each item in RecyclerView should

    be backed by a ViewModel •ListViewModel and ItemViewModels •ItemViewModels normally don’t retrieve their own state so no need to extend Googles ViewModel
  37. 79.

    list item ViewModels In MVVM: •each item in RecyclerView should

    be backed by a ViewModel •ListViewModel and ItemViewModels •ItemViewModels normally don’t retrieve their own state so no need to extend Googles ViewModel
  38. 82.

    list item ViewModels •Key could be position? public <T extends

    ViewModel> T get( String key, Class<T> modelClass)
  39. 85.

    list item ViewModels “ViewModelProvider, which will create ViewModels via the

    given Factory and retain them in a ViewModelStore “
  40. 86.

    public class ViewModelStore { private final HashMap<String, ViewModel> mMap =

    new HashMap<>(); final void put(String key, ViewModel viewModel) {…} final ViewModel get(String key) { …} public final void clear() {…} }
  41. 87.

    public class ViewModelStore { private final HashMap<String, ViewModel> mMap =

    new HashMap<>(); final void put(String key, ViewModel viewModel) {…} final ViewModel get(String key) { …} public final void clear() {…} }
  42. 93.

    Can I do it differently? •Let’s assume we build transactional

    flow spreading over multiple Activities and wand to keep data in ViewModel •Can not use the retained fragment approach •Need to override how the ViewModel is stored •Need to override the ViewModelStore is stored
  43. 94.

    Viewmodelstoreowner “A responsibility of an implementation of this interface is

    to retain owned ViewModelStore during the configuration changes and call ViewModelStore#clear(), when this scope is going to be destroyed.“
  44. 97.

    ViewModelproviders public static ViewModelProvider of( Fragment fragment, Factory factory) {

    return new ViewModelProvider( ViewModelStores.of(fragment), factory); }
  45. 101.

    How to extend life? Toothpick: A scope tree based Dependency

    Injection (DI) library https://github.com/stephanenicolas/toothpick
  46. 102.

    lets do it class LongLivingViewModelStoreOwner implements ViewModelStoreOwner { @Override public

    ViewModelStore getViewModelStore() { return Toothpick.openScope(SCOPE_NAME) .getInstance(ViewModelStore.class); } public void discardViewModel() { Toothpick.closeScope(SCOPE_NAME); }
  47. 103.

    let`s sum up •Well designed API •Know the life time:

    Never have Activivity or View reference in ViewModel •Be aware of Recreation •Careful with RecyclerView •Still alpha