From f35ffe52ccd1234607ae9125cb19a54b07e948b1 Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Wed, 11 Jun 2025 19:39:11 +0200 Subject: [PATCH] Fix initial position is only retrieved on position change (#149) --- .../de/timklge/karooheadwind/HeadingFlow.kt | 32 +++++++++++++++++-- .../karooheadwind/KarooHeadwindExtension.kt | 2 +- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt b/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt index a7f3268..057b3d9 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt @@ -5,7 +5,8 @@ import android.util.Log import de.timklge.karooheadwind.datatypes.GpsCoordinates import de.timklge.karooheadwind.util.signedAngleDifference import io.hammerhead.karooext.KarooSystemService -import kotlinx.coroutines.awaitCancellation +import io.hammerhead.karooext.models.DataType +import io.hammerhead.karooext.models.StreamState import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine @@ -13,6 +14,7 @@ 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 @@ -99,7 +101,33 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow + val lat = dataPoint.values[DataType.Field.LOC_LATITUDE] + val lng = dataPoint.values[DataType.Field.LOC_LONGITUDE] + val orientation = dataPoint.values[DataType.Field.LOC_BEARING] + + if (lat != null && lng != null) { + emit(GpsCoordinates(lat, lng, orientation)) + + Log.i(KarooHeadwindExtension.TAG, "No last known position found, fetched initial GPS position") + } else { + emit(null) + + Log.w(KarooHeadwindExtension.TAG, "No last known position found, initial GPS position is unavailable") + } + } ?: run { + emit(null) + + Log.w(KarooHeadwindExtension.TAG, "No last known position found, initial GPS position is unavailable") + } + } else { + emit(lastKnownPosition) + + Log.i(KarooHeadwindExtension.TAG, "Using last known position: $lastKnownPosition") + } } val gpsFlow = streamLocation() diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt b/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt index 2913fbe..31dc8ec 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt @@ -113,7 +113,7 @@ class KarooHeadwindExtension : KarooExtension("karoo-headwind", BuildConfig.VERS old == new } } - .debounce(Duration.ofSeconds(5)) + .throttle(5_000L) var requestedGpsCoordinates: List = emptyList()