- 新增 BaseDialog 抽象基类,统一对话框样式和行为 - 新增 EnvSwitchDialog 支持 TEST/UAT/PROD 三环境切换 - 修复 BaseDialog 和 EnvSwitchDialog 的包名(com.shuwei.intelligent.shelves → com.sw.platecabinet) - 新增 View.hideKeyboard() 扩展函数到 CommonExt.kt - 重构 GlobalData 环境 URL 常量命名(TEST_URL/PRO_URL → TEST_BASE_URL/UAT_BASE_URL/PROD_BASE_URL) - MyApp 启动时从 SpTool 读取已保存的环境配置 - SpTool 新增 baseUrl 持久化支持 - 修正 drawable 资源颜色引用为硬编码值 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
70 lines
2.4 KiB
Kotlin
70 lines
2.4 KiB
Kotlin
package com.sw.platecabinet.dialog
|
|
|
|
import android.content.Context
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.WindowManager
|
|
import com.sw.plate.utils.ToastUtils
|
|
import com.sw.platecabinet.GlobalData
|
|
import com.sw.platecabinet.databinding.DialogEnvSwitchBinding
|
|
import com.sw.platecabinet.ext.dp
|
|
import com.sw.platecabinet.utils.SpTool
|
|
|
|
/**
|
|
* 环境切换弹窗
|
|
*
|
|
* 提供 TEST / UAT / PROD 三套环境的切换功能,
|
|
* 初始化时自动根据 [GlobalData.appBaseUrl] 选中当前环境,
|
|
* 确认后更新 [GlobalData.appBaseUrl] 并通过 [onEnvChanged] 回调通知外部。
|
|
*
|
|
* @param context 上下文
|
|
* @param onEnvChanged 确认切换后的回调,参数为新的 baseUrl
|
|
*/
|
|
class EnvSwitchDialog(
|
|
context: Context,
|
|
private val onEnvChanged: (newBaseUrl: String) -> Unit = {}
|
|
) : BaseDialog(
|
|
context,
|
|
defWidth = 600.dp,
|
|
defHeight = WindowManager.LayoutParams.WRAP_CONTENT
|
|
) {
|
|
|
|
private lateinit var binding: DialogEnvSwitchBinding
|
|
|
|
override fun getRootView(): View {
|
|
binding = DialogEnvSwitchBinding.inflate(LayoutInflater.from(context))
|
|
return binding.root
|
|
}
|
|
|
|
override fun initView() {
|
|
// 根据当前 appBaseUrl 预选对应 RadioButton,不匹配则不选
|
|
when (GlobalData.appBaseUrl) {
|
|
GlobalData.TEST_BASE_URL -> binding.rbTest.isChecked = true
|
|
GlobalData.UAT_BASE_URL -> binding.rbUat.isChecked = true
|
|
GlobalData.PROD_BASE_URL -> binding.rbProd.isChecked = true
|
|
}
|
|
|
|
// 取消按钮
|
|
binding.btnCancel.setOnClickListener { dismiss() }
|
|
|
|
// 确认按钮
|
|
binding.btnConfirm.setOnClickListener {
|
|
// 未选中任何选项时提示用户
|
|
if (binding.rgEnv.checkedRadioButtonId == -1) {
|
|
ToastUtils.showToast("请选择环境")
|
|
return@setOnClickListener
|
|
}
|
|
val newUrl = when (binding.rgEnv.checkedRadioButtonId) {
|
|
binding.rbTest.id -> GlobalData.TEST_BASE_URL
|
|
binding.rbUat.id -> GlobalData.UAT_BASE_URL
|
|
binding.rbProd.id -> GlobalData.PROD_BASE_URL
|
|
else -> return@setOnClickListener
|
|
}
|
|
GlobalData.appBaseUrl = newUrl
|
|
SpTool.baseUrl = newUrl
|
|
onEnvChanged(newUrl)
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|