Skip to content

Commit

Permalink
feat: Add StyleImport for v11
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas committed Sep 21, 2023
1 parent c72d4da commit 582f404
Show file tree
Hide file tree
Showing 24 changed files with 554 additions and 16 deletions.
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ To manually generate the changes, run `npm run generate`.

Notice, that changing the documentation in the individual <COMPONENT>.md within `/docs` will not suffice.
The correct way is the above described


## Generated Java files

`android/src/main/old-arch/` contains files generated with codegen scripts from `src/specs` to update those use the `codegen-old-arch.sh` script
1 change: 1 addition & 0 deletions __tests__/interface.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Public Interface', () => {
'Callout',
'Camera',
'UserLocation',
'StyleImport',

// modules
'offlineManager',
Expand Down
168 changes: 168 additions & 0 deletions android/src/main/java/com/rnmapbox/rnmbx/RCTMGLPackage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.rnmapbox.rnmbx

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.JavaScriptModule
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.uimanager.ViewManager
import com.rnmapbox.rnmbx.components.annotation.RCTMGLCalloutManager
import com.rnmapbox.rnmbx.components.annotation.RCTMGLMarkerViewManager
import com.rnmapbox.rnmbx.components.annotation.RCTMGLPointAnnotationManager
import com.rnmapbox.rnmbx.components.camera.RCTMGLCameraManager
import com.rnmapbox.rnmbx.components.images.RCTMGLImageManager
import com.rnmapbox.rnmbx.components.images.RCTMGLImagesManager
import com.rnmapbox.rnmbx.components.location.RCTMGLNativeUserLocationManager
import com.rnmapbox.rnmbx.components.mapview.NativeMapViewModule
import com.rnmapbox.rnmbx.components.mapview.RCTMGLAndroidTextureMapViewManager
import com.rnmapbox.rnmbx.components.mapview.RCTMGLMapViewManager
import com.rnmapbox.rnmbx.components.styles.RNMBXStyleImportManager
import com.rnmapbox.rnmbx.components.styles.atmosphere.RCTMGLAtmosphereManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLBackgroundLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLCircleLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLFillExtrusionLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLFillLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLHeatmapLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLLineLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLRasterLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLSkyLayerManager
import com.rnmapbox.rnmbx.components.styles.layers.RCTMGLSymbolLayerManager
import com.rnmapbox.rnmbx.components.styles.light.RCTMGLLightManager
import com.rnmapbox.rnmbx.components.styles.sources.RCTMGLImageSourceManager
import com.rnmapbox.rnmbx.components.styles.sources.RCTMGLRasterDemSourceManager
import com.rnmapbox.rnmbx.components.styles.sources.RCTMGLRasterSourceManager
import com.rnmapbox.rnmbx.components.styles.sources.RCTMGLShapeSourceManager
import com.rnmapbox.rnmbx.components.styles.sources.RCTMGLVectorSourceManager
import com.rnmapbox.rnmbx.components.styles.terrain.RCTMGLTerrainManager
import com.rnmapbox.rnmbx.modules.RCTMGLLocationModule
import com.rnmapbox.rnmbx.modules.RCTMGLLogging
import com.rnmapbox.rnmbx.modules.RCTMGLModule
import com.rnmapbox.rnmbx.modules.RCTMGLOfflineModule
import com.rnmapbox.rnmbx.modules.RCTMGLSnapshotModule

class RCTMGLPackage : TurboReactPackage() {
override fun getModule(
s: String,
reactApplicationContext: ReactApplicationContext
): NativeModule? {
when (s) {
RCTMGLModule.REACT_CLASS -> return RCTMGLModule(reactApplicationContext)
RCTMGLLocationModule.REACT_CLASS -> return RCTMGLLocationModule(reactApplicationContext)
RCTMGLOfflineModule.REACT_CLASS -> return RCTMGLOfflineModule(reactApplicationContext)
RCTMGLSnapshotModule.REACT_CLASS -> return RCTMGLSnapshotModule(reactApplicationContext)
RCTMGLLogging.REACT_CLASS -> return RCTMGLLogging(reactApplicationContext)
NativeMapViewModule.NAME -> return NativeMapViewModule(reactApplicationContext)
}
return null
}

@Deprecated("")
fun createJSModules(): List<Class<out JavaScriptModule?>> {
return emptyList()
}

override fun createViewManagers(reactApplicationContext: ReactApplicationContext): List<ViewManager<*, *>> {
val managers: MutableList<ViewManager<*, *>> = ArrayList()

// components
managers.add(RCTMGLCameraManager(reactApplicationContext))
managers.add(RCTMGLAndroidTextureMapViewManager(reactApplicationContext))
managers.add(RCTMGLMapViewManager(reactApplicationContext))
managers.add(RNMBXStyleImportManager(reactApplicationContext))

// annotations
managers.add(RCTMGLMarkerViewManager(reactApplicationContext))
managers.add(RCTMGLPointAnnotationManager(reactApplicationContext))
managers.add(RCTMGLCalloutManager())
managers.add(RCTMGLNativeUserLocationManager())

// sources
managers.add(RCTMGLVectorSourceManager(reactApplicationContext))
managers.add(RCTMGLShapeSourceManager(reactApplicationContext))
managers.add(RCTMGLRasterDemSourceManager(reactApplicationContext))
managers.add(RCTMGLRasterSourceManager(reactApplicationContext))
managers.add(RCTMGLImageSourceManager())

// images
managers.add(RCTMGLImagesManager(reactApplicationContext))
managers.add(RCTMGLImageManager(reactApplicationContext))

// layers
managers.add(RCTMGLFillLayerManager())
managers.add(RCTMGLFillExtrusionLayerManager())
managers.add(RCTMGLHeatmapLayerManager())
managers.add(RCTMGLLineLayerManager())
managers.add(RCTMGLCircleLayerManager())
managers.add(RCTMGLSymbolLayerManager())
managers.add(RCTMGLRasterLayerManager())
managers.add(RCTMGLSkyLayerManager())
managers.add(RCTMGLTerrainManager())
managers.add(RCTMGLAtmosphereManager())
managers.add(RCTMGLBackgroundLayerManager())
managers.add(RCTMGLLightManager())
return managers
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
val isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
moduleInfos[RCTMGLModule.REACT_CLASS] = ReactModuleInfo(
RCTMGLModule.REACT_CLASS,
RCTMGLModule.REACT_CLASS,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
false // isTurboModule
)
moduleInfos[RCTMGLLocationModule.REACT_CLASS] = ReactModuleInfo(
RCTMGLLocationModule.REACT_CLASS,
RCTMGLLocationModule.REACT_CLASS,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
false // isTurboModule
)
moduleInfos[RCTMGLOfflineModule.REACT_CLASS] = ReactModuleInfo(
RCTMGLOfflineModule.REACT_CLASS,
RCTMGLOfflineModule.REACT_CLASS,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
false // isTurboModule
)
moduleInfos[RCTMGLSnapshotModule.REACT_CLASS] = ReactModuleInfo(
RCTMGLSnapshotModule.REACT_CLASS,
RCTMGLSnapshotModule.REACT_CLASS,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
false // isTurboModule
)
moduleInfos[RCTMGLLogging.REACT_CLASS] = ReactModuleInfo(
RCTMGLLogging.REACT_CLASS,
RCTMGLLogging.REACT_CLASS,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
false // isTurboModule
)
moduleInfos[NativeMapViewModule.NAME] = ReactModuleInfo(
NativeMapViewModule.NAME,
NativeMapViewModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
)
moduleInfos
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
feature = childView
} else if (childView is RCTLayer<*>) {
feature = childView as AbstractMapFeature?
} else if (childView is AbstractMapFeature) {
feature = childView as AbstractMapFeature
} else if (childView is ViewGroup) {
val children = childView
Logger.w(LOG_TAG, "Adding non map components as a child of a map is deprecated!")
Expand All @@ -448,7 +450,6 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie

val addToMap = styleLoaded


var entry = FeatureEntry(feature, childView, false)
if (addToMap) {
feature?.addToMap(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mapbox.rctmgl.components.styles

import android.content.Context
import android.view.ViewGroup
import com.mapbox.bindgen.Value
import com.mapbox.rctmgl.components.AbstractMapFeature
import com.mapbox.rctmgl.components.mapview.RCTMGLMapView

class RNMBXStyleImport(context: Context) : AbstractMapFeature(context) {
var id: String? = null;

var config: HashMap<String, Value> = hashMapOf()
set(value: HashMap<String, Value>) {
field = value
if (mMapView != null) { updateConfig() }
}

override fun addToMap(mapView: RCTMGLMapView) {
super.addToMap(mapView)
updateConfig()
}

fun updateConfig() {
withMapView {mapView ->
id?.let { id ->
val config = this.config
if (config.isNotEmpty()) {
mapView.mapView.getMapboxMap().getStyle()
?.setStyleImportConfigProperties(id, config)
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mapbox.rctmgl.components.styles

import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableType
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNMBXStyleImportManagerInterface
import com.mapbox.rctmgl.components.AbstractEventEmitter
import com.mapbox.rctmgl.components.mapview.RCTMGLMapView
import com.mapbox.bindgen.Value
import com.mapbox.rctmgl.utils.Logger
import com.mapbox.rctmgl.utils.extensions.toValueHashMap
import org.json.JSONObject

class RNMBXStyleImportManager(context: ReactApplicationContext) :
AbstractEventEmitter<RNMBXStyleImport>(context),
RNMBXStyleImportManagerInterface<RNMBXStyleImport> {
override fun customEvents(): Map<String, String>? {
return MapBuilder.builder<String, String>().build()
}

override fun getName(): String {
return REACT_CLASS
}

override fun createViewInstance(context: ThemedReactContext): RNMBXStyleImport {
return RNMBXStyleImport(context)
}

companion object {
const val REACT_CLASS = "RNMBXStyleImport"
}

@ReactProp(name = "id")
override fun setId(view: RNMBXStyleImport, value: String?) {
if (value != null) {
view.id = value
}
}

@ReactProp(name = "existing")
override fun setExisting(view: RNMBXStyleImport, value: Boolean) {

}

@ReactProp(name = "config")
override fun setConfig(view: RNMBXStyleImport, value: Dynamic) {
if (value.type != ReadableType.Map) {
Logger.e(REACT_CLASS, "config expected Map but received: ${value.type}")
} else {
view.config = value.asMap().toValueHashMap()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.rnmapbox.rnmbx.utils.extensions

import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableType
import com.mapbox.bindgen.Value

fun ReadableMap.toValueHashMap(): HashMap<String, Value> {
var result = hashMapOf<String, Value>()
var iterator = keySetIterator()
while (iterator.hasNextKey()) {
val i = iterator.nextKey()

result[i] = when (getType(i)) {
ReadableType.Null -> Value.nullValue()
ReadableType.Boolean -> Value.valueOf(getBoolean(i))
ReadableType.Number -> Value.valueOf(getDouble(i))
ReadableType.String -> Value.valueOf(getString(i)!!)
ReadableType.Array -> getArray(i)!!.toValue()
ReadableType.Map -> getMap(i)!!.toValue()
}
}
return result
}

fun ReadableMap.toValue() : Value {
return Value.valueOf(toValueHashMap())
}

fun ReadableArray.toValue(): Value {
var result = ArrayList<Value>(size())

for (i in 0 until size()) {
result.add(
when (getType(i)) {
ReadableType.Null -> Value.nullValue()
ReadableType.Boolean -> Value.valueOf(getBoolean(i))
ReadableType.Number -> Value.valueOf(getDouble(i))
ReadableType.String -> Value.valueOf(getString(i))
ReadableType.Array -> getArray(i).toValue()
ReadableType.Map -> getMap(i).toValue()
})
}
return Value.valueOf(result)
}

fun Dynamic.toValue(): Value {
return when (type) {
ReadableType.Null -> Value.nullValue()
ReadableType.Boolean -> Value.valueOf(asBoolean())
ReadableType.Number -> Value.valueOf(asDouble())
ReadableType.String -> Value.valueOf(asString())
ReadableType.Array -> asArray().toValue()
ReadableType.Map -> asMap().toValue()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* This file should be updated after modifying `RNMBXAndroidTextureMapViewNativeComponent.ts`.
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaDelegate.js
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* This file should be updated after modifying `RNMBXAndroidTextureMapViewNativeComponent.ts`.
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaInterface.js
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* This file should be updated after modifying `MBXMapViewNativeComponent.ts`.
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaDelegate.js
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* This file should be updated after modifying `MBXMapViewNativeComponent.ts`.
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaInterface.js
*/
Expand Down
Loading

0 comments on commit 582f404

Please sign in to comment.