|
| 1 | +package com.rnmapbox.rnmbx.components.annotation |
| 2 | + |
| 3 | +import com.mapbox.maps.MapView |
| 4 | +import com.mapbox.maps.plugin.annotation.Annotation |
| 5 | +import com.mapbox.maps.plugin.annotation.AnnotationConfig |
| 6 | +import com.mapbox.maps.plugin.annotation.annotations |
| 7 | +import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationClickListener |
| 8 | +import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationDragListener |
| 9 | +import com.mapbox.maps.plugin.annotation.generated.PointAnnotation |
| 10 | +import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager |
| 11 | +import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions |
| 12 | +import com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager |
| 13 | +import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotation |
| 14 | +import com.rnmapbox.rnmbx.utils.Logger |
| 15 | + |
| 16 | +class RNMBXPointAnnotationCoordinator(val mapView: MapView) { |
| 17 | + val manager: PointAnnotationManager; |
| 18 | + var annotationClicked = false |
| 19 | + var annotationDragged = false |
| 20 | + |
| 21 | + var selected: RNMBXPointAnnotation? = null |
| 22 | + |
| 23 | + val annotations: MutableMap<String, RNMBXPointAnnotation> = hashMapOf() |
| 24 | + val callouts: MutableMap<String, RNMBXPointAnnotation> = hashMapOf() |
| 25 | + |
| 26 | + init { |
| 27 | + manager = mapView.annotations.createPointAnnotationManager(AnnotationConfig(layerId = "RNMBX-mapview-annotations")) |
| 28 | + manager.addClickListener(OnPointAnnotationClickListener { pointAnnotation -> |
| 29 | + onAnnotationClick(pointAnnotation) |
| 30 | + false |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + fun getAndClearAnnotationClicked(): Boolean { |
| 35 | + if (annotationClicked) { |
| 36 | + annotationClicked = false |
| 37 | + return true |
| 38 | + } |
| 39 | + return false |
| 40 | + } |
| 41 | + |
| 42 | + fun getAndClearAnnotationDragged(): Boolean { |
| 43 | + if (annotationDragged) { |
| 44 | + annotationDragged = false |
| 45 | + return true |
| 46 | + } |
| 47 | + return false |
| 48 | + } |
| 49 | + |
| 50 | + fun lookupForClick(point: PointAnnotation): RNMBXPointAnnotation? { |
| 51 | + for (annotation in annotations.values) { |
| 52 | + if (point.id == annotation.mapboxID) { |
| 53 | + return annotation; |
| 54 | + } |
| 55 | + if (point.id == annotation.calloutMapboxID) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + } |
| 59 | + Logger.e(LOG_TAG, "Failed to find RNMBXPointAnnotation for ${point.id}") |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + fun onAnnotationClick(pointAnnotation: RNMBXPointAnnotation) { |
| 64 | + var oldSelected: RNMBXPointAnnotation? = selected |
| 65 | + var newSelected: RNMBXPointAnnotation? = pointAnnotation |
| 66 | + |
| 67 | + annotationClicked = true |
| 68 | + |
| 69 | + if (newSelected == oldSelected) { |
| 70 | + newSelected = null |
| 71 | + } |
| 72 | + |
| 73 | + manager.addDragListener(object : OnPointAnnotationDragListener { |
| 74 | + override fun onAnnotationDragStarted(_annotation: Annotation<*>) { |
| 75 | + annotationDragged = true; |
| 76 | + var reactAnnotation: RNMBXPointAnnotation? = null |
| 77 | + for (key in annotations.keys) { |
| 78 | + val annotation = annotations[key] |
| 79 | + val curMarkerID = annotation?.mapboxID |
| 80 | + if (_annotation.id == curMarkerID) { |
| 81 | + reactAnnotation = annotation |
| 82 | + } |
| 83 | + } |
| 84 | + reactAnnotation?.let { it.onDragStart() } |
| 85 | + } |
| 86 | + |
| 87 | + override fun onAnnotationDrag(_annotation: Annotation<*>) { |
| 88 | + var reactAnnotation: RNMBXPointAnnotation? = null |
| 89 | + for (key in annotations.keys) { |
| 90 | + val annotation = annotations[key] |
| 91 | + val curMarkerID = annotation?.mapboxID |
| 92 | + if (_annotation.id == curMarkerID) { |
| 93 | + reactAnnotation = annotation |
| 94 | + } |
| 95 | + } |
| 96 | + reactAnnotation?.let { it.onDrag() } |
| 97 | + } |
| 98 | + |
| 99 | + override fun onAnnotationDragFinished(_annotation: Annotation<*>) { |
| 100 | + annotationDragged = false; |
| 101 | + var reactAnnotation: RNMBXPointAnnotation? = null |
| 102 | + for (key in annotations.keys) { |
| 103 | + val annotation = annotations[key] |
| 104 | + val curMarkerID = annotation?.mapboxID |
| 105 | + if (_annotation.id == curMarkerID) { |
| 106 | + reactAnnotation = annotation |
| 107 | + } |
| 108 | + } |
| 109 | + reactAnnotation?.let { it.onDragEnd() } |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + oldSelected?.let { deselectAnnotation(it) } |
| 114 | + newSelected?.let { selectAnnotation(it) } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + fun onAnnotationClick(point: PointAnnotation) { |
| 119 | + lookupForClick(point)?.let { |
| 120 | + onAnnotationClick(it) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + fun deselectSelectedAnnotation(): Boolean { |
| 125 | + selected?.let { |
| 126 | + deselectAnnotation(it) |
| 127 | + return true |
| 128 | + } |
| 129 | + return false |
| 130 | + } |
| 131 | + |
| 132 | + fun selectAnnotation(annotation: RNMBXPointAnnotation) { |
| 133 | + selected = annotation |
| 134 | + annotation.doSelect(true) |
| 135 | + } |
| 136 | + |
| 137 | + fun deselectAnnotation(annotation: RNMBXPointAnnotation) { |
| 138 | + selected = null |
| 139 | + annotation.doDeselect() |
| 140 | + } |
| 141 | + |
| 142 | + fun remove(annotation: RNMBXPointAnnotation) { |
| 143 | + if (annotation == selected) { |
| 144 | + selected = null |
| 145 | + } |
| 146 | + annotations.remove(annotation.iD) |
| 147 | + } |
| 148 | + |
| 149 | + fun delete(annotation: PointAnnotation) { |
| 150 | + manager.delete(annotation) |
| 151 | + } |
| 152 | + |
| 153 | + fun update(annotation: PointAnnotation) { |
| 154 | + manager.update(annotation) |
| 155 | + } |
| 156 | + |
| 157 | + fun create(options: PointAnnotationOptions): PointAnnotation { |
| 158 | + return manager.create(options) |
| 159 | + } |
| 160 | + |
| 161 | + fun add(annotation: RNMBXPointAnnotation) { |
| 162 | + annotations[annotation.iD!!] = annotation |
| 163 | + } |
| 164 | + |
| 165 | + companion object { |
| 166 | + const val LOG_TAG = "RNMBXPointAnnotationCoordinator"; |
| 167 | + } |
| 168 | +} |
0 commit comments