Skip to content

Commit

Permalink
feat: rename native mapStyle prop
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiKilian committed Dec 16, 2024
1 parent 6989cd4 commit 1b5889b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class MLRNMapView extends MapView implements OnMapReadyCallback, MapLibre

private LocalizationPlugin mLocalizationPlugin;

private String mStyleURL;
private String mMapStyle;

private Integer mPreferredFramesPerSecond;
private boolean mLocalizeLabels;
Expand Down Expand Up @@ -169,7 +169,7 @@ public MLRNMapView(Context context, MLRNMapViewManager manager, MapLibreMapOptio

mHandler = new Handler();

mStyleURL = MLRNModule.DEFAULT_STYLE_URL;
mMapStyle = MLRNModule.DEFAULT_STYLE_URL;

setLifecycleListeners();

Expand Down Expand Up @@ -434,10 +434,10 @@ public boolean isJSONValid(String test) {
public void onMapReady(final MapLibreMap mapboxMap) {
mMap = mapboxMap;

if (isJSONValid(mStyleURL)) {
mMap.setStyle(new Style.Builder().fromJson(mStyleURL));
if (isJSONValid(mMapStyle)) {
mMap.setStyle(new Style.Builder().fromJson(mMapStyle));
} else {
mMap.setStyle(new Style.Builder().fromUri(mStyleURL));
mMap.setStyle(new Style.Builder().fromUri(mMapStyle));
}

reflow();
Expand Down Expand Up @@ -774,21 +774,21 @@ private float getDisplayDensity() {
return mContext.getResources().getDisplayMetrics().density;
}

public void setReactStyleURL(String styleURL) {
mStyleURL = styleURL;
public void setReactMapStyle(String mapStyle) {
mMapStyle = mapStyle;

if (mMap != null) {
removeAllSourcesFromMap();

if (isJSONValid(mStyleURL)) {
mMap.setStyle(new Style.Builder().fromJson(mStyleURL), new Style.OnStyleLoaded() {
if (isJSONValid(mMapStyle)) {
mMap.setStyle(new Style.Builder().fromJson(mMapStyle), new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
addAllSourcesToMap();
}
});
} else {
mMap.setStyle(styleURL, new Style.OnStyleLoaded() {
mMap.setStyle(mapStyle, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
addAllSourcesToMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.maplibre.reactnative.components.mapview;

import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -11,23 +10,17 @@
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import org.maplibre.android.geometry.LatLngBounds;
import org.maplibre.android.log.Logger;

import org.maplibre.android.maps.MapLibreMap;
import org.maplibre.reactnative.components.AbstractEventEmitter;
import org.maplibre.reactnative.events.constants.EventKeys;
import org.maplibre.reactnative.utils.ConvertUtils;
import org.maplibre.reactnative.utils.ExpressionParser;
import org.maplibre.reactnative.utils.GeoJSONUtils;
import org.maplibre.geojson.FeatureCollection;
import org.maplibre.geojson.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -111,9 +104,9 @@ public MLRNMapView getByReactTag(int reactTag) {

//region React Props

@ReactProp(name="styleURL")
public void setStyleURL(MLRNMapView mapView, String styleURL) {
mapView.setReactStyleURL(styleURL);
@ReactProp(name="mapStyle")
public void setMapStyle(MLRNMapView mapView, String mapStyle) {
mapView.setReactMapStyle(mapStyle);
}

@ReactProp(name="preferredFramesPerSecond")
Expand Down
2 changes: 1 addition & 1 deletion ios/MLRN/MLRNMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef void (^StyleLoadedBlock) (MLNStyle* __nonnull style);
@property (nonatomic, assign) NSInteger *reactCompassViewPosition;
@property (nonatomic, assign) CGPoint reactCompassViewMargins;

@property (nonatomic, copy) NSString *reactStyleURL;
@property (nonatomic, copy) NSString *reactMapStyle;
@property (nonatomic, assign) NSInteger reactPreferredFramesPerSecond;

@property (nonatomic, assign) MLNCoordinateBounds maxBounds;
Expand Down
14 changes: 7 additions & 7 deletions ios/MLRN/MLRNMapView.m
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ - (void)setReactContentInset:(NSArray<NSNumber *> *)reactContentInset
self.contentInset = UIEdgeInsetsMake(top, left, bottom, right);
}

- (void)setReactStyleURL:(NSString *)reactStyleURL
- (void)setReactMapStyle:(NSString *)reactMapStyle
{
_reactStyleURL = reactStyleURL;
_reactMapStyle = reactMapStyle;
[self _removeAllSourcesFromMap];
self.styleURL = [self _getStyleURLFromKey:_reactStyleURL];
self.styleURL = [self _getStyleURLFromKey:_reactMapStyle];
}

- (void)setReactPreferredFramesPerSecond:(NSInteger)reactPreferredFramesPerSecond
Expand Down Expand Up @@ -500,13 +500,13 @@ - (MLRNSource *)getTouchableSourceWithHighestZIndex:(NSArray<MLRNSource *> *)tou
return nil;
}

- (NSURL*)_getStyleURLFromKey:(NSString *)styleURL
- (NSURL*)_getStyleURLFromKey:(NSString *)mapStyle
{
NSURL *url = [NSURL URLWithString:styleURL];
NSURL *url = [NSURL URLWithString:mapStyle];
if (url) {
return url;
} else if (RCTJSONParse(styleURL, nil)) {
return [MLRNUtils styleURLFromStyleJSON:styleURL];
} else if (RCTJSONParse(mapStyle, nil)) {
return [MLRNUtils styleURLFromStyleJSON:mapStyle];
}
return url;
}
Expand Down
2 changes: 1 addition & 1 deletion ios/MLRN/MLRNMapViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ - (UIView *)view
RCT_REMAP_VIEW_PROPERTY(compassViewMargins, reactCompassViewMargins, CGPoint)

RCT_REMAP_VIEW_PROPERTY(contentInset, reactContentInset, NSArray)
RCT_REMAP_VIEW_PROPERTY(styleURL, reactStyleURL, NSString)
RCT_REMAP_VIEW_PROPERTY(mapStyle, reactMapStyle, NSString)
RCT_REMAP_VIEW_PROPERTY(preferredFramesPerSecond, reactPreferredFramesPerSecond, NSInteger)

RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
Expand Down
10 changes: 5 additions & 5 deletions src/components/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ type CallableProps = {
}[keyof MapViewProps];

interface NativeProps extends Omit<MapViewProps, "onPress" | "onLongPress"> {
styleURL?: string;
mapStyle?: string;
onPress(event: NativeSyntheticEvent<{ payload: GeoJSON.Feature }>): void;
onLongPress(event: NativeSyntheticEvent<{ payload: GeoJSON.Feature }>): void;
}
Expand Down Expand Up @@ -769,12 +769,12 @@ const MapView = memo(
const nativeProps = useMemo(() => {
const { mapStyle, ...otherProps } = props;

let styleURL = undefined;
let nativeMapStyle = undefined;
if (mapStyle) {
if (typeof mapStyle === "string") {
styleURL = mapStyle;
nativeMapStyle = mapStyle;
} else if (typeof mapStyle === "object") {
styleURL = JSON.stringify(mapStyle);
nativeMapStyle = JSON.stringify(mapStyle);
}
}

Expand All @@ -789,7 +789,7 @@ const MapView = memo(
surfaceView,
regionWillChangeDebounceTime,
regionDidChangeDebounceTime,
styleURL,
mapStyle: nativeMapStyle,
contentInset: contentInsetValue,
style: styles.matchParent,
};
Expand Down

0 comments on commit 1b5889b

Please sign in to comment.