Comments (12)
This functionality is going to be covered by the upcoming Hilt tool in Jetpack which integrates Dagger with view models and work manager.
from inflationinject.
You might want to check @InflationInject @InflationModule which is used to avoid that another level of factories (LayoutInflator.Factory).
from inflationinject.
@mtiidla have you found more elegant solution to this?
from inflationinject.
@mtiidla have you found more elegant solution to this?
@eerikk No, unfortunately not. I have recently switched projects and am currently not using Dagger2, but Koin instead. Koin solves this exact issue as you can see here. However, I personally care much more about Dagger2 and I am planning to look into the LayoutInflater Factory solution mentioned by Ethan1983, but haven't gotten around to it yet.
from inflationinject.
My solution was to have a callback on ViewModel creation in ViewModelFactory:
https://gist.github.com/alashow/67cc52c606ddc7637e75185673b7f9a6
from inflationinject.
My solution was to have a callback on ViewModel creation in ViewModelFactory:
https://gist.github.com/alashow/67cc52c606ddc7637e75185673b7f9a6
Thanks for proposing your solution, however it is not solving the problem. The main idea is to avoid an init method on the viewmodel and use constructor to injection arguments. Otherwise we could very well write an init method on every viewmodel and just call it (which you have just abstracted away).
from inflationinject.
Yeah, the reason why I abstracted it is to init the ViewModel on creation.
from inflationinject.
I made https://github.com/evant/injectedvmprovider to try to deal with this, it lets you skip the ViewModelFactory. While we aren't currently using it with AssistedInject, we are using it with manual factories and it's working pretty well. Your example would be:
The ViewModel
class WordDetailViewModel @AssistedInject constructor(
private val repository: WordRepository,
@Assisted argument: WordDetailScreenArg
) : ViewModel() {
val wordDetail: LiveData<Word> = repository.word(argument.word)
fun onDeleteWordClicked() {
wordDetail.value?.let {
repository.deleteWord(it.word)
}
}
@AssistedInject.Factory
interface Factory {
fun create(argument: WordDetailScreenArg): WordDetailViewModel
}
}
Injection
@Inject
lateinit var viewModelFactory: WordDetailViewModel.Factory;
override fun onAttach(context: Context?) {
super.onAttach(context)
val arguments: WordDetailScreenArg = ...
Injection.appComponent.inject(this)
viewModel = InjectedViewModelProviders.of(this).get(viewModelFactory, f -> f.create(arguments))
}
from inflationinject.
Well with this approach are we not gonna end with each individual ViewModel mapped to its own ViewModelFactory. Thus, we are sacrificing the cost of loosing a generic ViewModelFactory. Please correct me if I misunderstood.
from inflationinject.
https://github.com/tfcporciuncula/dagger-journey
from inflationinject.
We've started to use this kind of solution in our project. Dagger helps us out here 👇
- Generic code that lets us inject into
Fragment
andViewModel
. Important part here is the code insidecompanion object
. We make fragment argument available for ViewModel.
@Module
interface ExampleModule {
@PerFragment
@ContributesAndroidInjector(modules = [ExampleViewModelModule::class])
fun contributesExampleFragment(): ExampleFragment
@Module(includes = [ViewModelFactoryModule::class])
abstract class ExampleViewModelModule {
@Binds
@IntoMap
@ViewModelKey(ExampleViewModel::class)
abstract fun bindExampleViewModel(viewModel: ExampleViewModel): ViewModel
@Module
companion object {
@Provides
@JvmStatic
fun providesExampleParam(fragment: ExampleFragment): String {
return fragment.requireArguments()
.getString(ExampleFragment.ARG_EXAMPLE_STRING)!!
}
}
}
}
This way we can inject the param simply to our ViewModel
class ExampleViewModel @Inject constructor(
private val exampleString: String
): ViewModel{}
If you have multiple parameters with same type, you can use @Named
annotation to prevent type conflicts. 👇
@Provides
@Named(ExampleFragment.EXAMPLE_STRING)
@JvmStatic
fun providesExampleParam(fragment: ExampleFragment): String {
return fragment.requireArguments()
.getString(ExampleFragment.ARG_EXAMPLE_STRING)!!
}
...and now you can simply access it from ViewModel
class ExampleViewModel @Inject constructor(
@Named(ExampleFragment.EXAMPLE_STRING) private val exampleString: String
): ViewModel{}
from inflationinject.
This library uses Assisted Inject and SavedStateHandle to pass arguments into your viewmodel
https://github.com/hansenji/ViewModelInject.
from inflationinject.
Related Issues (20)
- The generated module should be annotated with `@DisableInstallInCheck` HOT 1
- Factory in viewmodel using hilt @ViewModelInject needs fully qualified class name HOT 4
- @DisableInstallInCheck for generated modules ( Hilt ) HOT 2
- @AssistedModule's @Module must include AssistedInject_AssistedModule HOT 3
- AssistedFactory_Factory not regenerated after the files it references are deleted HOT 2
- AssistedInject in Android UI tests HOT 1
- Carry annotations from interface into generated implementation HOT 6
- error: @AssistedModule's @Module must not include HOT 1
- Factory method parameters do not match constructor @Assisted parameters. Both parameter type and name must match. HOT 1
- Deprecate use with dagger 2 HOT 1
- @dagger.Module(includes = {AssistedInject_SampleAssistedInjectModule.class})
- Runtime gets Kotlin stdlib added despite being written in Java
- Port Turbine's release infra
- KSP Support HOT 6
- InflationInject throws error when a custom view is a kotlin class with @JvmOverloads marked constructor HOT 5
- InflationInjectionModule should generate module entries in a stable order to prevent Gradle cache misses
- LD
- Dependency Dashboard
- Get the Gradle wrapper validation action out of the critical path
- LD
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inflationinject.