Corrections et optimisations géolocalisation
This commit is contained in:
parent
55aff5f900
commit
834b1643de
@ -11,7 +11,7 @@ import { Challenge } from '@/utils/features/challenges/challengesSlice'
|
|||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { router } from 'expo-router'
|
import { router } from 'expo-router'
|
||||||
import { useShareIntentContext } from 'expo-share-intent'
|
import { useShareIntentContext } from 'expo-share-intent'
|
||||||
import { ReactNode, useEffect } from 'react'
|
import React, { ReactNode, useEffect } from 'react'
|
||||||
|
|
||||||
export default function GameProvider({ children }: { children: ReactNode }) {
|
export default function GameProvider({ children }: { children: ReactNode }) {
|
||||||
const auth = useAuth()
|
const auth = useAuth()
|
||||||
|
@ -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 { useQuery } from '@tanstack/react-query'
|
||||||
import { isAuthValid } from '@/utils/features/auth/authSlice'
|
import React, { ReactNode, useEffect } from 'react'
|
||||||
import { socket } from '@/utils/socket'
|
import { Platform } from 'react-native'
|
||||||
import { PlayerLocation } from '@/utils/features/location/locationSlice'
|
|
||||||
import { Constants } from '@/constants/Constants'
|
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 }) {
|
export default function GeolocationProvider({ children }: { children: ReactNode }) {
|
||||||
useStartGeolocationServiceEffect()
|
useStartGeolocationServiceEffect()
|
||||||
@ -51,10 +51,14 @@ export default function GeolocationProvider({ children }: { children: ReactNode
|
|||||||
setLastPlayerLocations(lastLocationsQuery.data)
|
setLastPlayerLocations(lastLocationsQuery.data)
|
||||||
}, [lastLocationsQuery.status, lastLocationsQuery.dataUpdatedAt])
|
}, [lastLocationsQuery.status, lastLocationsQuery.dataUpdatedAt])
|
||||||
|
|
||||||
socket.on('last-location', (data: PlayerLocation) => {
|
useEffect(() => {
|
||||||
if (data.playerId)
|
const locationListener = async (data: PlayerLocation) => {
|
||||||
setLastPlayerLocation(data)
|
if (data.playerId)
|
||||||
})
|
setLastPlayerLocation(data)
|
||||||
|
}
|
||||||
|
socket.on('last-location', locationListener)
|
||||||
|
return () => { socket.off('last-location', locationListener) }
|
||||||
|
}, [])
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
{children}
|
{children}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
import { Accuracy } from 'expo-location'
|
||||||
|
|
||||||
export const Constants = {
|
export const Constants = {
|
||||||
|
LOCATION_ACCURACY: Accuracy.BestForNavigation,
|
||||||
MIN_DELAY_LOCATION_SENT: 20,
|
MIN_DELAY_LOCATION_SENT: 20,
|
||||||
QUERY_REFETCH_INTERVAL: 15,
|
QUERY_REFETCH_INTERVAL: 15,
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import { PlayerLocation, setLastLocation } from './features/location/locationSli
|
|||||||
import store from './store'
|
import store from './store'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
import { socket } from './socket'
|
import { socket } from './socket'
|
||||||
|
import { Constants } from '@/constants/Constants'
|
||||||
|
|
||||||
const LOCATION_TASK = "fetch-geolocation"
|
const LOCATION_TASK = "fetch-geolocation"
|
||||||
|
|
||||||
@ -16,7 +17,6 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
|||||||
const { locations } = data
|
const { locations } = data
|
||||||
const lastLoc: Location.LocationObject = locations.at(-1)
|
const lastLoc: Location.LocationObject = locations.at(-1)
|
||||||
store.dispatch(setLastLocation(lastLoc))
|
store.dispatch(setLastLocation(lastLoc))
|
||||||
console.log("sending-loc", lastLoc, socket.active)
|
|
||||||
const playerId = store.getState().game.playerId
|
const playerId = store.getState().game.playerId
|
||||||
if (socket.active && playerId) {
|
if (socket.active && playerId) {
|
||||||
const lastLocToSend: PlayerLocation = {
|
const lastLocToSend: PlayerLocation = {
|
||||||
@ -25,11 +25,11 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
|||||||
latitude: lastLoc.coords.latitude,
|
latitude: lastLoc.coords.latitude,
|
||||||
speed: lastLoc.coords.speed ?? 0,
|
speed: lastLoc.coords.speed ?? 0,
|
||||||
accuracy: lastLoc.coords.accuracy ?? 0,
|
accuracy: lastLoc.coords.accuracy ?? 0,
|
||||||
altitude: lastLoc.coords.accuracy ?? 0,
|
altitude: lastLoc.coords.altitude ?? 0,
|
||||||
altitudeAccuracy: lastLoc.coords.altitudeAccuracy ?? 0,
|
altitudeAccuracy: lastLoc.coords.altitudeAccuracy ?? 0,
|
||||||
timestamp: new Date(lastLoc.timestamp).toISOString(),
|
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") {
|
if (Platform.OS !== "web") {
|
||||||
await Location.startLocationUpdatesAsync(LOCATION_TASK, {
|
await Location.startLocationUpdatesAsync(LOCATION_TASK, {
|
||||||
accuracy: Location.Accuracy.BestForNavigation,
|
accuracy: Constants.LOCATION_ACCURACY,
|
||||||
activityType: Location.ActivityType.OtherNavigation,
|
activityType: Location.ActivityType.OtherNavigation,
|
||||||
deferredUpdatesInterval: 100,
|
deferredUpdatesInterval: 1000,
|
||||||
timeInterval: 100,
|
timeInterval: 1000,
|
||||||
foregroundService: {
|
foregroundService: {
|
||||||
killServiceOnDestroy: false,
|
killServiceOnDestroy: false,
|
||||||
notificationBody: "Géolocalisation activée pour « Traintrape-moi »",
|
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)
|
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
||||||
}
|
}
|
||||||
else {
|
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
|
return locationSubscription.remove
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user