Reputation: 119
As the title says I want to inject two string params into my ViewModel from my Compose Activity using Koin. And I don't want to create a Factory ViewModel.
I saw how to inject Objects but I'm confused when it comes to parameters. This was so simple using Dagger Hilt I feel stupid for asking this..Any tips please?
I call this from the compose activity
val someViewModel: SnapshotViewModel by viewModel {
parametersOf(displayName, securityName)
}
and in my Koin Module I do this but I get an error
Too many arguments for public constructor
val module = module {
single { params -> SnapshotViewModel(params.get<String>(), params.get<String>())}}
And here I try to inject them in my ViewModel
private val displayName: String by inject()
private val securityName: String by inject()
Upvotes: 0
Views: 3166
Reputation: 1116
No need to call by inject()
in your property definitions. Either you pass the values via constructor or via injection
class SnapshotViewModel(displayName: String, securityName: String)
val module = module {
single { params ->
SnapshotViewModel(params.get<String>(), params.get<String>())
}
}
or
val module = module {
single { (displayName: String, securityName: String) ->
SnapshotViewModel(displayName, securityName)
}
}
KoinComponent
private val displayName: String
get() = getKoin().getProperty("displayName", "defaulValue")
private val securityName: String
get() = getKoin().getProperty("securityName", "defaulValue")
KoinComponent
in order to have access to getKoin()
getKoin().setProperty("displayName", "Value")
getKoin().setProperty("securityName", "newValue")
Upvotes: 0