Skip to content
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

Merged
merged 6 commits into from
Mar 1, 2018

Conversation

amaksoft
Copy link
Contributor

Sometimes it is necessary to store multiple instances of same ViewModel type in the one ViewModelStore. The only way to resolve this case is to call ViewModelProvider.get(String key, Class<T> modelClass) with a String key parameter. I think we need to add a key parameter to viewmodel extension if possible.

@arnaudgiuliani
Copy link
Member

Hello,

Ok I see. Do you have a concrete example on using different instances of the same ViewModel?

@amaksoft
Copy link
Contributor Author

amaksoft commented Feb 21, 2018

Sometimes I use generic ViewModels in my projects. Here is an example.
It's a generalized ViewModel for making an HTTP request without creating similar classes for different screens/requests.

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.
It's merely an example. I'm sure people can think of more ways to use multiple instances of ViewModel of the same type.

@arnaudgiuliani
Copy link
Member

Yes I understand. I will make the patch on a next rc version. Can you check it? I will provide it in 0.9.0-rc-3

Can be cool if you have a very small project example to test this key feature.

@amaksoft
Copy link
Contributor Author

Sure! I can even add a robolectric based unit-test to my PR to test this

@arnaudgiuliani arnaudgiuliani changed the base branch from master to 0.9.0 February 28, 2018 14:26
@arnaudgiuliani
Copy link
Member

I've just redirect your PR on the right branch. Check the conflicting diffs.

@arnaudgiuliani arnaudgiuliani merged commit 5e62054 into InsertKoinIO:0.9.0 Mar 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants