如何让功能更简单

how to make function simpler

我努力遵循坚实的原则,并希望让我的代码变得更好。所以想把一些问题可能用不到的功能分开来

这是我已经分离但仍然多余的代码

private lateinit var dialogView : View
    private lateinit var b : AlertDialog    
override fun modalDialog(
        view:Int,
        listener: bodyModalDialog, submitButtonModalDialog: submitButtonModalDialog,
        btnSubmit:Int,
        btnCancel:Int) {
        val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
        val inflater =  this.layoutInflater
        dialogView = inflater.inflate(view, null)
        dialogBuilder.setView(dialogView)
        dialogBuilder.setCancelable(false)
        var uiSubmitButton  = dialogView.findViewById<View>(btnSubmit)
        var uiCancelButton  = dialogView.findViewById<View>(btnCancel)
        listener.bodyDialog(dialogView)
        uiSubmitButton.setOnClickListener {
            submitButtonModalDialog.submitButton(dialogView)
            b.cancel()
        }
        uiCancelButton.setOnClickListener {
            b.cancel()
        }
        b = dialogBuilder.create()
        b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        b.show()
    }

    override fun modalDialogNoBody(
        view:Int,
        listener: submitButtonModalDialog,
        btnSubmit:Int,
        btnCancel:Int) {
        val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
        val inflater =  this.layoutInflater
        dialogView = inflater.inflate(view, null)
        dialogBuilder.setView(dialogView)
        dialogBuilder.setCancelable(false)
        var uiSubmitButton  = dialogView.findViewById<View>(btnSubmit)
        var uiCancelButton  = dialogView.findViewById<View>(btnCancel)
        uiSubmitButton.setOnClickListener {
            listener.submitButton(dialogView)
            b.cancel()
        }
        uiCancelButton.setOnClickListener {
            b.cancel()
        }
        b = dialogBuilder.create()
        b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        b.show()
    }

当问题需要 body 而其中一些不需要时,我试图从该代码中分离出来。因此,如果我让他们可以访问 body,它将只是空的,所以我就这样分开了。但是,代码是多余的,因为它用其中之一编写了相同的代码,但没有 body。所以我试图让其中一个只接受其他代码并添加 body 过程,但我不知道该怎么做。我试着做一个 object 所以当它调用 true 时意味着没有 body 如果 false 有 body 就像这个代码

object Modal{
    fun isNoBody(view:Int,
                 listener: submitButtonModalDialog,
                 btnSubmit:Int,
                 btnCancel:Int):Boolean{
            val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
            val inflater =  this.layoutInflater
            dialogView = inflater.inflate(view, null)
            dialogBuilder.setView(dialogView)
            dialogBuilder.setCancelable(false)
            var uiSubmitButton  = dialogView.findViewById<View>(btnSubmit)
            var uiCancelButton  = dialogView.findViewById<View>(btnCancel)
            uiSubmitButton.setOnClickListener {
                listener.submitButton(dialogView)
                b.cancel()
            }
            uiCancelButton.setOnClickListener {
                b.cancel()
            }
            b = dialogBuilder.create()
            b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            b.show()
        return true
    }
}

但它不会工作,因为无法在那里调用 AlertDialogView。即使我将它们更改为 public。请帮忙

我得到了答案。所以我做的是用两个不同的界面制作按钮和主体。因为如果想使用 body 你需要自动按钮所以我只是在需要的时候让它调用,就像这样

override fun modalDialog(view:Int,
                         listener: SubmitButtonModalDialog,
                         btnSubmit:Int,
                         btnCancel:Int) {
    val dialogBuilder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert)
    val inflater =  this.layoutInflater
    dialogView = inflater.inflate(view, null)
    dialogBuilder.setView(dialogView)
    dialogBuilder.setCancelable(false)
    var uiSubmitButton  = dialogView.findViewById<View>(btnSubmit)
    var uiCancelButton  = dialogView.findViewById<View>(btnCancel)
    uiSubmitButton.setOnClickListener {
        listener.submitButton(dialogView)
        b.cancel()
    }
    uiCancelButton.setOnClickListener {
        b.cancel()
    }
    b = dialogBuilder.create()
    b.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    b.show()
}

override fun modalDialogBody(listener: BodyModalDialog) {
    listener.bodyDialog(dialogView)
}