Monica
Monica

Reputation: 436

How to create a koin module for interface

I have an abstract class:

abstract class MyFirstViewModel(
  private val firstParamArgs: FirstParamArgs,
  private val secondParam: SecondParam):ViewModel(),ActionHandler{
override fun handleAction(result: ActionResult){
// implementation of this function
}
interface ActionHandler {
  fun handleAction(result: ActionResult)
}

also I have these classes:

class MySecondViewModel(
      private val firstParamArgs: FirstParamArgs,
      private val secondParam: SecondParam):MyFirstViewModel(firstParamArgs, secondParam){}

class MyThirdViewModel(
      private val firstParamArgs: FirstParamArgs,
      private val secondParam: SecondParam):MyFirstViewModel(firstParamArgs, secondParam){}

and koin modules for these classes:

  viewModel<MyFirstViewModel> { (args: FirstParamArgs) ->
    MySecondViewModel(
      get())}
  viewModel<MyFirstViewModel> { (args: FirstParamArgs) ->
    MyThirdViewModel(
      get())}

and I have this viewmodule:

class MyInputViewModel(
inputParamArgs:InputParamArgs,
handleRequest:HandleRequest):ViewModel(){
private val handler: ActionHandler by inject()

private fun actionHandling(){
val result = handler::handleAction }
}

and koin module for this viewModule:

 viewModel { (args: InputParamArgs) ->
    MyInputViewModel(
      args,
      get()
    )
  }

My question is how to create koin module for ActionHandler interface?

Upvotes: 0

Views: 32

Answers (1)

Monica
Monica

Reputation: 436

I made it this way:

factory<ActionHandler> { (firstParamArgs: FirstParamArgs, secondParam: SecondParam) ->
    MySecondViewModel(firstParamArgs, secondParam)
}

 private val handler: ActionHandler by inject { parametersOf(inputParamArgs.firstParamArgs, inputParamArgs.secondParam) }

Upvotes: 0

Related Questions