-
-
Notifications
You must be signed in to change notification settings - Fork 719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow retrieving ViewModels from stores by key #58
Conversation
Hello, Ok I see. Do you have a concrete example on using different instances of the same ViewModel? |
Sometimes I use generic ViewModels in my projects. Here is an example. Here is how to use it: val wiewModel: RequestViewModel<User> = ViewModelProviders.of(this).get("UpdateUser", RequestViewModel.class) as RequestViewModel<User>
viewModel.response.observe(this, Observer { resp: Response<User> ->
showProgress(resp?.status == Response.Status.LOADING)
when (resp?.status) {
Response.Status.SUCCESS -> {
updateUserFields(resp?.data)
}
Response.Status.ERROR -> {
showError(resp?.error)
}
Response.Status.LOADING -> {}
}
})
btSave.setOnCLickListener {
viewmodel.makeRequest(httpService.updateUser(makeUpdatedUser()))
}
This way you can have dozens of view models of the same type and even same type parameter, but making different requests. I'm not saying it's the best practices, just exploring this pattern. Feel free to use it if you like. |
Yes I understand. I will make the patch on a next Can be cool if you have a very small project example to test this |
Sure! I can even add a robolectric based unit-test to my PR to test this |
I've just redirect your PR on the right branch. Check the conflicting diffs. |
Sometimes it is necessary to store multiple instances of same
ViewModel
type in the oneViewModelStore
. The only way to resolve this case is to callViewModelProvider.get(String key, Class<T> modelClass)
with a String key parameter. I think we need to add a key parameter toviewmodel
extension if possible.