Skip to content

Commit

Permalink
fix #8: Use new location change event
Browse files Browse the repository at this point in the history
  • Loading branch information
timklge committed Jan 28, 2025
1 parent 0503202 commit f2b652e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
12 changes: 12 additions & 0 deletions app/src/main/kotlin/de/timklge/karooheadwind/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
20 changes: 3 additions & 17 deletions app/src/main/kotlin/de/timklge/karooheadwind/HeadingFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,11 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow<GpsCoordinat

val initialFlow = flow {
val lastKnownPosition = context.getLastKnownPosition()
if (lastKnownPosition != null) 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
}
}
emit(lastKnownPosition)
}

val gpsFlow = streamLocation().map { GpsCoordinates(it.lat, it.lng, it.orientation) }
val concatenatedFlow = concatenate(initialFlow, gpsFlow)

return concatenatedFlow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2b652e

Please sign in to comment.