Skip to content
Open
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
3 changes: 2 additions & 1 deletion configs/jsactions/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default async args => {
types: ["mendix-client", "react-native"],
allowSyntheticDefaultImports: true,
compilerOptions: {
newLine: "CRLF"
newLine: "CRLF",
jsx: "react-native"
}
});

Expand Down
5 changes: 3 additions & 2 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.1.0",
"version": "7.2.0",
"license": "Apache-2.0",
"copyright": "© Mendix Technology BV 2022. All rights reserved.",
"repository": {
Expand All @@ -27,9 +27,10 @@
},
"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,14 +6,8 @@
// - 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 Geolocation, {
GeolocationError,
GeolocationOptions,
GeolocationResponse
} from "@react-native-community/geolocation";

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

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

let reactNativeModule: { NativeModules: typeof NativeModules; Platform: typeof Platform } | undefined;
let geolocationModule: typeof import("@react-native-community/geolocation").default | Geolocation;

if (navigator && navigator.product === "ReactNative") {
reactNativeModule = require("react-native");

if (!reactNativeModule) {
return Promise.reject(new Error("React Native module could not be found"));
}

if (reactNativeModule.NativeModules.RNFusedLocation) {
geolocationModule = (await import("@react-native-community/geolocation")).default;
} else if (reactNativeModule.NativeModules.RNCGeolocation) {
geolocationModule = Geolocation;
} else {
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);

function onSuccess(position: GeolocationResponse | GeoPosition): void {
const options = buildLocationOptions(timeout, maximumAge, highAccuracy);
try {
const position = await getCurrentPosition(options);
return new Promise((resolve, reject) => {
mx.data.create({
entity: "NanoflowCommons.Geolocation",
callback: mxObject => {
const geolocation = mapPositionToMxObject(mxObject, position);
resolve(geolocation);
resolve(mapPositionToMxObject(mxObject, position));
},
error: () =>
reject(new Error("Could not create 'NanoflowCommons.Geolocation' object to store location"))
});
}

function onError(error: GeolocationError | GeoError): void {
return reject(new Error(error.message));
}
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return Promise.reject(new Error(`Could not get current location: ${message}`));
}

function getOptions(): GeolocationOptions | GeoOptions {
let timeoutNumber = timeout && Number(timeout.toString());
const maximumAgeNumber = maximumAge && Number(maximumAge.toString());
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 (reactNativeModule?.Platform.OS === "ios") {
if (timeoutNumber === undefined) {
timeoutNumber = 30000;
} else if (timeoutNumber === 0) {
timeoutNumber = 3600000;
}
// 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 (Platform.OS === "ios") {
if (timeoutNumber === undefined) {
timeoutNumber = 30000;
} else if (timeoutNumber === 0) {
timeoutNumber = 3600000;
}

return {
timeout: timeoutNumber,
maximumAge: maximumAgeNumber,
enableHighAccuracy: highAccuracy
};
}

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 {
timeout: timeoutNumber,
maximumAge: maximumAgeNumber,
accuracy: highAccuracy ? { android: "high", ios: "best" } : { android: "balanced", ios: "hundredMeters" }
};
}
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)));
}
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)));
}
return mxObject;
}

// END USER CODE
}
Loading
Loading