Data cache

This commit is contained in:
Piotr Dec 2023-08-25 08:25:06 +02:00
parent 89e16fb805
commit b5cc580cf8
No known key found for this signature in database
GPG key ID: ABD6CB83D6110D27
5 changed files with 178 additions and 40 deletions

View file

@ -0,0 +1,66 @@
package eu.ztsh.garmin.data
import com.mapbox.navigation.ui.maneuver.model.Maneuver
class DataCache {
private val stateCache: State = State()
private var maneuverCache: Maneuver? = null
// state
fun hasChanged(lanes: Lanes?): Boolean {
return stateCache.lineArrows == null || stateCache.lineArrows != lanes
}
fun hasChanged(outlines: Outlines?): Boolean {
return stateCache.lineOutlines == null || stateCache.lineOutlines != outlines
}
fun hasChanged(distance: Distance?): Boolean {
return stateCache.distance == null || stateCache.distance != distance
}
fun hasChanged(direction: Direction?): Boolean {
return stateCache.direction == null || stateCache.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(state: State) {
if (state.lineArrows != null) {
stateCache.lineArrows = state.lineArrows
}
if (state.lineOutlines != null) {
state.lineOutlines = state.lineOutlines
}
if (state.direction != null) {
stateCache.direction = state.direction
}
if (state.distance != null) {
stateCache.distance = state.distance
}
if (state.speed != null) {
stateCache.speed = state.speed
}
if (state.arrival != null) {
stateCache.arrival = state.arrival
}
}
// maneuver
fun hasChanged(maneuver: Maneuver): Boolean {
return maneuverCache == null || maneuverCache!! != maneuver
}
fun update(maneuver: Maneuver) {
maneuverCache = maneuver
}
}

View file

@ -10,10 +10,12 @@ class ManeuverMapper {
val state = State()
maneuver.apply {
this.primary.apply {
state.direction = Direction()
when (this.type) {
"roundabout" -> {
state.direction.out = OutType.RightRoundabout
state.direction!!.out = OutType.RightRoundabout
}
"arrive" -> {
state.flag = true
}
@ -21,17 +23,18 @@ class ManeuverMapper {
when (this.modifier) {
"right" -> {
when (this.type) {
"turn" -> state.direction.angle = OutAngle.Right
"turn" -> state.direction!!.angle = OutAngle.Right
"roundabout" -> {
when (this.degrees) {
137.0 -> state.direction.angle = OutAngle.EasyRight
137.0 -> state.direction!!.angle = OutAngle.EasyRight
}
}
}
}
"left" -> {
when (this.type) {
"turn" -> state.direction.angle = OutAngle.Left
"turn" -> state.direction!!.angle = OutAngle.Left
}
}
}
@ -39,13 +42,14 @@ class ManeuverMapper {
this.stepDistance.apply {
this.distanceRemaining?.apply {
distanceFormatter.formatDistance(distanceRemaining!!).split(" ").apply {
state.distance = this[0].toInt()
state.unit = when (this[1]) {
"m" -> Unit.Metres
"km" -> Unit.Kilometres
else -> {
Unit.Any}
}
state.distance = Distance(
this[0].toInt(),
when (this[1]) {
"m" -> Unit.Metres
"km" -> Unit.Kilometres
else -> Unit.Any
}
)
}
}

View file

@ -52,20 +52,64 @@ enum class Lane(val value: Int) {
}
open class Arrows(val lanes: List<Lane>) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Arrows
return lanes == other.lanes
}
override fun hashCode(): Int {
return lanes.hashCode()
}
}
class Lanes(lanes: List<Lane>) : Arrows(lanes)
class Outlines(lanes: List<Lane>) : Arrows(lanes)
class Distance(val distance: Int, val unit: Unit) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Distance
if (distance != other.distance) return false
if (unit != other.unit) return false
return true
}
override fun hashCode(): Int {
var result = distance
result = 31 * result + unit.hashCode()
return result
}
}
class Speed(val speed: Int, val limit: Int)
class Arrival(val hours: Int, val minutes: Int)
class State {
var lineArrows: List<Lane> = listOf()
var lineOutlines: List<Lane> = listOf()
var direction = Direction()
var distance: Int = 0
var unit: Unit = Unit.Any
var speed: Int = 0
var limit: Int = 0
var hours: Int = 0
var minutes: Int = 0
var traffic: Boolean = false
var flag: Boolean = false
var control: Boolean = false
var lineArrows: Lanes? = null
var lineOutlines: Outlines? = null
var direction : Direction? = null
var distance: Distance? = null
var speed: Speed? = null
var arrival: Arrival? = null
// TODO: support
var traffic: Boolean? = null
var flag: Boolean? = null
var control: Boolean? = null
}