Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ class RNMBXPointAnnotationManager(reactApplicationContext: ReactApplicationConte
annotation.setAnchor(mapValue.getDouble("x").toFloat(), mapValue.getDouble("y").toFloat())
}

override fun setSelected(
annotation: RNMBXPointAnnotation,
value: Dynamic?
) {
value?.let {
annotation.isSelected = it.asBoolean()
}
}

@ReactProp(name = "draggable")
override fun setDraggable(annotation: RNMBXPointAnnotation, draggable: Dynamic) {
annotation.setDraggable(draggable.asBoolean())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
case "anchor":
mViewManager.setAnchor(view, new DynamicFromObject(value));
break;
case "selected":
mViewManager.setSelected(view, new DynamicFromObject(value));
break;
default:
super.setProperty(view, propName, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface RNMBXPointAnnotationManagerInterface<T extends View> {
void setDraggable(T view, Dynamic value);
void setId(T view, Dynamic value);
void setAnchor(T view, Dynamic value);
void setSelected(T view, Dynamic value);
}
14 changes: 14 additions & 0 deletions ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,19 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
// We handle taps ourselfs
// onTap(annotations: annotations)
}

func selected(pointAnnotation: RNMBXPointAnnotation) {
if (selected != nil) {
deselectCurrentlySelected(deselectAnnotationOnTap: false)
}
selected = pointAnnotation
}

func unselected(pointAnnotation: RNMBXPointAnnotation) {
if (selected == pointAnnotation) {
deselectCurrentlySelected(deselectAnnotationOnTap: false)
}
}

func deselectCurrentlySelected(deselectAnnotationOnTap: Bool = false) -> Bool {
if let selected = selected {
Expand Down Expand Up @@ -1754,6 +1767,7 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
#endif

func add(_ annotation: PointAnnotation, _ rnmbxPointAnnotation: RNMBXPointAnnotation) {
rnmbxPointAnnotation.manager = self
manager.annotations.append(annotation)
manager.refresh()
#if RNMBX_11
Expand Down
23 changes: 22 additions & 1 deletion ios/RNMBX/RNMBXPointAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ final class WeakRef<T: AnyObject> {
}
@objc
public class RNMBXPointAnnotation : RNMBXInteractiveElement {
weak var manager:RNMBXPointAnnotationManager? = nil

static let key = "RNMBXPointAnnotation"
static var gid = 0;

Expand All @@ -26,12 +28,29 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
var image : UIImage? = nil
var reactSubviews : [UIView] = []


@objc public var onDeselected: RCTBubblingEventBlock? = nil
@objc public var onDrag: RCTBubblingEventBlock? = nil
@objc public var onDragEnd: RCTBubblingEventBlock? = nil
@objc public var onSelected: RCTBubblingEventBlock? = nil

private var selected: Bool? = nil {
didSet {
update { annotation in
if let selected = selected {
annotation.isSelected = selected
}
}
}
}

@objc public func setReactSelected(_ _selected: Bool) {
if (_selected == true && self.selected != true) {
manager?.selected(pointAnnotation: self)
} else if (_selected == false && self.selected == true) {
manager?.unselected(pointAnnotation: self)
}
}

@objc public var coordinate : String? {
didSet {
_updateCoordinate()
Expand Down Expand Up @@ -173,6 +192,7 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
}

func doSelect() {
self.selected = true
let event = makeEvent(isSelect: true)
if let onSelected = onSelected {
onSelected(event.toJSON())
Expand All @@ -181,6 +201,7 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
}

func doDeselect(deselectAnnotationOnMapTap: Bool = false) {
self.selected = false
let event = makeEvent(isSelect: false, deselectAnnotationOnMapTap: deselectAnnotationOnMapTap)
if let onDeselected = onDeselected {
onDeselected(event.toJSON())
Expand Down
15 changes: 10 additions & 5 deletions ios/RNMBX/RNMBXPointAnnotationComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import <react/renderer/components/rnmapbox_maps_specs/Props.h>
#import <react/renderer/components/rnmapbox_maps_specs/RCTComponentViewHelpers.h>

#import "RNMBXFabricPropConvert.h"

using namespace facebook::react;

@interface RNMBXPointAnnotationComponentView () <RCTRNMBXPointAnnotationViewProtocol>
Expand Down Expand Up @@ -122,23 +124,26 @@ - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childCompo

- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
const auto &newProps = static_cast<const RNMBXPointAnnotationProps &>(*props);
id coordinate = RNMBXConvertFollyDynamicToId(newProps.coordinate);
const auto &newViewProps = static_cast<const RNMBXPointAnnotationProps &>(*props);
const auto &oldViewProps = static_cast<const RNMBXPointAnnotationProps &>(*oldProps);
id coordinate = RNMBXConvertFollyDynamicToId(newViewProps.coordinate);
if (coordinate != nil) {
_view.coordinate = coordinate;
}
id draggable = RNMBXConvertFollyDynamicToId(newProps.draggable);
id draggable = RNMBXConvertFollyDynamicToId(newViewProps.draggable);
if (draggable != nil) {
_view.draggable = draggable;
}
id idx = RNMBXConvertFollyDynamicToId(newProps.id);
id idx = RNMBXConvertFollyDynamicToId(newViewProps.id);
if (idx != nil) {
_view.id = idx;
}
id anchor = RNMBXConvertFollyDynamicToId(newProps.anchor);
id anchor = RNMBXConvertFollyDynamicToId(newViewProps.anchor);
if (anchor != nil) {
_view.anchor = anchor;
}

RNMBX_REMAP_OPTIONAL_PROP_BOOL(selected, reactSelected);

[super updateProps:props oldProps:oldProps];
}
Expand Down
1 change: 1 addition & 0 deletions src/specs/RNMBXPointAnnotationNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface NativeProps extends ViewProps {
draggable: UnsafeMixed<boolean>;
id: UnsafeMixed<string>;
anchor: UnsafeMixed<any>;
selected: UnsafeMixed<boolean>;

onMapboxPointAnnotationDeselected: DirectEventHandler<OnMapboxPointAnnotationDeselectedEventType>;
onMapboxPointAnnotationDrag: DirectEventHandler<OnMapboxPointAnnotationDragEventType>;
Expand Down
3 changes: 3 additions & 0 deletions src/specs/codegenUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// codegen will generate folly::dynamic in place of this type, but it's not exported by RN
// since codegen doesn't really follow imports, this way we can trick it into generating the correct type
// while keeping typescript happy
//
// For booleans: UnsafeMixed<boolean> preserves nullability (true/false/null) vs boolean which defaults to false
// This allows native code to distinguish between "prop not set" vs "prop explicitly set to false"
export type UnsafeMixed<T> = T;

// Fabric doesn't support optional props, so we need to use UnsafeMixed
Expand Down
Loading