From b08319a2a23440153c36d900c1490bc718645856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Thu, 19 Oct 2023 16:21:29 +0200 Subject: [PATCH] fix(android): fix onUserTrackingModeChange (#3122) --- .../rnmbx/components/camera/RNMBXCamera.kt | 73 ++++++++++++++++--- .../location/LocationComponentManager.kt | 2 +- .../components/location/UserTrackingMode.kt | 1 + 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/RNMBXCamera.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/RNMBXCamera.kt index 0261a64ea..7e92bab58 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/RNMBXCamera.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/RNMBXCamera.kt @@ -319,21 +319,77 @@ 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) { + + PuckBearingSource.HEADING -> { + UserTrackingMode.FollowWithHeading + } + 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() ) } @@ -346,12 +402,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, diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/location/LocationComponentManager.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/location/LocationComponentManager.kt index 1f87cd478..1deba35c9 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/location/LocationComponentManager.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/location/LocationComponentManager.kt @@ -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) { diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/location/UserTrackingMode.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/location/UserTrackingMode.kt index 853202cbe..9d9b7adb2 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/location/UserTrackingMode.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/location/UserTrackingMode.kt @@ -25,6 +25,7 @@ object UserTrackingMode { FOLLOW -> return "normal" FollowWithCourse -> return "course" FollowWithHeading -> return "compass" + NONE -> return null } return null }