In Dagger, how can I add a model to Module? For example I added the presenter in the following way:
@Module
class AboutModule(val appContext: Context) {
@FragmentScope
@Provides
fun providePresenter(): AboutListContract.Presenter {
return AboutListPresenter(appContext = appContext)
}
}
Now i want want to add my View model, also with appContext.
class AboutViewModel(val appContext: Context): ViewModel() {
UPDATE: Can i add my view model smth like this?
@Module
class AboutModule(val appContext: Context) {
@FragmentScope
@Provides
fun provideModel(model: AboutViewModel): ViewModel {
return AboutViewModel(appContext = appContext)
}
}
I solved my problem with next solution:
@Module
class AboutModule(val appContext: Context) {
@FragmentScope
@Provides
fun provideFactory(): AboutViewModelFactory {
return AboutViewModelFactory(appContext)
}
}
And in fragment write smth like this:
class AboutFragment : BaseFragment(), OnItemClickListener {
lateinit var viewModel: AboutViewModel
@Inject lateinit var viewModelFactory: AboutViewModelFactory
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
injectDependencies()
viewModel = ViewModelProviders
.of(this, viewModelFactory)
.get(AboutViewModel::class.java)
}
private fun injectDependencies() {
activity?.let {
DaggerAboutComponent.builder().aboutModule(AboutModule(it)).build().inject(this)
}
}
}
Nice advise: https://stackoverflow.com/a/60884492/6387618