refactor(activity): 优化启动页跳转延迟及子设备页面稳定性,修复导航栏闪烁问题
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.shuwei.dish.match.base
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
@@ -43,9 +44,16 @@ open class BaseActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// 提前隐藏系统栏,避免窗口创建时导航栏闪烁
|
||||
hideSystemBars()
|
||||
// 主设备竖屏,子设备横屏
|
||||
requestedOrientation = if (GlobalData.deviceRole == DeviceRole.MASTER)
|
||||
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
||||
else
|
||||
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
||||
ActivityManager.addActivity(this)
|
||||
binding = ActivityBaseBinding.inflate(layoutInflater)
|
||||
if (this !is InitActivity) {
|
||||
if (this !is InitActivity || GlobalData.deviceRole == DeviceRole.SLAVE) {
|
||||
binding.root.setBackgroundColor(ContextCompat.getColor(this, R.color.bg_color))
|
||||
}
|
||||
setContentView(binding.root)
|
||||
|
||||
@@ -54,6 +54,10 @@ object ScaleServiceManager {
|
||||
get() = wsServer?.onConnectionChanged
|
||||
set(value) { wsServer?.onConnectionChanged = value }
|
||||
|
||||
/** 当前是否有主设备连接(仅子设备使用) */
|
||||
val isMasterConnected: Boolean
|
||||
get() = wsServer?.isConnected == true
|
||||
|
||||
/**
|
||||
* 启动所有秤服务,在 Application.onCreate() 中调用
|
||||
*/
|
||||
|
||||
@@ -28,6 +28,10 @@ class ScaleWebSocketServer(private val deviceId: String, private val context: Co
|
||||
/** 主设备连接状态变化回调:true=已连接,false=已断开;在主线程外调用,需自行切换线程 */
|
||||
var onConnectionChanged: ((connected: Boolean) -> Unit)? = null
|
||||
|
||||
/** 当前是否有主设备连接 */
|
||||
val isConnected: Boolean
|
||||
get() = server?.connections?.isNotEmpty() == true
|
||||
|
||||
/** 每个秤地址的最近一次推送时间,用于节流 */
|
||||
private val lastPushTime = mutableMapOf<Int, Long>()
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.shuwei.dish.match.ui
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.os.SystemClock
|
||||
@@ -26,7 +27,6 @@ import com.shuwei.dish.match.utils.ext.startActivity
|
||||
import com.shuwei.dish.match.viewmodel.AppViewModel
|
||||
import com.shuwei.dish.match.viewmodel.factory.AppFactory
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class InitActivity : BaseActivity() {
|
||||
@@ -63,8 +63,9 @@ class InitActivity : BaseActivity() {
|
||||
WeightUtil.startContinuousRead()
|
||||
|
||||
initViews()
|
||||
|
||||
// countDown()
|
||||
val dpi = resources.displayMetrics.density
|
||||
val metrics = resources.displayMetrics.toString()
|
||||
Log.d(TAG, "dpi=$dpi, $metrics")
|
||||
}
|
||||
|
||||
private fun initViewModel() {
|
||||
@@ -74,24 +75,27 @@ class InitActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun startNextPage() {
|
||||
// 子设备:直接进入小屏专属页面,不走原有大屏业务流程
|
||||
if (GlobalData.deviceRole == DeviceRole.SLAVE) {
|
||||
startActivity<SlaveActivity>()
|
||||
return
|
||||
}
|
||||
// 主设备:走原有路由逻辑
|
||||
val launchPageType = SpTool.getInt(SpTool.LAUNCH_PAGE_TYPE, -1)
|
||||
when (launchPageType) {
|
||||
0 -> {
|
||||
startActivity<SelectDishActivity>()
|
||||
lifecycleScope.launch {
|
||||
delay(2000)
|
||||
// 子设备:直接进入小屏专属页面,不走原有大屏业务流程
|
||||
if (GlobalData.deviceRole == DeviceRole.SLAVE) {
|
||||
startActivity<SlaveActivity>()
|
||||
return@launch
|
||||
}
|
||||
// 主设备:走原有路由逻辑
|
||||
val launchPageType = SpTool.getInt(SpTool.LAUNCH_PAGE_TYPE, -1)
|
||||
when (launchPageType) {
|
||||
0 -> {
|
||||
startActivity<SelectDishActivity>()
|
||||
}
|
||||
|
||||
1 -> {
|
||||
goSampling()
|
||||
}
|
||||
1 -> {
|
||||
goSampling()
|
||||
}
|
||||
|
||||
else -> {
|
||||
startActivity<HomeActivity>()
|
||||
else -> {
|
||||
startActivity<HomeActivity>()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,25 +121,6 @@ class InitActivity : BaseActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun countDown() {
|
||||
lifecycleScope.launch {
|
||||
flow {
|
||||
(2 downTo 1).forEach {
|
||||
delay(1000)
|
||||
emit(it)
|
||||
}
|
||||
}.collect {
|
||||
// 倒计时结束执行跳转
|
||||
if (it == 1) {
|
||||
startNextPage()
|
||||
//保留初始化页面,不关闭
|
||||
// finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onDestroy() {
|
||||
WeightUtil.stopContinuousRead()
|
||||
super.onDestroy()
|
||||
@@ -257,7 +242,7 @@ class InitActivity : BaseActivity() {
|
||||
*/
|
||||
private fun openNetworkSettings() {
|
||||
val intent = Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)
|
||||
startActivity(intent)
|
||||
startActivity(intent) {}
|
||||
}
|
||||
|
||||
private fun addBackKeyListener() {
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import com.shuwei.dish.match.base.BaseActivity
|
||||
import com.shuwei.dish.match.base.GlobalData
|
||||
import com.shuwei.dish.match.databinding.ActivitySlaveBinding
|
||||
@@ -44,9 +45,21 @@ class SlaveActivity : BaseActivity() {
|
||||
binding.rvScaleList.itemAnimator = null // 关闭默认动画,避免数据频繁更新时出现闪烁错乱
|
||||
binding.rvScaleList.adapter = adapter
|
||||
|
||||
updateMasterStatus(false)
|
||||
updateMasterStatus(ScaleServiceManager.isMasterConnected)
|
||||
listenLocalScales()
|
||||
listenMasterConnection()
|
||||
addBackKeyListener()
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用返回键,防止退回 InitActivity 后重复打开本页
|
||||
*/
|
||||
private fun addBackKeyListener() {
|
||||
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
// 禁用返回键,不执行任何操作
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.util.Log
|
||||
import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace
|
||||
//import com.aithings.Weigher
|
||||
import com.wabon.wbintelligenthardwaresdk.api.SensorScale
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
typealias WeightCallback = (address: Int, state: Int, weight: Double) -> Unit
|
||||
|
||||
@@ -17,8 +18,7 @@ object WeightUtil {
|
||||
const val STATE_OVER_WEIGHT = SensorScale.STATE_OVER_WEIGHT
|
||||
|
||||
var isConnected = false
|
||||
var weightFuncMap: MutableMap<String, WeightCallback?>? =
|
||||
null
|
||||
val weightFuncMap: ConcurrentHashMap<String, WeightCallback?> = ConcurrentHashMap()
|
||||
|
||||
fun init() {
|
||||
Weigher2.init()
|
||||
@@ -38,7 +38,7 @@ object WeightUtil {
|
||||
}
|
||||
val useWeight = (weight*1000).roundedOneDecimalPlace()
|
||||
Log.d(TAG, "readWeight, address=$address,state=$stateStr, weight=$useWeight")
|
||||
weightFuncMap?.forEach { (key, value) ->
|
||||
weightFuncMap.forEach { (key, value) ->
|
||||
value?.invoke(address, state, useWeight)
|
||||
}
|
||||
|
||||
@@ -82,18 +82,15 @@ object WeightUtil {
|
||||
}
|
||||
}
|
||||
|
||||
fun removeWeightListener(weightKey: String){
|
||||
weightFuncMap?.remove(weightKey)
|
||||
fun removeWeightListener(weightKey: String) {
|
||||
weightFuncMap.remove(weightKey)
|
||||
}
|
||||
|
||||
fun addWeightListener(
|
||||
weightKey: String,
|
||||
getWeight: WeightCallback = { _, _, _ -> }
|
||||
) {
|
||||
if (weightFuncMap == null) {
|
||||
weightFuncMap = mutableMapOf()
|
||||
}
|
||||
weightFuncMap?.put(weightKey, getWeight)
|
||||
weightFuncMap[weightKey] = getWeight
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user