Reputation: 7809
I implemented a custom ViewModelFactory
and Dagger DI to ViewModel
's members were inject by Dagger automatically, all code is below:
class ViewModelFactory @Inject constructor(
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
): ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val creator = creators[modelClass] ?: creators.entries.firstOrNull {
modelClass.isAssignableFrom(it.key)
}?.value ?: throw IllegalArgumentException("unknown model class $modelClass")
try {
@Suppress("UNCHECKED_CAST")
return creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
DI:
@Suppress("unused")
@Module
abstract class ViewModelModule {
@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
AppModule::class,
ViewModelModule::class,
ActivityModule::class
]
)
interface AppComponent: AndroidInjector<App> {
@Component.Builder
abstract class Builder: AndroidInjector.Builder<App>()
}
@Suppress("unused")
@Module
abstract class ActivityModule {
@ContributesAndroidInjector(
modules = [LoginFragmentBuilderModule::class, LoginViewModelModule::class]
)
abstract fun contributeLoginActivity(): LoginActivity
@ContributesAndroidInjector(modules = [])
abstract fun contributeMainActivity(): MainActivity
}
@Suppress("unused")
@Module
abstract class LoginViewModelModule {
@Binds
@IntoMap
@ViewModelKey(AccountViewModel::class)
abstract fun bindAccountViewModel(accountViewModel: AccountViewModel): ViewModel
}
@Suppress("unused")
@Module
abstract class LoginFragmentBuilderModule {
@ContributesAndroidInjector
abstract fun contributeAccountFragment(): AccountFragment
}
@Module
object AppModule {
@JvmStatic
@Provides
fun provideGitHubApi(retrofit: Retrofit): IGitHubApi {
return retrofit.create(IGitHubApi::class.java)
}
// A lot of other providers
}
AccountViewModel::
class AccountViewModel(app: Application) : AndroidViewModel(app) {
@Inject
lateinit var gitHubApi: IGitHubApi
}
I wrote it using the tutorial but I get an error on the compile time:
e: /test/di/AppComponent.java:8: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] test.ui.login.models.AccountViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.contedevel.juke.App> {
^
test.ui.login.models.AccountViewModel is injected at
test.di.LoginViewModelModule.bindAccountViewModel(accountViewModel)
java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
test.di.ViewModelFactory.<init>(creators)
test.di.ViewModelFactory is injected at
test.di.ViewModelModule.bindViewModelFactory(factory)
androidx.lifecycle.ViewModelProvider.Factory is injected at
test.ui.login.AccountFragment.viewModelFactory
test.ui.login.AccountFragment is injected at
dagger.android.AndroidInjector.inject(T)
component path: test.di.AppComponent → test.di.ActivityModule_ContributeLoginActivity.LoginActivitySubcomponent → test.di.LoginFragmentBuilderModule_ContributeAccountFragment.AccountFragmentSubcomponent
Upvotes: 0
Views: 141
Reputation: 3112
Firstly how Dagger will work in this scenario. You are binding the instance of AccountViewModel
through a parameter in bindAccountViewModel
. However, now dagger will create the instance of AccountViewModel
and it will get bind to a Map with key as AccountViewModel::class
.
Per your stack trace, Dagger gave you suggestion that you can use constructor injection or Provide your ViewModel
using a Provides method in a module. From our current implementation, the second suggestion becomes prevalent since we are not creating the instance of AccountViewModel. Therefore you need to pass the IGithubApi as a constructor parameter in an AccountViewModel
that will be a viable solution in this scenario.
class AccountViewModel @Inject constructor(private val api: IGithubAPI, app: Application) : AndroidViewModel(app)
Upvotes: 1
Reputation: 736
From your stack trace:
test.ui.login.models.AccountViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method
Your AccountViewModel
does not have an @Inject
constructor.
Try this:
class AccountViewModel @Inject constructor(
private val gitHubApi: IGitHubApi,
app: Application) : AndroidViewModel(app) {
}
Upvotes: 1