76 lines
2.9 KiB
TypeScript
76 lines
2.9 KiB
TypeScript
import * as Location from 'expo-location'
|
|
import * as TaskManager from 'expo-task-manager'
|
|
import { Platform } from 'react-native'
|
|
import { PlayerLocation, setLastLocation } from './features/location/locationSlice'
|
|
import store from './store'
|
|
import { useEffect } from 'react'
|
|
import { socket } from './socket'
|
|
import { Constants } from '@/constants/Constants'
|
|
|
|
const LOCATION_TASK = "fetch-geolocation"
|
|
|
|
TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
|
if (error) {
|
|
console.error(error)
|
|
return
|
|
}
|
|
const { locations } = data
|
|
const lastLoc: Location.LocationObject = locations.at(-1)
|
|
store.dispatch(setLastLocation(lastLoc))
|
|
const playerId = store.getState().game.playerId
|
|
if (socket.active && playerId) {
|
|
const lastLocToSend: PlayerLocation = {
|
|
playerId: playerId,
|
|
longitude: lastLoc.coords.longitude,
|
|
latitude: lastLoc.coords.latitude,
|
|
speed: lastLoc.coords.speed ?? 0,
|
|
accuracy: lastLoc.coords.accuracy ?? 0,
|
|
altitude: lastLoc.coords.altitude ?? 0,
|
|
altitudeAccuracy: lastLoc.coords.altitudeAccuracy ?? 0,
|
|
timestamp: new Date(lastLoc.timestamp).toISOString(),
|
|
}
|
|
socket.emit('last-location', lastLocToSend)
|
|
}
|
|
})
|
|
|
|
export async function startGeolocationService(): Promise<void | (() => void)> {
|
|
if (Platform.OS !== "web" && await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK))
|
|
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
|
|
|
await Location.enableNetworkProviderAsync().catch(error => alert(error))
|
|
|
|
const { status: foregroundStatus } = await Location.requestForegroundPermissionsAsync()
|
|
if (foregroundStatus !== 'granted')
|
|
alert("Vous devez activer votre géolocalisation pour utiliser l'application.")
|
|
|
|
const { status: backgroundStatus } = await Location.requestBackgroundPermissionsAsync()
|
|
if (backgroundStatus !== 'granted')
|
|
alert("Vous devez activer votre géolocalisation en arrière-plan pour utiliser l'application.")
|
|
|
|
if (Platform.OS !== "web") {
|
|
await Location.startLocationUpdatesAsync(LOCATION_TASK, {
|
|
accuracy: Constants.LOCATION_ACCURACY,
|
|
activityType: Location.ActivityType.OtherNavigation,
|
|
deferredUpdatesInterval: 1000,
|
|
timeInterval: 1000,
|
|
foregroundService: {
|
|
killServiceOnDestroy: false,
|
|
notificationBody: "Géolocalisation activée pour « Traintrape-moi »",
|
|
notificationTitle: "Traintrape-moi",
|
|
notificationColor: "#FFFF00",
|
|
}
|
|
})
|
|
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
|
}
|
|
else {
|
|
const locationSubscription = await Location.watchPositionAsync({ accuracy: Constants.LOCATION_ACCURACY }, location_nouveau => store.dispatch(setLastLocation(location_nouveau)))
|
|
return locationSubscription.remove
|
|
}
|
|
}
|
|
|
|
export const useStartGeolocationServiceEffect = () => useEffect(() => {
|
|
let cleanup: void | (() => void) = () => {}
|
|
startGeolocationService().then(result => cleanup = result)
|
|
return cleanup
|
|
}, [])
|