如何在演示者中为两种不同的用法传递单个上下文参数?

How to pass a single context parameter for 2 different usage in presenter?

所以我为我的应用程序实现了这个 MVP,

我的活动:

class ValidateOTPActivity : AppCompatActivity(), ValidateOTPListener {

  private lateinit var presenter: ValidateOTPPresenter
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    presenter = ValidateOTPPresenter(this, this)
    ...
    ...
  }
}

我的演示者:

class ValidateOTPPresenter constructor(val context: Context, val listener: ValidateOTPListener) {
...
...
...
}

我的监听器:

interface ValidateOTPListener {
    fun onValidationSuccess(response: JSONObject)
    fun onValidationFailed()
}

我想在演示者中同时使用 Context 和 ValidateOTPListener,如何避免在 presenter = ValidateOTPPresenter(this, this) 中传递两个 this?我只想传一个this,可以吗?

您可以将Context转换为ValidateOTPPresenter

中的接口
class ValidateOTPPresenter constructor(private val context: Context){
    private var listener:ValidateOTPListener? = null
    init{
        if(context is ValidateOTPListener){
           listener = context
        }
    }
}

您好,您可以这样做:

   class ValidateOTPActivity : AppCompatActivity(), ValidateOTPListener {

      private lateinit var presenter: ValidateOTPPresenter
        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        presenter = ValidateOTPPresenter(this)
        ...
        ...
      }
    }

然后在您的演示者中:

 class ValidateOTPPresenter constructor(val context: Context) {
  val otpListener:ValidateOTPListener
  init{
    otpListener= context as ValidateOTPListener
   }
...
...
...
}