From 9c76618d19e41079fbeb250807f0319d8027893a Mon Sep 17 00:00:00 2001 From: Tim Kluge Date: Mon, 16 Dec 2024 23:03:22 +0100 Subject: [PATCH] Add setting for text on headwind indicator datafield --- .../datatypes/HeadwindDirectionDataType.kt | 17 +++++++++------- .../karooheadwind/screens/MainScreen.kt | 20 ++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/HeadwindDirectionDataType.kt b/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/HeadwindDirectionDataType.kt index 62649a0..317cd40 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/HeadwindDirectionDataType.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/HeadwindDirectionDataType.kt @@ -7,10 +7,9 @@ import androidx.compose.ui.unit.DpSize import androidx.glance.appwidget.ExperimentalGlanceRemoteViewsApi import androidx.glance.appwidget.GlanceRemoteViews import de.timklge.karooheadwind.KarooHeadwindExtension -import de.timklge.karooheadwind.OpenMeteoCurrentWeatherResponse -import de.timklge.karooheadwind.OpenMeteoData import de.timklge.karooheadwind.getRelativeHeadingFlow import de.timklge.karooheadwind.screens.HeadwindSettings +import de.timklge.karooheadwind.screens.WindDirectionIndicatorTextSetting import de.timklge.karooheadwind.streamCurrentWeatherData import de.timklge.karooheadwind.streamDataFlow import de.timklge.karooheadwind.streamSettings @@ -29,12 +28,10 @@ import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.launch -import kotlin.math.abs import kotlin.math.cos import kotlin.math.roundToInt @@ -106,11 +103,17 @@ class HeadwindDirectionDataType( Log.d(KarooHeadwindExtension.TAG, "Updating headwind direction view") val windSpeed = streamData.windSpeed val windDirection = streamData.value - val headwindSpeed = cos( (windDirection + 180) * Math.PI / 180.0) * windSpeed - val windSpeedText = headwindSpeed.roundToInt().toString() + val text = when (streamData.settings.windDirectionIndicatorTextSetting) { + WindDirectionIndicatorTextSetting.HEADWIND_SPEED -> { + val headwindSpeed = cos( (windDirection + 180) * Math.PI / 180.0) * windSpeed + headwindSpeed.roundToInt().toString() + } + WindDirectionIndicatorTextSetting.WIND_SPEED -> windSpeed.roundToInt().toString() + WindDirectionIndicatorTextSetting.NONE -> "" + } val result = glance.compose(context, DpSize.Unspecified) { - HeadwindDirection(baseBitmap, windDirection.roundToInt(), config.textSize, windSpeedText) + HeadwindDirection(baseBitmap, windDirection.roundToInt(), config.textSize, text) } emitter.updateView(result.remoteViews) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/screens/MainScreen.kt b/app/src/main/kotlin/de/timklge/karooheadwind/screens/MainScreen.kt index 5b182e3..1779251 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/screens/MainScreen.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/screens/MainScreen.kt @@ -60,11 +60,18 @@ enum class PrecipitationUnit(val id: String, val label: String){ INCH("inch", "Inch") } +enum class WindDirectionIndicatorTextSetting(val id: String, val label: String){ + HEADWIND_SPEED("headwind-speed", "Headwind speed"), + WIND_SPEED("absolute-wind-speed", "Absolute wind speed"), + NONE("none", "None") +} + @Serializable data class HeadwindSettings( val windUnit: WindUnit = WindUnit.KILOMETERS_PER_HOUR, val precipitationUnit: PrecipitationUnit = PrecipitationUnit.MILLIMETERS, val welcomeDialogAccepted: Boolean = false, + val windDirectionIndicatorTextSetting: WindDirectionIndicatorTextSetting = WindDirectionIndicatorTextSetting.HEADWIND_SPEED, ){ companion object { val defaultSettings = Json.encodeToString(HeadwindSettings()) @@ -93,6 +100,7 @@ fun MainScreen() { var selectedWindUnit by remember { mutableStateOf(WindUnit.KILOMETERS_PER_HOUR) } var selectedPrecipitationUnit by remember { mutableStateOf(PrecipitationUnit.MILLIMETERS) } var welcomeDialogVisible by remember { mutableStateOf(false) } + var selectedWindDirectionIndicatorTextSetting by remember { mutableStateOf(WindDirectionIndicatorTextSetting.HEADWIND_SPEED) } val stats by ctx.streamStats().collectAsState(HeadwindStats()) val location by karooSystem.getGpsCoordinateFlow().collectAsState(initial = null) @@ -104,6 +112,7 @@ fun MainScreen() { selectedWindUnit = settings.windUnit selectedPrecipitationUnit = settings.precipitationUnit welcomeDialogVisible = !settings.welcomeDialogAccepted + selectedWindDirectionIndicatorTextSetting = settings.windDirectionIndicatorTextSetting } } @@ -138,10 +147,19 @@ fun MainScreen() { selectedPrecipitationUnit = PrecipitationUnit.entries.find { unit -> unit.id == selectedOption.id }!! } + val windDirectionIndicatorTextSettingDropdownOptions = WindDirectionIndicatorTextSetting.entries.toList().map { unit -> DropdownOption(unit.id, unit.label) } + val windDirectionIndicatorTextSettingSelection by remember(selectedWindDirectionIndicatorTextSetting) { + mutableStateOf(windDirectionIndicatorTextSettingDropdownOptions.find { option -> option.id == selectedWindDirectionIndicatorTextSetting.id }!!) + } + Dropdown(label = "Text on headwind indicator", options = windDirectionIndicatorTextSettingDropdownOptions, selected = windDirectionIndicatorTextSettingSelection) { selectedOption -> + selectedWindDirectionIndicatorTextSetting = WindDirectionIndicatorTextSetting.entries.find { unit -> unit.id == selectedOption.id }!! + } + FilledTonalButton(modifier = Modifier .fillMaxWidth() .height(50.dp), onClick = { - val newSettings = HeadwindSettings(windUnit = selectedWindUnit, precipitationUnit = selectedPrecipitationUnit, welcomeDialogAccepted = true) + val newSettings = HeadwindSettings(windUnit = selectedWindUnit, precipitationUnit = selectedPrecipitationUnit, + welcomeDialogAccepted = true, windDirectionIndicatorTextSetting = selectedWindDirectionIndicatorTextSetting) coroutineScope.launch { saveSettings(ctx, newSettings)