Skip to content

Commit

Permalink
fix(ios,fabric): do not apply camera stops on render unless stops wer… (
Browse files Browse the repository at this point in the history
#3349)

* fix(ios,fabric): do not apply camera stops on render unless stops were changed

* fix(android,fabric): do not apply reapply camera on re-render
  • Loading branch information
mfazekas authored Jan 30, 2024
1 parent acde414 commit 60d4079
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class CameraStop {
private var mMode = CameraMode.EASE
private var mDuration = 2000
private var mCallback: Animator.AnimatorListener? = null

var ts: Int? = null

fun setBearing(bearing: Double) {
mBearing = bearing
}
Expand Down Expand Up @@ -153,6 +156,10 @@ class CameraStop {
): CameraStop {
val stop = CameraStop()

if (readableMap.hasKey("__updateTS")) {
stop.ts = readableMap.getInt("__updateTS")
}

if (readableMap.hasKey("pitch")) {
stop.setTilt(readableMap.getDouble("pitch"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
private var mMaxBounds: LatLngBounds? = null


var ts: Int? = null;


private val mCameraCallback: Animator.AnimatorListener = object : Animator.AnimatorListener {
override fun onAnimationStart(animator: Animator) {}
override fun onAnimationEnd(animator: Animator) {
Expand Down Expand Up @@ -110,10 +113,12 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
}
}
fun setStop(stop: CameraStop) {
mCameraStop = stop
stop.setCallback(mCameraCallback)
if (mMapView != null) {
stop.let { updateCamera(it) }
if (stop.ts != mCameraStop?.ts) {
mCameraStop = stop
stop.setCallback(mCameraCallback)
if (mMapView != null) {
stop.let { updateCamera(it) }
}
}
}

Expand Down
34 changes: 19 additions & 15 deletions ios/RNMBX/RNMBXCameraComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,58 +69,62 @@ + (ComponentDescriptorProvider)componentDescriptorProvider

- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
const auto &newProps = static_cast<const RNMBXCameraProps &>(*props);
id maxBounds = RNMBXConvertFollyDynamicToId(newProps.maxBounds);
const auto &oldViewProps = static_cast<const RNMBXCameraProps &>(*oldProps);
const auto &newViewProps = static_cast<const RNMBXCameraProps &>(*props);
id maxBounds = RNMBXConvertFollyDynamicToId(newViewProps.maxBounds);
if (maxBounds != nil) {
_view.maxBounds = maxBounds;
}
id animationDuration = RNMBXConvertFollyDynamicToId(newProps.animationDuration);
id animationDuration = RNMBXConvertFollyDynamicToId(newViewProps.animationDuration);
if (animationDuration != nil) {
_view.animationDuration = animationDuration;
}
id animationMode = RNMBXConvertFollyDynamicToId(newProps.animationMode);
id animationMode = RNMBXConvertFollyDynamicToId(newViewProps.animationMode);
if (animationMode != nil) {
_view.animationMode = animationMode;
}
id defaultStop = RNMBXConvertFollyDynamicToId(newProps.defaultStop);
id defaultStop = RNMBXConvertFollyDynamicToId(newViewProps.defaultStop);
if (defaultStop != nil) {
_view.defaultStop = defaultStop;
}
id followUserLocation = RNMBXConvertFollyDynamicToId(newProps.followUserLocation);
id followUserLocation = RNMBXConvertFollyDynamicToId(newViewProps.followUserLocation);
if (followUserLocation != nil) {
_view.followUserLocation = followUserLocation;
}
id followUserMode = RNMBXConvertFollyDynamicToId(newProps.followUserMode);
id followUserMode = RNMBXConvertFollyDynamicToId(newViewProps.followUserMode);
if (followUserMode != nil) {
_view.followUserMode = followUserMode;
}
id followZoomLevel = RNMBXConvertFollyDynamicToId(newProps.followZoomLevel);
id followZoomLevel = RNMBXConvertFollyDynamicToId(newViewProps.followZoomLevel);
if (followZoomLevel != nil) {
_view.followZoomLevel = followZoomLevel;
}
id followPitch = RNMBXConvertFollyDynamicToId(newProps.followPitch);
id followPitch = RNMBXConvertFollyDynamicToId(newViewProps.followPitch);
if (followPitch != nil) {
_view.followPitch = followPitch;
}
id followHeading = RNMBXConvertFollyDynamicToId(newProps.followHeading);
id followHeading = RNMBXConvertFollyDynamicToId(newViewProps.followHeading);
if (followHeading != nil) {
_view.followHeading = followHeading;
}
id followPadding = RNMBXConvertFollyDynamicToId(newProps.followPadding);
id followPadding = RNMBXConvertFollyDynamicToId(newViewProps.followPadding);
if (followPadding != nil) {
_view.followPadding = followPadding;
}
id maxZoomLevel = RNMBXConvertFollyDynamicToId(newProps.maxZoomLevel);
id maxZoomLevel = RNMBXConvertFollyDynamicToId(newViewProps.maxZoomLevel);
if (maxZoomLevel != nil) {
_view.maxZoomLevel = maxZoomLevel;
}
id minZoomLevel = RNMBXConvertFollyDynamicToId(newProps.minZoomLevel);
id minZoomLevel = RNMBXConvertFollyDynamicToId(newViewProps.minZoomLevel);
if (minZoomLevel != nil) {
_view.minZoomLevel = minZoomLevel;
}
id stop = RNMBXConvertFollyDynamicToId(newProps.stop);
if (stop != nil) {

if (!oldProps || oldViewProps.stop != newViewProps.stop) {
id stop = RNMBXConvertFollyDynamicToId(newViewProps.stop);
if (stop != nil) {
_view.stop = stop;
}
}
[super updateProps:props oldProps:oldProps];
}
Expand Down
10 changes: 8 additions & 2 deletions src/components/Camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ export const Camera = memo(
return;
}

lastTS += 1;

if (!config.type)
// @ts-expect-error The compiler doesn't understand that the `config` union type is guaranteed
// to be an object type.
Expand All @@ -398,13 +400,15 @@ export const Camera = memo(
_nativeStops = [..._nativeStops, _nativeStop];
}
nativeCamera.current?.setNativeProps({
stop: { stops: _nativeStops },
stop: { stops: _nativeStops, __updateTS: lastTS },
});
}
} else if (config.type === 'CameraStop') {
const _nativeStop = buildNativeStop(config);
if (_nativeStop) {
nativeCamera.current?.setNativeProps({ stop: _nativeStop });
nativeCamera.current?.setNativeProps({
stop: { __updateTS: lastTS, ..._nativeStop },
});
}
}
};
Expand Down Expand Up @@ -590,6 +594,8 @@ export const Camera = memo(
),
);

let lastTS = 0;

const RNMBXCamera = NativeCameraView;

export type Camera = CameraRef;

0 comments on commit 60d4079

Please sign in to comment.