Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: keep makeNativeCameraStop within Camera
Browse files Browse the repository at this point in the history
KiwiKilian committed Nov 11, 2024
1 parent 46c3fc1 commit 9259bbf
Showing 3 changed files with 65 additions and 75 deletions.
8 changes: 4 additions & 4 deletions __tests__/components/Camera.test.tsx
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import Camera, {
CameraRef,
CameraStop,
CameraStops,
nativeAnimationMode,
getNativeCameraMode,
NativeCameraProps,
UserTrackingMode,
} from "../../javascript/components/Camera";
@@ -634,15 +634,15 @@ describe("Camera", () => {
describe("utils", () => {
describe("nativeAnimationMode", () => {
test('returns "Flight" for "flyTo"', () => {
expect(nativeAnimationMode("flyTo")).toStrictEqual("Flight");
expect(getNativeCameraMode("flyTo")).toStrictEqual("Flight");
});

test('returns "None" for "moveTo"', () => {
expect(nativeAnimationMode("moveTo")).toStrictEqual("None");
expect(getNativeCameraMode("moveTo")).toStrictEqual("None");
});

test('returns "None" as default', () => {
expect(nativeAnimationMode()).toStrictEqual("None");
expect(getNativeCameraMode()).toStrictEqual("None");
});
});
});
67 changes: 61 additions & 6 deletions javascript/components/Camera.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { point } from "@turf/helpers";
import React, { forwardRef, memo, useImperativeHandle, useMemo } from "react";
import { requireNativeComponent, ViewProps } from "react-native";

@@ -7,7 +8,6 @@ import { MaplibreGLEvent } from "../types";
import BaseProps from "../types/BaseProps";
import { CameraMode } from "../types/CameraMode";
import { makeNativeBounds } from "../utils/makeNativeBounds";
import { makeNativeStop } from "../utils/makeNativeStop";

export const NATIVE_MODULE_NAME = "RCTMLNCamera";

@@ -27,7 +27,7 @@ export type UserTrackingModeChangeCallback = (
>,
) => void;

export function nativeAnimationMode(mode?: CameraAnimationMode): CameraMode {
export function getNativeCameraMode(mode?: CameraAnimationMode): CameraMode {
switch (mode) {
case "flyTo":
return CameraModes.Flight;
@@ -42,6 +42,61 @@ export function nativeAnimationMode(mode?: CameraAnimationMode): CameraMode {
}
}

function makeNativeCameraStop(stop?: CameraStop): NativeCameraStop | undefined {
if (!stop) {
return undefined;
}

const nativeStop: NativeCameraStop = {};

if (stop.animationDuration !== undefined) {
nativeStop.duration = stop.animationDuration;
}
if (stop.animationMode !== undefined) {
nativeStop.mode = getNativeCameraMode(stop.animationMode);
}
if (stop.centerCoordinate) {
nativeStop.centerCoordinate = JSON.stringify(point(stop.centerCoordinate));
}
if (stop.heading !== undefined) {
nativeStop.heading = stop.heading;
}
if (stop.pitch !== undefined) {
nativeStop.pitch = stop.pitch;
}
if (stop.zoomLevel !== undefined) {
nativeStop.zoom = stop.zoomLevel;
}

if (stop.bounds && stop.bounds.ne && stop.bounds.sw) {
const { ne, sw } = stop.bounds;
nativeStop.bounds = makeNativeBounds(ne, sw);
}

const paddingTop = stop.padding?.paddingTop ?? stop.bounds?.paddingTop;
if (paddingTop !== undefined) {
nativeStop.paddingTop = paddingTop;
}

const paddingRight = stop.padding?.paddingRight ?? stop.bounds?.paddingRight;
if (paddingRight !== undefined) {
nativeStop.paddingRight = paddingRight;
}

const paddingBottom =
stop.padding?.paddingBottom ?? stop.bounds?.paddingBottom;
if (paddingBottom !== undefined) {
nativeStop.paddingBottom = paddingBottom;
}

const paddingLeft = stop.padding?.paddingLeft ?? stop.bounds?.paddingLeft;
if (paddingLeft !== undefined) {
nativeStop.paddingLeft = paddingLeft;
}

return nativeStop;
}

export interface CameraRef {
setCamera: (config: CameraStop | CameraStops) => void;

@@ -217,7 +272,7 @@ const Camera = memo(
const nativeCamera = useNativeRef<NativeCameraProps>();

const nativeStop = useMemo(() => {
return makeNativeStop({
return makeNativeCameraStop({
animationDuration,
animationMode,
bounds,
@@ -239,7 +294,7 @@ const Camera = memo(
]);

const nativeDefaultStop = useMemo(() => {
return makeNativeStop(defaultSettings);
return makeNativeCameraStop(defaultSettings);
}, [defaultSettings]);

const nativeMaxBounds = useMemo(() => {
@@ -254,12 +309,12 @@ const Camera = memo(
nativeCamera.current?.setNativeProps({
stop: {
stops: config.stops
.map((stopItem) => makeNativeStop(stopItem))
.map((stopItem) => makeNativeCameraStop(stopItem))
.filter((stopItem) => !!stopItem),
},
});
} else {
const nativeStop = makeNativeStop(config);
const nativeStop = makeNativeCameraStop(config);

if (nativeStop) {
nativeCamera.current?.setNativeProps({ stop: nativeStop });
65 changes: 0 additions & 65 deletions javascript/utils/makeNativeStop.ts

This file was deleted.

0 comments on commit 9259bbf

Please sign in to comment.