Skip to content

Commit

Permalink
fix(android): fix onUserTrackingModeChange
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas committed Oct 19, 2023
1 parent 72cb640 commit 9455e8a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,76 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
when (status) {
ViewportStatus.Idle -> return null
is ViewportStatus.State ->
return toFollowUserMode(status)
return toFollowUserMode(status.state)
is ViewportStatus.Transition ->
return toFollowUserMode(status.toState)
}
}

fun toUserTrackingMode(state: ViewportState): Int {
when (state) {
is FollowPuckViewportState -> {
return when (state.options.bearing) {
is FollowPuckViewportStateBearing.SyncWithLocationPuck -> {
val location = mMapView?.mapView?.location2
if (location?.puckBearingEnabled == true) {
when (location.puckBearingSource) {
com.mapbox.maps.plugin.PuckBearingSource.HEADING -> {
UserTrackingMode.FollowWithHeading
}
com.mapbox.maps.plugin.PuckBearingSource.COURSE -> {
UserTrackingMode.FollowWithCourse
}
else -> {
UserTrackingMode.FOLLOW
}
}
} else {
UserTrackingMode.FOLLOW
}
}

is FollowPuckViewportStateBearing.Constant ->
UserTrackingMode.FOLLOW

else -> {
Logger.w(LOG_TAG, "Unexpected bearing: ${state.options.bearing}")
UserTrackingMode.FOLLOW
}
}
}

is OverviewViewportState -> {
return UserTrackingMode.NONE
}

else -> {
return UserTrackingMode.NONE // TODO
}
}
}

fun toUserTrackingMode(status: ViewportStatus): Int {
return when (status) {
ViewportStatus.Idle -> UserTrackingMode.NONE
is ViewportStatus.State ->
toUserTrackingMode(status.state)

is ViewportStatus.Transition ->
toUserTrackingMode(status.toState)
}
}

fun toReadableMap(status: ViewportStatus): ReadableMap {
when (status) {
ViewportStatus.Idle -> return writableMapOf("state" to "idle")
return when (status) {
ViewportStatus.Idle -> writableMapOf("state" to "idle")
is ViewportStatus.State ->
return writableMapOf(
writableMapOf(
"state" to status.toString()
)

is ViewportStatus.Transition ->
return writableMapOf(
writableMapOf(
"transition" to status.toString()
)
}
Expand All @@ -346,12 +401,11 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
toStatus: ViewportStatus,
reason: ViewportStatusChangeReason
) {
if (reason == ViewportStatusChangeReason.USER_INTERACTION) {
if (reason == ViewportStatusChangeReason.USER_INTERACTION || reason == ViewportStatusChangeReason.TRANSITION_SUCCEEDED) {
val followUserLocation = toFollowUserLocation(toStatus)



mManager.handleEvent(MapUserTrackingModeEvent(this@RNMBXCamera, UserTrackingMode.NONE,
val mode = toUserTrackingMode(toStatus)
mManager.handleEvent(MapUserTrackingModeEvent(this@RNMBXCamera, mode,
writableMapOf(
"followUserMode" to toFollowUserMode(toStatus),
"followUserLocation" to followUserLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class LocationComponentManager(mapView: RNMBXMapView, context: Context) {

private fun applyStateChanges(map: RNMBXMapView, oldState: State, newState: State, fullUpdate: Boolean) {
val mapView = map.mapView
if (map.getLifecycleState() != Lifecycle.State.STARTED) {
if (map.getLifecycleState() != Lifecycle.State.STARTED && map.getLifecycleState() != Lifecycle.State.INITIALIZED) {
// In case lifecycle was already stopped, so we're part of shutdown, do not call updateSettings as it'll just restart
// the loationComponent that will not be stopped. See https://github.com/mapbox/mapbox-maps-android/issues/2017
if (!newState.enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object UserTrackingMode {
FOLLOW -> return "normal"
FollowWithCourse -> return "course"
FollowWithHeading -> return "compass"
NONE -> return null
}
return null
}
Expand Down

0 comments on commit 9455e8a

Please sign in to comment.