Feature request: Generate AssistedFactory for assisted injection
Is your feature request related to a problem? Please describe. When using assisted injection, a factory interface needs to be defined, but this interface is only boilerplate and should be possible to generate.
Describe the solution you'd like
A new annotation is introduced which generates an @AssistedFactory interface based on a constructor.
E.g.
@AutoAssistedFactory
class Example @AssistedInject constructor(@Assisted a: A, b: B, @Assisted c: C)
generates
@AssistedFactory interface ExampleFactory {
fun create(a: A, c: C): Example
}
Alternatives Could also make sense to put the annotation on the constructor instead of the class.
Additional context
We're using assisted injection mainly with jetpack compose ViewModel injection, where the factory needs to be specified in the @HiltViewModel annotation.
Would be nice to make sure any solution works with that, e.g.
@HiltViewModel(assistedFactory = ExampleViewModelFactory::class)
@AutoAssistedFactory
class ExampleViewModel @AssistedInject constructor(@Assisted a: A, b: B, @Assisted c: C) : ViewModel()
Generated:
@AssistedFactory interface ExampleViewModelFactory {
fun create(a: A, c: C): ExampleViewModel
}
An optional additional function could be generated to reduce the code necessary to use the factory with compose hiltViewModel
fun exampleViewModel(
a: A,
c: C,
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
): ExampleViewModel = hiltViewModel<ExampleViewModel, ExampleViewModelFactory>(viewModelStoreOwner, key) { it.create(a, c) }
This is an interesting idea. It is annoying when you need to access generated code before it's generated. The workflow because tedious because you need to add your VM with your annotations, build, then continue writing code that uses the generated accessors.
Another problem is that when you use the bridge pattern it will be a lot trickier to generate the correct code so the usage of this feature would be a bit limited.
You could also generate this code yourself in your own project quite easily and it would be a fun project to do. Learning how to generate code is very useful.
Although I'm leaning towards this being too specific, I'll think about this some more before I decide.