Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use puckBearingEnabled prop in Android #3742

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.mapbox.maps.MapboxMap
import com.mapbox.maps.Style
import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants
import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck
import com.mapbox.maps.plugin.locationcomponent.location
import com.mapbox.maps.plugin.locationcomponent.R as LR
import com.rnmapbox.rnmbx.R
Expand Down Expand Up @@ -54,8 +55,22 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O

// region bearing
var androidRenderMode: RenderMode? = null
set(value) {
field = value
_apply()
}
Comment on lines +58 to +61
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary for hot reloading to work on this property.


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 @@ -124,29 +139,45 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O

private fun _apply(mapView: MapView) {
val location2 = mapView.location2;
// Log a warning if both puckBearingEnabled and androidRenderMode are provided
if (puckBearingEnabled != null && androidRenderMode != null) {
Logger.w(LOG_TAG, "Both `puckBearingEnabled` and `androidRenderMode` are provided. `androidRenderMode` takes precedence, and `puckBearingEnabled` will be ignored.")
}

val withBearing = androidRenderMode?.let { it != RenderMode.NORMAL } ?: (puckBearingEnabled == true)
val puck = createDefault2DPuck(withBearing = withBearing)

if (visible) {
if (images.isEmpty()) {
location2.locationPuck =
makeDefaultLocationPuck2D(mContext, androidRenderMode ?: RenderMode.NORMAL)
} else {
location2.locationPuck = LocationPuck2D(
topImage = images[PuckImagePart.TOP],
bearingImage = images[PuckImagePart.BEARING],
shadowImage = images[PuckImagePart.SHADOW],
scaleExpression = scale?.toJson()
)
// 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)
}
} else {
val empty =
AppCompatResourcesV11.getDrawableImageHolder(mContext, R.drawable.empty)
location2.locationPuck = LocationPuck2D(
topImage = empty,
bearingImage = empty,
}

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

// Handle visibility: use empty placeholders if not visible
if (!visible) {
val empty = AppCompatResourcesV11.getDrawableImageHolder(mContext, R.drawable.empty)
(puck as? LocationPuck2D)?.apply {
topImage = empty
bearingImage = empty
shadowImage = empty
)
}
}

location2.locationPuck = puck

this.puckBearing?.let {
location2.puckBearing = it
}
Expand All @@ -167,7 +198,7 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
ReadableType.Number ->
location2.pulsingColor = pulsing.getInt("color")
else ->
Logger.e(LOG_TAG, "pusling.color should be either a map or a number, but was ${pulsing.getDynamic("color")}")
Logger.e(LOG_TAG, "pulsing.color should be either a map or a number, but was ${pulsing.getDynamic("color")}")
}
}
pulsing.getAndLogIfNotBoolean("isEnabled")?.let { enabled ->
Expand Down Expand Up @@ -278,24 +309,3 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
const val LOG_TAG = "RNMBXNativeUserLocation"
}
}

fun makeDefaultLocationPuck2D(context: Context, renderMode: RenderMode): LocationPuck2D {
return LocationPuck2D(
topImage = AppCompatResourcesV11.getDrawableImageHolder(
context,
LR.drawable.mapbox_user_icon
),
bearingImage = AppCompatResourcesV11.getDrawableImageHolder(
context,
when (renderMode) {
RenderMode.GPS -> LR.drawable.mapbox_user_bearing_icon
RenderMode.COMPASS -> LR.drawable.mapbox_user_puck_icon
RenderMode.NORMAL -> LR.drawable.mapbox_user_stroke_icon
}
),
shadowImage = AppCompatResourcesV11.getDrawableImageHolder(
context,
LR.drawable.mapbox_user_icon_shadow
)
);
}
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