https://kotlinlang.org logo
#dagger
Title
# dagger
m

miqbaldc

07/26/2020, 9:15 PM
Is there any alternatives ways to provides a
viewModelScope
? Moved to this issue instead with more details and examples: https://github.com/google/dagger/issues/2004
g

gildor

07/27/2020, 2:03 AM
viewModelScope is not a dispatcher, it’s CoroutineScope You can get scope from it, using
Copy code
viewModelScope.coroutineContext[CoroutineDispatcher]!!
m

miqbaldc

07/27/2020, 12:21 PM
Really apologize for miss information, I’ve update my code to use
CoroutineScope
instead. But still compile error because I still don’t know how to provide dependency inside view model like the above
g

gildor

07/27/2020, 12:22 PM
Ahhh, I see, you cannot do this like that, you should inject view model into your method
To so that, you need component with view model
m

miqbaldc

07/27/2020, 12:30 PM
Do you mean something like this?
Copy code
@DisableInstallInCheck
@Module
interface ViewModelModule {

    @get:[Binds LoginScope IntoMap ViewModelKey(LoginViewModel::class)]
    val LoginViewModel.loginViewModel: ViewModel

    companion object {
        @Provides
        fun provideLoginViewModelScope(viewModel: LoginViewModel): CoroutineScope = viewModel.viewModelScope
    }
}
Unfortunately, the above official docs doesn’t quite work for me (because using DFM). Thus vanilla dagger is needed, that’s why I’m using
@DisableInstallInCheck
in
@Module
configured inside DFM
g

gildor

07/27/2020, 12:32 PM
DFM?
m

miqbaldc

07/27/2020, 12:32 PM
my apologize, it stands for *D*ynamic *F*eature *M*odule, as stated in android studio official docs for using Hilt in multi-modules here: https://developer.android.com/training/dependency-injection/hilt-multi-module#dfm
g

gildor

07/27/2020, 12:34 PM
Anyway, your original snippet is definitely impossible to make work, you cannot magically capture instance value, you need something like official doc, inject VM to this binding Don't know exact details how inject VM, I would recommend to ask on Stackoverflow or report your case on dagger issue tracker
m

miqbaldc

07/27/2020, 12:36 PM
noted, really appreciate and a big thanks @gildor
2 Views