I've got a small question regarding `SavedStateHan...
# kotlin-inject
t
I've got a small question regarding
SavedStateHandle
- say I have a ViewModel that needs a class that needs `SavedStateHandle`:
Copy code
@Inject 
class MyViewModel(private val test: SomeTestClass): ViewModel()

@Inject
class SomeTestClass(private val savedStateHandle: SavedStateHandle) {...}
And I have my own view model factory:
Copy code
@Inject
class ViewModelFactory(
    val myViewModel: (SavedStateHandle) -> MyViewModel
): ViewModelProvider.Factory {
    override fun <T : ViewModel> create(
        modelClass: Class<T>,
        extras: CreationExtras
    ): T {
      val savedStateHandle = extras.createSavedStateHandle()
      return when (modelClass) {
        MyViewModel::class.java -> myViewModel(savedStateHandle) as T
        ...
      }
    }
}
Is there a way to get the
SavedStateHandle
to be automatically passed down to
SomeTestClass
without me needing to create an instance of the class in the factory?
s
I moved away from viewmodel factories for this sort of reason. now I use hilt view model wrapping a "presenter" class that it delegates to. Then I can use @ViewModelScoped to pass something like a SavedStateHandle to any dependencies that are also view model scoped.
For a bonus it cuts out a lot of boilerplate we had in viewmodel factories
ah, i just realised what channel we're in. sorry. Assumed you would be using hilt
t
Ah no worries! I'm definitely seeing the boilerplate problem now though after starting to refactor a large codebase from Hilt to kotlin-inject 😄
s
as i understand, this kind of thing is outside of the scope of kotlin-inject, but the authors hope that others will build layers on-top of kotlin-inject that provide this kind of functionality, a bit like Hilt is built on top of Dagger
t
Yeah that makes sense, was kind of afraid of this being the case (was hoping I was just missing something obvious). Thanks!