From 086912117696695976c6e20e62896171ff5de417 Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Thu, 30 Jan 2025 18:34:06 +0100 Subject: [PATCH] fix #8: Use new location change event (#31) * fix #8: Use new location change event * Skip location values without orientation, remove averaging --- app/build.gradle.kts | 4 +- app/manifest.json | 6 +-- .../de/timklge/karooheadwind/Extensions.kt | 12 ++++++ .../de/timklge/karooheadwind/HeadingFlow.kt | 42 +++---------------- .../karooheadwind/KarooHeadwindExtension.kt | 2 +- gradle/libs.versions.toml | 2 +- 6 files changed, 25 insertions(+), 43 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f587fed..1769ebf 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -15,8 +15,8 @@ android { applicationId = "de.timklge.karooheadwind" minSdk = 26 targetSdk = 35 - versionCode = 9 - versionName = "1.2.1" + versionCode = 10 + versionName = "1.2.2" } signingConfigs { diff --git a/app/manifest.json b/app/manifest.json index 4c8f8c9..cf409ee 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -3,9 +3,9 @@ "packageName": "de.timklge.karooheadwind", "iconUrl": "https://github.com/timklge/karoo-headwind/releases/latest/download/karoo-headwind.png", "latestApkUrl": "https://github.com/timklge/karoo-headwind/releases/latest/download/app-release.apk", - "latestVersion": "1.2.1", - "latestVersionCode": 9, + "latestVersion": "1.2.2", + "latestVersionCode": 10, "developer": "timklge", "description": "Provides headwind direction, wind speed and other weather data fields", - "releaseNotes": "Add tailwind and ride speed data field, reduce font size in tailwind indicators" + "releaseNotes": "Reduce font size in tailwind indicators, update gps bearing retrieval" } \ No newline at end of file diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt b/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt index e030229..c06df2a 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt @@ -32,6 +32,7 @@ import io.hammerhead.karooext.KarooSystemService import io.hammerhead.karooext.models.DataType import io.hammerhead.karooext.models.HttpResponseState import io.hammerhead.karooext.models.OnHttpResponse +import io.hammerhead.karooext.models.OnLocationChanged import io.hammerhead.karooext.models.OnStreamState import io.hammerhead.karooext.models.StreamState import io.hammerhead.karooext.models.UserProfile @@ -72,6 +73,17 @@ fun KarooSystemService.streamDataFlow(dataTypeId: String): Flow { } } +fun KarooSystemService.streamLocation(): Flow { + return callbackFlow { + val listenerId = addConsumer { event: OnLocationChanged -> + trySendBlocking(event) + } + awaitClose { + removeConsumer(listenerId) + } + } +} + fun Flow.throttle(timeout: Long): Flow = flow { var lastEmissionTime = 0L diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt b/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt index 7855fda..3740a84 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map @@ -57,26 +58,6 @@ fun KarooSystemService.getHeadingFlow(context: Context): Flow { headingValue ?: HeadingResponse.NoGps } .distinctUntilChanged() - .scan(emptyList()) { acc, value -> /* Average over 3 values */ - if (value !is HeadingResponse.Value) return@scan listOf(value) - - val newAcc = acc + value - if (newAcc.size > 3) newAcc.drop(1) else newAcc - } - .map { data -> - Log.i(KarooHeadwindExtension.TAG, "Heading value: $data") - - if (data.isEmpty()) return@map HeadingResponse.NoGps - if (data.firstOrNull() !is HeadingResponse.Value) return@map data.first() - - val avgValues = data.mapNotNull { (it as? HeadingResponse.Value)?.diff } - - if (avgValues.isEmpty()) return@map HeadingResponse.NoGps - - val avg = avgValues.average() - - HeadingResponse.Value(avg) - } } fun concatenate(vararg flows: Flow) = flow { @@ -115,24 +96,13 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow - val lat = values?.get(DataType.Field.LOC_LATITUDE) - val lon = values?.get(DataType.Field.LOC_LONGITUDE) - val bearing = values?.get(DataType.Field.LOC_BEARING) - - if (lat != null && lon != null){ - // Log.d(KarooHeadwindExtension.TAG, "Updated gps coordinates: $lat $lon") - GpsCoordinates(lat, lon, bearing) - } else { - // Log.w(KarooHeadwindExtension.TAG, "Gps unavailable: $values") - null - } - } + val gpsFlow = streamLocation() + .filter { it.orientation != null } + .map { GpsCoordinates(it.lat, it.lng, it.orientation) } val concatenatedFlow = concatenate(initialFlow, gpsFlow) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt b/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt index f178061..e2cd036 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt @@ -41,7 +41,7 @@ import kotlin.math.absoluteValue import kotlin.time.Duration.Companion.hours import kotlin.time.Duration.Companion.minutes -class KarooHeadwindExtension : KarooExtension("karoo-headwind", "1.2.1") { +class KarooHeadwindExtension : KarooExtension("karoo-headwind", "1.2.2") { companion object { const val TAG = "karoo-headwind" } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a602d0d..4996e51 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,7 +19,7 @@ compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = " [libraries] androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" } -hammerhead-karoo-ext = { group = "io.hammerhead", name = "karoo-ext", version = "1.1.2" } +hammerhead-karoo-ext = { group = "io.hammerhead", name = "karoo-ext", version = "1.1.3" } androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" } # compose