I've run into the fact that I can't inject a repository into my view model. I have two modules app and data. In I build all dependencies through Application Component
@Component(
dependencies = [DataComponent::class],
modules = [AppModule::class, NavigationModule::class],
)
interface ApplicationComponent {
fun injectApp(app: App)
fun inject(fragment: FirstFragment)
fun inject(fragment: SecondFragment)
fun inject(mainActivity: MainActivity)
}
@Module
abstract class DataBinds {
@Binds
abstract fun bindAuthRepository(authRepositoryImpl: AuthRepositoryImpl): AuthRepository
}
@Component(modules = [DataBinds::class])
interface DataComponent
On the app, I'm already putting everything together
class App : Application() {
lateinit var appComponent: ApplicationComponent
lateinit var dataComponent: DataComponent
val cicerone: Cicerone<Router> by lazy {
Cicerone.create()
}
override fun onCreate() {
super.onCreate()
dataComponent = DaggerDataComponent.builder().build()
appComponent = DaggerApplicationComponent.builder()
.dataComponent(dataComponent)
.appModule(AppModule(this))
.build()
}
}
And inject the dependency in the view model
class SplashViewModel @Inject constructor(private val authRepository: AuthRepository) :
ViewModel() {
private val _stateFlow = MutableStateFlow("test")
val stateFlow = _stateFlow
fun checkUserLoggedIn() {
viewModelScope.launch {
authRepository.isUserAuthorized().collect {
_stateFlow.value = "User is logged in: $it"
}
}
}
}
But I get an error when checking the graph
error: [Dagger/MissingBinding] ru.yangel.domain.repository.AuthRepository cannot be provided without an @Provides-annotated method. public abstract interface ApplicationComponent { ^
I tried just adding modules to the App Component and everything worked, but I want to do as a multi-module architecture where in each module I will store a different small graph and assemble it into one big one in the app
You need to add the provider method to ApplicationComponent
@Component(
dependencies = [DataComponent::class],
modules = [AppModule::class, NavigationModule::class],
)
interface ApplicationComponent {
fun injectApp(app: App)
fun inject(fragment: FirstFragment)
fun inject(fragment: SecondFragment)
fun inject(mainActivity: MainActivity)
fun provideAuthRepository(): AuthRepository // << this
}
Maybe to DataComponent
too