Skip to content

Commit ff17d39

Browse files
committed
fix(point annotation): selected property should select/deselect a point annotation
Co-Authored-By: Miklós Fazekas <mfazekas@szemafor.com>
1 parent 03382dd commit ff17d39

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

android/src/main/java/com/rnmapbox/rnmbx/components/annotation/RNMBXPointAnnotationManager.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ class RNMBXPointAnnotationManager(reactApplicationContext: ReactApplicationConte
7878
annotation.setAnchor(mapValue.getDouble("x").toFloat(), mapValue.getDouble("y").toFloat())
7979
}
8080

81+
override fun setSelected(
82+
annotation: RNMBXPointAnnotation,
83+
value: Dynamic?
84+
) {
85+
value?.let {
86+
annotation.isSelected = it.asBoolean()
87+
}
88+
}
89+
8190
@ReactProp(name = "draggable")
8291
override fun setDraggable(annotation: RNMBXPointAnnotation, draggable: Dynamic) {
8392
annotation.setDraggable(draggable.asBoolean())

ios/RNMBX/RNMBXMapView.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,19 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
18931893
// onTap(annotations: annotations)
18941894
}
18951895

1896+
func selected(pointAnnotation: RNMBXPointAnnotation) {
1897+
if (selected != nil) {
1898+
deselectCurrentlySelected(deselectAnnotationOnTap: false)
1899+
}
1900+
selected = pointAnnotation
1901+
}
1902+
1903+
func unselected(pointAnnotation: RNMBXPointAnnotation) {
1904+
if (selected == pointAnnotation) {
1905+
deselectCurrentlySelected(deselectAnnotationOnTap: false)
1906+
}
1907+
}
1908+
18961909
func deselectCurrentlySelected(deselectAnnotationOnTap: Bool = false) -> Bool {
18971910
if let selected = selected {
18981911
selected.doDeselect(deselectAnnotationOnMapTap: deselectAnnotationOnTap)
@@ -2120,6 +2133,7 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
21202133
)
21212134

21222135
func add(_ annotation: PointAnnotation, _ rnmbxPointAnnotation: RNMBXPointAnnotation) {
2136+
rnmbxPointAnnotation.manager = self
21232137
manager.annotations.append(annotation)
21242138
manager.refresh()
21252139
annotations.setObject(rnmbxPointAnnotation, forKey: annotation.id as NSString)

ios/RNMBX/RNMBXPointAnnotation.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ final class WeakRef<T: AnyObject> {
1010
}
1111
@objc
1212
public class RNMBXPointAnnotation : RNMBXInteractiveElement {
13+
weak var manager: RNMBXPointAnnotationManager? = nil
14+
1315
static let key = "RNMBXPointAnnotation"
1416
static var gid = 0;
1517

@@ -31,7 +33,25 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
3133
@objc public var onDrag: RCTBubblingEventBlock? = nil
3234
@objc public var onDragEnd: RCTBubblingEventBlock? = nil
3335
@objc public var onSelected: RCTBubblingEventBlock? = nil
34-
36+
37+
private var selected: Bool? = nil {
38+
didSet {
39+
update { annotation in
40+
if let selected = selected {
41+
annotation.isSelected = selected
42+
}
43+
}
44+
}
45+
}
46+
47+
@objc public func setReactSelected(_ _selected: Bool) {
48+
if (_selected == true && self.selected != true) {
49+
manager?.selected(pointAnnotation: self)
50+
} else if (_selected == false && self.selected == true) {
51+
manager?.unselected(pointAnnotation: self)
52+
}
53+
}
54+
3555
@objc public var coordinate : String? {
3656
didSet {
3757
_updateCoordinate()
@@ -173,14 +193,16 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
173193
}
174194

175195
func doSelect() {
196+
self.selected = true
176197
let event = makeEvent(isSelect: true)
177198
if let onSelected = onSelected {
178199
onSelected(event.toJSON())
179200
}
180201
onSelect()
181202
}
182-
203+
183204
func doDeselect(deselectAnnotationOnMapTap: Bool = false) {
205+
self.selected = false
184206
let event = makeEvent(isSelect: false, deselectAnnotationOnMapTap: deselectAnnotationOnMapTap)
185207
if let onDeselected = onDeselected {
186208
onDeselected(event.toJSON())

ios/RNMBX/RNMBXPointAnnotationComponentView.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
130130
RNMBX_OPTIONAL_PROP_BOOL(draggable)
131131
RNMBX_OPTIONAL_PROP_NSString(id)
132132
RNMBX_OPTIONAL_PROP_NSDictionary(anchor)
133+
RNMBX_REMAP_OPTIONAL_PROP_BOOL(selected, reactSelected)
133134

134135
[super updateProps:props oldProps:oldProps];
135136
}

src/specs/RNMBXPointAnnotationNativeComponent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface NativeProps extends ViewProps {
2828
draggable: UnsafeMixed<boolean>;
2929
id: UnsafeMixed<string>;
3030
anchor: UnsafeMixed<any>;
31+
selected: UnsafeMixed<boolean>;
3132

3233
onMapboxPointAnnotationDeselected: DirectEventHandler<OnMapboxPointAnnotationDeselectedEventType>;
3334
onMapboxPointAnnotationDrag: DirectEventHandler<OnMapboxPointAnnotationDragEventType>;

0 commit comments

Comments
 (0)