fix: 状态上报闪烁修复 + 休眠按钮状态约束
- _latestStatus 改用合并策略 (prev.copy),保留上次报告的非 null 字段, 避免 4 个独立订阅 (Basic/Error/Motion/Device) 交替覆盖导致闪烁 - StatusBar 电量改用独立 batteryStatus StateFlow,不依赖 status?.batteryStatus - 休眠按钮仅非站立状态 (空闲/趴下/阻尼) 可切换,站立/RL 控制时禁用 Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -171,6 +171,7 @@ fun MainScreen(
|
||||
StatusBar(
|
||||
connectionState = connectionState,
|
||||
status = status,
|
||||
batteryStatus = batteryStatus,
|
||||
pingLatencyMs = pingLatencyMs,
|
||||
wifiSignalLevel = wifiSignalLevel,
|
||||
onStatusClick = { statusPanelVisible = true },
|
||||
@@ -255,11 +256,13 @@ fun MainScreen(
|
||||
viewModel.sendLightControl(ledStatus?.front ?: 0, if (backOn) 0 else 1)
|
||||
}
|
||||
}
|
||||
// 休眠/唤醒
|
||||
// 休眠/唤醒 — 仅趴下/空闲等非站立状态可切换
|
||||
val isSleeping = status?.basicStatus?.sleep == 1
|
||||
val canSleep = motionState != ControlCommands.MOTION_STAND &&
|
||||
motionState != ControlCommands.MOTION_RL_CONTROL
|
||||
SleepToggleButton(
|
||||
isSleeping = isSleeping,
|
||||
connected = connectionState == ProtocolClient.State.CONNECTED,
|
||||
connected = connectionState == ProtocolClient.State.CONNECTED && canSleep,
|
||||
onToggle = { viewModel.sendSleepSettings(!isSleeping) }
|
||||
)
|
||||
// 充电/结束充电
|
||||
@@ -307,6 +310,7 @@ fun MainScreen(
|
||||
private fun StatusBar(
|
||||
connectionState: ProtocolClient.State,
|
||||
status: com.example.m20_gamepad.network.models.StatusReport?,
|
||||
batteryStatus: com.example.m20_gamepad.network.models.BatteryStatus?,
|
||||
pingLatencyMs: Long?,
|
||||
wifiSignalLevel: Int,
|
||||
onStatusClick: () -> Unit,
|
||||
@@ -340,7 +344,7 @@ private fun StatusBar(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
status?.batteryStatus?.let {
|
||||
batteryStatus?.let {
|
||||
val avgLevel = (it.batteryLevelLeft + it.batteryLevelRight) / 2
|
||||
StatusChip("电量 ${avgLevel.toInt()}%")
|
||||
}
|
||||
|
||||
@@ -229,7 +229,30 @@ class MainViewModel(
|
||||
|
||||
/** 状态上报分发:更新机器人运动状态机 + 各分项缓存。 */
|
||||
private fun handleStatusReport(report: StatusReport) {
|
||||
_latestStatus.value = report
|
||||
// 合并:保留上次报告中非 null 的字段,避免部分报告覆盖导致 UI 闪烁
|
||||
val prev = _latestStatus.value
|
||||
_latestStatus.value = if (prev != null) {
|
||||
prev.copy(
|
||||
type = report.type,
|
||||
command = report.command,
|
||||
time = report.time,
|
||||
errorCode = report.errorCode ?: prev.errorCode,
|
||||
errorMessage = report.errorMessage ?: prev.errorMessage,
|
||||
basicStatus = report.basicStatus ?: prev.basicStatus,
|
||||
motionStatus = report.motionStatus ?: prev.motionStatus,
|
||||
motorStatus = report.motorStatus ?: prev.motorStatus,
|
||||
batteryList = report.batteryList ?: prev.batteryList,
|
||||
batteryStatus = report.batteryStatus ?: prev.batteryStatus,
|
||||
deviceTemperature = report.deviceTemperature ?: prev.deviceTemperature,
|
||||
led = report.led ?: prev.led,
|
||||
gps = report.gps ?: prev.gps,
|
||||
devEnable = report.devEnable ?: prev.devEnable,
|
||||
cpu = report.cpu ?: prev.cpu,
|
||||
errorList = report.errorList ?: prev.errorList
|
||||
)
|
||||
} else {
|
||||
report
|
||||
}
|
||||
_lastUpdate.value = report.time
|
||||
robot.updateFromReport(report)
|
||||
report.errorList?.let { _errorList.value = it }
|
||||
|
||||
Reference in New Issue
Block a user