From 6b57581edd202fcc24054097fb6650c647d1b375 Mon Sep 17 00:00:00 2001 From: Beer van der Drift Date: Wed, 11 Sep 2024 10:43:11 +0200 Subject: [PATCH] feat: allow merging StyleImport configs --- .../rnmbx/components/styles/RNMBXStyleImport.kt | 12 +++++++++--- .../components/styles/RNMBXStyleImportManager.kt | 5 +++++ .../RNMBXStyleImportManagerDelegate.java | 3 +++ .../RNMBXStyleImportManagerInterface.java | 1 + docs/StyleImport.md | 10 ++++++++++ ios/RNMBX/RNMBXStyleImport.swift | 15 +++++++++++++-- ios/RNMBX/RNMBXStyleImportManager.m | 1 + src/components/StyleImport.tsx | 6 ++++++ 8 files changed, 48 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImport.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImport.kt index 4f1f52d14..46109f636 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImport.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImport.kt @@ -10,6 +10,7 @@ import com.rnmapbox.rnmbx.v11compat.style.* class RNMBXStyleImport(context: Context) : AbstractMapFeature(context) { var id: String? = null; + var merge: Boolean = false; var config: HashMap = hashMapOf() set(value: HashMap) { @@ -27,11 +28,16 @@ class RNMBXStyleImport(context: Context) : AbstractMapFeature(context) { id?.let { id -> val config = this.config if (config.isNotEmpty()) { - mapView.mapView.getMapboxMap().getStyle() - ?.setStyleImportConfigProperties(id, config) + val mapStyle = mapView.mapView.getMapboxMap().getStyle() + if (merge) { + config.forEach { (key, value) -> + mapStyle?.setStyleImportConfigProperty(id, key, value) + } + } else { + mapStyle?.setStyleImportConfigProperties(id, config) + } } } } - } } diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImportManager.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImportManager.kt index fafc00bef..e82e25055 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImportManager.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/styles/RNMBXStyleImportManager.kt @@ -55,4 +55,9 @@ class RNMBXStyleImportManager(context: ReactApplicationContext) : view.config = value.asMap().toValueHashMap() } } + + @ReactProp(name = "merge") + override fun setMerge(view: RNMBXStyleImport, value: Boolean) { + view.merge = value + } } \ No newline at end of file diff --git a/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerDelegate.java b/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerDelegate.java index 5a867b7ed..50527803c 100644 --- a/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerDelegate.java +++ b/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerDelegate.java @@ -31,6 +31,9 @@ public void setProperty(T view, String propName, @Nullable Object value) { case "config": mViewManager.setConfig(view, new DynamicFromObject(value)); break; + case "merge": + mViewManager.setMerge(view, value == null ? false : (boolean) value); + break; default: super.setProperty(view, propName, value); } diff --git a/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerInterface.java b/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerInterface.java index af2991593..b50e8b38a 100644 --- a/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerInterface.java +++ b/android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXStyleImportManagerInterface.java @@ -17,4 +17,5 @@ public interface RNMBXStyleImportManagerInterface { void setId(T view, @Nullable String value); void setExisting(T view, boolean value); void setConfig(T view, Dynamic value); + void setMerge(T view, boolean value); } diff --git a/docs/StyleImport.md b/docs/StyleImport.md index 92e3eda9f..73cead0ea 100644 --- a/docs/StyleImport.md +++ b/docs/StyleImport.md @@ -49,6 +49,16 @@ See https://github.com/mapbox/mapbox-maps-ios/blob/main/Sources/MapboxMaps/Docum +### merge + +```tsx +boolean +``` +_required_ +whether to merge the style import with the existing style import config or replace it entirely. Defaults to `false` + + + diff --git a/ios/RNMBX/RNMBXStyleImport.swift b/ios/RNMBX/RNMBXStyleImport.swift index 6c30d4fbc..d56e4cf32 100644 --- a/ios/RNMBX/RNMBXStyleImport.swift +++ b/ios/RNMBX/RNMBXStyleImport.swift @@ -19,6 +19,9 @@ open class RNMBXStyleImport : UIView, RNMBXMapComponent { } } } + + @objc + var merge: Bool = false; public func waitForStyleLoad() -> Bool { true @@ -37,8 +40,16 @@ open class RNMBXStyleImport : UIView, RNMBXMapComponent { func apply(mapView: MapView) { if let config = config, let id = id { #if RNMBX_11 - logged("RNMBXStyleImport.setStyleImportConfigProperties id=\(id)") { - try mapView.mapboxMap.setStyleImportConfigProperties(for: id, configs: config) + if merge { + for (key, value) in config { + logged("RNMBXStyleImport.setStyleImportConfigProperty for id=\(id), config=\(key), value=\(value)") { + try mapView.mapboxMap.setStyleImportConfigProperty(for: id, config: key, value: value) + } + } + } else { + logged("RNMBXStyleImport.setStyleImportConfigProperties id=\(id)") { + try mapView.mapboxMap.setStyleImportConfigProperties(for: id, configs: config) + } } #else Logger.error("RNMBXStyleImport.setStyleImportConfigProperties is only implemented on v11") diff --git a/ios/RNMBX/RNMBXStyleImportManager.m b/ios/RNMBX/RNMBXStyleImportManager.m index 7b34cb646..86a76a1d5 100644 --- a/ios/RNMBX/RNMBXStyleImportManager.m +++ b/ios/RNMBX/RNMBXStyleImportManager.m @@ -6,5 +6,6 @@ @interface RCT_EXTERN_REMAP_MODULE(RNMBXStyleImport, RNMBXStyleImportManager, RC RCT_EXPORT_VIEW_PROPERTY(id, NSString) RCT_EXPORT_VIEW_PROPERTY(existing, BOOL) RCT_EXPORT_VIEW_PROPERTY(config, NSDictionary) +RCT_EXPORT_VIEW_PROPERTY(merge, BOOL) @end diff --git a/src/components/StyleImport.tsx b/src/components/StyleImport.tsx index de7e0b7c3..6519efa5a 100644 --- a/src/components/StyleImport.tsx +++ b/src/components/StyleImport.tsx @@ -21,6 +21,12 @@ type Props = { config: { [key: string]: string; }; + + /** + * Determines whether the style import config should merge with the existing style import config. + * @default false + */ + merge?: boolean; }; /**