Hello, I got one question, can I use assisted inje...
# compose
o
Hello, I got one question, can I use assisted injection in a composable ? meaning my view model has an assisted parameter, but I do not know how to get the factory in the composable function to pass it to the
viewModel
method
d
You can try creating a new ViewModelProvider with a custom factory
Copy code
ViewModelProvider(viewModelStoreOwner, object : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        @Suppress("UNCHECKED_CAST")
        return factory() as T
    }
})
viewModelStoreOwner can be from
LocalViewModelStoreOwner.current
, which is probably the navBackStackEntry if you are using navigation component
o
will try it, thanks 🙂
m
why not pass the vm as a parameter to the function?
o
because I would have to inject the vm in the main activity, meaning I have some composables that has it's own vm, they should get it from the navigation component, but if I have assisted inject I need to provide somehow the Factory, but I dunno how, I tried the custom provider @Desmond Teo suggested but then I will need to pass the factory from the activity, but in my design the factory is in a different module than the activity and it's internal so activity should not be aware of it
so I'm not using assisted inject anymore until I found a way to provide the factory in a "cleaner" way