Skip to content

Commit

Permalink
chore(ios,v10): various fixes, warn if below/aboveLayerID doesn't exi… (
Browse files Browse the repository at this point in the history
rnmapbox#2694)

* chore(ios,v10): various fixes, warn if below/aboveLayerID doesn't exists.

* chore(android,v10): kotlin, added and require existing when referring to existing layers/sources
  • Loading branch information
mfazekas authored Mar 14, 2023
1 parent 87266c2 commit 5d89372
Show file tree
Hide file tree
Showing 98 changed files with 1,955 additions and 1,927 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.facebook.react.bridge.ReadableMap
import com.mapbox.maps.MapboxMap
import com.mapbox.rctmgl.utils.ImageEntry
import com.mapbox.rctmgl.utils.DownloadMapImageTask
import com.mapbox.rctmgl.utils.Logger
import java.util.AbstractMap
import java.util.ArrayList

Expand Down Expand Up @@ -39,11 +40,12 @@ class RCTMGLStyle(private val mContext: Context, reactStyle: ReadableMap?, map:
}

@JvmOverloads
fun addImage(styleValue: RCTMGLStyleValue, callback: DownloadMapImageTask.OnAllImagesLoaded? = null) {
fun addImage(styleValue: RCTMGLStyleValue, styleKey: String, callback: DownloadMapImageTask.OnAllImagesLoaded? = null) {
if (!styleValue.shouldAddImage()) {
callback?.onAllImagesLoaded()
return
}
Logger.w(LOG_TAG,"Deprecated: Image in style is deprecated, use images component instead. key: $styleKey [image-in-style-deprecated]")
val uriStr = styleValue.imageURI
val images = arrayOf<Map.Entry<String, ImageEntry>>(
AbstractMap.SimpleEntry<String, ImageEntry>(
Expand All @@ -59,4 +61,8 @@ class RCTMGLStyle(private val mContext: Context, reactStyle: ReadableMap?, map:
mReactStyle = reactStyle
mMap = map
}

companion object {
const val LOG_TAG = "RCTMGLStyle"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void setFillLayerStyle(final FillLayer layer, RCTMGLStyle style) {
RCTMGLStyleFactory.setFillTranslateAnchor(layer, styleValue);
break;
case "fillPattern":
style.addImage(styleValue, new DownloadMapImageTask.OnAllImagesLoaded() {
style.addImage(styleValue, styleKey, new DownloadMapImageTask.OnAllImagesLoaded() {
@Override
public void onAllImagesLoaded() {
try {
Expand Down Expand Up @@ -172,7 +172,7 @@ public static void setLineLayerStyle(final LineLayer layer, RCTMGLStyle style) {
RCTMGLStyleFactory.setLineDasharray(layer, styleValue);
break;
case "linePattern":
style.addImage(styleValue, new DownloadMapImageTask.OnAllImagesLoaded() {
style.addImage(styleValue, styleKey, new DownloadMapImageTask.OnAllImagesLoaded() {
@Override
public void onAllImagesLoaded() {
try {
Expand Down Expand Up @@ -240,7 +240,7 @@ public static void setSymbolLayerStyle(final SymbolLayer layer, RCTMGLStyle styl
RCTMGLStyleFactory.setIconTextFitPadding(layer, styleValue);
break;
case "iconImage":
style.addImage(styleValue, new DownloadMapImageTask.OnAllImagesLoaded() {
style.addImage(styleValue, styleKey, new DownloadMapImageTask.OnAllImagesLoaded() {
@Override
public void onAllImagesLoaded() {
try {
Expand Down Expand Up @@ -573,7 +573,7 @@ public static void setFillExtrusionLayerStyle(final FillExtrusionLayer layer, RC
RCTMGLStyleFactory.setFillExtrusionTranslateAnchor(layer, styleValue);
break;
case "fillExtrusionPattern":
style.addImage(styleValue, new DownloadMapImageTask.OnAllImagesLoaded() {
style.addImage(styleValue, styleKey, new DownloadMapImageTask.OnAllImagesLoaded() {
@Override
public void onAllImagesLoaded() {
try {
Expand Down Expand Up @@ -729,7 +729,7 @@ public static void setBackgroundLayerStyle(final BackgroundLayer layer, RCTMGLSt
RCTMGLStyleFactory.setBackgroundColorTransition(layer, styleValue);
break;
case "backgroundPattern":
style.addImage(styleValue, new DownloadMapImageTask.OnAllImagesLoaded() {
style.addImage(styleValue, styleKey, new DownloadMapImageTask.OnAllImagesLoaded() {
@Override
public void onAllImagesLoaded() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.mapbox.maps.extension.style.expressions.generated.Expression
import com.mapbox.maps.extension.style.layers.*
import com.mapbox.maps.extension.style.layers.properties.generated.Visibility
import com.mapbox.rctmgl.components.styles.layers.RCTLayer
import com.mapbox.rctmgl.components.styles.sources.RCTSource
import com.mapbox.rctmgl.modules.RCTMGLLogging
import com.mapbox.rctmgl.utils.ExpressionParser
import java.lang.ClassCastException
Expand All @@ -43,10 +44,19 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
protected var mLayer: T? = null
protected var mHadFilter = false

protected var mExisting : Boolean? = null

fun setSourceID(sourceID: String?) {
mSourceID = sourceID
}

fun checkID(): String? {
if (iD == null) {
Logger.w(LOG_TAG, "iD is null in layer")
}
return iD;
}

fun setAboveLayerID(aboveLayerID: String?) {
if (mAboveLayerID != null && mAboveLayerID == aboveLayerID) {
return
Expand Down Expand Up @@ -76,7 +86,7 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
mLayerIndex = layerIndex
if (mLayer != null) {
removeFromMap(mMapView!!)
addAtIndex(mLayerIndex!!)
addAtIndex(layerIndex)
}
}

Expand Down Expand Up @@ -121,6 +131,10 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
}
}

fun setExisting(existing: Boolean) {
mExisting = existing
}

fun add() {
if (!hasInitialized()) {
return
Expand Down Expand Up @@ -175,7 +189,7 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
if (!hasInitialized()) {
return
}
if (style == null) return
val style = this.style ?: return
val layerSize = style!!.styleLayers.size
if (index >= layerSize) {
FLog.e(
Expand All @@ -184,13 +198,16 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
)
index = layerSize - 1
}
style!!.addLayerAt(mLayer!!, index)
mMapView!!.layerAdded(mLayer!!)
val layer = mLayer ?: return
val mapView = mMapView ?: return
style.addLayerAt(layer, index)
mapView.layerAdded(layer)
}

protected fun insertLayer() {
if (style == null) return
if (style!!.getLayer(iD!!) != null) {
val style = this.style ?: return
val id = checkID() ?: return
if (style.styleLayerExists(id)) {
return // prevent adding a layer twice
}
if (mAboveLayerID != null) {
Expand Down Expand Up @@ -218,8 +235,8 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
// override if you want to update the filter
}

private fun getLayerAs(style: Style?, id: String?): T? {
val result = style!!.getLayer(iD!!)
private fun getLayerAs(style: Style, id: String?): T? {
val result = style.getLayer(iD!!)
return try {
result as T?
} catch (exception: ClassCastException) {
Expand All @@ -230,14 +247,24 @@ abstract class RCTLayer<T : Layer?>(protected var mContext: Context) : AbstractS
override fun addToMap(mapView: RCTMGLMapView) {
super.addToMap(mapView)
mMap = mapView.getMapboxMap()
if (style == null) return
val existingLayer = getLayerAs(style, iD)
val style = style ?: return
val id = checkID() ?: return

val exists = style.styleLayerExists(id)
var existingLayer: T? = null;
if (exists) {
if (mExisting == null) {
Logger.e(LOG_TAG, "Layer $id seems to refer to an existing layer but existing flag is not specified, this is deprecated")
}
existingLayer = getLayerAs(style, id)
}
if (existingLayer != null) {
mLayer = existingLayer
} else {
mLayer = makeLayer()
insertLayer()
}

addStyles()
if (mFilter != null) {
mHadFilter = true
Expand Down

This file was deleted.

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

import android.content.Context
import com.mapbox.maps.extension.style.layers.generated.BackgroundLayer
import com.mapbox.rctmgl.components.styles.RCTMGLStyle
import com.mapbox.rctmgl.components.styles.RCTMGLStyleFactory

class RCTMGLBackgroundLayer(context: Context?) : RCTLayer<BackgroundLayer?>(
context!!
) {
override fun makeLayer(): BackgroundLayer {
return BackgroundLayer(iD!!)
}

override fun addStyles() {
RCTMGLStyleFactory.setBackgroundLayerStyle(
mLayer,
RCTMGLStyle(context, mReactStyle, mMap!!)
)
}
}

This file was deleted.

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

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

class RCTMGLBackgroundLayerManager : ViewGroupManager<RCTMGLBackgroundLayer>() {
override fun getName(): String {
return REACT_CLASS
}

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

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

@ReactProp(name = "existing")
fun setExisting(layer: RCTMGLBackgroundLayer, existing: Boolean) {
layer.setExisting(existing)
}

@ReactProp(name = "sourceID")
fun setSourceID(layer: RCTMGLBackgroundLayer, sourceID: String?) {
layer.setSourceID(sourceID)
}

@ReactProp(name = "aboveLayerID")
fun setAboveLayerID(layer: RCTMGLBackgroundLayer, aboveLayerID: String?) {
layer.setAboveLayerID(aboveLayerID)
}

@ReactProp(name = "belowLayerID")
fun setBelowLayerID(layer: RCTMGLBackgroundLayer, belowLayerID: String?) {
layer.setBelowLayerID(belowLayerID)
}

@ReactProp(name = "layerIndex")
fun setLayerIndex(layer: RCTMGLBackgroundLayer, layerIndex: Int) {
layer.setLayerIndex(layerIndex)
}

@ReactProp(name = "minZoomLevel")
fun setMinZoomLevel(layer: RCTMGLBackgroundLayer, minZoomLevel: Double) {
layer.setMinZoomLevel(minZoomLevel)
}

@ReactProp(name = "maxZoomLevel")
fun setMaxZoomLevel(layer: RCTMGLBackgroundLayer, maxZoomLevel: Double) {
layer.setMaxZoomLevel(maxZoomLevel)
}

@ReactProp(name = "reactStyle")
fun setReactStyle(layer: RCTMGLBackgroundLayer, style: ReadableMap?) {
layer.setReactStyle(style)
}

companion object {
const val REACT_CLASS = "RCTMGLBackgroundLayer"
}
}
Loading

0 comments on commit 5d89372

Please sign in to comment.