feat(dialog): 添加环境切换对话框并修复包名引用
- 新增 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>
This commit is contained in:
@@ -34,10 +34,13 @@ object GlobalData {
|
||||
* 具体业务baseurl
|
||||
*/
|
||||
var appBaseUrl: String = ""
|
||||
//测试环境
|
||||
const val TEST_URL: String = "http://192.168.1.201:14801"
|
||||
//生产环境
|
||||
const val PRO_URL: String = "https://dev.yixiong-tech.com:8081"
|
||||
|
||||
/**
|
||||
* 具体业务 BaseUrl
|
||||
*/
|
||||
const val TEST_BASE_URL: String = "http://192.168.1.201:14801"
|
||||
const val UAT_BASE_URL: String = "https://dev.yixiong-tech.com:8081"
|
||||
const val PROD_BASE_URL: String = "https://api.dm.yixiong-tech.com:8443"
|
||||
|
||||
/**
|
||||
* 设备id
|
||||
@@ -65,4 +68,5 @@ object GlobalKey {
|
||||
const val KEY_FIRST_RUN = "firstRun"
|
||||
const val KEY_USER_INFO = "userInfoKey"
|
||||
const val PARAM_EQUIPMENT_INFO = "equipmentUserInfo"
|
||||
const val KEY_BASE_URL = "baseUrlKey"
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import android.util.Log
|
||||
import com.sw.plate.App
|
||||
import com.sw.plate.utils.AppUtil
|
||||
import com.sw.platecabinet.utils.CrashHandler
|
||||
import com.sw.platecabinet.utils.SpTool
|
||||
import timber.log.Timber
|
||||
|
||||
class MyApp : App() {
|
||||
@@ -35,7 +36,16 @@ class MyApp : App() {
|
||||
Log.d("MyApp", "initialize: deviceId=$deviceId")
|
||||
GlobalData.deviceId = deviceId
|
||||
|
||||
GlobalData.appBaseUrl = if ("14d2dad5-7d33-3ced-8a59-f14cb1ef2d21" == deviceId) GlobalData.TEST_URL else GlobalData.PRO_URL
|
||||
val url = SpTool.baseUrl
|
||||
if (url.isNotBlank()) {
|
||||
GlobalData.appBaseUrl = url
|
||||
} else {
|
||||
GlobalData.appBaseUrl =
|
||||
if ("14d2dad5-7d33-3ced-8a59-f14cb1ef2d21" == deviceId)
|
||||
GlobalData.TEST_BASE_URL
|
||||
else
|
||||
GlobalData.PROD_BASE_URL
|
||||
}
|
||||
|
||||
// TODO: 临时测试----------------
|
||||
// if ("2987f0c5-5754-33e9-b00a-251db5e2e55f" == deviceId) {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.sw.platecabinet.dialog
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import com.sw.platecabinet.R
|
||||
import com.sw.platecabinet.ext.dp
|
||||
import com.sw.platecabinet.ext.hideKeyboard
|
||||
|
||||
abstract class BaseDialog(
|
||||
context: Context,
|
||||
var defWidth: Int = 400.dp,
|
||||
var defHeight: Int = 300.dp
|
||||
) : Dialog(context, R.style.MyDialog) {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val rootView = getRootView()
|
||||
setContentView(rootView)
|
||||
setCancelable(false)
|
||||
setCanceledOnTouchOutside(false)
|
||||
window?.apply {
|
||||
setLayout(defWidth, defHeight)
|
||||
setBackgroundDrawable(ColorDrawable())
|
||||
setGravity(Gravity.CENTER)
|
||||
}
|
||||
rootView.setOnClickListener { it.hideKeyboard() }
|
||||
initView()
|
||||
}
|
||||
|
||||
abstract fun getRootView(): View
|
||||
|
||||
abstract fun initView()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.sw.platecabinet.ext
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Resources
|
||||
import android.util.TypedValue
|
||||
import android.view.View
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import androidx.annotation.Dimension
|
||||
|
||||
/**
|
||||
@@ -63,4 +65,12 @@ fun View.invisible() {
|
||||
|
||||
fun View.gone() {
|
||||
visibility = View.GONE
|
||||
}
|
||||
|
||||
/**
|
||||
* 隐藏软键盘
|
||||
*/
|
||||
fun View.hideKeyboard() {
|
||||
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
imm.hideSoftInputFromWindow(windowToken, 0)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.sw.platecabinet.utils
|
||||
|
||||
import com.sw.inbound.utils.SPUtil
|
||||
import com.sw.inbound.utils.SPUtil.Companion.getInstance
|
||||
import com.sw.platecabinet.GlobalKey
|
||||
import com.sw.platecabinet.MyApp
|
||||
|
||||
object SpTool {
|
||||
@@ -45,4 +46,10 @@ object SpTool {
|
||||
spUtil?.boolean(IS_FIRST_GET_FACE, value)
|
||||
}
|
||||
|
||||
var baseUrl: String
|
||||
get() = spUtil?.get(GlobalKey.KEY_BASE_URL, "") ?: ""
|
||||
set(value) {
|
||||
spUtil?.put(GlobalKey.KEY_BASE_URL, value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="#30000000">
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
|
||||
<solid android:color="@color/white" />
|
||||
<corners android:radius="8dp" />
|
||||
<stroke
|
||||
android:width="1dp"
|
||||
android:color="#5C77F7" />
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="#30000000">
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
|
||||
<solid android:color="#5C77F7" />
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@color/white"/>
|
||||
<corners android:radius="12dp"/>
|
||||
</shape>
|
||||
@@ -0,0 +1,106 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="600dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/bg_white_radius12"
|
||||
android:orientation="vertical"
|
||||
android:paddingHorizontal="40dp"
|
||||
android:paddingVertical="36dp">
|
||||
|
||||
<!-- 标题 -->
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:text="环境切换"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="36sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<!-- 分割线 -->
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="2dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:background="#FFDCDCF0" />
|
||||
|
||||
<!-- 环境选项 -->
|
||||
<RadioGroup
|
||||
android:id="@+id/rgEnv"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="32dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbTest"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="72dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:text="测试环境"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbUat"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="72dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:text="UAT 环境"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbProd"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="72dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:text="生产环境"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
<!-- 分割线 -->
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="2dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:background="#FFDCDCF0" />
|
||||
|
||||
<!-- 底部按钮 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="88dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:gravity="center"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/btnCancel"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_weight="1"
|
||||
android:background="@drawable/bg_cancel"
|
||||
android:text="取消"
|
||||
android:textColor="#5C77F7"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/btnConfirm"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_weight="1"
|
||||
android:background="@drawable/bg_confirm"
|
||||
android:text="确认"
|
||||
android:textColor="#FFFFFF"
|
||||
android:textSize="28sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user