如何创建界面的koin模块

问题描述 投票:0回答:1

我有一个抽象类:

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)
}

我也有这些课程:

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){}

以及这些类的 koin 模块:

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

我有这个视图模块:

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

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

以及此 viewModule 的 koin 模块:

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

我的问题是如何为 ActionHandler 接口创建 koin 模块?

android dependency-injection viewmodel koin
1个回答
0
投票

我是这样做的:

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

 private val handler: ActionHandler by inject { parametersOf(inputParamArgs.firstParamArgs, inputParamArgs.secondParam) }
© www.soinside.com 2019 - 2024. All rights reserved.