From 489264e73853686d24e66dcb1f447c5d4b9eb59b Mon Sep 17 00:00:00 2001 From: renwj Date: Wed, 23 Oct 2024 17:34:55 +0800 Subject: [PATCH] =?UTF-8?q?[6.7.0][=E8=BF=90=E8=90=A5=E9=9D=A2=E6=9D=BF]?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../preferences/PreferenceWithSpeedSetting.kt | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) 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