Skip to content

Commit

Permalink
feat(ios): mapViewImpl so native code can register custom mapView fac…
Browse files Browse the repository at this point in the history
…tory
  • Loading branch information
mfazekas committed Dec 15, 2023
1 parent bad4488 commit a944fb6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
case "compassImage":
mViewManager.setCompassImage(view, new DynamicFromObject(value));
break;
case "mapViewImpl":
mViewManager.setMapViewImpl(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 @@ -39,4 +39,5 @@ public interface RNMBXMapViewManagerInterface<T extends View> {
void setAttributionViewMargins(T view, Dynamic value);
void setAttributionViewPosition(T view, Dynamic value);
void setCompassImage(T view, Dynamic value);
void setMapViewImpl(T view, Dynamic value);
}
54 changes: 45 additions & 9 deletions ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@
import Turf
import MapKit

public typealias RNMBXMapViewFactoryFunc = (String, UIView) -> MapView?;

/**
* Experimental MapView factory for advanced usecases
*/
public class RNMBXMapViewFactory {
private static var factories: [String: RNMBXMapViewFactoryFunc] = [:];

static func get(_ id: String) -> RNMBXMapViewFactoryFunc? {
if let id = id.split(separator: ":", maxSplits: 1).first {
return factories[String(id)]
}
return nil
}

public static func register(_ id: String, factory: @escaping RNMBXMapViewFactoryFunc) {
factories.updateValue(factory, forKey: id)
}
}

class FeatureEntry {
let feature: RNMBXMapComponent
let view: UIView
Expand Down Expand Up @@ -132,6 +152,9 @@ open class RNMBXMapView: UIView {

@objc
public var deselectAnnotationOnTap: Bool = false

@objc
public var mapViewImpl : String? = nil

#if RNMBX_11
var cancelables = Set<AnyCancelable>()
Expand All @@ -149,19 +172,32 @@ open class RNMBXMapView: UIView {

var _mapView: MapView! = nil
func createMapView() {
#if RNMBX_11
_mapView = MapView(frame: self.bounds, mapInitOptions: MapInitOptions())
#else
let resourceOptions = ResourceOptions(accessToken: RNMBXModule.accessToken!)
_mapView = MapView(frame: frame, mapInitOptions: MapInitOptions(resourceOptions: resourceOptions))
#endif
_mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(_mapView)
if let mapViewImpl = mapViewImpl, let mapViewInstance = createAndAddMapViewImpl(mapViewImpl, self) {
_mapView = mapViewInstance
} else {
#if RNMBX_11
_mapView = MapView(frame: self.bounds, mapInitOptions: MapInitOptions())
#else
let resourceOptions = ResourceOptions(accessToken: RNMBXModule.accessToken!)
_mapView = MapView(frame: frame, mapInitOptions: MapInitOptions(resourceOptions: resourceOptions))
#endif
_mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(_mapView)
}

_mapView.gestures.delegate = self
setupEvents()
}


func createAndAddMapViewImpl(_ impl: String, _ view: RNMBXMapView) -> MapView? {
if let factory = RNMBXMapViewFactory.get(impl) {
return factory(impl, view) as? MapView;
} else {
Logger.log(level:.error, message: "No mapview factory registered for: \(impl)")
return nil
}
}

var mapView : MapView! {
get { return _mapView }
}
Expand Down
8 changes: 7 additions & 1 deletion scripts/autogenHelpers/DocJSONBuilder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ class DocJSONBuilder {
}

postprocess(component, name) {
// Remove all private methods and parse examples from docblock
// Remove all private methods, and properties and parse examples from docblock

if (!Array.isArray(component.methods)) {
return;
}

for (const [propName, prop] of Object.entries(component.props)) {
if (prop.description.includes('@private')) {
delete component.props[propName];
}
}

component.name = name;

// Main description
Expand Down
5 changes: 5 additions & 0 deletions src/components/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ type Props = ViewProps & {
* the onPress event for the taps that deselect the annotation. Default is false.
*/
deselectAnnotationOnTap?: boolean;

/**
* @private Experimental support for custom MapView instances
*/
mapViewImpl?: string;
};

type CallbablePropKeys =
Expand Down
2 changes: 2 additions & 0 deletions src/specs/RNMBXMapViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface NativeProps extends ViewProps {
onLongPress?: DirectEventHandler<OnPressEventType>;
onMapChange?: DirectEventHandler<OnMapChangeEventType>;
onCameraChanged?: DirectEventHandler<OnCameraChangedEventType>;

mapViewImpl?: OptionalProp<string>;
}

export default codegenNativeComponent<NativeProps>(
Expand Down

0 comments on commit a944fb6

Please sign in to comment.