fix #8: Use new location change event (#31)

* fix #8: Use new location change event

* Skip location values without orientation, remove averaging
This commit is contained in:
timklge 2025-01-30 18:34:06 +01:00 committed by GitHub
parent d0089e755b
commit 0869121176
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 43 deletions

View File

@ -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 {

View File

@ -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"
}

View File

@ -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<StreamState> {
}
}
fun KarooSystemService.streamLocation(): Flow<OnLocationChanged> {
return callbackFlow {
val listenerId = addConsumer { event: OnLocationChanged ->
trySendBlocking(event)
}
awaitClose {
removeConsumer(listenerId)
}
}
}
fun<T> Flow<T>.throttle(timeout: Long): Flow<T> = flow {
var lastEmissionTime = 0L

View File

@ -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<HeadingResponse> {
headingValue ?: HeadingResponse.NoGps
}
.distinctUntilChanged()
.scan(emptyList<HeadingResponse>()) { 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 <T> concatenate(vararg flows: Flow<T>) = flow {
@ -115,24 +96,13 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow<GpsCoordinat
val initialFlow = flow {
val lastKnownPosition = context.getLastKnownPosition()
if (lastKnownPosition != null) emit(lastKnownPosition)
emit(lastKnownPosition)
}
val gpsFlow = streamDataFlow(DataType.Type.LOCATION)
.map { (it as? StreamState.Streaming)?.dataPoint?.values }
.map { values ->
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)

View File

@ -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"
}

View File

@ -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