mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
fix(ui): Prevent NaN values in environment metrics (#4316)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
847152acd7
commit
50a04a98b8
3 changed files with 18 additions and 8 deletions
|
|
@ -26,6 +26,8 @@ object UnitConversions {
|
|||
|
||||
/** Formats temperature as a string with the unit suffix. */
|
||||
fun Float.toTempString(isFahrenheit: Boolean): String {
|
||||
if (this.isNaN()) return "--"
|
||||
|
||||
val temp = if (isFahrenheit) celsiusToFahrenheit(this) else this
|
||||
val unit = if (isFahrenheit) "F" else "C"
|
||||
|
||||
|
|
|
|||
|
|
@ -77,6 +77,12 @@ class UnitConversionsTest {
|
|||
assertEquals("-40°F", (-40.0f).toTempString(true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toTempString handles NaN`() {
|
||||
assertEquals("--", Float.NaN.toTempString(false))
|
||||
assertEquals("--", Float.NaN.toTempString(true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `celsiusToFahrenheit converts correctly`() {
|
||||
mapOf(
|
||||
|
|
|
|||
|
|
@ -145,15 +145,17 @@ internal fun EnvironmentMetrics(
|
|||
}
|
||||
if (hasTemperature() && hasRelativeHumidity()) {
|
||||
val dewPoint = UnitConversions.calculateDewPoint(temperature, relativeHumidity)
|
||||
add(
|
||||
DrawableMetricInfo(
|
||||
Res.string.dew_point,
|
||||
dewPoint.toTempString(isFahrenheit),
|
||||
org.meshtastic.feature.node.R.drawable.ic_outlined_dew_point_24,
|
||||
),
|
||||
)
|
||||
if (!dewPoint.isNaN()) {
|
||||
add(
|
||||
DrawableMetricInfo(
|
||||
Res.string.dew_point,
|
||||
dewPoint.toTempString(isFahrenheit),
|
||||
org.meshtastic.feature.node.R.drawable.ic_outlined_dew_point_24,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
if (hasSoilTemperature()) {
|
||||
if (hasSoilTemperature() && !soilTemperature.isNaN()) {
|
||||
add(
|
||||
DrawableMetricInfo(
|
||||
Res.string.soil_temperature,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue