From 3ce0360d919175f605de792b5d326a25201151e9 Mon Sep 17 00:00:00 2001 From: Xor290 Date: Sat, 28 Feb 2026 23:18:13 +0100 Subject: [PATCH] chore: fix geoloca --- .../node_modules/.package-lock.json | 5 ++ frontend-admin/package-lock.json | 6 ++ frontend-admin/package.json | 1 + .../src/screens/delivery/DashboardScreen.tsx | 64 ++++++++++++++----- 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/frontend-admin/node_modules/.package-lock.json b/frontend-admin/node_modules/.package-lock.json index 37cca0ab..5bdb0f45 100644 --- a/frontend-admin/node_modules/.package-lock.json +++ b/frontend-admin/node_modules/.package-lock.json @@ -6614,6 +6614,11 @@ } } }, + "node_modules/react-native-geolocation-service": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/react-native-geolocation-service/-/react-native-geolocation-service-5.3.1.tgz", + "integrity": "sha512-LTXPtPNmrdhx+yeWG47sAaCgQc3nG1z+HLLHlhK/5YfOgfLcAb9HAkhREPjQKPZOUx8pKZMIpdGFUGfJYtimXQ==" + }, "node_modules/react-native-gesture-handler": { "version": "2.28.0", "resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz", diff --git a/frontend-admin/package-lock.json b/frontend-admin/package-lock.json index 04efc1b0..5c08de55 100644 --- a/frontend-admin/package-lock.json +++ b/frontend-admin/package-lock.json @@ -24,6 +24,7 @@ "react": "19.1.0", "react-dom": "19.1.0", "react-native": "0.81.5", + "react-native-geolocation-service": "^5.3.1", "react-native-gesture-handler": "~2.28.0", "react-native-maps": "1.20.1", "react-native-reanimated": "~4.1.1", @@ -6963,6 +6964,11 @@ } } }, + "node_modules/react-native-geolocation-service": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/react-native-geolocation-service/-/react-native-geolocation-service-5.3.1.tgz", + "integrity": "sha512-LTXPtPNmrdhx+yeWG47sAaCgQc3nG1z+HLLHlhK/5YfOgfLcAb9HAkhREPjQKPZOUx8pKZMIpdGFUGfJYtimXQ==" + }, "node_modules/react-native-gesture-handler": { "version": "2.28.0", "resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.28.0.tgz", diff --git a/frontend-admin/package.json b/frontend-admin/package.json index 74d33820..5b9811db 100644 --- a/frontend-admin/package.json +++ b/frontend-admin/package.json @@ -25,6 +25,7 @@ "react": "19.1.0", "react-dom": "19.1.0", "react-native": "0.81.5", + "react-native-geolocation-service": "^5.3.1", "react-native-gesture-handler": "~2.28.0", "react-native-maps": "1.20.1", "react-native-reanimated": "~4.1.1", diff --git a/frontend-admin/src/screens/delivery/DashboardScreen.tsx b/frontend-admin/src/screens/delivery/DashboardScreen.tsx index f17f4825..54c814dd 100644 --- a/frontend-admin/src/screens/delivery/DashboardScreen.tsx +++ b/frontend-admin/src/screens/delivery/DashboardScreen.tsx @@ -19,7 +19,10 @@ import { Modal, StatusBar, ScrollView, + Platform, + PermissionsAndroid, } from "react-native"; +import Geolocation from "react-native-geolocation-service"; import { Ionicons } from "@expo/vector-icons"; import * as Location from "expo-location"; import { spacing, fontSize, borderRadius } from "../../theme"; @@ -291,29 +294,58 @@ export default function DashboardScreen() { // -------------------------------------------------- const sendingRef = useRef(false); + const getLocationWithFallback = useCallback( + (): Promise => { + return new Promise(async (resolve, reject) => { + if (Platform.OS === "android") { + const granted = await PermissionsAndroid.request( + PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, + ); + if (granted !== PermissionsAndroid.RESULTS.GRANTED) { + return reject(new Error("Permission refusée")); + } + } + + Geolocation.getCurrentPosition( + (position) => { + resolve({ + coords: { + latitude: position.coords.latitude, + longitude: position.coords.longitude, + altitude: position.coords.altitude ?? 0, + accuracy: position.coords.accuracy ?? 0, + altitudeAccuracy: position.coords.altitudeAccuracy ?? 0, + heading: position.coords.heading ?? 0, + speed: position.coords.speed ?? 0, + }, + timestamp: position.timestamp, + } as Location.LocationObject); + }, + (error) => reject(new Error(error.message)), + { + enableHighAccuracy: true, + timeout: 30000, + maximumAge: 10000, + forceRequestLocation: true, + forceLocationManager: true, + }, + ); + }); + }, + [], + ); + const sendCurrentLocation = useCallback(async () => { if (sendingRef.current) return; sendingRef.current = true; try { - const loc = await Promise.race([ - Location.getCurrentPositionAsync({ - accuracy: Location.Accuracy.High, - }), - new Promise((_, reject) => - setTimeout(() => reject(new Error("GPS timeout")), 8000), - ), - ]); + const loc = await getLocationWithFallback(); - if ( - !loc || - typeof loc !== "object" || - !("coords" in loc) || - !loc.coords - ) { + if (!loc?.coords) { return; } - const coords = (loc as Location.LocationObject).coords; + const coords = loc.coords; const { latitude, longitude } = coords; const wasNull = !lastCoordsRef.current; @@ -333,7 +365,7 @@ export default function DashboardScreen() { } finally { sendingRef.current = false; } - }, []); + }, [getLocationWithFallback]); const startLocationTracking = useCallback(async () => { const { status: fgStatus } =