Skip to content
Merged
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 @@ -36,7 +36,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
private val mTitle: String? = null
private val mSnippet: String? = null
private var mAnchor: Array<Float>? = null
private val mIsSelected = false
private var mIsSelected = false
private var mDraggable = false
private var mChildView: View? = null
private var mChildBitmap: Bitmap? = null
Expand Down Expand Up @@ -187,7 +187,19 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
}
}

fun setReactSelected(selected: Boolean) {
if (selected && !mIsSelected) {
pointAnnotations?.let {
it.deselectSelectedAnnotation()
it.selectAnnotation(this)
}
} else if (!selected && mIsSelected) {
pointAnnotations?.deselectAnnotation(this)
}
}

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

fun doDeselect() {
mIsSelected = false
mManager.handleEvent(makeEvent(false))
mCalloutSymbol?.let { mCalloutSymbol ->
pointAnnotations?.delete(mCalloutSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ class RNMBXPointAnnotationManager(reactApplicationContext: ReactApplicationConte
annotation.setAnchor(mapValue.getDouble("x").toFloat(), mapValue.getDouble("y").toFloat())
}

override fun setSelected(
annotation: RNMBXPointAnnotation,
value: Dynamic
) {
if (value.isNull) {
Logger.e("RNMBXPointAnnotationManager", "selected value is null")
return
}
annotation.setReactSelected(value.asBoolean())
}

@ReactProp(name = "draggable")
override fun setDraggable(annotation: RNMBXPointAnnotation, draggable: Dynamic) {
annotation.setDraggable(draggable.asBoolean())
Expand Down
10 changes: 10 additions & 0 deletions ios/RNMBX/RNMBXCalloutComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ + (ComponentDescriptorProvider)componentDescriptorProvider
return concreteComponentDescriptorProvider<RNMBXCalloutComponentDescriptor>();
}

- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
[_view insertSubview:childComponentView atIndex:index];
}

- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
[childComponentView removeFromSuperview];
}

- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
const auto &newProps = static_cast<const RNMBXCalloutProps &>(*props);
Expand Down
19 changes: 18 additions & 1 deletion ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ open class RNMBXMapView: UIView, RCTInvalidating {
}()

lazy var calloutAnnotationManager : MapboxMaps.PointAnnotationManager = {
return mapView.annotations.makePointAnnotationManager(id: "RNMBX-mapview-callouts")
let manager = mapView.annotations.makePointAnnotationManager(id: "RNMBX-mapview-callouts")
manager.iconAllowOverlap = true
return manager
}()

var _mapView: MapView! = nil
Expand Down Expand Up @@ -1893,6 +1895,20 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
// onTap(annotations: annotations)
}

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

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

func deselectCurrentlySelected(deselectAnnotationOnTap: Bool = false) -> Bool {
if let selected = selected {
selected.doDeselect(deselectAnnotationOnMapTap: deselectAnnotationOnTap)
Expand Down Expand Up @@ -2120,6 +2136,7 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
)

func add(_ annotation: PointAnnotation, _ rnmbxPointAnnotation: RNMBXPointAnnotation) {
rnmbxPointAnnotation.manager = self
manager.annotations.append(annotation)
manager.refresh()
annotations.setObject(rnmbxPointAnnotation, forKey: annotation.id as NSString)
Expand Down
28 changes: 25 additions & 3 deletions 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 @@ -31,7 +33,25 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
@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,14 +193,16 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
}

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

func doDeselect(deselectAnnotationOnMapTap: Bool = false) {
self.selected = false
let event = makeEvent(isSelect: false, deselectAnnotationOnMapTap: deselectAnnotationOnMapTap)
if let onDeselected = onDeselected {
onDeselected(event.toJSON())
Expand All @@ -192,7 +214,7 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {
if let callout = callout,
let calloutImage = _createViewSnapshot(view: callout),
let point = point() {

var calloutPtAnnotation = PointAnnotation(point: point)
calloutId = calloutPtAnnotation.id
let name = "rnviewcallout-\(gid())-\(calloutPtAnnotation.id)"
Expand Down
1 change: 1 addition & 0 deletions ios/RNMBX/RNMBXPointAnnotationComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
RNMBX_OPTIONAL_PROP_BOOL(draggable)
RNMBX_OPTIONAL_PROP_NSString(id)
RNMBX_OPTIONAL_PROP_NSDictionary(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
Loading