Skip to content

Commit

Permalink
Fix: Android User Location / Camera issues (#3363)
Browse files Browse the repository at this point in the history
* Android User Location/ Camera Fixes

- Fixed puck images not being fetched after initial addToMap
- Fixed puck images not being correctly applied when changed on the fly
- Fixed camera follow props not properly accepting null/undefined values
- Fixed crash in ExpressionParser by wrapping in a try/catch

* Update ExpressionParser.java

* Use the correct logger in expression parser
  • Loading branch information
mysport12 authored Feb 13, 2024
1 parent 70826c7 commit 1ea117a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
_updateViewportState()
}

fun setFollowZoomLevel(zoomLevel: Double) {
fun setFollowZoomLevel(zoomLevel: Double?) {
mFollowZoomLevel = zoomLevel
_updateViewportState();
}

fun setFollowPitch(pitch: Double) {
fun setFollowPitch(pitch: Double?) {
mFollowPitch = pitch
_updateViewportState();
}

fun setFollowHeading(heading: Double) {
fun setFollowHeading(heading: Double?) {
mFollowHeading = heading
_updateViewportState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.rnmapbox.rnmbx.components.AbstractEventEmitter
import com.rnmapbox.rnmbx.components.camera.CameraStop.Companion.fromReadableMap
import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLngBounds
import com.rnmapbox.rnmbx.utils.extensions.asBooleanOrNull
import com.rnmapbox.rnmbx.utils.extensions.asDoubleOrNull
import com.rnmapbox.rnmbx.utils.extensions.asStringOrNull

class RNMBXCameraManager(private val mContext: ReactApplicationContext) :
AbstractEventEmitter<RNMBXCamera?>(
Expand Down Expand Up @@ -71,22 +73,22 @@ class RNMBXCameraManager(private val mContext: ReactApplicationContext) :

@ReactProp(name = "followUserMode")
override fun setFollowUserMode(camera: RNMBXCamera, value: Dynamic) {
camera.setFollowUserMode(value.asString())
camera.setFollowUserMode(value.asStringOrNull())
}

@ReactProp(name = "followZoomLevel")
override fun setFollowZoomLevel(camera: RNMBXCamera, value: Dynamic) {
camera.setFollowZoomLevel(value.asDouble())
camera.setFollowZoomLevel(value.asDoubleOrNull())
}

@ReactProp(name = "followPitch")
override fun setFollowPitch(camera: RNMBXCamera, value: Dynamic) {
camera.setFollowPitch(value.asDouble())
camera.setFollowPitch(value.asDoubleOrNull())
}

@ReactProp(name = "followHeading")
override fun setFollowHeading(camera: RNMBXCamera, value: Dynamic) {
camera.setFollowHeading(value.asDouble())
camera.setFollowHeading(value.asDoubleOrNull())
}

@ReactProp(name = "followPadding")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class RenderMode {
class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), OnMapReadyCallback, Style.OnStyleLoaded {
private var mEnabled = true
private var mMap: MapboxMap? = null
private var mMBXMapView: RNMBXMapView? = null
private var mRenderMode : RenderMode = RenderMode.NORMAL;
private var mContext : Context = context

Expand All @@ -63,7 +64,7 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
SHADOW
}

private var imageNames = mutableMapOf<PuckImagePart, String>()
private var imageNames = mutableMapOf<PuckImagePart, String?>()
private var subscriptions = mutableMapOf<PuckImagePart, Subscription>()
private var images = mutableMapOf<PuckImagePart, ImageHolder>()

Expand Down Expand Up @@ -98,23 +99,10 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
}

private fun imageNameUpdated(image: PuckImagePart, name: String?) {
if (name != null) {
imageNames[image] = name
} else {
imageNames.remove(image)
}
subscriptions[image]?.let {
it.cancel()
}
subscriptions.remove(image)

if (name == null) {
imageUpdated(image, null)
return
imageNames[image] = name
mMBXMapView?.let {
_fetchImages(it)
}

imageManager?.let { subscribe(it, image, name) }

}

private fun imageUpdated(image: PuckImagePart, imageHolder: ImageHolder?) {
Expand Down Expand Up @@ -208,6 +196,7 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
mapView.getMapboxMap()
mapView.getMapAsync(this)
mMapView?.locationComponentManager?.showNativeUserLocation(true)
mMBXMapView = mapView
_fetchImages(mapView)
_apply()
}
Expand All @@ -216,6 +205,7 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
mEnabled = false
mMapView?.locationComponentManager?.showNativeUserLocation(false)
mMap?.getStyle(this)
mMBXMapView = null
return super.removeFromMap(mapView, reason)
}

Expand Down Expand Up @@ -244,7 +234,6 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
subscriptions.remove(image)
Logger.e("RNMBXNativeUserLocation", "subscribe: there is alread a subscription for image: $image")
}

subscriptions[image] = imageManager.subscribe(name, Resolver { _, imageData ->
imageUpdated(image, imageData.toImageHolder())
})
Expand All @@ -260,19 +249,27 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
private fun _fetchImages(map: RNMBXMapView) {
map.mapView?.getMapboxMap()?.getStyle()?.let { style ->
imageNames.forEach { (part,name) ->
if (style.hasStyleImage(name)) {
style.getStyleImage(name)?.let { image ->
images[part] = image.toImageHolder()
if (name != null) {
if (style.hasStyleImage(name)) {
style.getStyleImage(name)?.let { image ->
images[part] = image.toImageHolder()
}
} else {
images.remove(part)
}
} else {
images.remove(part)
}
}
}

removeSubscriptions()
val imageManager = map.imageManager
this.imageManager = imageManager
_apply()
imageNames.forEach { (part,name) ->
subscribe(imageManager, part, name)
if (name != null) {
subscribe(imageManager, part, name)
}
}
}
// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.mapbox.maps.extension.style.expressions.generated.Expression;
import com.rnmapbox.rnmbx.utils.Logger;

import java.util.Locale;

import javax.annotation.Nullable;

public class ExpressionParser {
static final String LOG_TAG = "RNMBXMapView";
static final String TYPE_STRING = "string";
static final String TYPE_ARRAY = "array";
static final String TYPE_NUMBER = "number";
Expand All @@ -22,10 +24,14 @@ public class ExpressionParser {
if (rawExpressions == null || rawExpressions.size() == 0) {
return null;
}

JsonArray array = ConvertUtils.toJsonArray(rawExpressions);
String jsonString = new Gson().toJson(array);
return Expression.fromRaw(jsonString);
try {
JsonArray array = ConvertUtils.toJsonArray(rawExpressions);
String jsonString = new Gson().toJson(array);
return Expression.fromRaw(jsonString);
} catch (Exception e) {
Logger.e(LOG_TAG, "An error occurred while attempting to parse the expression", e);
return null;
}
}

public static @Nullable Expression fromTyped(ReadableMap rawExpressions) {
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/rnmapbox/rnmbx/utils/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ object Logger {
}
}

fun e(tag: String, msg: String, tr: Throwable) {
@JvmStatic fun e(tag: String, msg: String, tr: Throwable) {
if (logLevel <= Log.ERROR) {
logger.e(tag, msg, tr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ fun Dynamic.asBooleanOrNull(): Boolean? {
} else {
asBoolean()
}
}

fun Dynamic.asDoubleOrNull(): Double? {
return if (isNull) {
null
} else {
asDouble()
}
}

fun Dynamic.asStringOrNull(): String? {
return if (isNull) {
null
} else {
asString()
}
}

0 comments on commit 1ea117a

Please sign in to comment.