Skip to content

Commit

Permalink
fix: send correct point anntoation events on iOS (#3260)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas authored Dec 10, 2023
1 parent 21b28da commit e0424ff
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
}
}

fun onSelect(shouldSendEvent: Boolean) {
fun doSelect(shouldSendEvent: Boolean) {
if (calloutView != null) {
makeCallout()
}
Expand All @@ -177,7 +177,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
}
}

fun onDeselect() {
fun doDeselect() {
mManager.handleEvent(makeEvent(false))
if (mCalloutSymbol != null) {
mMapView?.pointAnnotationManager?.delete(mCalloutSymbol!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1560,16 +1560,25 @@ class RNMBXPointAnnotationManager(val mapView: MapView) {
return false
}

fun onAnnotationClick(symbol: PointAnnotation) {
annotationClicked = true
var oldSelected: RNMBXPointAnnotation? = selected
var newSelected: RNMBXPointAnnotation? = null

fun lookup(point: PointAnnotation): RNMBXPointAnnotation? {
for (annotation in annotations.values) {
if (symbol.id == annotation.mapboxID && selected?.mapboxID != symbol.id) {
newSelected = annotation
if (point.id == annotation.mapboxID) {
return annotation;
}
}
Logger.e(LOG_TAG, "Failed to find RNMBXPointAnntoation for ${point.id}")
return null;
}

fun onAnnotationClick(pointAnnotation: RNMBXPointAnnotation) {
var oldSelected: RNMBXPointAnnotation? = selected
var newSelected: RNMBXPointAnnotation? = pointAnnotation

annotationClicked = true

if (newSelected == oldSelected) {
newSelected = null
}

manager.addDragListener(object : OnPointAnnotationDragListener {
override fun onAnnotationDragStarted(_annotation: Annotation<*>) {
Expand Down Expand Up @@ -1613,6 +1622,13 @@ class RNMBXPointAnnotationManager(val mapView: MapView) {

oldSelected?.let { deselectAnnotation(it) }
newSelected?.let { selectAnnotation(it) }

}

fun onAnnotationClick(point: PointAnnotation) {
lookup(point)?.let {
onAnnotationClick(it)
}
}

fun deselectSelectedAnnotation(): Boolean {
Expand All @@ -1625,12 +1641,12 @@ class RNMBXPointAnnotationManager(val mapView: MapView) {

fun selectAnnotation(annotation: RNMBXPointAnnotation) {
selected = annotation
annotation.onSelect(true)
annotation.doSelect(true)
}

fun deselectAnnotation(annotation: RNMBXPointAnnotation) {
selected = null
annotation.onDeselect()
annotation.doDeselect()
}

fun remove(annotation: RNMBXPointAnnotation) {
Expand All @@ -1655,6 +1671,10 @@ class RNMBXPointAnnotationManager(val mapView: MapView) {
fun add(annotation: RNMBXPointAnnotation) {
annotations[annotation.iD!!] = annotation
}

companion object {
const val LOG_TAG = "RNMBXPointAnnotationManager";
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ object GeoJSONUtils {
val map: WritableMap = WritableNativeMap()
map.putString("type", "Feature")
map.putMap("geometry", toPointGeometry(latLng))
properties?.let {
if (it.hasKey("id") == true) {
map.putString("id", it.getString("id"));
}
}
map.putMap("properties", properties)
return map
}
Expand Down
2 changes: 2 additions & 0 deletions ios/RNMBX/RNMBXEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class RNMBXEvent : NSObject, RNMBXEventProtocol {
case onUserTrackingModeChange
case vectorSourceLayerPress
case shapeSourceLayerPress
case annotationSelected = "annotationselected"
case annotationDeselected = "annotationdeselected"
}

init(type: EventType, payload: [String:Any?]?) {
Expand Down
63 changes: 35 additions & 28 deletions ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ extension RNMBXMapView: GestureManagerDelegate {
pointAnnotationManager.handleTap(sender) { (_: UITapGestureRecognizer) in
DispatchQueue.main.async {
if (self.deselectAnnotationOnTap) {
if (self.pointAnnotationManager.deselecteCurrentlySelected(self._tapEvent(tapPoint))) {
if (self.pointAnnotationManager.deselectCurrentlySelected(deselectAnnotationOnTap: true)) {
return
}
}
Expand Down Expand Up @@ -1364,46 +1364,53 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
// onTap(annotations: annotations)
}

func deselecteCurrentlySelected(_ event: RNMBXEvent) -> Bool {
func deselectCurrentlySelected(deselectAnnotationOnTap: Bool = false) -> Bool {
if let selected = selected {
if let onDeselected = selected.onDeselected {
onDeselected(event.toJSON())
}
selected.onDeselect()
selected.doDeselect(deselectAnnotationOnMapTap: deselectAnnotationOnTap)
self.selected = nil
return true
}
return false
}

func onAnnotationClick(pointAnnotation: RNMBXPointAnnotation) {
let oldSelected = selected
var newSelected: RNMBXPointAnnotation? = pointAnnotation

if (newSelected == oldSelected) {
newSelected = nil
}

deselectCurrentlySelected()

if let newSelected = newSelected {
newSelected.doSelect()
selected = newSelected
}
}

func lookup(_ annotation: PointAnnotation) -> RNMBXPointAnnotation? {
guard let userInfo = annotation.userInfo else {
return nil
}
if let rnmbxPointAnnotationWeakRef = userInfo[RNMBXPointAnnotation.key] as? WeakRef<RNMBXPointAnnotation> {
if let rnmbxPointAnnotation = rnmbxPointAnnotationWeakRef.object {
return rnmbxPointAnnotation
}
}
return nil
}


func onTap(annotations: [Annotation]) {
guard annotations.count > 0 else {
fatalError("didDetectTappedAnnotations: No annotations found")
}

for annotation in annotations {
if let pointAnnotation = annotation as? PointAnnotation,
let userInfo = pointAnnotation.userInfo {

if let RNMBXPointAnnotation = userInfo[RNMBXPointAnnotation.key] as? WeakRef<RNMBXPointAnnotation> {
if let pt = RNMBXPointAnnotation.object {
let position = pt.superview?.convert(pt.layer.position, to: nil)
let location = pt.map?.mapboxMap.coordinate(for: position!)
var geojson = Feature(geometry: .point(Point(location!)))
geojson.identifier = .string(pt.id)
geojson.properties = [
"screenPointX": .number(Double(position!.x)),
"screenPointY": .number(Double(position!.y))
]
let event = RNMBXEvent(type:.tap, payload: logged("doHandleTap") { try geojson.toJSON() })
deselecteCurrentlySelected(event)
guard let onSelected = pt.onSelected else {
return
}
onSelected(event.toJSON())
pt.onSelect()
selected = pt
}
if let annotation = annotation as? PointAnnotation {
if let pointAnnotation = lookup(annotation) {
onAnnotationClick(pointAnnotation: pointAnnotation)
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions ios/RNMBX/RNMBXPointAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,39 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
return image
}

func makeEvent(isSelect: Bool, deselectAnnotationOnMapTap: Bool = false) -> RNMBXEvent {
let position = superview?.convert(layer.position, to: nil)
let location = map?.mapboxMap.coordinate(for: position!)
var geojson = Feature(geometry: .point(Point(location!)))
geojson.identifier = .string(id)
var properties : [String: JSONValue?] = [
"screenPointX": .number(Double(position!.x)),
"screenPointY": .number(Double(position!.y))
]
if deselectAnnotationOnMapTap {
properties["deselectAnnotationOnMapTap"] = true
}
geojson.properties = properties
let event = RNMBXEvent(type:isSelect ? .annotationSelected : .annotationDeselected, payload: logged("doHandleTap") { try geojson.toJSON() })
return event
}

func doSelect() {
let event = makeEvent(isSelect: true)
if let onSelected = onSelected {
onSelected(event.toJSON())
}
onSelect()
}

func doDeselect(deselectAnnotationOnMapTap: Bool = false) {
let event = makeEvent(isSelect: false, deselectAnnotationOnMapTap: deselectAnnotationOnMapTap)
if let onDeselected = onDeselected {
onDeselected(event.toJSON())
}
onDeselect()
}

func onSelect() {
if let callout = callout,
let calloutImage = _createViewSnapshot(view: callout),
Expand Down

0 comments on commit e0424ff

Please sign in to comment.