[m2] 优化电量回调处理逻辑,减少频繁切换主线程的操作

This commit is contained in:
renwj
2023-03-15 17:45:23 +08:00
parent 90acff0056
commit c11e456401

View File

@@ -1,5 +1,6 @@
package com.mogo.och.bus.passenger.ui.widget
import android.annotation.*
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
@@ -14,9 +15,10 @@ import com.mogo.eagle.core.function.call.autopilot.CallerBatteryManagementSystem
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsManager
import com.mogo.eagle.core.function.call.hmi.CallerHmiViewControlListenerManager
import com.mogo.eagle.core.function.call.setting.CallerSkinModeListenerManager
import com.mogo.eagle.core.utilcode.util.ThreadUtils
import com.mogo.eagle.core.utilcode.kotlin.*
import com.mogo.och.bus.passenger.R
import kotlinx.android.synthetic.m2.p_m2_view_status_bar.view.*
import kotlinx.coroutines.*
import me.jessyan.autosize.utils.AutoSizeUtils
/**
@@ -32,11 +34,16 @@ class M2StatusBarView @JvmOverloads constructor(
const val TAG = "M2StatusBarView"
}
@Volatile
private var oldBmsSoc: Float = -1.0f
init {
LayoutInflater.from(context).inflate(R.layout.p_m2_view_status_bar, this, true)
setBackgroundColor(Color.parseColor("#80FFFFFF"))
}
@SuppressLint("ClickableViewAccessibility")
override fun onAttachedToWindow() {
super.onAttachedToWindow()
post {
@@ -51,8 +58,12 @@ class M2StatusBarView @JvmOverloads constructor(
//电量
CallerBatteryManagementSystemListenerManager.addListener(TAG,this)
progress.progress = 50
tv_power_cos.text = "50%"
progress?.also {
it.progress = 50
}
tv_power_cos?.also {
it.text = "50%"
}
}
override fun onSkinModeChange(skinMode: Int) {
@@ -69,19 +80,24 @@ class M2StatusBarView @JvmOverloads constructor(
CallerDevaToolsManager.hideStatusBar()
}
@SuppressLint("SetTextI18n")
override fun onBatteryManagementSystemStates(states: ChassisStatesOuterClass.BMSSystemStates) {
ThreadUtils.runOnUiThread {
val bmsSoc = states.bmsSoc
if(bmsSoc >1){
progress.progress = bmsSoc.toInt()
tv_power_cos.text = "${bmsSoc.toInt()}%"
}else{
val currenPower = (bmsSoc * 100).toInt()
progress.progress = currenPower
tv_power_cos.text = "$currenPower%"
val bmsSoc = states.bmsSoc
try {
if (oldBmsSoc != bmsSoc ) {
scope.launch {
if(bmsSoc >1){
progress?.also { it.progress = bmsSoc.toInt() }
tv_power_cos?.also { it.text = "${bmsSoc.toInt()}%" }
}else{
val power = (bmsSoc * 100).toInt()
progress?.also { it.progress = power }
tv_power_cos?.also {it.text = "$power%" }
}
}
}
} finally {
oldBmsSoc = bmsSoc
}
}
}