First, make a comparison
Instead of using a DSL, the code looks like this
var dialog= CommonDialog.Builder(ActivityUtils.getTopActivity())
.setContentView(R.layout.order_message_dialog)
.setWidth(ScreenUtils.getAppScreenWidth()- SizeUtils.dp2px(60f))
.setCancelableOnTouchOutside(true)
.forGravity(Gravity.CENTER)
.create().show()
Copy the code
With a DSL, the code looks like this
Val dialog=DialogUtil{setPContext(this@MainActivity) // Mandatory. Cannot be empty r.style. dialog SetContentView (r.layout.order_message_dialog) // Mandatory, add layout forGravity(Gravity.CENTER) // Set layout display position, // formBottom(true)// Set the layout to display at the bottom of the screen, default display in the middle (Boolean value: display pop-up animation from the bottom)}Copy the code
It still looks a lot cleaner
Two. Build the constructor
It’s very simple to use, no more nonsense, just serve
fun DialogUtil(block: CommonDialogBuild.() -> Unit): CommonDialog = CommonDialogBuild().apply(block).show() class CommonDialogBuild{ lateinit var context:Context val themeResId:Int=R.style.dialog lateinit var P: AlertController.AlertParams fun setPContext(mContext:Context) { context=mContext P=AlertController.AlertParams(context, themeResId) } fun setContentView(view: View) {P.mView = View P.mViewLayoutResId = 0} // Set layoutId fun setContentView(layoutId: Int) {P.mView = null P.mViewLayoutResId = layoutId} // setText fun setText(viewId: Int, text: CharSequence) {P.m textarray. put(viewId, text)} fun setOnClickListener(view: Int, listener: View.OnClickListener) { P.mClickArray.put(view, listener) } fun setVisibility(view: Int, visibility: Int) { P.mVisibility.put(view, Whether the visibility)} / / full screen Width fun fullWidth () {p. Width = ViewGroup. LayoutParams. MATCH_PARENT} fun fullHeight () { P. Height = ViewGroup. LayoutParams. MATCH_PARENT} / * * * * * @ from the bottom of the pop-up param isAnimation animation * @ return * / fun formBottom(isAnimation: Boolean) { if (isAnimation) { P.mAnimations = R.style.dialog_from_bottom_anim } P.mGravity = Gravity.BOTTOM } /** * * * @param gravity * @return */ fun forGravity(gravity: Int) {P.mGravity = Gravity} /** * set Dialog width @param width @param height @return */ fun setWidthAndHeight(width: Int, height: Int) {P.mWidth = Width P.mHeight = Height} /*** * set Dialog Width ** / fun setWidth(Width: Int) {P.mWidth = Width} /** * addDefaultAnimation ** @return */ fun addDefaultAnimation() {P.mAnimations = R.style.dialog_scale_anim} /** * setAnimations ** @param styleAnimation * @return */ fun setAnimations(styleAnimation: Int) {P.mAnimations = styleAnimation} /** * Click back to see if you can disappear */ setCancelable(cancelable: Boolean) {p. Cancelable = Cancelable} / whether can cancel * * * * click on margin/fun setCancelableOnTouchOutside (cancelableOnTouchOutside: Boolean) {p. CancelableOnTouchOutside = CancelableOnTouchOutside} / * * * * / cancel to monitor events fun setOnCancelListener(onCancelListener: DialogInterface. OnCancelListener) {p. OnCancelListener = OnCancelListener} / * * * * / fun disappear to monitor events setOnDismissListener(onDismissListener: DialogInterface. OnDismissListener) {p. OnDismissListener = OnDismissListener} / * * * * click on the return key to monitor events/fun setOnKeyListener(onKeyListener: DialogInterface. OnKeyListener) {p. OnKeyListener = OnKeyListener} / * * @ * * * create dialog return * / fun the create () : CommonDialog { val dialog = CommonDialog(P.mContext, P.mThemeResId) P.apply(dialog.mAlert) dialog.setCancelable(P.mCancelable) dialog.setCanceledOnTouchOutside(P.mCancelableOnTouchOutside) dialog.setOnCancelListener(P.mOnCancelListener) dialog.setOnDismissListener(P.mOnDismissListener) if (P.mOnKeyListener ! = null) {dialog.setonKeyListener (P.mOnKeyListener)} return dialog} /** * display dialog ** @return */ fun show(): CommonDialog { val dialog = create() dialog.show() return dialog } }Copy the code
Kotlin design mode, simplify the code
With the builder pattern, let’s look at the decorator pattern
Fun string.show () {if(this.isnotempty ()) toastutils.showshort (this)} "hint ".show()Copy the code
Demo address: github.com/hhyq520/Com…