From 4dc0eb23647d22e536a0d30619ba3440bc795250 Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Sat, 22 Feb 2025 21:13:48 +0100 Subject: [PATCH] fix #46: Include intermediate directions of intercardinal and cardinal directions in wind direction data field (#47) --- .../datatypes/WindDirectionDataType.kt | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/WindDirectionDataType.kt b/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/WindDirectionDataType.kt index df7184d..20df2d8 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/WindDirectionDataType.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/WindDirectionDataType.kt @@ -30,7 +30,6 @@ import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.launch @@ -40,6 +39,15 @@ class WindDirectionDataType(val karooSystem: KarooSystemService, context: Contex @OptIn(ExperimentalGlanceRemoteViewsApi::class) private val glance = GlanceRemoteViews() + companion object { + private val windDirections = arrayOf( + "N", "NNE", "NE", "ENE", + "E", "ESE", "SE", "SSE", + "S", "SSW", "SW", "WSW", + "W", "WNW", "NW", "NNW" + ) + } + override fun getValue(data: OpenMeteoCurrentWeatherResponse): Double { return data.current.windDirection } @@ -74,18 +82,9 @@ class WindDirectionDataType(val karooSystem: KarooSystemService, context: Contex emitter.updateView(result.remoteViews) } .collect { windBearing -> - val windCardinalDirection = ((windBearing % 360) / 45.0).roundToInt() % 8 - val text = when(windCardinalDirection){ - 0 -> "N" - 1 -> "NE" - 2 -> "E" - 3 -> "SE" - 4 -> "S" - 5 -> "SW" - 6 -> "W" - 7 -> "NW" - else -> "N/A" - } + val windCardinalDirectionIndex = ((windBearing % 360) / 22.5).roundToInt() % 16 + + val text = windDirections[windCardinalDirectionIndex] Log.d( KarooHeadwindExtension.TAG,"Updating wind direction view") val result = glance.compose(context, DpSize.Unspecified) { Box(modifier = GlanceModifier.fillMaxSize(),