Amélioration Géolocalisation
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { Accuracy } from 'expo-location'
|
||||
|
||||
export interface RunPayload {
|
||||
id: number
|
||||
@ -37,6 +38,11 @@ export interface GameState {
|
||||
chaseFreeTime: number | null // date
|
||||
penaltyStart: number | null // date
|
||||
penaltyEnd: number | null //date
|
||||
settings: Settings
|
||||
}
|
||||
|
||||
export interface Settings {
|
||||
locationAccuracy: Accuracy | null
|
||||
}
|
||||
|
||||
const initialState: GameState = {
|
||||
@ -49,6 +55,9 @@ const initialState: GameState = {
|
||||
chaseFreeTime: null,
|
||||
penaltyStart: null,
|
||||
penaltyEnd: null,
|
||||
settings: {
|
||||
locationAccuracy: Accuracy.Highest,
|
||||
}
|
||||
}
|
||||
|
||||
export const gameSlice = createSlice({
|
||||
@ -77,10 +86,13 @@ export const gameSlice = createSlice({
|
||||
updatePenalty: (state, action: PayloadAction<PenaltyPayload>) => {
|
||||
state.penaltyStart = action.payload.penaltyStart
|
||||
state.penaltyEnd = action.payload.penaltyEnd
|
||||
},
|
||||
setLocationAccuracy: (state, action: PayloadAction<Accuracy | null>) => {
|
||||
state.settings.locationAccuracy = action.payload
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const { setPlayerId, updateMoney, updateActiveChallengeId, updateGameState, updatePenalty } = gameSlice.actions
|
||||
export const { setLocationAccuracy, setPlayerId, updateMoney, updateActiveChallengeId, updateGameState, updatePenalty } = gameSlice.actions
|
||||
|
||||
export default gameSlice.reducer
|
||||
|
@ -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