@@ -43,14 +43,6 @@ import com.highcapable.yukihookapi.annotation.CauseProblemsApi
4343import com.highcapable.yukihookapi.hook.factory.method
4444import com.highcapable.yukihookapi.hook.type.android.LayoutInflaterClass
4545
46- /* *
47- * 构造对话框
48- * @param isUseBlackTheme 是否使用深色主题
49- * @param initiate 对话框方法体
50- */
51- fun Context.showDialog (isUseBlackTheme : Boolean = false, initiate : DialogBuilder .() -> Unit ) =
52- DialogBuilder (context = this , isUseBlackTheme).apply (initiate).show()
53-
5446/* *
5547 * 显示时间选择对话框
5648 * @param timeSet 当前时间 - 不写将使用当前时间格式:HH:mm
@@ -59,20 +51,45 @@ fun Context.showDialog(isUseBlackTheme: Boolean = false, initiate: DialogBuilder
5951fun Context.showTimePicker (timeSet : String = "", result : (String ) -> Unit ) =
6052 TimePickerDialog (this , { _, h, m -> result(" ${h.autoZero} :${m.autoZero} " ) }, timeSet.hour, timeSet.minute, true ).show()
6153
54+ /* *
55+ * 构造 [VB] 自定义 View 对话框
56+ * @param initiate 对话框方法体
57+ */
58+ @JvmName(name = " showDialog-VB" )
59+ inline fun <reified VB : ViewBinding > Context.showDialog (initiate : DialogBuilder <VB >.() -> Unit ) =
60+ DialogBuilder <VB >(context = this , VB ::class .java).apply (initiate).show()
61+
62+ /* *
63+ * 构造对话框
64+ * @param initiate 对话框方法体
65+ */
66+ inline fun Context.showDialog (initiate : DialogBuilder <* >.() -> Unit ) = DialogBuilder <ViewBinding >(context = this ).apply (initiate).show()
67+
6268/* *
6369 * 对话框构造器
6470 * @param context 实例
65- * @param isUseBlackTheme 是否使用深色主题 - 对 AndroidX 风格无效
71+ * @param bindingClass [ViewBinding] 的 [Class] 实例 or null
6672 */
67- class DialogBuilder (val context : Context , private val isUseBlackTheme : Boolean ) {
73+ class DialogBuilder < VB : ViewBinding > (val context : Context , private val bindingClass : Class < * > ? = null ) {
6874
6975 private var instanceAndroidX: androidx.appcompat.app.AlertDialog .Builder ? = null // 实例对象
7076 private var instanceAndroid: android.app.AlertDialog .Builder ? = null // 实例对象
7177
7278 private var dialogInstance: Dialog ? = null // 对话框实例
79+ private var customLayoutView: View ? = null // 自定义布局
7380
74- @CauseProblemsApi
75- var customLayoutView: View ? = null // 自定义布局
81+ /* *
82+ * 获取 [DialogBuilder] 绑定布局对象
83+ * @return [VB]
84+ */
85+ val binding by lazy {
86+ bindingClass?.method {
87+ name = " inflate"
88+ param(LayoutInflaterClass )
89+ }?.get()?.invoke<VB >(LayoutInflater .from(context))?.apply {
90+ customLayoutView = root
91+ } ? : error(" This dialog maybe not a custom view dialog" )
92+ }
7693
7794 /* *
7895 * 是否需要使用 AndroidX 风格对话框
@@ -83,12 +100,7 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
83100 init {
84101 if (isUsingAndroidX)
85102 runInSafe { instanceAndroidX = MaterialAlertDialogBuilder (context) }
86- else runInSafe {
87- instanceAndroid = android.app.AlertDialog .Builder (
88- context,
89- if (isUseBlackTheme) android.R .style.Theme_Material_Dialog else android.R .style.Theme_Material_Light_Dialog
90- )
91- }
103+ else runInSafe { instanceAndroid = android.app.AlertDialog .Builder (context, android.R .style.Theme_Material_Light_Dialog ) }
92104 }
93105
94106 /* * 设置对话框不可关闭 */
@@ -135,18 +147,6 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
135147 else customLayoutView?.findViewWithTag<TextView >(" progressContent" )?.text = value
136148 }
137149
138- /* *
139- * 设置对话框自定义布局
140- * @return [ViewBinding]
141- */
142- inline fun <reified T : ViewBinding > bind () =
143- T ::class .java.method {
144- name = " inflate"
145- param(LayoutInflaterClass )
146- }.get().invoke<T >(LayoutInflater .from(context))?.apply {
147- customLayoutView = root
148- } ? : error(" binding failed" )
149-
150150 /* *
151151 * 设置对话框确定按钮
152152 * @param text 按钮文本内容
@@ -184,7 +184,10 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
184184 fun cancel () = dialogInstance?.cancel()
185185
186186 /* * 显示对话框 */
187- internal fun show () =
187+ @CauseProblemsApi
188+ fun show () {
189+ /* * 若当前自定义 View 的对话框没有调用 [binding] 将会对其手动调用一次以确保显示布局 */
190+ if (bindingClass != null ) binding
188191 if (isUsingAndroidX) runInSafe {
189192 instanceAndroidX?.create()?.apply {
190193 customLayoutView?.let { setView(it) }
@@ -196,8 +199,7 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
196199 window?.setBackgroundDrawable(
197200 GradientDrawable (
198201 GradientDrawable .Orientation .TOP_BOTTOM ,
199- if (isUseBlackTheme) intArrayOf(0xFF2D2D2D .toInt(), 0xFF2D2D2D .toInt())
200- else intArrayOf(Color .WHITE , Color .WHITE )
202+ intArrayOf(Color .WHITE , Color .WHITE )
201203 ).apply {
202204 shape = GradientDrawable .RECTANGLE
203205 gradientType = GradientDrawable .LINEAR_GRADIENT
@@ -206,4 +208,5 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
206208 dialogInstance = this
207209 }?.show()
208210 }
211+ }
209212}
0 commit comments