2024-12-11 00:30:21 +00:00
|
|
|
import { Constants } from '@/constants/Constants'
|
2024-12-06 20:49:28 +00:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
import { LocationObject } from 'expo-location'
|
|
|
|
|
|
|
|
interface LocationState {
|
2024-12-11 00:30:21 +00:00
|
|
|
lastOwnLocation: LocationObject | null
|
|
|
|
lastSentLocation: LocationObject | null
|
|
|
|
queuedLocations: LocationObject[]
|
2024-12-06 20:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: LocationState = {
|
2024-12-11 00:30:21 +00:00
|
|
|
lastOwnLocation: null,
|
|
|
|
lastSentLocation: null,
|
|
|
|
queuedLocations: []
|
2024-12-06 20:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const locationSlice = createSlice({
|
|
|
|
name: 'location',
|
|
|
|
initialState: initialState,
|
|
|
|
reducers: {
|
2024-12-11 00:30:21 +00:00
|
|
|
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()
|
2024-12-06 20:49:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-12-11 00:30:21 +00:00
|
|
|
export const { setLastLocation, unqueueLocation } = locationSlice.actions
|
2024-12-06 20:49:28 +00:00
|
|
|
|
|
|
|
export default locationSlice.reducer
|