fabioCollini / mv2m

Android MVVM lightweight library based on Android Data Binding
Apache License 2.0
183 stars 21 forks source link

Multiple Model or ViewModel #2

Closed punoii closed 8 years ago

punoii commented 8 years ago

Hello, Your m2vm project is awesome! Thank you very much.

I have a question. What if I have multiple model or viewmodel? How can I manage the view that has more than one model or viewmodel?

fabioCollini commented 8 years ago

Thanks, glad you like it! Multiple viewModels are not managed right now, however you can create a "facade" viewModel that delegates to other viewModels. Every fragment/activity uses a single viewModel and save/restore it in a retained fragment. Another easy solution is to create multiple fragments, every fragment has his own viewModel. Can you tell me a use case where you need multiple viewModels? Is it just for a better code organization or is there another reason? In next days I'll document a way to manage recycler view, maybe is this the reason?

punoii commented 8 years ago

Thank you very much for your very fast response. and yes. it is just for a better code organization right now. I will try follow your suggestion. Thanks!.

How about multiple model? (not viewmodel). Should I put into HashMap? (just for a better code organization as well).

Thank you again and hope you keep update this project. this is useful for everyone. I have seen your video presentation in youtube it will be great if there is english subtitle.

fabioCollini commented 8 years ago

The model is automatically saved in Activity/Fragment instance state, so the simplest solution is the same I said for ViewModels: create a facade model that contains all the "submodels". Usually you don't need an HashMap, you can just use some fields in the model.