riku
2020-10-10 5771916ffb24807dd57c1969baa6371611c334da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cn.flightfeather.thirdapp.util
 
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.support.annotation.LayoutRes
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import cn.flightfeather.thirdapp.R
import cn.flightfeather.thirdapp.module.base.BaseActivity
import cn.flightfeather.thirdapp.view.PopupWindowWithMask
 
/**
 * @author riku
 * Date: 2020/5/14
 */
object DialogUtil2 {
 
    fun showAlertDialog(
            context: Context?,
            content: String,
            onConfirmed: (dialog: Dialog) -> Unit ={ it.dismiss()},
            onCanceled: (dialog: Dialog) -> Unit = {it.dismiss() }
    ): Dialog? {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_alert_2, null)
        return context?.let {
            Dialog(it).apply {
                setCancelable(true)
                setCanceledOnTouchOutside(true)
                setContentView(view)
                view.findViewById<TextView>(R.id.tv_dialog_content).text = content
                view.findViewById<TextView>(R.id.tv_positive).setOnClickListener { onConfirmed(this) }
                view.findViewById<TextView>(R.id.tv_neutral).setOnClickListener { onCanceled(this) }
                window?.attributes?.apply {
                    gravity = Gravity.CENTER
                    width = (ScreenUtils.getScreenWidth(context) * 0.8).toInt()
                    windowAnimations = R.style.dialog_anim_style
                }
                window?.setBackgroundDrawableResource(android.R.color.transparent)
                window?.setGravity(Gravity.CENTER)
                show()
            }
        }
    }
 
    fun showBottomDialog(
            activity: Activity?,
            anchorView: View,
            textList: List<String>,
            listeners: List<(popupWindow: PopupWindow) -> Unit>,
            onCanceled: (popupWindow: PopupWindow) -> Unit = {}
    ) {
        val view = LayoutInflater.from(activity).inflate(R.layout.dialog_bottom_sheet, null)
        activity?.let {
            PopupWindowWithMask(it).apply popupWindow@{
                isFocusable = true
                isOutsideTouchable = true
                contentView = view
                view.findViewById<TextView>(R.id.txt_choice_1).apply {
                    if (textList.isNotEmpty() && listeners.isNotEmpty()) {
                        text = textList.first()
                        setOnClickListener { listeners.first().invoke(this@popupWindow) }
                    }
                }
                view.findViewById<TextView>(R.id.txt_choice_2).apply {
                    if (textList.size > 1 && listeners.size > 1) {
                        text = textList[1]
                        setOnClickListener { listeners[1].invoke(this@popupWindow) }
                    }
                }
                view.findViewById<TextView>(R.id.txt_cancel).setOnClickListener {
                    onCanceled(this)
                    dismiss()
                }
                height = ViewGroup.LayoutParams.WRAP_CONTENT
                width = ViewGroup.LayoutParams.MATCH_PARENT
                animationStyle = R.style.PopWin_bottom_anim_style
                background.alpha = 0
            }.run {
                showAtLocation(anchorView, Gravity.BOTTOM, 0, 0)
            }
        }
    }
 
    class Builder constructor(private val context: Context){
        var view: View = LayoutInflater.from(context).inflate(R.layout.dialog_alert, null)
        private val dialog = Dialog(context).apply {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
            setContentView(view)
            window?.setBackgroundDrawableResource(android.R.color.transparent)
            window?.attributes?.apply {
                gravity = Gravity.CENTER
                width = (ScreenUtils.getScreenWidth(context) * 0.9).toInt()
            }
            window?.setGravity(Gravity.CENTER)
        }
 
        fun setContentView(@LayoutRes layoutResID: Int) {
            view = LayoutInflater.from(context).inflate(layoutResID, null)
            dialog.setContentView(view)
        }
 
        fun setTitle(t: String) {
            view.findViewById<TextView>(R.id.tv_dialog_title).text = t
        }
 
        fun setContent(c: String) {
            view.findViewById<TextView>(R.id.tv_dialog_content).text = c
        }
 
        fun setPositiveButton(text: String, l: DialogInterface.OnClickListener) {
            view.findViewById<TextView>(R.id.tv_positive).apply {
                this.text = text
                setOnClickListener{
                    l.onClick(dialog, 0)
                }
            }
        }
 
        fun setNegativeButton(text: String, l: DialogInterface.OnClickListener) {
            view.findViewById<TextView>(R.id.tv_negative).apply {
                this.text = text
                setOnClickListener{
                    l.onClick(dialog, 1)
                }
            }
        }
 
        fun setNeutralButton(text: String, l: DialogInterface.OnClickListener) {
            view.findViewById<TextView>(R.id.tv_neutral).apply {
                this.text = text
                setOnClickListener{
                    l.onClick(dialog, 2)
                }
            }
        }
 
        fun create(): Dialog = dialog
    }
}