Skip to content

Commit 068576c

Browse files
committed
fix: Error while updating prop shadowImage
1 parent d061296 commit 068576c

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

android/src/main/java/com/rnmapbox/rnmbx/components/location/RNMBXNativeUserLocationManager.kt

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import com.google.gson.Gson
1111
import com.google.gson.stream.JsonWriter
1212
import com.mapbox.bindgen.Value
1313
import com.mapbox.maps.extension.style.expressions.generated.Expression
14+
import com.rnmapbox.rnmbx.components.location.RNMBXNativeUserLocationManager.Companion.TAG
1415
import com.rnmapbox.rnmbx.rncompat.dynamic.*
1516
import com.rnmapbox.rnmbx.utils.Logger
17+
import com.rnmapbox.rnmbx.utils.extensions.asBooleanOrNull
18+
import com.rnmapbox.rnmbx.utils.extensions.asStringOrNull
1619
import com.rnmapbox.rnmbx.utils.extensions.toJsonArray
1720
import java.io.StringWriter
1821
import javax.annotation.Nonnull
@@ -27,51 +30,50 @@ class RNMBXNativeUserLocationManager : ViewGroupManager<RNMBXNativeUserLocation>
2730

2831
@ReactProp(name = "androidRenderMode")
2932
override fun setAndroidRenderMode(userLocation: RNMBXNativeUserLocation, mode: Dynamic) {
30-
if (!mode.isNull) {
31-
Logger.e("RNMBXNativeUserLocationManager", "androidRenderMode is deprecated, use puckBearing instead")
32-
}
33-
when (mode.asString()) {
34-
"compass" -> userLocation.androidRenderMode = RenderMode.COMPASS
35-
"gps" -> userLocation.androidRenderMode = RenderMode.GPS
36-
"normal" -> userLocation.androidRenderMode = RenderMode.NORMAL
33+
mode.asStringOrNull()?.let {
34+
Logger.e(TAG, "androidRenderMode is deprecated, use puckBearing instead")
35+
36+
when (it) {
37+
"compass" -> userLocation.androidRenderMode = RenderMode.COMPASS
38+
"gps" -> userLocation.androidRenderMode = RenderMode.GPS
39+
"normal" -> userLocation.androidRenderMode = RenderMode.NORMAL
40+
}
3741
}
3842
}
3943

4044
@ReactProp(name = "puckBearing")
4145
override fun setPuckBearing(view: RNMBXNativeUserLocation, value: Dynamic) {
42-
when (value?.asString()) {
46+
when (value.asStringOrNull()) {
4347
"heading" -> view.puckBearing = PuckBearing.HEADING
4448
"course" -> view.puckBearing = PuckBearing.COURSE
4549
null -> Unit
4650
else ->
47-
Logger.e("RNMBXNativeUserLocationManager", "unexpected value for puckBearing: $value")
51+
Logger.e(TAG, "unexpected value for puckBearing: $value")
4852
}
4953
}
5054

5155
@ReactProp(name = "puckBearingEnabled")
5256
override fun setPuckBearingEnabled(view: RNMBXNativeUserLocation, value: Dynamic) {
53-
if (!value.isNull) {
54-
if (value.type == ReadableType.Boolean) {
55-
view.puckBearingEnabled = value.asBoolean()
56-
} else {
57-
Logger.e("RNMBXNativeUserLocationManager", "unexpected value for puckBearingEnabled: $value")
58-
}
57+
value.asBooleanOrNull()?.let {
58+
view.puckBearingEnabled = it
59+
} ?: run {
60+
Logger.e(TAG, "unexpected value for puckBearingEnabled: $value")
5961
}
6062
}
6163

6264
@ReactProp(name = "topImage")
6365
override fun setTopImage(view: RNMBXNativeUserLocation, value: Dynamic?) {
64-
view.topImage = value?.asString()
66+
view.topImage = value?.asStringOrNull()
6567
}
6668

6769
@ReactProp(name = "bearingImage")
6870
override fun setBearingImage(view: RNMBXNativeUserLocation, value: Dynamic?) {
69-
view.bearingImage = value?.asString()
71+
view.bearingImage = value?.asStringOrNull()
7072
}
7173

7274
@ReactProp(name = "shadowImage")
7375
override fun setShadowImage(view: RNMBXNativeUserLocation, value: Dynamic?) {
74-
view.shadowImage = value?.asString()
76+
view.shadowImage = value?.asStringOrNull()
7577
}
7678

7779
@ReactProp(name = "scale", defaultDouble = 1.0)
@@ -98,11 +100,10 @@ class RNMBXNativeUserLocationManager : ViewGroupManager<RNMBXNativeUserLocation>
98100

99101
companion object {
100102
const val REACT_CLASS = "RNMBXNativeUserLocation"
103+
const val TAG = "RNMBXNativeUserLocationManager"
101104
}
102105
}
103106

104-
105-
106107
fun _convertToDoubleValueOrExpression(value: Dynamic?, name: String): Value? {
107108
if (value == null) {
108109
return null
@@ -111,7 +112,7 @@ fun _convertToDoubleValueOrExpression(value: Dynamic?, name: String): Value? {
111112
ReadableType.Array -> {
112113
val array = value.asArray()
113114
if (array == null) {
114-
Logger.e("RNMBXNativeUserLocationManager", "_convertToDoubleValueOrExpression: array is null for $name")
115+
Logger.e(TAG, "_convertToDoubleValueOrExpression: array is null for $name")
115116
return null
116117
}
117118
Expression.fromRaw(Gson().toJson(array.toJsonArray()))
@@ -120,10 +121,10 @@ fun _convertToDoubleValueOrExpression(value: Dynamic?, name: String): Value? {
120121
Value.valueOf(value.asDouble())
121122
else -> {
122123
Logger.e(
123-
"RNMBXNativeUserLocationmanager",
124-
"_convertToExpressionString: cannot convert $name to a double or double exrpession. $value"
124+
TAG,
125+
"_convertToExpressionString: cannot convert $name to a double or double expression. $value"
125126
)
126-
return null
127+
null
127128
}
128129
}
129130
}

0 commit comments

Comments
 (0)