Skip to content

Commit

Permalink
Give androidRenderMode precedence when determing rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
RmStorm committed Jan 27, 2025
1 parent 91d2e35 commit 6e60469
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O

// region bearing
var androidRenderMode: RenderMode? = null
set(value) {
field = value
_apply()
}

var puckBearing: PuckBearing? = null
set(value) {
field = value
_apply()
}

var puckBearingEnabled: Boolean? = null
set(value) {
field = value
_apply()
}

// endregion

enum class PuckImagePart {
Expand Down Expand Up @@ -125,36 +140,42 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O

private fun _apply(mapView: MapView) {
val location2 = mapView.location2;

val withBearing = puckBearingEnabled ?: when (androidRenderMode ?: RenderMode.NORMAL) {
RenderMode.GPS -> true
RenderMode.COMPASS -> true
RenderMode.NORMAL -> false
// Log a warning if both puckBearingEnabled and androidRenderMode are provided
if (puckBearingEnabled != null && androidRenderMode != null) {
Logger.e(LOG_TAG, "Both `puckBearingEnabled` and `androidRenderMode` are provided. `androidRenderMode` takes precedence, and `puckBearingEnabled` will be ignored.")
}

// Always start with the default puck
location2.locationPuck = createDefault2DPuck(withBearing = withBearing)
val withBearing = androidRenderMode?.let { it != RenderMode.NORMAL } ?: (puckBearingEnabled == true)
val puck = createDefault2DPuck(withBearing = withBearing)

// If custom images are provided, overwrite the corresponding fields
// Update the bearing image based on androidRenderMode
androidRenderMode?.let { renderMode ->
when (renderMode) {
RenderMode.GPS -> puck.bearingImage = AppCompatResourcesV11.getDrawableImageHolder(context,LR.drawable.mapbox_user_bearing_icon)
RenderMode.COMPASS -> puck.bearingImage = AppCompatResourcesV11.getDrawableImageHolder(context,LR.drawable.mapbox_user_puck_icon)
RenderMode.NORMAL -> puck.bearingImage = AppCompatResourcesV11.getDrawableImageHolder(context,LR.drawable.mapbox_user_stroke_icon)
}
}

// Overwrite images if custom ones are provided
if (images.isNotEmpty()) {
(location2.locationPuck as? LocationPuck2D)?.apply {
(puck as? LocationPuck2D)?.apply {
topImage = images[PuckImagePart.TOP] ?: topImage
bearingImage = images[PuckImagePart.BEARING] ?: bearingImage
shadowImage = images[PuckImagePart.SHADOW] ?: shadowImage
scaleExpression = scale?.toJson() ?: scaleExpression
}
}

// If visibility is false, overwrite the images with empty placeholders
// Handle visibility: use empty placeholders if not visible
if (!visible) {
Logger.e(LOG_TAG, "Setting puck to invisible")
val empty = AppCompatResourcesV11.getDrawableImageHolder(mContext, R.drawable.empty)
(location2.locationPuck as? LocationPuck2D)?.apply {
topImage = empty
bearingImage = empty
shadowImage = empty
}
(puck as? LocationPuck2D)?.apply {topImage = empty; bearingImage = empty; shadowImage = empty}
}

location2.locationPuck = puck

this.puckBearing?.let {
location2.puckBearing = it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ class RNMBXNativeUserLocationManager : ViewGroupManager<RNMBXNativeUserLocation>
override fun setAndroidRenderMode(userLocation: RNMBXNativeUserLocation, mode: Dynamic) {
if (!mode.isNull) {
Logger.e("RNMBXNativeUserLocationManager", "androidRenderMode is deprecated, use puckBearing instead")
} else {
userLocation.androidRenderMode = null
return
}
when (mode.asString()) {
"compass" -> userLocation.androidRenderMode = RenderMode.COMPASS
"gps" -> userLocation.androidRenderMode = RenderMode.GPS
"normal" -> userLocation.androidRenderMode = RenderMode.NORMAL
else -> {
Logger.e("RNMBXNativeUserLocationManager", "Invalid androidRenderMode value: ${mode.asString()}")
userLocation.androidRenderMode = null
}
}
}

Expand Down

0 comments on commit 6e60469

Please sign in to comment.