From 834b1643de200e5d0a0c5aed13540a1cef6a95e7 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Tue, 17 Dec 2024 22:03:22 +0100 Subject: [PATCH] =?UTF-8?q?Corrections=20et=20optimisations=20g=C3=A9oloca?= =?UTF-8?q?lisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/components/providers/GameProvider.tsx | 2 +- .../providers/GeolocationProvider.tsx | 30 +++++++++++-------- client/constants/Constants.ts | 3 ++ client/utils/geolocation.ts | 14 ++++----- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/client/components/providers/GameProvider.tsx b/client/components/providers/GameProvider.tsx index f315b2e..fb281ef 100644 --- a/client/components/providers/GameProvider.tsx +++ b/client/components/providers/GameProvider.tsx @@ -11,7 +11,7 @@ import { Challenge } from '@/utils/features/challenges/challengesSlice' import { useQuery } from '@tanstack/react-query' import { router } from 'expo-router' import { useShareIntentContext } from 'expo-share-intent' -import { ReactNode, useEffect } from 'react' +import React, { ReactNode, useEffect } from 'react' export default function GameProvider({ children }: { children: ReactNode }) { const auth = useAuth() diff --git a/client/components/providers/GeolocationProvider.tsx b/client/components/providers/GeolocationProvider.tsx index 9dfb1e5..0d32b1e 100644 --- a/client/components/providers/GeolocationProvider.tsx +++ b/client/components/providers/GeolocationProvider.tsx @@ -1,14 +1,14 @@ -import { ReactNode, useEffect } from 'react' -import { useAuth } from '@/hooks/useAuth' -import { useQueuedLocations, useSetLastPlayerLocation, useSetLastPlayerLocations, useUnqueueLocation } from '@/hooks/useLocation' -import { useGeolocationMutation } from '@/hooks/mutations/useGeolocationMutation' -import { useStartGeolocationServiceEffect } from '@/utils/geolocation' -import { Platform } from 'react-native' import { useQuery } from '@tanstack/react-query' -import { isAuthValid } from '@/utils/features/auth/authSlice' -import { socket } from '@/utils/socket' -import { PlayerLocation } from '@/utils/features/location/locationSlice' +import React, { ReactNode, useEffect } from 'react' +import { Platform } from 'react-native' import { Constants } from '@/constants/Constants' +import { useAuth } from '@/hooks/useAuth' +import { useGeolocationMutation } from '@/hooks/mutations/useGeolocationMutation' +import { useQueuedLocations, useSetLastPlayerLocation, useSetLastPlayerLocations, useUnqueueLocation } from '@/hooks/useLocation' +import { isAuthValid } from '@/utils/features/auth/authSlice' +import { PlayerLocation } from '@/utils/features/location/locationSlice' +import { useStartGeolocationServiceEffect } from '@/utils/geolocation' +import { socket } from '@/utils/socket' export default function GeolocationProvider({ children }: { children: ReactNode }) { useStartGeolocationServiceEffect() @@ -51,10 +51,14 @@ export default function GeolocationProvider({ children }: { children: ReactNode setLastPlayerLocations(lastLocationsQuery.data) }, [lastLocationsQuery.status, lastLocationsQuery.dataUpdatedAt]) - socket.on('last-location', (data: PlayerLocation) => { - if (data.playerId) - setLastPlayerLocation(data) - }) + useEffect(() => { + const locationListener = async (data: PlayerLocation) => { + if (data.playerId) + setLastPlayerLocation(data) + } + socket.on('last-location', locationListener) + return () => { socket.off('last-location', locationListener) } + }, []) return <> {children} diff --git a/client/constants/Constants.ts b/client/constants/Constants.ts index 1183c68..4c862d8 100644 --- a/client/constants/Constants.ts +++ b/client/constants/Constants.ts @@ -1,4 +1,7 @@ +import { Accuracy } from 'expo-location' + export const Constants = { + LOCATION_ACCURACY: Accuracy.BestForNavigation, MIN_DELAY_LOCATION_SENT: 20, QUERY_REFETCH_INTERVAL: 15, } diff --git a/client/utils/geolocation.ts b/client/utils/geolocation.ts index d6bc2c7..f841aad 100644 --- a/client/utils/geolocation.ts +++ b/client/utils/geolocation.ts @@ -5,6 +5,7 @@ import { PlayerLocation, setLastLocation } from './features/location/locationSli import store from './store' import { useEffect } from 'react' import { socket } from './socket' +import { Constants } from '@/constants/Constants' const LOCATION_TASK = "fetch-geolocation" @@ -16,7 +17,6 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => { const { locations } = data const lastLoc: Location.LocationObject = locations.at(-1) store.dispatch(setLastLocation(lastLoc)) - console.log("sending-loc", lastLoc, socket.active) const playerId = store.getState().game.playerId if (socket.active && playerId) { const lastLocToSend: PlayerLocation = { @@ -25,11 +25,11 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => { latitude: lastLoc.coords.latitude, speed: lastLoc.coords.speed ?? 0, accuracy: lastLoc.coords.accuracy ?? 0, - altitude: lastLoc.coords.accuracy ?? 0, + altitude: lastLoc.coords.altitude ?? 0, altitudeAccuracy: lastLoc.coords.altitudeAccuracy ?? 0, timestamp: new Date(lastLoc.timestamp).toISOString(), } - socket.emit('last-location', { playerId: playerId, loc: lastLocToSend }) + socket.emit('last-location', lastLocToSend) } }) @@ -49,10 +49,10 @@ export async function startGeolocationService(): Promise void)> { if (Platform.OS !== "web") { await Location.startLocationUpdatesAsync(LOCATION_TASK, { - accuracy: Location.Accuracy.BestForNavigation, + accuracy: Constants.LOCATION_ACCURACY, activityType: Location.ActivityType.OtherNavigation, - deferredUpdatesInterval: 100, - timeInterval: 100, + deferredUpdatesInterval: 1000, + timeInterval: 1000, foregroundService: { killServiceOnDestroy: false, notificationBody: "Géolocalisation activée pour « Traintrape-moi »", @@ -63,7 +63,7 @@ export async function startGeolocationService(): Promise void)> { return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK) } else { - const locationSubscription = await Location.watchPositionAsync({accuracy: Location.Accuracy.BestForNavigation}, location_nouveau => store.dispatch(setLastLocation(location_nouveau))) + const locationSubscription = await Location.watchPositionAsync({ accuracy: Constants.LOCATION_ACCURACY }, location_nouveau => store.dispatch(setLastLocation(location_nouveau))) return locationSubscription.remove } }