2024-12-17 21:03:22 +00:00
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import React, { ReactNode, useEffect } from 'react'
|
|
|
|
import { Platform } from 'react-native'
|
|
|
|
import { Constants } from '@/constants/Constants'
|
2024-12-11 00:30:21 +00:00
|
|
|
import { useAuth } from '@/hooks/useAuth'
|
2024-12-19 16:33:17 +00:00
|
|
|
import { useSetLocationAccuracy } from '@/hooks/useGame'
|
2024-12-11 00:30:21 +00:00
|
|
|
import { useGeolocationMutation } from '@/hooks/mutations/useGeolocationMutation'
|
2024-12-17 21:03:22 +00:00
|
|
|
import { useQueuedLocations, useSetLastPlayerLocation, useSetLastPlayerLocations, useUnqueueLocation } from '@/hooks/useLocation'
|
2024-12-13 22:07:37 +00:00
|
|
|
import { isAuthValid } from '@/utils/features/auth/authSlice'
|
2024-12-17 00:06:23 +00:00
|
|
|
import { PlayerLocation } from '@/utils/features/location/locationSlice'
|
2024-12-17 21:03:22 +00:00
|
|
|
import { useStartGeolocationServiceEffect } from '@/utils/geolocation'
|
2024-12-19 16:33:17 +00:00
|
|
|
import * as SecureStore from '@/utils/SecureStore'
|
2024-12-17 21:03:22 +00:00
|
|
|
import { socket } from '@/utils/socket'
|
2024-12-19 16:33:17 +00:00
|
|
|
import { Accuracy } from 'expo-location'
|
2024-12-11 00:30:21 +00:00
|
|
|
|
|
|
|
export default function GeolocationProvider({ children }: { children: ReactNode }) {
|
|
|
|
useStartGeolocationServiceEffect()
|
|
|
|
|
|
|
|
const auth = useAuth()
|
2024-12-19 16:33:17 +00:00
|
|
|
const setLocationAccuracy = useSetLocationAccuracy()
|
2024-12-11 00:30:21 +00:00
|
|
|
const geolocationsQueue = useQueuedLocations()
|
|
|
|
const unqueueLocation = useUnqueueLocation()
|
2024-12-13 00:18:55 +00:00
|
|
|
const setLastPlayerLocations = useSetLastPlayerLocations()
|
2024-12-17 00:06:23 +00:00
|
|
|
const setLastPlayerLocation = useSetLastPlayerLocation()
|
2024-12-11 00:30:21 +00:00
|
|
|
const geolocationMutation = useGeolocationMutation({
|
|
|
|
auth,
|
2024-12-15 19:42:21 +00:00
|
|
|
onPostSuccess: (data, variables) => unqueueLocation(variables),
|
|
|
|
onError: ({ response, error }) => console.error(response, error),
|
2024-12-11 00:30:21 +00:00
|
|
|
})
|
2024-12-19 16:33:17 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
SecureStore.getItemAsync('locationAccuracy').then(locationAccuracyString => {
|
|
|
|
if (!locationAccuracyString)
|
|
|
|
setLocationAccuracy(Accuracy.Balanced)
|
|
|
|
else if (locationAccuracyString === 'null')
|
|
|
|
setLocationAccuracy(null)
|
|
|
|
else
|
|
|
|
setLocationAccuracy(+locationAccuracyString)
|
|
|
|
})
|
|
|
|
}, [])
|
2024-12-11 00:30:21 +00:00
|
|
|
|
2024-12-14 16:21:55 +00:00
|
|
|
if (Platform.OS !== "web") {
|
2024-12-11 00:30:21 +00:00
|
|
|
useEffect(() => {
|
2024-12-15 17:01:13 +00:00
|
|
|
if (geolocationsQueue.length === 0 || geolocationMutation.isPending || !isAuthValid(auth))
|
2024-12-14 16:21:55 +00:00
|
|
|
return
|
2024-12-15 19:42:21 +00:00
|
|
|
const locToSend = geolocationsQueue[0]
|
|
|
|
geolocationMutation.mutate(locToSend)
|
2024-12-14 16:21:55 +00:00
|
|
|
}, [auth, geolocationMutation.status, geolocationsQueue])
|
|
|
|
}
|
2024-12-11 00:30:21 +00:00
|
|
|
|
2024-12-13 00:18:55 +00:00
|
|
|
const lastLocationsQuery = useQuery({
|
|
|
|
queryKey: ['get-last-locations', auth.token],
|
|
|
|
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/geolocations/last-locations/`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${auth.token}`,
|
|
|
|
"Content-Type": "application/json",
|
2024-12-13 22:07:37 +00:00
|
|
|
},
|
|
|
|
}).then(resp => resp.json()),
|
2024-12-13 23:53:22 +00:00
|
|
|
initialData: [],
|
2024-12-13 22:07:37 +00:00
|
|
|
enabled: isAuthValid(auth),
|
2024-12-17 00:12:54 +00:00
|
|
|
refetchInterval: Constants.QUERY_REFETCH_INTERVAL * 1000,
|
2024-12-13 00:18:55 +00:00
|
|
|
})
|
|
|
|
useEffect(() => {
|
|
|
|
if (lastLocationsQuery.isSuccess && lastLocationsQuery.data)
|
|
|
|
setLastPlayerLocations(lastLocationsQuery.data)
|
2024-12-13 23:53:22 +00:00
|
|
|
}, [lastLocationsQuery.status, lastLocationsQuery.dataUpdatedAt])
|
2024-12-13 00:18:55 +00:00
|
|
|
|
2024-12-17 21:03:22 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const locationListener = async (data: PlayerLocation) => {
|
|
|
|
if (data.playerId)
|
|
|
|
setLastPlayerLocation(data)
|
|
|
|
}
|
|
|
|
socket.on('last-location', locationListener)
|
|
|
|
return () => { socket.off('last-location', locationListener) }
|
|
|
|
}, [])
|
2024-12-17 00:06:23 +00:00
|
|
|
|
2024-12-11 00:30:21 +00:00
|
|
|
return <>
|
|
|
|
{children}
|
|
|
|
</>
|
|
|
|
}
|