Search code examples
androidfunctionkotlinsolid-principles

how to make function simpler


I tried to follow solid principles and want to make my code better. So I want to separate the function that may not be used in some problems.

this is the code that I already to separate but still redundant

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

from that code I tried to separate when the problem need the body and some of them not. So if i let them can access the body it will be just empty so I separate like that. But, the code are redundant becouse it write the same code with one of them there is no body. So i tried to make one of them just take the other code and add body procedure, but I have no idea how to do that. I tried to make an object so when it called true then means there is no body and if false there is body like this code

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

but it won't work because AlertDialog and View can't be called there. even if I change them to be public. please help


Solution

  • I got my answer. So what I do is I make button and body with two different interfaces. and because if want to use body you need button automatically so I just make it call when it needed only like this

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