Skip to content

Commit

Permalink
feat: migrate components to new arch (#3136)
Browse files Browse the repository at this point in the history
* feat: migrate js code of components

* feat: migrate Android code of components

* feat: migrate iOS code of components

* fix: remove mapFeature and duplicate manager

* feat: add Image module and fix tests
  • Loading branch information
WoLewicki authored Oct 26, 2023
1 parent 7ed9e28 commit d2c55d7
Show file tree
Hide file tree
Showing 72 changed files with 1,219 additions and 288 deletions.
11 changes: 11 additions & 0 deletions android/src/main/java/com/rnmapbox/rnmbx/RNMBXPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotationManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotationModule
import com.rnmapbox.rnmbx.components.camera.RNMBXCameraManager
import com.rnmapbox.rnmbx.components.images.RNMBXImageManager
import com.rnmapbox.rnmbx.components.images.RNMBXImageModule
import com.rnmapbox.rnmbx.components.images.RNMBXImagesManager
import com.rnmapbox.rnmbx.components.location.RNMBXNativeUserLocationManager
import com.rnmapbox.rnmbx.components.mapview.NativeMapViewModule
Expand Down Expand Up @@ -69,6 +70,7 @@ class RNMBXPackage : TurboReactPackage() {
RNMBXLogging.REACT_CLASS -> return RNMBXLogging(reactApplicationContext)
NativeMapViewModule.NAME -> return NativeMapViewModule(reactApplicationContext, getViewTagResolver(reactApplicationContext))
RNMBXShapeSourceModule.NAME -> return RNMBXShapeSourceModule(reactApplicationContext, getViewTagResolver(reactApplicationContext))
RNMBXImageModule.NAME -> return RNMBXImageModule(reactApplicationContext, getViewTagResolver(reactApplicationContext))
RNMBXPointAnnotationModule.NAME -> return RNMBXPointAnnotationModule(reactApplicationContext, getViewTagResolver(reactApplicationContext))
}
return null
Expand Down Expand Up @@ -188,6 +190,15 @@ class RNMBXPackage : TurboReactPackage() {
false, // isCxxModule
isTurboModule // isTurboModule
)
moduleInfos[RNMBXImageModule.NAME] = ReactModuleInfo(
RNMBXImageModule.NAME,
RNMBXImageModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
)
moduleInfos[RNMBXPointAnnotationModule.NAME] = ReactModuleInfo(
RNMBXPointAnnotationModule.NAME,
RNMBXPointAnnotationModule.NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.rnmapbox.rnmbx.components.images

import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.DynamicFromArray
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNMBXImageManagerInterface
import com.rnmapbox.rnmbx.components.AbstractEventEmitter
import com.rnmapbox.rnmbx.events.constants.EventKeys

class RNMBXImageManager(private val mContext: ReactApplicationContext) : AbstractEventEmitter<RNMBXImage>(
mContext
) {
), RNMBXImageManagerInterface<RNMBXImage> {
override fun getName(): String {
return "RNMBXImage"
}
Expand All @@ -27,41 +25,33 @@ mContext

// region React properties
@ReactProp(name="name")
fun setName(image: RNMBXImage, value: String) {
image.name = value
override fun setName(image: RNMBXImage, value: Dynamic) {
image.name = value.asString()
}

@ReactProp(name="sdf")
fun setSdf(image: RNMBXImage, value: Boolean) {
image.sdf = value
override fun setSdf(image: RNMBXImage, value: Dynamic) {
image.sdf = value.asBoolean()
}

@ReactProp(name="stretchX")
fun setStretchX(image: RNMBXImage, value: Dynamic) {
override fun setStretchX(image: RNMBXImage, value: Dynamic) {
image.stretchX = RNMBXImagesManager.convertStretch(value) ?: listOf()
}

@ReactProp(name="stretchY")
fun setStretchY(image: RNMBXImage, value: Dynamic) {
override fun setStretchY(image: RNMBXImage, value: Dynamic) {
image.stretchY = RNMBXImagesManager.convertStretch(value) ?: listOf()
}

@ReactProp(name="content")
fun setContent(image: RNMBXImage, value: Dynamic) {
override fun setContent(image: RNMBXImage, value: Dynamic) {
image.content = RNMBXImagesManager.convertContent(value)
}

@ReactProp(name="scale")
fun setScale(image: RNMBXImage, value: Double) {
image.scale = value
}
// endregion

// region React methods
override fun receiveCommand(root: RNMBXImage, commandId: String?, args: ReadableArray?) {
if (commandId == "refresh") {
root.refresh()
}
override fun setScale(image: RNMBXImage, value: Dynamic) {
image.scale = value.asDouble()
}
// endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.rnmapbox.rnmbx.components.images

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule
import com.rnmapbox.rnmbx.NativeRNMBXImageModuleSpec
import com.rnmapbox.rnmbx.utils.ViewTagResolver

@ReactModule(name = RNMBXImageModule.NAME)
class RNMBXImageModule(reactContext: ReactApplicationContext?, private val viewTagResolver: ViewTagResolver) :
NativeRNMBXImageModuleSpec(reactContext) {

companion object {
const val NAME = "RNMBXImageModule"
}

private fun withImageOnUIThread(viewRef: Double?, reject: Promise, fn: (RNMBXImage) -> Unit) {
if (viewRef == null) {
reject.reject(Exception("viewRef is null for RNMBXImage"))
} else {
viewTagResolver.withViewResolved(viewRef.toInt(), reject, fn)
}
}

@ReactMethod
override fun refresh(viewRef: Double?, promise: Promise) {
withImageOnUIThread(viewRef, promise) {
it.refresh()
promise.resolve(null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.facebook.react.bridge.*
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNMBXImagesManagerInterface
import com.mapbox.maps.ImageContent
import com.mapbox.maps.ImageStretches
import com.rnmapbox.rnmbx.components.AbstractEventEmitter
Expand All @@ -20,7 +21,7 @@ import java.util.*
class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
AbstractEventEmitter<RNMBXImages?>(
mContext
) {
), RNMBXImagesManagerInterface<RNMBXImages> {
override fun getName(): String {
return "RNMBXImages"
}
Expand Down Expand Up @@ -67,9 +68,9 @@ class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
}

@ReactProp(name = "images")
fun setImages(images: RNMBXImages, map: ReadableMap) {
override fun setImages(images: RNMBXImages, map: Dynamic) {
val imagesList = mutableListOf<Map.Entry<String, ImageEntry>>()
map.forEach { imageName, imageInfo ->
map.asMap().forEach { imageName, imageInfo ->
when (imageInfo) {
is ReadableMap -> {
val uri = imageInfo.getString("uri")
Expand Down Expand Up @@ -128,8 +129,8 @@ class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
}

@ReactProp(name = "hasOnImageMissing")
fun setHasOnImageMissing(images: RNMBXImages, value: Boolean?) {
images.setHasOnImageMissing(value!!)
override fun setHasOnImageMissing(images: RNMBXImages, value: Dynamic) {
images.setHasOnImageMissing(value.asBoolean())
}

fun toNativeImage(dynamic: Dynamic): NativeImage? {
Expand Down Expand Up @@ -165,10 +166,10 @@ class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
}

@ReactProp(name = "nativeImages")
fun setNativeImages(images: RNMBXImages, arr: ReadableArray) {
override fun setNativeImages(images: RNMBXImages, arr: Dynamic) {
val nativeImages = mutableListOf<NativeImage>();
for (i in 0 until arr.size()) {
val nativeImage = toNativeImage(arr.getDynamic(i))
for (i in 0 until arr.asArray().size()) {
val nativeImage = toNativeImage(arr.asArray().getDynamic(i))
if (nativeImage != null) {
nativeImages.add(nativeImage)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package com.rnmapbox.rnmbx.components.location

import com.facebook.react.bridge.Dynamic
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
import com.rnmapbox.rnmbx.utils.Logger
import com.facebook.react.viewmanagers.RNMBXNativeUserLocationManagerInterface
import javax.annotation.Nonnull

class RNMBXNativeUserLocationManager : ViewGroupManager<RNMBXNativeUserLocation>() {
class RNMBXNativeUserLocationManager : ViewGroupManager<RNMBXNativeUserLocation>(),
RNMBXNativeUserLocationManagerInterface<RNMBXNativeUserLocation> {
@Nonnull
override fun getName(): String {
return REACT_CLASS
}

@ReactProp(name = "androidRenderMode")
fun setAndroidRenderMode(userLocation: RNMBXNativeUserLocation, mode: String) {
when (mode) {
override fun setAndroidRenderMode(userLocation: RNMBXNativeUserLocation, mode: Dynamic) {
when (mode.asString()) {
"compass" -> userLocation.setAndroidRenderMode(RenderMode.COMPASS);
"gps" -> userLocation.setAndroidRenderMode(RenderMode.GPS);
"normal" -> userLocation.setAndroidRenderMode(RenderMode.NORMAL);
}
}

@ReactProp(name = "iosShowsUserHeadingIndicator")
override fun setIosShowsUserHeadingIndicator(view: RNMBXNativeUserLocation, value: Dynamic) {
// iOS only
}

@Nonnull
override fun createViewInstance(@Nonnull reactContext: ThemedReactContext): RNMBXNativeUserLocation {
return RNMBXNativeUserLocation(reactContext)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rnmapbox.rnmbx.components.styles.light

import com.facebook.react.bridge.Dynamic
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNMBXLightManagerInterface

class RNMBXLightManager : ViewGroupManager<RNMBXLight>(),
RNMBXLightManagerInterface<RNMBXLight> {
override fun getName(): String {
return REACT_CLASS
}

override fun createViewInstance(reactContext: ThemedReactContext): RNMBXLight {
return RNMBXLight(reactContext)
}

@ReactProp(name = "reactStyle")
override fun setReactStyle(light: RNMBXLight, reactStyle: Dynamic) {
light.setReactStyle(reactStyle.asMap())
}

companion object {
const val REACT_CLASS = "RNMBXLight"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.rnmapbox.rnmbx.components.styles.terrain

import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.Dynamic
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.RNMBXTerrainManagerInterface

class RNMBXTerrainManager : ViewGroupManager<RNMBXTerrain>() {
class RNMBXTerrainManager : ViewGroupManager<RNMBXTerrain>(),
RNMBXTerrainManagerInterface<RNMBXTerrain> {
override fun getName(): String {
return REACT_CLASS
}
Expand All @@ -14,19 +16,14 @@ class RNMBXTerrainManager : ViewGroupManager<RNMBXTerrain>() {
return RNMBXTerrain(reactContext)
}

@ReactProp(name = "id")
fun setId(layer: RNMBXTerrain, id: String?) {
layer.iD = id
}

@ReactProp(name = "sourceID")
fun setSourceID(layer: RNMBXTerrain, sourceID: String?) {
layer.setSourceID(sourceID)
override fun setSourceID(layer: RNMBXTerrain, sourceID: Dynamic) {
layer.setSourceID(sourceID.asString())
}

@ReactProp(name = "reactStyle")
fun setReactStyle(terrain: RNMBXTerrain, reactStyle: ReadableMap?) {
terrain.setReactStyle(reactStyle)
override fun setReactStyle(terrain: RNMBXTerrain, reactStyle: Dynamic) {
terrain.setReactStyle(reactStyle.asMap())
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* 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
*/

package com.facebook.react.viewmanagers;

import android.view.View;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.DynamicFromObject;
import com.facebook.react.uimanager.BaseViewManagerDelegate;
import com.facebook.react.uimanager.BaseViewManagerInterface;

public class RNMBXImageManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & RNMBXImageManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RNMBXImageManagerDelegate(U viewManager) {
super(viewManager);
}
@Override
public void setProperty(T view, String propName, @Nullable Object value) {
switch (propName) {
case "stretchX":
mViewManager.setStretchX(view, new DynamicFromObject(value));
break;
case "stretchY":
mViewManager.setStretchY(view, new DynamicFromObject(value));
break;
case "content":
mViewManager.setContent(view, new DynamicFromObject(value));
break;
case "sdf":
mViewManager.setSdf(view, new DynamicFromObject(value));
break;
case "name":
mViewManager.setName(view, new DynamicFromObject(value));
break;
case "scale":
mViewManager.setScale(view, new DynamicFromObject(value));
break;
default:
super.setProperty(view, propName, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* 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
*/

package com.facebook.react.viewmanagers;

import android.view.View;
import com.facebook.react.bridge.Dynamic;

public interface RNMBXImageManagerInterface<T extends View> {
void setStretchX(T view, Dynamic value);
void setStretchY(T view, Dynamic value);
void setContent(T view, Dynamic value);
void setSdf(T view, Dynamic value);
void setName(T view, Dynamic value);
void setScale(T view, Dynamic value);
}
Loading

0 comments on commit d2c55d7

Please sign in to comment.