I make project with mvvm pattern with koin for DI, but i always have No definition found repository
I alredy define repository in module app before viewmodel, but i get some error
Gradle app
// Koin for Android
implementation "org.koin:koin-android:$rootProject.koin_version"
// Koin Android Scope features
implementation "org.koin:koin-androidx-scope:$rootProject.koin_version"
// Koin Android ViewModel features
implementation "org.koin:koin-androidx-viewmodel:$rootProject.koin_version"
module
val dataModule = module {
//remoteData
single { AppRemoteData() }
//repository
single{ AppRepository(get()) as AppDataSource}
// viewmodel
viewModel{ ListHomeViewModel(get()) }
viewModel { LoginViewModel(get()) }
define module
val myAppModule = listOf(appModule, dataModule)
in app
startKoin {
androidLogger(Level.DEBUG)
androidContext(this@MainApp)
modules(myAppModule)
}
Repository class
class AppRepository(val appRemoteDataSource: AppRemoteData) : AppDataSource {
override fun loginAccount(email: String, password: String) : LiveData<String> {
val data = MutableLiveData<String>()
appRemoteDataSource.loginAccount(email,password,object : AppDataSource.LoginCallback{
override fun onSucces(id: String?) {
//berhasil
data.postValue(id)
}
override fun onFailed(message: String?) {
data.postValue(message)
d(message)
}
override fun onFailure(message: String?) {
data.postValue(message)
d(message)
}
})
return data
}
AppRemoteData
class AppRemoteData {
val ref = FirebaseDatabase.getInstance().getReference("user")
var auth = FirebaseAuth.getInstance()
fun loginAccount(email: String, password: String, callback: AppDataSource.LoginCallback) {
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener {
task ->
if(task.isComplete){
callback.onSucces(task.result?.user?.providerId)
}else{
callback.onFailed(task.exception?.localizedMessage)
}
}
}}
here error message
The error message is telling you that Koin couldn't create a LoginViewModel
instance for you, because it would've had to provide an instance of AppRepository
during its creation, but you didn't tell it how to do that.
My guess is that you've accidentally used the AppRepository
type in the LoginViewModel
constructor directly, instead of using your AppDataSource
that you've bound the repository instance to in your module.
So if you have something like this, that would require an AppRepository
specifically:
class LoginViewModel(val dataSource: AppRepository) : ViewModel()
You should replace it with this, where you're only asking Koin for an AppDataSource
, which you did configure it to be able to provide:
class LoginViewModel(val dataSource: AppDataSource) : ViewModel()
if in your ViewModel has an interface as parameter.
class MyViewModel(val interfaceName: InterfaceName) : ViewModel()
In your module definition use singleBy<> Instead of Single().
singleBy<InterfaceName,InterfaceNameImplementation>()
Finally in for your ViewModel
viewModel { MyViewModel(get()) }
This worked for me in Koin 2.0
Hope it helps :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.