How to produce the SavedStateHandle in your ViewModel using AssistedInject


One of my previous article Dagger Multibinding Saved My Time "claimed" to have found the right practice for providing ViewModels without producing a ViewModel Factory for each ViewModel. Apparently, I was wrong.  There are 2 main problems with that approach:

1 - I might forget to add a ViewModel in the Map graph.
2 - With the new SavedStateHandle which stays uniqely in each ViewModel I can't use a generic ViewModel Factory.  (you can check the implementation in the article provided above).

So,  what does AssistedInject do to help solve this case?

Check this example below:


Dagger doesn't know how to create  a SavedStateHandle. And since the SavedStateHandle cannot be used as a Singleton, we want a different state for each of our fragments. In order to achieve this, let's allow the AssistedInject library create a unique Factory for us. It might require a little more work on configurations, but after the set up, it's just kindergarten game for you.

Instead of  annotating constructor with @Inject , we annotate it with @AssistedInject:


The constructor parameters which cannot be evaluated by Dagger, should be marked with @Assisted. Now, we should create the Factory. With AssistedInject it's pretty easy. You just define an interface and annotate it with the @AssistedInject.Factory annotation and it is going to have a method with parameters that Dagger doesn't know how to create, and will return the current ViewModel:


Perhaps someone would ask why not having different scopes for each ViewModel in order to provide that unknown parameter uniquely in each ViewModel? In that case, you will have a component for each fragment, a module and you should not forget to perform DI in all creation state of each fragment. So the short answer is: Don't do it.

One more step. We need to somehow connect the strings here. How does Dagger provide dependencies that don't know how to create? It will ask the component if there is any of the modules providing the dependencies. But there isn't any module, or is there 👀 ? Yes, the AssistedInject has another small step to conclude:


In this way, the our ViewModel Factories will be auto generated. We just have to call them from the Singleton component:


That's it.

Since the SavedStateViewModel Factory is now the default ViewModel Factory, to provide a SavedStateHandle, we can't extend our ViewModelProvider.Factory. Instead, we need to extend our AbstractSavedStateViewModelFactory:


And with the help of Kotlins' delegation, we achieve something like this in our fragment:


And that's it.

Credits:
Zhuinden
It's complicated, but it doesn't have to be: a Dagger journey by Fred Porciúncula, Blinkist EN