chore: fix crash gps
This commit is contained in:
@@ -281,17 +281,44 @@ export default function DashboardScreen() {
|
||||
// --------------------------------------------------
|
||||
// Location tracking
|
||||
// --------------------------------------------------
|
||||
const sendingRef = useRef(false);
|
||||
|
||||
const sendCurrentLocation = useCallback(async () => {
|
||||
if (sendingRef.current) return;
|
||||
sendingRef.current = true;
|
||||
|
||||
try {
|
||||
const loc = await Location.getCurrentPositionAsync({
|
||||
accuracy: Location.Accuracy.High,
|
||||
});
|
||||
const { latitude, longitude } = loc.coords;
|
||||
const loc = await Promise.race([
|
||||
Location.getCurrentPositionAsync({
|
||||
accuracy: Location.Accuracy.High,
|
||||
}),
|
||||
new Promise<never>((_, reject) =>
|
||||
setTimeout(() => reject(new Error("GPS timeout")), 8000),
|
||||
),
|
||||
]);
|
||||
|
||||
// validation runtime + typage TS
|
||||
if (
|
||||
!loc ||
|
||||
typeof loc !== "object" ||
|
||||
!("coords" in loc) ||
|
||||
!loc.coords
|
||||
) {
|
||||
console.log("GPS invalide — update ignoré");
|
||||
return;
|
||||
}
|
||||
|
||||
const coords = (loc as Location.LocationObject).coords;
|
||||
const { latitude, longitude } = coords;
|
||||
|
||||
setLastCoords({ lat: latitude, lng: longitude });
|
||||
setLastUpdate(new Date());
|
||||
|
||||
await updateMyLocation(latitude, longitude);
|
||||
} catch {
|
||||
/* silent */
|
||||
} catch (err) {
|
||||
console.log("GPS error:", err);
|
||||
} finally {
|
||||
sendingRef.current = false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user