Skip to content

Commit a82c7a5

Browse files
kediarovgithub-actions[bot]
authored andcommitted
AnnotationManagerImpl cleanup images on delete follow-up (#11751)
Follow-up of PR mapbox/mapbox-sdk#11010 https://mapbox.atlassian.net/browse/MAPSAND-2671 ## Summary - Extracts the repeated icon image ID generation expression (`ICON_DEFAULT_NAME_PREFIX + manager.hashCode().toString(16) + "_" + bitmap.hashCode()`) into `PointAnnotation.iconImageId(manager, bitmap)` in the companion object - Renames internal `StyleImages` → `PointAnnotationStyleImages` for clarity and adds KDoc to the class and its methods cc @mapbox/maps-android cc @mapbox/maps-ios GitOrigin-RevId: eafd0fcacea28f34ff54da7490eb5d2ca9ab52e1
1 parent ad7ef70 commit a82c7a5

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
lines changed

plugin-annotation/src/main/java/com/mapbox/maps/plugin/annotation/AnnotationManagerImpl.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal constructor(
8282
private var draggingAnnotation: T? = null
8383
private val annotationMap = LinkedHashMap<String, T>()
8484
private val dragAnnotationMap = LinkedHashMap<String, T>()
85-
private val styleImages = StyleImages()
85+
private val styleImages = PointAnnotationStyleImages()
8686
internal val dataDrivenPropertyDefaultValues: JsonObject = JsonObject()
8787

8888
private val interactionsCancelableSet = mutableSetOf<Cancelable>()
@@ -998,21 +998,34 @@ internal constructor(
998998
return properties.optBoolean("cluster", false)
999999
}
10001000

1001-
private inner class StyleImages() {
1001+
/**
1002+
* Tracks reference counts for style images associated with [PointAnnotation]s that use
1003+
* auto-generated icon image IDs starting with [PointAnnotation.ICON_DEFAULT_NAME_PREFIX].
1004+
*
1005+
* Each unique image ID is reference-counted so that the underlying style image is only removed
1006+
* from the map style when no annotation still references it. Images whose IDs do not start with
1007+
* the default prefix must be managed externally.
1008+
*/
1009+
private inner class PointAnnotationStyleImages() {
10021010

1011+
/** Map from image ID to the number of annotations currently referencing it. */
10031012
private val images: MutableMap<String, Int> = mutableMapOf()
10041013
private val style get() = delegateProvider.mapStyleManagerDelegate
10051014

10061015
fun put(annotation: T) {
10071016
val imageId = (annotation as? PointAnnotation)?.iconImageInternal ?: return
1008-
if (!imageId.startsWith(PointAnnotation.ICON_DEFAULT_NAME_PREFIX)) return
10091017
put(imageId)
10101018
}
10111019

10121020
fun put(imageId: String) {
1021+
if (!imageId.startsWith(PointAnnotation.ICON_DEFAULT_NAME_PREFIX)) return
10131022
images[imageId] = (images[imageId] ?: 0) + 1
10141023
}
10151024

1025+
/**
1026+
* Decrements the reference count for the icon image of [annotation].
1027+
* When the count reaches zero the image is removed from the map style.
1028+
*/
10161029
fun remove(annotation: T) {
10171030
val imageId = (annotation as? PointAnnotation)?.iconImageInternal ?: return
10181031
if (!imageId.startsWith(PointAnnotation.ICON_DEFAULT_NAME_PREFIX)) return
@@ -1027,6 +1040,10 @@ internal constructor(
10271040
}
10281041
}
10291042

1043+
/**
1044+
* Removes all tracked images from the map style and clears the reference-count map.
1045+
* Called when the annotation manager is destroyed or all annotations are deleted.
1046+
*/
10301047
fun clear() {
10311048
val style = style
10321049
images.keys.forEach { imageId ->

plugin-annotation/src/main/java/com/mapbox/maps/plugin/annotation/generated/PointAnnotation.kt

Lines changed: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin-annotation/src/main/java/com/mapbox/maps/plugin/annotation/generated/PointAnnotationManager.kt

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin-annotation/src/test/java/com/mapbox/maps/plugin/annotation/StyleImageRefCountTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.mapbox.maps.extension.style.layers.generated.SymbolLayer
1616
import com.mapbox.maps.extension.style.sources.addSource
1717
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
1818
import com.mapbox.maps.logE
19-
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation.Companion.ICON_DEFAULT_NAME_PREFIX
19+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation.Companion.iconImageId
2020
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
2121
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions
2222
import com.mapbox.maps.plugin.delegates.MapCameraManagerDelegate
@@ -370,7 +370,7 @@ class StyleImageRefCountTest {
370370
}
371371

372372
private fun imageIdFor(b: Bitmap, mgr: PointAnnotationManager = manager) =
373-
ICON_DEFAULT_NAME_PREFIX + mgr.hashCode().toString(16) + "_" + b.hashCode()
373+
iconImageId(mgr, b)
374374

375375
private fun createWithBitmap(b: Bitmap) = manager.create(
376376
PointAnnotationOptions()

plugin-annotation/src/test/java/com/mapbox/maps/plugin/annotation/generated/PointAnnotationManagerTest.kt

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)