Skip to content

Commit

Permalink
Fix android crash, fix android location puck images not updating prop…
Browse files Browse the repository at this point in the history
…erly
  • Loading branch information
mysport12 committed Feb 5, 2024
1 parent d3b705c commit 6ec4127
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 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 @@ -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 6ec4127

Please sign in to comment.