Corrections et optimisations géolocalisation

This commit is contained in:
Emmy D'Anello 2024-12-17 22:03:22 +01:00
parent 55aff5f900
commit 834b1643de
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 28 additions and 21 deletions

View File

@ -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()

View File

@ -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}

View File

@ -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,
}

View File

@ -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 | (() => 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 | (() => 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
}
}