Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions configs/jsactions/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ export default async args => {
types: ["mendix-client", "react-native"],
allowSyntheticDefaultImports: true,
compilerOptions: {
newLine: "CRLF",
// `react-native-nitro-geolocation` ships no compiled JS; its entry points are `.tsx`
// (`main: "src/index"`, `browser: "src/index.web.tsx"`). The TS plugin therefore needs
// `jsx` set to parse those `.tsx` files, otherwise the build fails with
// `TS6142: Module ... was resolved to '.../src/index.tsx', but '--jsx' is not set`.
jsx: "react-native"
newLine: "CRLF"
}
});

Expand Down Expand Up @@ -97,6 +92,16 @@ export default async args => {
overwrite: true
}
);
} else if (args.configProject === "nanoflowcommons") {
// `invariant` is being used silently by @react-native-community/geolocation; it is not listed as a dependency nor peerDependency.
// https://github.dev/react-native-geolocation/react-native-geolocation/blob/1786929f2be581da91082ff857c2393da5e597b3/js/implementation.native.js#L13
await copyAsync(
dirname(require.resolve("invariant")),
join(outDir, "node_modules", "invariant"),
{
overwrite: true
}
);
}

// this is helpful to copy the files and folders to a test project path for dev/testing purposes.
Expand Down
2 changes: 0 additions & 2 deletions packages/jsActions/nanoflow-actions-native/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

- Migrated the geolocation actions from `@react-native-community/geolocation` to `react-native-nitro-geolocation`.

## [7.3.0] Nanoflow Commons - 2026-9-2

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions packages/jsActions/nanoflow-actions-native/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nanoflow-actions-native",
"moduleName": "Nanoflow Commons",
"version": "7.3.1",
"version": "7.3.0",
"license": "Apache-2.0",
"copyright": "© Mendix Technology BV 2022. All rights reserved.",
"repository": {
Expand All @@ -27,9 +27,9 @@
},
"dependencies": {
"@react-native-async-storage/async-storage": "2.2.0",
"@react-native-community/geolocation": "3.4.0",
"invariant": "^2.2.4",
"js-base64": "~3.7.2",
"react-native-nitro-geolocation": "1.4.3",
"react-native-nitro-modules": "0.36.1",
"react-native-permissions": "5.5.1",
"react-native-geocoder": "0.5.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import { Big } from "big.js";
import { getCurrentPosition, GeolocationResponse, LocationRequestOptions } from "react-native-nitro-geolocation";
import Geolocation, {
GeolocationError,
GeolocationOptions,
GeolocationResponse
} from "@react-native-community/geolocation";

import type { Platform, NativeModules } from "react-native";
import type { GeoError, GeoPosition, GeoOptions } from "../../typings/Geolocation";

// BEGIN EXTRA CODE
// END EXTRA CODE
Expand All @@ -32,114 +39,94 @@ export async function GetCurrentLocation(
): Promise<mendix.lib.MxObject> {
// BEGIN USER CODE

const isReactNative = navigator && navigator.product === "ReactNative";
const isWeb = navigator && navigator.geolocation;
let reactNativeModule: { NativeModules: typeof NativeModules; Platform: typeof Platform } | undefined;
let geolocationModule: typeof import("@react-native-community/geolocation").default | Geolocation;

if (!isReactNative && !isWeb) {
return Promise.reject(new Error("Geolocation module could not be found"));
}
if (navigator && navigator.product === "ReactNative") {
reactNativeModule = require("react-native");

// We keep a manual web branch (backed by `navigator.geolocation`) rather than relying on the
// library's web entry, because it is not guaranteed that the Mendix web client resolves the
// package's `browser`/`exports` condition. If that is ever confirmed, this branch can be dropped.
const options = buildLocationOptions(timeout, maximumAge, highAccuracy);
try {
let position: GeolocationResponse;
if (!reactNativeModule) {
return Promise.reject(new Error("React Native module could not be found"));
}

if (isReactNative) {
position = await getCurrentPosition(options);
if (reactNativeModule.NativeModules.RNFusedLocation) {
geolocationModule = (await import("@react-native-community/geolocation")).default;
} else if (reactNativeModule.NativeModules.RNCGeolocation) {
geolocationModule = Geolocation;
} else {
position = await new Promise<GeolocationResponse>((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
pos => resolve(normalizeWebPosition(pos)),
err => reject(err),
{
timeout: options.timeout,
maximumAge: options.maximumAge,
enableHighAccuracy: highAccuracy ?? false
}
);
});
return Promise.reject(new Error("Geolocation module could not be found"));
}
} else if (navigator && navigator.geolocation) {
geolocationModule = navigator.geolocation;
} else {
return Promise.reject(new Error("Geolocation module could not be found"));
}

return new Promise((resolve, reject) => {
const options = getOptions();
geolocationModule?.getCurrentPosition(onSuccess, onError, options);

return new Promise((resolve, reject) => {
function onSuccess(position: GeolocationResponse | GeoPosition): void {
mx.data.create({
entity: "NanoflowCommons.Geolocation",
callback: mxObject => {
resolve(mapPositionToMxObject(mxObject, position));
const geolocation = mapPositionToMxObject(mxObject, position);
resolve(geolocation);
},
error: () =>
reject(new Error("Could not create 'NanoflowCommons.Geolocation' object to store location"))
});
});
} catch (error: any) {
const message = error?.message ?? String(error);
return Promise.reject(new Error(`Could not get current location: ${message}`));
}

function buildLocationOptions(
timeout: Big | undefined,
maximumAge: Big | undefined,
highAccuracy: boolean | undefined
): LocationRequestOptions {
let timeoutNumber = timeout ? timeout.toNumber() : undefined;
const maximumAgeNumber = maximumAge ? maximumAge.toNumber() : undefined;
}

// If the timeout is 0 or undefined (empty), it causes a crash on iOS.
// If the timeout is undefined (empty); we set timeout to 30 sec (default timeout)
// If the timeout is 0; we set timeout to 1 hour (no timeout)
if (isReactNative && require("react-native").Platform.OS === "ios") {
if (timeoutNumber === undefined) {
timeoutNumber = 30000;
} else if (timeoutNumber === 0) {
timeoutNumber = 3600000;
}
function onError(error: GeolocationError | GeoError): void {
return reject(new Error(error.message));
}

return {
timeout: timeoutNumber,
maximumAge: maximumAgeNumber,
accuracy: highAccuracy ? { android: "high", ios: "best" } : { android: "balanced", ios: "hundredMeters" }
};
}
function getOptions(): GeolocationOptions | GeoOptions {
let timeoutNumber = timeout && Number(timeout.toString());
const maximumAgeNumber = maximumAge && Number(maximumAge.toString());

// NOTE: `normalizeWebPosition` is duplicated verbatim in `GetCurrentLocationMinimumAccuracy.ts`.
// The Mendix action model does not allow sharing code across action files, so if you change this
// function, keep the copy in the other action in sync.
function normalizeWebPosition(pos: GeolocationPosition): GeolocationResponse {
return {
coords: {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude,
altitude: pos.coords.altitude ?? null,
accuracy: pos.coords.accuracy,
altitudeAccuracy: pos.coords.altitudeAccuracy ?? null,
heading: pos.coords.heading ?? null,
speed: pos.coords.speed ?? null
},
timestamp: pos.timestamp
};
}
// If the timeout is 0 or undefined (empty), it causes a crash on iOS.
// If the timeout is undefined (empty); we set timeout to 30 sec (default timeout)
// If the timeout is 0; we set timeout to 1 hour (no timeout)
if (reactNativeModule?.Platform.OS === "ios") {
if (timeoutNumber === undefined) {
timeoutNumber = 30000;
} else if (timeoutNumber === 0) {
timeoutNumber = 3600000;
}
}

function mapPositionToMxObject(mxObject: mendix.lib.MxObject, pos: GeolocationResponse): mendix.lib.MxObject {
mxObject.set("Timestamp", new Date(pos.timestamp));
mxObject.set("Latitude", new Big(pos.coords.latitude.toFixed(8)));
mxObject.set("Longitude", new Big(pos.coords.longitude.toFixed(8)));
mxObject.set("Accuracy", new Big(pos.coords.accuracy.toFixed(8)));
if (pos.coords.altitude != null) {
mxObject.set("Altitude", new Big(pos.coords.altitude.toFixed(8)));
return {
timeout: timeoutNumber,
maximumAge: maximumAgeNumber,
enableHighAccuracy: highAccuracy
};
}
if (pos.coords.altitudeAccuracy != null && pos.coords.altitudeAccuracy !== -1) {
mxObject.set("AltitudeAccuracy", new Big(pos.coords.altitudeAccuracy.toFixed(8)));
}
if (pos.coords.heading != null && pos.coords.heading !== -1) {
mxObject.set("Heading", new Big(pos.coords.heading.toFixed(8)));
}
if (pos.coords.speed != null && pos.coords.speed !== -1) {
mxObject.set("Speed", new Big(pos.coords.speed.toFixed(8)));

function mapPositionToMxObject(
mxObject: mendix.lib.MxObject,
position: GeolocationResponse | GeoPosition
): mendix.lib.MxObject {
mxObject.set("Timestamp", new Date(position.timestamp));
mxObject.set("Latitude", new Big(position.coords.latitude.toFixed(8)));
mxObject.set("Longitude", new Big(position.coords.longitude.toFixed(8)));
mxObject.set("Accuracy", new Big(position.coords.accuracy.toFixed(8)));
if (position.coords.altitude != null) {
mxObject.set("Altitude", new Big(position.coords.altitude.toFixed(8)));
}
if (position.coords.altitudeAccuracy != null && position.coords.altitudeAccuracy !== -1) {
mxObject.set("AltitudeAccuracy", new Big(position.coords.altitudeAccuracy.toFixed(8)));
}
if (position.coords.heading != null && position.coords.heading !== -1) {
mxObject.set("Heading", new Big(position.coords.heading.toFixed(8)));
}
if (position.coords.speed != null && position.coords.speed !== -1) {
mxObject.set("Speed", new Big(position.coords.speed.toFixed(8)));
}
return mxObject;
}
return mxObject;
}
});

// END USER CODE
}
Loading
Loading