Prototype envoi géolocalisations

This commit is contained in:
2024-12-11 01:30:21 +01:00
parent db7a0b970d
commit bdd53eb8bb
10 changed files with 140 additions and 32 deletions

View File

@ -2,7 +2,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import * as SecureStore from '@/utils/SecureStore'
import { Platform } from 'react-native'
interface AuthState {
export interface AuthState {
loggedIn: boolean,
name: string | null,
token: string | null,

View File

@ -1,24 +1,37 @@
import { Constants } from '@/constants/Constants'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { LocationObject } from 'expo-location'
interface LocationState {
location: LocationObject | null
lastOwnLocation: LocationObject | null
lastSentLocation: LocationObject | null
queuedLocations: LocationObject[]
}
const initialState: LocationState = {
location: null
lastOwnLocation: null,
lastSentLocation: null,
queuedLocations: []
}
export const locationSlice = createSlice({
name: 'location',
initialState: initialState,
reducers: {
setLocation: (state, action: PayloadAction<LocationObject>) => {
state.location = action.payload
setLastLocation: (state, action: PayloadAction<LocationObject>) => {
const location: LocationObject = action.payload
state.lastOwnLocation = location
if (state.lastSentLocation === null || (location.timestamp - state.lastSentLocation.timestamp) >= Constants.MIN_DELAY_LOCATION_SENT * 1000) {
state.lastSentLocation = location
state.queuedLocations.push(location)
}
},
unqueueLocation: (state, action: PayloadAction<LocationObject>) => {
state.queuedLocations.pop()
},
},
})
export const { setLocation } = locationSlice.actions
export const { setLastLocation, unqueueLocation } = locationSlice.actions
export default locationSlice.reducer

View File

@ -1,7 +1,7 @@
import * as Location from 'expo-location'
import * as TaskManager from 'expo-task-manager'
import { Platform } from 'react-native'
import { setLocation } from './features/location/locationSlice'
import { setLastLocation } from './features/location/locationSlice'
import store from './store'
import { useEffect } from 'react'
@ -13,10 +13,7 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
return
}
const { locations } = data
store.dispatch(setLocation(locations.at(-1)))
for (let location of locations) {
// TODO Envoyer les positions au serveur
}
store.dispatch(setLastLocation(locations.at(-1)))
})
export async function startGeolocationService(): Promise<void | (() => void)> {
@ -48,7 +45,7 @@ export async function startGeolocationService(): Promise<void | (() => void)> {
return async () => await Location.stopLocationUpdatesAsync(LOCATION_TASK)
}
else {
const locationSubscription = await Location.watchPositionAsync({accuracy: Location.Accuracy.BestForNavigation}, location_nouveau => store.dispatch(setLocation(location_nouveau)))
const locationSubscription = await Location.watchPositionAsync({accuracy: Location.Accuracy.BestForNavigation}, location_nouveau => store.dispatch(setLastLocation(location_nouveau)))
return locationSubscription.remove
}
}