I have DashBoardViewModel which will provide the data of message,news,events etc for my DashBoardActivity.So is it appropraite to use MessageViewModel,NewsViewModel etc as a member of DashBoardViewModel.
Is this approach bad or good?
public class DashBoardViewModel extends AndroidVIewModel{
private MessageViewModel mvm;
private NewsViewModel nvm;
public LiveData> getData(){
return nvm.getNewsList();
}
}
@prabinshrestha Don't place your ViewModel into another ViewModel. You should provide your ViewModel from ViewModelProviders. ViewModelProviders needs a context to create your ViewModel with given type.
There is nothing wrong with providing multiple ViewModel for a single activity.
Sometimes you need composition in the view model layer though. Some view models can be "derived" from others. You don't want to have this logic in the presentation layer.
I agree with i-schuetz.
Right now it is possible to compose multiple ViewModels inside the presentation layer (activity) like this:
ViewModelProviders.of(this, viewModelFactory).get(FirstViewModel::class.java)
ViewModelProviders.of(this, viewModelFactory).get(SecondViewModel::class.java)
The problem is that some logic might be required in the presentation layer (activity) to interact with both those ViewModels.
I would like to be able to compose multiple ViewModels inside a ViewModel. That way the presentation layer will be bound to a single ViewModel which internally will include multiple ViewModels.
Right now this is not possible, because ViewModelProviders.of() requires an activity as the first parameter and can not be used from another ViewModel.
Hi people,
I totally agree with @ggeorgip: as long as we can have access to several ViewModels in the view, I don't understand how we would be able to combine their data (maybe we shouldn't need to and that's the error) without giving a VM to another (except from handling the combination in the view, which I very much dislike)...
Any advice about this?
Thanks
Total agree with @ggeorgip
@iammert Composing ViewModels will help to remove communication code(to pass data from one ViewModel to another) from presentation layer (Fragment, View etc.) On the other hand we will still have the possibility to have that view models separated and tested properly.
Most helpful comment
I agree with i-schuetz.
Right now it is possible to compose multiple ViewModels inside the presentation layer (activity) like this:
ViewModelProviders.of(this, viewModelFactory).get(FirstViewModel::class.java)
ViewModelProviders.of(this, viewModelFactory).get(SecondViewModel::class.java)
The problem is that some logic might be required in the presentation layer (activity) to interact with both those ViewModels.
I would like to be able to compose multiple ViewModels inside a ViewModel. That way the presentation layer will be bound to a single ViewModel which internally will include multiple ViewModels.
Right now this is not possible, because ViewModelProviders.of() requires an activity as the first parameter and can not be used from another ViewModel.