refactor(dialog): 优化 CommonDialog 代码质量

- 修复 setPositiveButton 中 lambda 隐式参数 it 被遮蔽的问题
- 提取 isMaster 为局部变量,消除重复计算
- addContentView 挂载前先 removeAllViews(),防止重复添加
This commit is contained in:
2026-05-15 11:56:50 +08:00
parent d3f02620af
commit ada83b64e6
@@ -53,7 +53,7 @@ open class CommonDialog(
/** 设置右侧确认按钮文字及点击回调(3按钮模式下对应底部按钮),点击后自动关闭弹窗 */
fun setPositiveButton(text: String, onClick: (() -> Unit)? = null): CommonDialog = apply {
positiveText = text
positiveClick = onClick?.let { { it(); true } }
positiveClick = onClick?.let { cb -> { cb(); true } }
}
/**
@@ -99,9 +99,9 @@ open class CommonDialog(
setContentView(binding.root)
// 弹窗不允许点击外部关闭
setCancelable(false)
val isMaster = GlobalData.deviceRole == DeviceRole.MASTER
window?.run {
val screenWidth = context.resources.displayMetrics.widthPixels
val isMaster = GlobalData.deviceRole == DeviceRole.MASTER
attributes = attributes.apply {
width = if (isMaster) (screenWidth * 0.72f).toInt() else (screenWidth * 0.60f).toInt()
}
@@ -111,7 +111,7 @@ open class CommonDialog(
setWindowAnimations(R.style.CommonDialogAnimation)
}
// 主/从设备边距微调:主设备内容区域更大,适当增加上下留白
val verticalMargin = if (GlobalData.deviceRole == DeviceRole.MASTER) 48.dp else 36.dp
val verticalMargin = if (isMaster) 48.dp else 36.dp
binding.tvTitle.updateLayoutParams<LinearLayout.LayoutParams> { topMargin = verticalMargin }
binding.tvContent.updateLayoutParams<LinearLayout.LayoutParams> { bottomMargin = verticalMargin }
@@ -148,6 +148,7 @@ open class CommonDialog(
// 挂载自定义内容 View,并执行 tvContent 的灵活控制回调
pendingContentView?.let {
binding.llContent.removeAllViews()
binding.llContent.addView(it)
binding.llContent.visibility = View.VISIBLE
tvContentConfigurator?.invoke(binding.tvContent)