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

HiltViewModel with @AssistedInject and key parameter on creation #4481

Open
peterMonteer opened this issue Oct 16, 2024 · 3 comments
Open

HiltViewModel with @AssistedInject and key parameter on creation #4481

peterMonteer opened this issue Oct 16, 2024 · 3 comments

Comments

@peterMonteer
Copy link

I have two different ViewModels being initialized in two different composables that are in the same route but on different hierarchy levels, using a custom factory, that passes a key parameter which is a user id.

It seems that Hilt replaces the first of those ViewModels based on the id passed via key, even if they are different ViewModels.

Expected behaviour:

Before checking the key parameter, Hilt looks at the ViewModel type to decide to replace.

Versions:

Hilt Version 2.49
Hilt Navigation Compose 1.2.0

@danysantiago
Copy link
Member

Can you share some code on how you are retrieving the ViewModels and the custom factory?

Is the key being passed to the hiltViewModel() API such that is can be piped to the ViewModelProvider? If you don't then the ViewModels will conflict because they are stored in the same route and their key is only the class.

@peterMonteer
Copy link
Author

Sure, thanks for the quick response.

ViewModel creation:

@Composable
fun CustomContactDialog(
    contact: Contact,
    viewModel: CustomContactDialogViewModel = customContactDialogViewModel(contact),
    onDismiss: () -> Unit
)
@Composable
private fun customContactDialogViewModel(contact: Contact) =
    hiltViewModel<CustomContactDialogViewModel, CustomContactDialogViewModel.CustomContactDialogViewModelFactory>(key = contact.id) {
        it.create(
            contact = contact
        )
    }

Assisted Factory:

@AssistedFactory
    interface CustomContactDialogViewModelFactory {
        fun create(contact: Contact): CustomContactDialogViewModel
    }

@danysantiago
Copy link
Member

If you are passing a key to hiltViewModel then even though these two ViewModels are in the same route they should be different and two different instances of these ViewModel should be created. There is really no 'replace' concept with ViewModels so I am not sure what you mean by that.

On the androidx side we have a test that validate that the key being used in hiltViewModel is piped down to the ViewModelStoreOwner and thus being used to differentiate between same-class ViewModels:

This same key is also piped in the version that uses assisted injection.

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

No branches or pull requests

2 participants