Skip to content

Commit b795efa

Browse files
committed
HMSMap: Optimized PolylineImpl
1 parent 02c558c commit b795efa

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

  • play-services-maps/core/hms/src/main/kotlin/org/microg/gms/maps/hms/model

play-services-maps/core/hms/src/main/kotlin/org/microg/gms/maps/hms/model/Polyline.kt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,54 @@ import com.huawei.hms.maps.model.Cap as HmsCap
2323
import com.huawei.hms.maps.model.CustomCap as HmsCustomCap
2424
import com.huawei.hms.maps.model.RoundCap as HmsRoundCap
2525
import com.huawei.hms.maps.model.SquareCap as HmsSquareCap
26+
import com.huawei.hms.maps.model.LatLng as HmsLatLng
2627
import org.microg.gms.maps.hms.utils.toGms
2728
import org.microg.gms.maps.hms.utils.toGmsPolylineWidth
2829
import org.microg.gms.maps.hms.utils.toHms
2930
import org.microg.gms.maps.hms.utils.toHmsPolylineWidth
3031

3132
class PolylineImpl(private val polyline: Polyline, polylineOptions: PolylineOptions) : IPolylineDelegate.Stub() {
3233
private var tag: Any? = null
34+
private val linePoints = arrayListOf<LatLng>()
35+
private var lastPointsHash: Int = 0
36+
37+
private val toHmsCache = mutableMapOf<LatLng, HmsLatLng>()
38+
private fun List<LatLng>.toHmsList(): List<HmsLatLng> {
39+
return this.map { latLng ->
40+
toHmsCache.getOrPut(latLng) { latLng.toHms() }
41+
}
42+
}
3343

3444
override fun remove() {
45+
linePoints.clear()
46+
toHmsCache.clear()
3547
polyline.remove()
3648
}
3749

3850
override fun getId(): String = polyline.id
3951

4052
override fun setPoints(points: List<LatLng>) {
41-
polyline.points = points.map { it.toHms() }
53+
if (linePoints.size == points.size && linePoints == points) {
54+
Log.d(TAG, "setPoints skipped: identical points")
55+
return
56+
}
57+
58+
val newHash = points.hashCode()
59+
if (newHash == lastPointsHash) {
60+
Log.d(TAG, "setPoints skipped: hash unchanged")
61+
return
62+
}
63+
lastPointsHash = newHash
64+
65+
linePoints.clear()
66+
linePoints.addAll(points)
67+
polyline.points = linePoints.toHmsList()
68+
Log.d(TAG, "setPoints updated, size=${linePoints.size}")
4269
}
4370

44-
override fun getPoints(): List<LatLng> = polyline.points.map { it.toGms() }
71+
override fun getPoints(): List<LatLng> {
72+
return linePoints
73+
}
4574

4675
override fun setWidth(width: Float) {
4776
polyline.width = toHmsPolylineWidth(width)

0 commit comments

Comments
 (0)