bufferapp / android-clean-architecture-mvi-boilerplate

A fork of our clean architecture boilerplate using the Model-View-Intent pattern
MIT License
977 stars 128 forks source link

ViewModel from View instead of Activity or Fragment #4

Closed GSala closed 4 years ago

GSala commented 6 years ago

When trying to encapsulate some behavior in a View instead of an Activity or a Fragment, I do not have access to the ViewModelProvider. What would be the mot appropriate way of having the same architecture when using Views?

I can see at least 4 options.

  1. Pass the ViewModelProvider as an argument to the View constructor
  2. Pass the already obtained ViewModel as an argument to the View constructor
  3. Pass a LifecycleOwner as an argument to the View constructor
  4. Hold the ViewModel in the containing Fragment/Activity and expose events from the view and consume states.
piotrek1543 commented 5 years ago

Only third option. Check GithubSample in android-architecture-component repo for an example. Please also check: https://academy.realm.io/posts/mvc-vs-mvp-vs-mvvm-vs-mvi-mobilization-moskala/ to get to know better a role of ViewModel in Android app's architecture.