diff --git a/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/operate/preferences/PreferenceWithSpeedSetting.kt b/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/operate/preferences/PreferenceWithSpeedSetting.kt index c5bd5d3df5..f990d1e229 100644 --- a/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/operate/preferences/PreferenceWithSpeedSetting.kt +++ b/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/operate/preferences/PreferenceWithSpeedSetting.kt @@ -2,23 +2,18 @@ package com.mogo.eagle.core.function.hmi.ui.operate.preferences import android.annotation.SuppressLint import android.content.Context -import android.text.Editable import android.util.AttributeSet import android.widget.EditText import android.widget.TextView import androidx.core.widget.doAfterTextChanged -import androidx.core.widget.doBeforeTextChanged -import androidx.core.widget.doOnTextChanged import androidx.preference.Preference import androidx.preference.PreferenceViewHolder import com.mogo.eagle.core.function.hmi.R import com.mogo.eagle.core.utilcode.kotlin.onClick import com.mogo.eagle.core.utilcode.kotlin.onClickWidthDuration -import com.mogo.eagle.core.utilcode.mogo.logger.Logger import com.mogo.eagle.core.utilcode.util.ToastUtils import java.math.BigDecimal import java.util.concurrent.ConcurrentHashMap -import kotlin.math.max class PreferenceWithSpeedSetting : Preference { @@ -74,7 +69,7 @@ class PreferenceWithSpeedSetting : Preference { if (cache.containsKey(key)) { cur = cache[key] ?: cur } - cur = BigDecimal.valueOf(cur).setScale(1).toDouble() + cur = cur.safeScale() if (speedLimit is EditText) { speedLimit.setText(cur.toString()) speedLimit.doAfterTextChanged { text -> @@ -89,14 +84,13 @@ class PreferenceWithSpeedSetting : Preference { btnOk.isEnabled = false holder.itemView.isEnabled = false ll.isSelected = false - ToastUtils.showShort("阈值最大为${BigDecimal.valueOf(max).setScale(1)}$unit") + ToastUtils.showShort("阈值最大为${max.safeScale()}$unit") return@doAfterTextChanged } - val temp = BigDecimal.valueOf(current).setScale(1).toDouble() + val temp = current.safeScale() if (cur == temp) { return@doAfterTextChanged } - Logger.d("RWJ", "--- cache ---- 2 ---: $cur, $temp") cur = temp btnOk.isEnabled = true ll.isSelected = true @@ -115,7 +109,7 @@ class PreferenceWithSpeedSetting : Preference { minus.onClickWidthDuration(100) { val minusAfter = cur - step if (minusAfter < min) { - ToastUtils.showShort("阈值最小为${BigDecimal.valueOf(min).setScale(1)}$unit") + ToastUtils.showShort("阈值最小为${min.safeScale()}$unit") return@onClickWidthDuration } cur = minusAfter @@ -135,7 +129,7 @@ class PreferenceWithSpeedSetting : Preference { add.onClickWidthDuration(100) { val addAfter = cur + step if (addAfter > max) { - ToastUtils.showShort("阈值最大为${BigDecimal.valueOf(max).setScale(1)}$unit") + ToastUtils.showShort("阈值最大为${max.safeScale()}$unit") return@onClickWidthDuration } holder.itemView.isEnabled = false @@ -159,4 +153,8 @@ class PreferenceWithSpeedSetting : Preference { } } } + + private fun Double.safeScale(s: Int = 1): Double { + return runCatching { BigDecimal.valueOf(this).setScale(s).toDouble() }.getOrNull() ?: this + } } \ No newline at end of file