Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/dialog/DialogFragmentTemplate.kt (Kotlin)

Code before:
    private val isDeleteDialog: Boolean = false,
    private val dismissAction: () -> Unit = { },
    private val materialDialog: MaterialAlertDialogBuilder? = null,
    private val config: MaterialAlertDialogBuilder.() -> Unit
) : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        isCancelable = cancelable
        return materialDialog?.create() ?: MaterialAlertDialogBuilder(requireContext()).apply(config).create()
    }

    override fun onStart() {
        super.onStart()
        if (isDeleteDialog) {
            (dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE)
                ?.setTextColor(requireContext().getColorCompat(R.color.colorTextDeleteButtonDialog))
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        dismissAction()
        super.onDismiss(dialog)
    }

    companion object {
        val TAG: String = DialogFragmentTemplate::class.java.simpleName
    }
}


Review comment (suggestion):
I think  whole `onDismiss` is not needed and `dismissAction()` can be removed and `MaterialAlertDialogBuilder.setOnDismissListener` can be used as other functions

Code after:
import kotlinx.parcelize.RawValue

class DialogFragmentTemplate : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialogParams = requireArguments().getParcelable<DialogTemplateParams>(DIALOG_TEMPLATE_PARAMS)
        requireNotNull(dialogParams) { "DialogTemplateParams is null" }
        isCancelable = dialogParams.cancelable == true
        return dialogParams.materialDialog?.apply(dialogParams.config)?.create()
            ?: MaterialAlertDialogBuilder(requireContext()).apply(dialogParams.config).create()
    }

    override fun onStart() {
        super.onStart()
        val dialogParams = requireArguments().getParcelable<DialogTemplateParams>(DIALOG_TEMPLATE_PARAMS)
        if (dialogParams?.isDeleteDialog == true) {
            (dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE)
                ?.setTextColor(requireContext().getColorCompat(R.color.colorTextDeleteButtonDialog))
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        val dialogParams = requireArguments().getParcelable<DialogTemplateParams>(DIALOG_TEMPLATE_PARAMS)
        dialogParams?.dismissAction?.let { it() }
        super.onDismiss(dialog)
    }

    @Parcelize
    data class DialogTemplateParams(
        val cancelable: Boolean = true,
        val isDeleteDialog: Boolean = false,
        val dismissAction: () -> Unit = { },
        val materialDialog: @RawValue MaterialAlertDialogBuilder? = null,
        val config: MaterialAlertDialogBuilder.() -> Unit
    ) : Parcelable

    companion object {
        val TAG: String = DialogFragmentTemplate::class.java.simpleName
        private val DIALOG_TEMPLATE_PARAMS = "${DialogFragmentTemplate.TAG}_DIALOG_TEMPLATE_PARAMS"

        fun newInstance(dialogParams: DialogTemplateParams) = DialogFragmentTemplate().apply {
            arguments = bundleOf(DIALOG_TEMPLATE_PARAMS to dialogParams)
        }
    }
}