Created
January 6, 2019 09:31
-
-
Save kyodgorbek/16b2811f4587c7c3738ef139d04c1810 to your computer and use it in GitHub Desktop.
CustomDialogAlertBuilder class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomAlertDialogBuilder(val context: Context?) { | |
private var alertDialogInfo = AlertDialogInfo() | |
fun addMessage(message: String?): CustomAlertDialogBuilder { | |
this.alertDialogInfo.message = message | |
return this | |
} | |
fun addMessage(@StringRes message: Int): CustomAlertDialogBuilder { | |
this.alertDialogInfo.message = context?.getString(message) | |
return this | |
} | |
fun setTextColor(@ColorInt colorInt: Int): CustomAlertDialogBuilder { | |
alertDialogInfo.textColor = colorInt | |
return this | |
} | |
fun setPositiveButton(text: String?, action: OnItemDialogClickListener?): CustomAlertDialogBuilder { | |
alertDialogInfo.positiveText = text | |
alertDialogInfo.onPositiveClickListener = action | |
return this | |
} | |
fun setNegativeButton(text: String?, action: OnItemDialogClickListener?): CustomAlertDialogBuilder { | |
alertDialogInfo.negativeText = text | |
alertDialogInfo.onNegativeClickListener = action | |
return this | |
} | |
fun setNeutralButton(text: String?, action: OnItemDialogClickListener?): CustomAlertDialogBuilder { | |
alertDialogInfo.neutralText = text | |
alertDialogInfo.onNeutralClickListener = action | |
return this | |
} | |
fun setPositiveButton(@StringRes text: Int, action: OnItemDialogClickListener?): CustomAlertDialogBuilder { | |
alertDialogInfo.positiveText = context?.getString(text) | |
alertDialogInfo.onPositiveClickListener = action | |
return this | |
} | |
fun setNegativeButton(@StringRes text: Int, action: OnItemDialogClickListener?): CustomAlertDialogBuilder { | |
alertDialogInfo.negativeText = context?.getString(text) | |
alertDialogInfo.onNegativeClickListener = action | |
return this | |
} | |
fun setNeutralButton(@StringRes text: Int, action: OnItemDialogClickListener?): CustomAlertDialogBuilder { | |
alertDialogInfo.neutralText = context?.getString(text) | |
alertDialogInfo.onNeutralClickListener = action | |
return this | |
} | |
fun setBackground(@DrawableRes colorInt: Int): CustomAlertDialogBuilder { | |
alertDialogInfo.background = colorInt | |
return this | |
} | |
fun setTitle(title: String?): CustomAlertDialogBuilder { | |
alertDialogInfo.titleText = title | |
return this | |
} | |
fun setTitle(@StringRes title: Int): CustomAlertDialogBuilder { | |
alertDialogInfo.titleText = context?.getString(title) | |
return this | |
} | |
fun setOnDismissListener(dismissListener: OnDismissListener): CustomAlertDialogBuilder { | |
alertDialogInfo.onDismissListener = dismissListener | |
return this | |
} | |
fun setCancelable(cancelable: Boolean): CustomAlertDialogBuilder { | |
alertDialogInfo.cancelable = cancelable | |
return this | |
} | |
fun build(): AlertDialog? { | |
var dialog: CustomAlertDialog? = null | |
context?.let { | |
dialog = CustomAlertDialog(context) | |
dialog?.setCancelable(listOf(alertDialogInfo.positiveText, alertDialogInfo.neutralText, alertDialogInfo.negativeText).filter { it != null }.size >= 2 || alertDialogInfo.cancelable) | |
dialog?.alertDialogInfo = alertDialogInfo | |
dialog?.setOnDismissListener { | |
alertDialogInfo.onDismissListener?.onDismiss() | |
} | |
} | |
return dialog | |
} | |
class AlertDialogInfo { | |
var message: String? = null | |
@ColorInt | |
var textColor: Int? = null | |
@DrawableRes | |
var background: Int? = null | |
var onPositiveClickListener: OnItemDialogClickListener? = null | |
var onNegativeClickListener: OnItemDialogClickListener? = null | |
var onNeutralClickListener: OnItemDialogClickListener? = null | |
var onDismissListener: OnDismissListener? = null | |
var positiveText: String? = null | |
var neutralText: String? = null | |
var negativeText: String? = null | |
var titleText: String? = null | |
var cancelable: Boolean = false | |
} | |
interface OnItemDialogClickListener { | |
fun onClick(dialog: CustomDialogInterface) | |
fun onClick(arg0: CustomAlertDialogBuilder, arg1: Int) | |
} | |
interface CustomDialogInterface : DialogInterface { | |
fun dismiss(onDismissListener: OnDismissListener) | |
fun dismiss(callDismissListener: Boolean) | |
} | |
interface OnDismissListener { | |
fun onDismiss() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment