I am implementing an android application using koin injection . I have implemented a function within a viewModel injected in my fragment using koin but when i call the function in my fragment , it does not run . How could i solve it ? here is hat i have already implemented :
class MyApp :Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(applicationContext)
modules(appModule)
}
}
}
Here are different modules :
val appModule by lazy{//by lazy permet de faire appel au module des que l'on a besoin d'un module
listOf(
viewModelModule,
useCaseModule,
repositoryModule,
dataModule,
otherModules
)
}
val otherModules:Module = module {
single<USSDApi> { USSDController }
single<HomePageFragmentPresenterInterface<HomePageFragmentInterface,HomePageFragmentInteractorInterface>> { (view:HomePageFragmentInterface) ->
HomePageFragmentPresenter (view, get())}
single { Handler() }
single<HomePageFragmentInteractorInterface> { HomePageFragmentInteractor(get()) }
}
val viewModelModule: Module = module{
viewModel { CallViewModel() }
viewModel { TransactionViewModel(transactionUseCase = get()) }
}
val useCaseModule: Module = module{
single { TransactionUseCase(transactionRepository = get(),androidApplication()) }
}
val repositoryModule: Module = module{
single<TransactionRepository>{TransactionRepositoryImpl(remoteApi = get (),androidApplication())}
}
val dataModule: Module = module{
// single<LocalApi>{ LocalApiImpl(appContext = get()) }
single { createLagfoClient().create(RemoteApi::class.java) }
}
Here is how i initialize my viewModel :
private val viewModel:TransactionViewModel by viewModel()
Here is the content of my viewModel :
class TransactionViewModel(private val transactionUseCase: TransactionUseCase):ViewModel() {
private val params = MutableLiveData<Params>()
val getTransactionResult: LiveData<Resource<*>> = params.switchMap{ params->
when (params){
is Params.GetTransactionInfo -> {
transactionUseCase.getTransactionInformation(params.numberOfRequest,params.token).asLiveData(contextIO())
}
is Params.ValidateTransaction->{
transactionUseCase.validateTransaction(params.token).asLiveData(contextIO())
}
}
}
fun getTransaction(numberOfRequest: Int,token: String){
params.value=Params.GetTransactionInfo(numberOfRequest,token)
}
fun validateTransaction(token: String){
params.value=Params.ValidateTransaction(token)
}
sealed class Params {
data class GetTransactionInfo(val numberOfRequest:Int,val token:String):Params()
data class ValidateTransaction(val token:String):Params()
}
}
When i try to call getTransaction , it does not run :
viewModel.getTransaction(1,Global.token)