From b809a48631bc21ff4a7690bf06b546f84f17f0f4 Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:16:44 +0200 Subject: [PATCH] Only update position if the estimated accuracy is within 500 meters (#172) --- .../de/timklge/karooheadwind/Extensions.kt | 13 ----------- .../de/timklge/karooheadwind/HeadingFlow.kt | 23 +++++++++++++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt b/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt index 0ebbeb5..96a6277 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt @@ -2,7 +2,6 @@ package de.timklge.karooheadwind import io.hammerhead.karooext.KarooSystemService import io.hammerhead.karooext.models.ActiveRidePage -import io.hammerhead.karooext.models.OnLocationChanged import io.hammerhead.karooext.models.OnNavigationState import io.hammerhead.karooext.models.OnStreamState import io.hammerhead.karooext.models.RideState @@ -14,7 +13,6 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.transform fun KarooSystemService.streamDataFlow(dataTypeId: String): Flow { @@ -28,17 +26,6 @@ fun KarooSystemService.streamDataFlow(dataTypeId: String): Flow { } } -fun KarooSystemService.streamLocation(): Flow { - return callbackFlow { - val listenerId = addConsumer { event: OnLocationChanged -> - trySendBlocking(event) - } - awaitClose { - removeConsumer(listenerId) - } - } -} - fun KarooSystemService.streamNavigationState(): Flow { return callbackFlow { val listenerId = addConsumer { event: OnNavigationState -> diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt b/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt index 057b3d9..1ba1cd9 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt @@ -12,11 +12,11 @@ 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.firstOrNull import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.mapNotNull sealed class HeadingResponse { data object NoGps: HeadingResponse() @@ -108,8 +108,9 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow + val lat = dataPoint.dataPoint.values[DataType.Field.LOC_LATITUDE] + val lng = dataPoint.dataPoint.values[DataType.Field.LOC_LONGITUDE] + val orientation = dataPoint.dataPoint.values[DataType.Field.LOC_BEARING] + val accuracy = dataPoint.dataPoint.values[DataType.Field.LOC_ACCURACY] + + Log.i(KarooHeadwindExtension.TAG, "Received GPS update: lat=$lat, lng=$lng, accuracy=$accuracy, orientation=$orientation") + + if (lat != null && lng != null && accuracy != null && accuracy < 500) { + GpsCoordinates(lat, lng, orientation) + } else { + null + } + } val concatenatedFlow = concatenate(initialFlow, gpsFlow)