|
| 1 | +package com.sun.ise.ui.common |
| 2 | + |
| 3 | +import android.app.AlertDialog |
| 4 | +import android.content.Context |
| 5 | +import android.graphics.Color |
| 6 | +import android.graphics.drawable.ColorDrawable |
| 7 | +import android.os.Bundle |
| 8 | +import android.view.View.OnClickListener |
| 9 | +import android.view.WindowManager |
| 10 | +import androidx.annotation.StringRes |
| 11 | +import com.sun.ise.util.StringUtils |
| 12 | +import kotlinx.android.synthetic.main.dialog_confirmation.* |
| 13 | + |
| 14 | +class MaterialDialog(context: Context) : AlertDialog(context) { |
| 15 | + |
| 16 | + private lateinit var dialogMessage: String |
| 17 | + private lateinit var positiveText: String |
| 18 | + private lateinit var negativeText: String |
| 19 | + private lateinit var positiveClickListener: OnClickListener |
| 20 | + private lateinit var negativeClickListener: OnClickListener |
| 21 | + |
| 22 | + private var canDismiss = true |
| 23 | + |
| 24 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 25 | + super.onCreate(savedInstanceState) |
| 26 | + setContentView(com.sun.ise.R.layout.dialog_confirmation) |
| 27 | + } |
| 28 | + |
| 29 | + override fun onStart() { |
| 30 | + super.onStart() |
| 31 | + if (StringUtils.checkNotEmpty(positiveText) && positiveClickListener != null) { |
| 32 | + buttonConfirm.text = positiveText |
| 33 | + buttonConfirm.setOnClickListener(positiveClickListener) |
| 34 | + } |
| 35 | + if (StringUtils.checkNotEmpty(negativeText) && negativeClickListener != null) { |
| 36 | + buttonCancel.text = negativeText |
| 37 | + buttonCancel.setOnClickListener(negativeClickListener) |
| 38 | + } |
| 39 | + this.setCanceledOnTouchOutside(canDismiss) |
| 40 | + window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) |
| 41 | + window?.clearFlags( |
| 42 | + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or |
| 43 | + WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + fun setMessage(message: String): MaterialDialog { |
| 48 | + dialogMessage = message |
| 49 | + return this |
| 50 | + } |
| 51 | + |
| 52 | + fun setMessage(@StringRes messageId: Int): MaterialDialog { |
| 53 | + setMessage(context.getString(messageId)) |
| 54 | + return this |
| 55 | + } |
| 56 | + |
| 57 | + fun setPositiveButton(text: String, listener: OnClickListener): MaterialDialog { |
| 58 | + this.positiveText = text |
| 59 | + this.positiveClickListener = listener |
| 60 | + return this |
| 61 | + } |
| 62 | + |
| 63 | + fun setPositiveButton(@StringRes textId: Int, listener: OnClickListener): MaterialDialog { |
| 64 | + this.setPositiveButton(context.getString(textId), listener) |
| 65 | + return this |
| 66 | + } |
| 67 | + |
| 68 | + fun setNegativeButton(text: String, listener: OnClickListener): MaterialDialog { |
| 69 | + this.negativeText = text |
| 70 | + this.negativeClickListener = listener |
| 71 | + return this |
| 72 | + } |
| 73 | + |
| 74 | + fun setNegativeButton(@StringRes textId: Int, listener: OnClickListener): MaterialDialog { |
| 75 | + this.setNegativeButton(context.getString(textId), listener) |
| 76 | + return this |
| 77 | + } |
| 78 | + |
| 79 | + fun dismissOnTouchOutside(dismiss: Boolean): MaterialDialog { |
| 80 | + this.canDismiss = dismiss |
| 81 | + return this |
| 82 | + } |
| 83 | +} |
0 commit comments