Skip to content

Commit ee8eedd

Browse files
authored
Improve getPropertyValue (#2998)
1 parent 125035d commit ee8eedd

File tree

21 files changed

+807
-2912
lines changed

21 files changed

+807
-2912
lines changed

extension-style/api/Release/metalava.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7957,10 +7957,12 @@ package com.mapbox.maps.extension.style.utils {
79577957
method public static com.mapbox.bindgen.Value toValue(com.mapbox.geojson.FeatureCollection);
79587958
method public static com.mapbox.bindgen.Value toValue(com.mapbox.geojson.Geometry);
79597959
method public static inline com.mapbox.maps.TransitionOptions transition(kotlin.jvm.functions.Function1<? super com.mapbox.maps.TransitionOptions.Builder,kotlin.Unit> block);
7960+
method public static <T> T! unwrap(com.mapbox.maps.StylePropertyValue, Class<T> clazz);
79607961
method public static inline <reified T> T! unwrap(com.mapbox.maps.StylePropertyValue);
79617962
method public static Object unwrapToAny(com.mapbox.bindgen.Value);
79627963
method public static com.mapbox.maps.extension.style.expressions.generated.Expression unwrapToExpression(com.mapbox.bindgen.Value);
79637964
method public static com.mapbox.maps.extension.style.types.StyleTransition unwrapToStyleTransition(com.mapbox.bindgen.Value);
7965+
method public static <T> T! unwrapToTyped(com.mapbox.bindgen.Value, Class<T> clazz);
79647966
method public static inline <reified T> T! unwrapToTyped(com.mapbox.bindgen.Value);
79657967
}
79667968

extension-style/api/extension-style.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6763,8 +6763,10 @@ public final class com/mapbox/maps/extension/style/utils/TypeUtilsKt {
67636763
public static final fun toValue (Lcom/mapbox/geojson/FeatureCollection;)Lcom/mapbox/bindgen/Value;
67646764
public static final fun toValue (Lcom/mapbox/geojson/Geometry;)Lcom/mapbox/bindgen/Value;
67656765
public static final fun transition (Lkotlin/jvm/functions/Function1;)Lcom/mapbox/maps/TransitionOptions;
6766+
public static final fun unwrap (Lcom/mapbox/maps/StylePropertyValue;Ljava/lang/Class;)Ljava/lang/Object;
67666767
public static final fun unwrapToAny (Lcom/mapbox/bindgen/Value;)Ljava/lang/Object;
67676768
public static final fun unwrapToExpression (Lcom/mapbox/bindgen/Value;)Lcom/mapbox/maps/extension/style/expressions/generated/Expression;
67686769
public static final fun unwrapToStyleTransition (Lcom/mapbox/bindgen/Value;)Lcom/mapbox/maps/extension/style/types/StyleTransition;
6770+
public static final fun unwrapToTyped (Lcom/mapbox/bindgen/Value;Ljava/lang/Class;)Ljava/lang/Object;
67696771
}
67706772

extension-style/src/main/java/com/mapbox/maps/extension/style/atmosphere/generated/Atmosphere.kt

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension-style/src/main/java/com/mapbox/maps/extension/style/layers/CustomLayer.kt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ class CustomLayer constructor(
7171
*
7272
* @return VISIBILITY as expression
7373
*/
74-
get() {
75-
getPropertyValue<Expression>("visibility")?.let {
76-
return it
77-
}
78-
return null
79-
}
74+
get() = getPropertyValue("visibility")
8075

8176
/**
8277
* Whether this layer is displayed.
@@ -117,9 +112,7 @@ class CustomLayer constructor(
117112
*
118113
* @return minzoom
119114
*/
120-
get() {
121-
return getPropertyValue("minzoom")
122-
}
115+
get() = getPropertyValue("minzoom")
123116

124117
/**
125118
* The minimum zoom level for the layer. At zoom levels less than the minzoom, the layer will be hidden.
@@ -152,9 +145,7 @@ class CustomLayer constructor(
152145
*
153146
* @return maxzoom
154147
*/
155-
get() {
156-
return getPropertyValue("maxzoom")
157-
}
148+
get() = getPropertyValue("maxzoom")
158149

159150
/**
160151
* The maximum zoom level for the layer. At zoom levels equal to or greater than the maxzoom, the layer will be hidden.
@@ -193,9 +184,7 @@ class CustomLayer constructor(
193184
*
194185
* @return slot
195186
*/
196-
get() {
197-
return getPropertyValue("slot")
198-
}
187+
get() = getPropertyValue("slot")
199188

200189
override fun addPersistentLayer(
201190
delegate: MapboxStyleManager,

extension-style/src/main/java/com/mapbox/maps/extension/style/layers/Layer.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.mapbox.maps.extension.style.layers.generated.BackgroundLayer
1313
import com.mapbox.maps.extension.style.layers.properties.PropertyValue
1414
import com.mapbox.maps.extension.style.layers.properties.generated.Visibility
1515
import com.mapbox.maps.extension.style.utils.unwrap
16+
import com.mapbox.maps.extension.style.utils.unwrapToExpressionOrLiteralExpression
1617

1718
/**
1819
* Base class for the different Layer types
@@ -223,13 +224,17 @@ abstract class Layer : StyleContract.StyleLayerExtension {
223224
}
224225

225226
internal inline fun <reified T> getPropertyValue(propertyName: String): T? {
227+
return getPropertyValueWithType(propertyName, T::class.java)
228+
}
229+
230+
private fun <T> getPropertyValueWithType(propertyName: String, clazz: Class<T>): T? {
226231
delegate?.let {
227232
return try {
228-
it.getStyleLayerProperty(layerId, propertyName).unwrap()
233+
it.getStyleLayerProperty(layerId, propertyName).unwrap(clazz)
229234
} catch (e: RuntimeException) {
230235
// logging an error is misleading as it is valid to set a property
231236
// with a concrete value (e.g. Double) and then read it as an expression
232-
if (T::class != Expression::class) {
237+
if (clazz != Expression::class.java) {
233238
Log.e(
234239
TAG,
235240
"Get layer property=$propertyName for layerId=$layerId failed: ${e.message}. " +
@@ -242,6 +247,22 @@ abstract class Layer : StyleContract.StyleLayerExtension {
242247
throw MapboxStyleException("Couldn't get $propertyName: layer is not added to style yet.")
243248
}
244249

250+
internal fun getPropertyValueAsExpressionOrLiteralExpression(propertyName: String): Expression? {
251+
delegate?.let {
252+
return try {
253+
it.getStyleLayerProperty(layerId, propertyName).unwrapToExpressionOrLiteralExpression()
254+
} catch (e: RuntimeException) {
255+
Log.e(
256+
TAG,
257+
"Get layer property=$propertyName for layerId=$layerId failed: ${e.message}. " +
258+
"Value obtained: ${it.getStyleLayerProperty(layerId, propertyName)}"
259+
)
260+
null
261+
}
262+
}
263+
throw MapboxStyleException("Couldn't get $propertyName: layer is not added to style yet.")
264+
}
265+
245266
/**
246267
* Returns a human readable string that includes the cached properties of the layer.
247268
*

extension-style/src/main/java/com/mapbox/maps/extension/style/layers/generated/BackgroundLayer.kt

Lines changed: 19 additions & 77 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)