Amélioration Géolocalisation
This commit is contained in:
@ -5,9 +5,11 @@ import { PlayerLocation, setLastLocation } from './features/location/locationSli
|
||||
import store from './store'
|
||||
import { useEffect } from 'react'
|
||||
import { socket } from './socket'
|
||||
import { Constants } from '@/constants/Constants'
|
||||
import { useSettings } from '@/hooks/useGame'
|
||||
import { useAuth } from '@/hooks/useAuth'
|
||||
import { isAuthValid } from './features/auth/authSlice'
|
||||
|
||||
const LOCATION_TASK = "fetch-geolocation"
|
||||
const LOCATION_TASK = "TRAINTRAPE_MOI_GEOLOCATION"
|
||||
|
||||
TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
||||
if (error) {
|
||||
@ -33,9 +35,12 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
||||
}
|
||||
})
|
||||
|
||||
export async function startGeolocationService(): Promise<void | (() => void)> {
|
||||
export async function startGeolocationService(locationAccuracy: Location.Accuracy | null): Promise<void | (() => void)> {
|
||||
if (Platform.OS !== "web" && await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK))
|
||||
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
||||
await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
||||
|
||||
if (locationAccuracy === null)
|
||||
return
|
||||
|
||||
await Location.enableNetworkProviderAsync().catch(error => alert(error))
|
||||
|
||||
@ -49,9 +54,9 @@ export async function startGeolocationService(): Promise<void | (() => void)> {
|
||||
|
||||
if (Platform.OS !== "web") {
|
||||
await Location.startLocationUpdatesAsync(LOCATION_TASK, {
|
||||
accuracy: Constants.LOCATION_ACCURACY,
|
||||
accuracy: locationAccuracy,
|
||||
activityType: Location.ActivityType.OtherNavigation,
|
||||
deferredUpdatesInterval: 1000,
|
||||
distanceInterval: 10,
|
||||
timeInterval: 1000,
|
||||
foregroundService: {
|
||||
killServiceOnDestroy: false,
|
||||
@ -63,13 +68,19 @@ export async function startGeolocationService(): Promise<void | (() => void)> {
|
||||
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
|
||||
}
|
||||
else {
|
||||
const locationSubscription = await Location.watchPositionAsync({ accuracy: Constants.LOCATION_ACCURACY }, location_nouveau => store.dispatch(setLastLocation(location_nouveau)))
|
||||
const locationSubscription = await Location.watchPositionAsync({ accuracy: locationAccuracy }, 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
|
||||
}, [])
|
||||
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])
|
||||
}
|
||||
|
Reference in New Issue
Block a user