2024-12-06 20:49:28 +00:00
|
|
|
import * as Location from 'expo-location'
|
|
|
|
import * as TaskManager from 'expo-task-manager'
|
|
|
|
import { Platform } from 'react-native'
|
2024-12-17 00:06:23 +00:00
|
|
|
import { PlayerLocation, setLastLocation } from './features/location/locationSlice'
|
2024-12-06 20:49:28 +00:00
|
|
|
import store from './store'
|
|
|
|
import { useEffect } from 'react'
|
2024-12-17 00:06:23 +00:00
|
|
|
import { socket } from './socket'
|
2024-12-19 15:47:41 +00:00
|
|
|
import { useSettings } from '@/hooks/useGame'
|
|
|
|
import { useAuth } from '@/hooks/useAuth'
|
|
|
|
import { isAuthValid } from './features/auth/authSlice'
|
2024-12-06 20:49:28 +00:00
|
|
|
|
2024-12-19 15:47:41 +00:00
|
|
|
const LOCATION_TASK = "TRAINTRAPE_MOI_GEOLOCATION"
|
2024-12-06 20:49:28 +00:00
|
|
|
|
|
|
|
TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
|
|
|
if (error) {
|
|
|
|
console.error(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const { locations } = data
|
2024-12-17 00:06:23 +00:00
|
|
|
const lastLoc: Location.LocationObject = locations.at(-1)
|
2024-12-19 16:50:41 +00:00
|
|
|
if (__DEV__)
|
|
|
|
console.log("Localisation reçue :", lastLoc)
|
2024-12-17 00:06:23 +00:00
|
|
|
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,
|
2024-12-17 21:03:22 +00:00
|
|
|
altitude: lastLoc.coords.altitude ?? 0,
|
2024-12-17 00:06:23 +00:00
|
|
|
altitudeAccuracy: lastLoc.coords.altitudeAccuracy ?? 0,
|
|
|
|
timestamp: new Date(lastLoc.timestamp).toISOString(),
|
|
|
|
}
|
2024-12-17 21:03:22 +00:00
|
|
|
socket.emit('last-location', lastLocToSend)
|
2024-12-17 00:06:23 +00:00
|
|
|
}
|
2024-12-06 20:49:28 +00:00
|
|
|
})
|
|
|
|
|
2024-12-19 15:47:41 +00:00
|
|
|
export async function startGeolocationService(locationAccuracy: Location.Accuracy | null): Promise<void | (() => void)> {
|
2024-12-06 20:49:28 +00:00
|
|
|
if (Platform.OS !== "web" && await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK))
|
2024-12-19 15:47:41 +00:00
|
|
|
await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
|
|
|
|
|
|
|
if (locationAccuracy === null)
|
|
|
|
return
|
2024-12-06 20:49:28 +00:00
|
|
|
|
|
|
|
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, {
|
2024-12-19 15:47:41 +00:00
|
|
|
accuracy: locationAccuracy,
|
2024-12-06 20:49:28 +00:00
|
|
|
activityType: Location.ActivityType.OtherNavigation,
|
2024-12-19 15:47:41 +00:00
|
|
|
distanceInterval: 10,
|
2024-12-17 21:03:22 +00:00
|
|
|
timeInterval: 1000,
|
2024-12-06 20:49:28 +00:00
|
|
|
foregroundService: {
|
|
|
|
killServiceOnDestroy: false,
|
|
|
|
notificationBody: "Géolocalisation activée pour « Traintrape-moi »",
|
|
|
|
notificationTitle: "Traintrape-moi",
|
|
|
|
notificationColor: "#FFFF00",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
|
|
|
}
|
|
|
|
else {
|
2024-12-19 15:47:41 +00:00
|
|
|
const locationSubscription = await Location.watchPositionAsync({ accuracy: locationAccuracy }, location_nouveau => store.dispatch(setLastLocation(location_nouveau)))
|
2024-12-06 20:49:28 +00:00
|
|
|
return locationSubscription.remove
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-19 15:47:41 +00:00
|
|
|
export const useStartGeolocationServiceEffect = () => {
|
|
|
|
const auth = useAuth()
|
|
|
|
const settings = useSettings()
|
|
|
|
return useEffect(() => {
|
|
|
|
if (!isAuthValid(auth))
|
|
|
|
return
|
|
|
|
let cleanup: void | (() => void) = () => {}
|
|
|
|
startGeolocationService(settings.locationAccuracy).then(result => cleanup = result)
|
|
|
|
return cleanup
|
|
|
|
}, [auth, settings.locationAccuracy])
|
|
|
|
}
|