NYDialog

/**
 * 当前release 1.0版本
 * 当前dialog支持如下:
 *  底部button支持一到两个,支持横竖向排列,支持热拔插
 *  头部容器支持自定义配置
 *  底部容器支持自定义设置
 *  文本title和文本content支持选择配置
 *  底部按钮支持自定义背景
 *  关闭按钮支持热插拔
 * ----------------------------------
 * beta版本支持如下:
 *  弹窗关闭配置 setCanceledOnTouchOutside
 *  弹窗支持背景多样化处理  纹路
 *  弹窗支持配置标识  标识
 * ----------------------------------
 * beta2版本支持如下:
 *  底部多button(大于两个button)配置
 *  支持button比例分割处理
 *-----------------------------------
 * beta3版本支持如下
 *  支持扩展自定义
 * create by LiYan
 */
class NYDialog(context: Context, config: AbsHintDialogMainInterface?, view: View?) : Dialog(context, R.style.dialog_translucent) {

    init {
        val layout = LayoutInflater.from(context).inflate(R.layout.hint_dialog, null)
        setContentView(view ?: layout)
        config?.init(layout, this, window)
    }

    companion object Builder {
        private var config: AbsHintDialogMainInterface? = null//可定制接口
        private var mTitle: String = ""//标题
        private var mContent: String = ""//文案
        private var mConfirmTxt: String = ""//确定按钮文案
        private var mCancelTxt: String = ""//取消按钮文案
        private var mCanceledOnTouchOutside: Boolean = true//是否需要关闭按钮 默认true
        private var confirmListener: () -> Unit = {}//确定监听
        private var cancelListener: () -> Unit = {}//取消监听
        private var mGravity: Int = Gravity.CENTER//Dialog显示的方向

        //设置Dialog方向
        private var mPaddingEnd: Int = 20
        private var mPaddingStart: Int = 20
        private var mPaddingTop: Int = 0
        private var mPaddingBottom: Int = 0

        //设置Dialog大小
        private var mWidth: Int = WindowManager.LayoutParams.MATCH_PARENT
        private var mHeight: Int = WindowManager.LayoutParams.WRAP_CONTENT

        /*
        * 设置自定义方法
        * 默认可以不写
        * */
        fun Builder(config: AbsHintDialogMainInterface): Builder {
            this.config = config
            return this
        }

        /*
        *设置Title
        * */
        fun title(title: String?): Builder {
            if (title != null) {
                this.mTitle = title
            }
            return this
        }

        /*
        * 设置内容
        * */
        fun content(content: String?): Builder {
            if (content != null) {
                this.mContent = content

            }
            return this
        }

        /*
        * 设置确定按钮文案
        * */
        fun confirmTxt(confirmTxt: String?): Builder {
            if (confirmTxt != null) {
                this.mConfirmTxt = confirmTxt

            }
            return this
        }

        /*
        * 设置方向
        * */
        fun setGravity(gravity: Int) {
            mGravity = gravity
        }

        /*设置取消按钮文案
        * */
        fun cancelTxt(cancelTxt: String?): Builder {
            if (cancelTxt != null) {
                this.mCancelTxt = cancelTxt
            }
            return this
        }

        /**
         * @param confirmLisenter: () -> Unit  确定按钮方法
         * @param cancelLisenter: () -> Unit   取消按钮方法
         * */
        fun click(confirmLisenter: () -> Unit, cancelLisenter: () -> Unit): Builder {
            this.confirmListener = confirmLisenter
            this.cancelListener = cancelLisenter
            return this
        }

        /*
        * 设置是否点击外部可以关闭
        * */
        fun serCancelable(canceledOnTouchOutside: Boolean) {
            this.mCanceledOnTouchOutside = canceledOnTouchOutside
        }

        //设置padding
        fun setNYDialogPadding(left: Int, top: Int, right: Int, bottom: Int) {
            mPaddingEnd = right
            mPaddingStart = left
            mPaddingTop = top
            mPaddingBottom = bottom
        }

        //设置大小
        fun setNYDialogParams(width: Int, height: Int) {
            mWidth = width
            mHeight = height
        }

        /*
        * 创建按钮
        * */
        fun create(context: Context): NYDialog {

            config?.apply {
                inflaterBtnTxt(mConfirmTxt, mCancelTxt)
                inflaterMainTxt(mTitle, mContent)
                setConfirmLisenter(confirmListener)
                setCancelLisenter(cancelListener)
                setNYDialogPadding(mPaddingStart, mPaddingTop, mPaddingEnd, mPaddingBottom)
                setNYDialogParams(mWidth, mHeight)
                setGravity(mGravity)
                //置空缓存解决弹窗缓存其他弹窗数据
                mTitle = ""
                mContent = ""
                mCancelTxt = ""
                mConfirmTxt = ""
                confirmListener = {}
                confirmListener = {}
                mPaddingStart = 20
                mPaddingEnd = 20
                mPaddingTop = 0
                mPaddingBottom = 0
                mWidth = WindowManager.LayoutParams.MATCH_PARENT
                mHeight = WindowManager.LayoutParams.WRAP_CONTENT
                mGravity = Gravity.CENTER
            }
            val nyDialog = NYDialog(context, config, null)
            nyDialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside)//点击外部可关闭
            return nyDialog
        }

    }

    abstract class AbsHintDialogMainInterface : INYDialogMainInterface {
        private var dialogTitle: String = ""
        private var dialogContent: String = ""
        private var dialogConfirmTitle: String = ""
        private var dialogCancelContent: String = ""
        private var confirmLisenter: () -> Unit = {}
        private var cancelLisenter: () -> Unit = {}

        //NY接口扩展
        protected var chirldren: (btnOk: View, btnCanCel: View, clContainer: LinearLayout) -> Unit = { view: View, view1: View, linearLayout: LinearLayout -> }

        //Dialog显示的方向
        private var mGravity: Int = Gravity.CENTER

        //设置底部按钮方向
        private var direct: Int = LinearLayout.VERTICAL

        //设置Dialog方向
        private var mPaddingEnd: Int = 0
        private var mPaddingStart: Int = 0
        private var mPaddingTop: Int = 0
        private var mPaddingBottom: Int = 0

        //设置Dialog大小
        private var mWidth: Int = 0
        private var mHeight: Int = 0

        //初始Dialog
        override fun init(layout: View, nyDialog: NYDialog, window: Window?) {
            window?.let {
                it.attributes.width = mWidth
                it.attributes.height = mHeight
                it.setGravity(Gravity.CENTER)
                it.decorView.setPadding(mPaddingStart, mPaddingTop, mPaddingEnd, mPaddingBottom)
                it.attributes.horizontalMargin = 0f
                it.attributes.verticalMargin = 0f
            }

            //clContainer
            val clContainer = layout.findViewById<LinearLayout>(R.id.clContainer)
            /*view填充*/
            //头部View填充
            val headerDesc = layout.findViewById<FrameLayout>(R.id.rl_header_desc)
            //底部View填充
            val bottomDesc = layout.findViewById<FrameLayout>(R.id.rl_bottom_desc)
            /*内容*/
            //Title
            val tvTitle = layout.findViewById<TextView>(R.id.ivVipTitleDes)
            //content
            val tvContent = layout.findViewById<TextView>(R.id.tvVipDayCome)
            /*按钮*/
            //Ok
            val btnOk = layout.findViewById<TextView>(R.id.btnOnline)
            //Cancel
            val btnCancel = layout.findViewById<TextView>(R.id.btn_random)
            // 关闭按钮
            val ivClose = layout.findViewById<ImageView>(R.id.ivClose)
            //bottom
            val bottomBtns = layout.findViewById<LinearLayout>(R.id.ll_bottom_btns)

            /*按钮监听*/
            btnOk.setOnClickListener {
                confirmLisenter()
                nyDialog.dismiss()

            }
            btnCancel.setOnClickListener {
                cancelLisenter()
                nyDialog.dismiss()
            }
            ivClose.setOnClickListener {
                cancelLisenter()
                nyDialog.dismiss()
            }

            /*是否需要关闭按钮*/
            if (needCloseIcon()) {
                ivClose.visibility = View.VISIBLE
            } else {
                ivClose.visibility = View.GONE
            }

            /*判断是否需要显示按钮*/
            if (provideTitleDesc().isNotEmpty()) tvTitle.text = provideTitleDesc() else tvTitle.visibility = View.GONE
            if (provideContentDesc().isNotEmpty()) tvContent.text = provideContentDesc() else tvContent.visibility = View.GONE

            if (provideConfirmDesc().isNotEmpty()) btnOk.text = provideConfirmDesc() else btnOk.visibility = View.GONE
            if (provideCancelDesc().isNotEmpty()) btnCancel.text = provideCancelDesc() else btnCancel.visibility = View.GONE

            if (!btnOk.isVisible && !btnCancel.isVisible) {
                bottomBtns.visibility = View.GONE
            }
            //设置按钮背景
            provideCancelBtnBg()?.let { btnCancel.setBackgroundResource(it) }//取消
            /*自定义按钮*/
            provideConfirmBtnAndCancel(btnOk, btnCancel)
            /*自定义文案*/
            provideTitleAndContent(tvTitle, tvContent)
            //头部添加View
            inflateDialogHeader()?.let {
                headerDesc.removeAllViews()
                headerDesc.addView(it)
            }
            //底部添加View
            inflateDialogBottom()?.let {
                bottomDesc.removeAllViews()
                bottomDesc.addView(it)
            }
            //按钮方向
            bottomBtns.orientation = provideBottomBtnOrientation()
            chirldren(btnOk, btnCancel, clContainer)

        }


        //设置padding
        fun setNYDialogPadding(left: Int, top: Int, right: Int, bottom: Int) {
            mPaddingEnd = right
            mPaddingStart = left
            mPaddingTop = top
            mPaddingBottom = bottom
        }

        //设置大小
        fun setNYDialogParams(width: Int, height: Int) {
            mWidth = width
            mHeight = height
        }

        //设置在屏幕中的位置
        fun setGravity(gravity: Int) {
            mGravity = gravity
        }

        //Dialog头部 默认为空
        override fun inflateDialogHeader(): View? {
            return null
        }

        //添加底部View 默认为空
        override fun inflateDialogBottom(): View? {
            return null
        }

        //设置底部按钮方向
        override fun provideBottomBtnOrientation(): Int {
            return direct
        }

        //是否需要关闭按钮   方法已过期
        @Deprecated("已移置Build", ReplaceWith("true"))
        override fun needCloseIcon(): Boolean {
            return true
        }

        /**
         *@link # provideConfirmBtnAndCancel()
         * 取消背景 方法过期
         * */
        @Deprecated("已移置 provideConfirmBtnAndCancel()", ReplaceWith("null"))
        open fun provideCancelBtnBg(): Int? {
            return null
        }

        /*文案*/
        //title
        private fun provideTitleDesc(): String {
            return dialogTitle
        }

        //content
        private fun provideContentDesc(): String {
            return dialogContent
        }

        //设置title内容
        fun inflaterMainTxt(vararg args: String) {
            if (args.size == 2) {
                if (args[0].isNotEmpty()) {
                    dialogTitle = args[0]
                }
                if (args[1].isNotEmpty()) {
                    dialogContent = args[1]
                }
            }
        }

        //确定按钮
        private fun provideConfirmDesc(): String {
            return dialogConfirmTitle
        }

        //取消按钮
        private fun provideCancelDesc(): String {
            return dialogCancelContent
        }

        //设置按钮文案
        fun inflaterBtnTxt(vararg args: String) {
            if (args.size == 2) {
                if (args[0].isNotEmpty()) {
                    dialogConfirmTitle = args[0]
                }
                if (args[1].isNotEmpty()) {
                    dialogCancelContent = args[1]
                }
            }
        }

        /*按钮监听*/
        //确定按钮Click
        internal fun setConfirmLisenter(confirmLisenter: () -> Unit) {
            this.confirmLisenter = confirmLisenter
        }

        //取消按钮Click
        internal fun setCancelLisenter(cancelLisenter: () -> Unit) {
            this.cancelLisenter = cancelLisenter
        }
        /*自定义样式*/

        //自定义按钮样式
        override fun provideConfirmBtnAndCancel(confirm: TextView, cancel: TextView) {
        }

        //自定义文案样式
        override fun provideTitleAndContent(title: TextView, content: TextView) {

        }

    }

    /**
     * 主体接口
     */
    private interface INYDialogMainInterface {
        //初始化
        fun init(layout: View, nyDialog: NYDialog, window: Window?)

        /**
         * 头部填充
         */
        fun inflateDialogHeader(): View?

        /**
         * 底部View填充
         */
        fun inflateDialogBottom(): View?

        /**
         * 关闭按钮是否需要
         */
        fun needCloseIcon(): Boolean


        /**
         * confirm/cancel 按钮设置
         */
        fun provideConfirmBtnAndCancel(confirm: TextView, cancel: TextView)

        /*
        * title/content
        * */
        fun provideTitleAndContent(title: TextView, content: TextView)


        /**
         * 底部按钮排列方向
         */
        fun provideBottomBtnOrientation(): Int


    }

    /**
     * 聚合接口
     */
    private interface INYDialogCompactInterface : INYDialogMainInterface {
        fun cancelBtnBg(): Int
        fun confirmBtnBg(): Int
        fun DialogBg(): Int
    }

    /*据和接口实现*/
    abstract class AbsHintDialogCompactInterface : AbsHintDialogMainInterface(), INYDialogCompactInterface {
        private var mCancelBg: Int = R.drawable.common_btn_primary_bg
        private var mConfirmBtnBg: Int = R.drawable.common_btn_primary_bg
        private var mDialogBg: Int = R.drawable.common_dialog_rect_white_bg

        //按钮取消背景颜色
        override fun cancelBtnBg(): Int {
            return mCancelBg
        }

        //确认按钮背景颜色
        override fun confirmBtnBg(): Int {
            return mConfirmBtnBg
        }

        override fun DialogBg(): Int {
            return mDialogBg
        }

        init {
            chirldren = { btnOk: View, btnCanCel: View, dialogBg: LinearLayout ->
                btnOk.setBackgroundResource(mConfirmBtnBg)
                btnCanCel.setBackgroundResource(mCancelBg)
                dialogBg.setBackgroundResource(mConfirmBtnBg)

            }
        }


    }

    /*定制接口
    * */


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,682评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,277评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,083评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,763评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,785评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,624评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,358评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,261评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,722评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,900评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,030评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,737评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,360评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,941评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,057评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,237评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,976评论 2 355

推荐阅读更多精彩内容

  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,536评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,190评论 4 8
  • 怎么对待生活,它也会怎么对你 人都是哭着来到这个美丽的人间。每个人从来到尘寰到升入天堂,整个生命的历程都是一本书,...
    静静在等你阅读 4,976评论 1 6