fix: Threading & optimizations

This commit is contained in:
Piotr Dec 2024-08-01 22:52:26 +02:00
parent 6d71245d9f
commit f5ec500343
Signed by: stawros
GPG key ID: F89F27AD8F881A91
4 changed files with 142 additions and 142 deletions

View file

@ -2,7 +2,10 @@ package eu.ztsh.garmin.data
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.core.trip.session.NavigationSessionState
import com.mapbox.navigation.tripdata.maneuver.model.Lane
import com.mapbox.navigation.tripdata.maneuver.model.Maneuver
import com.mapbox.navigation.tripdata.maneuver.model.PrimaryManeuver
import com.mapbox.navigation.tripdata.maneuver.model.StepDistance
class DataCache {
@ -11,38 +14,17 @@ class DataCache {
private var locationCache: LocationMatcherResult? = null
private var session: NavigationSessionState? = null
// state
fun hasChanged(lanes: Lanes): Boolean {
return garminManeuver.lanes != lanes
}
fun hasChanged(distance: Distance): Boolean {
return garminManeuver.distance != distance
}
fun hasChanged(direction: Direction): Boolean {
return garminManeuver.direction != direction
}
//
// fun hasChanged(speed: Speed?): Boolean {
// return stateCache.speed == null || stateCache.speed != speed
// }
//
// fun hasChanged(arrival: Arrival?): Boolean {
// return stateCache.arrival == null || stateCache.arrival != arrival
// }
// Merge states
fun update(item: GarminModelItem) {
when(item) {
is GarminManeuver -> garminManeuver.merge(item)
}
}
// maneuver
fun hasChanged(maneuver: Maneuver): Boolean {
return maneuverCache == null || maneuverCache!! != maneuver
fun hasChanged(guidance: Lane?): Boolean {
return guidance != null && maneuverCache.let { it == null || it.laneGuidance != guidance }
}
fun hasChanged(distance: StepDistance): Boolean {
return maneuverCache.let { it == null || it.stepDistance != distance }
}
fun hasChanged(primaryManeuver: PrimaryManeuver): Boolean {
return maneuverCache.let { it == null || it.primary != primaryManeuver }
}
fun update(maneuver: Maneuver) {

View file

@ -6,74 +6,78 @@ import com.mapbox.navigation.core.trip.session.LocationMatcherResult
class MapboxMapper {
companion object {
fun map(maneuver: Maneuver): GarminManeuver {
val state = GarminManeuver()
maneuver.apply {
this.primary.apply {
state.direction = Direction()
when (this.type) {
"roundabout" -> {
state.direction.out = OutType.RightRoundabout
}
"fork" -> state.direction.out = OutType.LongerLane
"arrive" -> {
state.flag = true
}
"turn" -> {
when (this.type) {
"straight" -> state.direction.angle = OutAngle.Straight
}
}
fun asDirection(maneuver: Maneuver): Direction {
val direction = Direction()
maneuver.primary.apply {
when (this.type) {
"roundabout" -> {
direction.out = OutType.RightRoundabout
}
when (this.modifier) {
"right" -> {
when (this.type) {
"turn" -> state.direction.angle = OutAngle.Right
"roundabout" -> {
when (this.degrees) {
137.0 -> state.direction.angle = OutAngle.EasyRight
180.0 -> state.direction.angle = OutAngle.Straight
}
}
"off ramp" -> {
state.direction.angle = OutAngle.EasyRight
state.direction.out = OutType.LongerLane
}
}
}
"left" -> {
when (this.type) {
"turn" -> state.direction.angle = OutAngle.Left
}
"fork" -> direction.out = OutType.LongerLane
"arrive" -> {
// flag = true
}
"turn" -> {
when (this.type) {
"straight" -> direction.angle = OutAngle.Straight
}
}
}
this.stepDistance.apply {
this.distanceRemaining?.apply {
distanceFormatter.formatDistance(this).split(" ").apply {
state.distance = Distance(
this[0].replace(',', '.').toDouble(),
when (this[1]) {
"m" -> Unit.Metres
"km" -> Unit.Kilometres
else -> Unit.Any
when (this.modifier) {
"right" -> {
when (this.type) {
"turn" -> direction.angle = OutAngle.Right
"roundabout" -> {
when (this.degrees) {
137.0 -> direction.angle = OutAngle.EasyRight
180.0 -> direction.angle = OutAngle.Straight
}
)
}
"off ramp" -> {
direction.angle = OutAngle.EasyRight
direction.out = OutType.LongerLane
}
}
}
}
// TODO: implement
state.lanes = Lanes(Arrows(listOf()), Arrows(listOf()))
this.laneGuidance?.apply {
this.allLanes.apply {
println()
"left" -> {
when (this.type) {
"turn" -> direction.angle = OutAngle.Left
"off ramp" -> {
direction.angle = OutAngle.EasyLeft
direction.out = OutType.LongerLane
}
}
}
}
}
return state
return direction
}
fun asDistance(maneuver: Maneuver): Distance {
return maneuver.stepDistance.let { step ->
step.distanceFormatter.formatDistance(step.distanceRemaining!!).split(" ").let {
Distance(
it[0].replace(',', '.').toDouble(),
when (it[1]) {
"m" -> Unit.Metres
"km" -> Unit.Kilometres
else -> Unit.Any
}
)
}
}
}
fun asLanes(maneuver: Maneuver): Lanes {
// TODO: implement
maneuver.laneGuidance?.apply {
this.allLanes.apply {
println()
}
}
return Lanes(Arrows(listOf()), Arrows(listOf()))
}
fun map(locationMatcherResult: LocationMatcherResult): GarminLocation {

View file

@ -89,6 +89,10 @@ class Distance(val distance: Double, val unit: Unit) {
return result
}
override fun toString(): String {
return "Distance($distance$unit)"
}
}
class Speed(val speed: Int, val limit: Int)