Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 2x 5x 2x 2x 3x 3x 3x 2x 1x 1x 1x 37x 232x 37x 37x 37x 37x 37x 5x 5x 5x 5x 5x 5x 5x 5x 1x 5x 3x 5x 5x 5x 1x 5x 5x 5x 5x 37x 30x 1x 1x 1x 1x 1x 37x 9x 1x 8x 8x 8x 1x 7x 1x 1x 7x 7x 6x 6x 5x 1x 37x 4x 2x 2x 2x 2x 37x | import { useCallback, useRef } from "react";
import * as Location from "expo-location";
import useLocationStore, {
MovementMode,
PermissionStatus,
} from "./useLocationStore";
import {
determineMovementMode,
getWatcherConfigForMode,
} from "../utils/locationUtils";
import type { NearbyBuildingUpdatesGetter } from "./useNearbyBuildings";
const MODE_STABILITY_THRESHOLD = 3;
type MutableRef<T> = { current: T };
function getStableMovementMode(
detectedMode: MovementMode,
stableModeRef: MutableRef<MovementMode>,
modeChangeCountRef: MutableRef<Map<MovementMode, number>>,
threshold: number,
): MovementMode | null {
if (detectedMode === stableModeRef.current) {
modeChangeCountRef.current.clear();
return null;
}
const count = (modeChangeCountRef.current.get(detectedMode) ?? 0) + 1;
modeChangeCountRef.current.set(detectedMode, count);
if (count < threshold) {
return null;
}
stableModeRef.current = detectedMode;
modeChangeCountRef.current.clear();
return detectedMode;
}
interface UseLocationWatcherArgs {
permissionStatus: PermissionStatus;
movementMode: MovementMode;
isNavigating: boolean;
getNearbyBuildingUpdates: NearbyBuildingUpdatesGetter;
}
export default function useLocationWatcher({
permissionStatus,
movementMode,
isNavigating,
getNearbyBuildingUpdates,
}: UseLocationWatcherArgs) {
const setIsWatchingLocation = useLocationStore(
(s) => s.setIsWatchingLocation,
);
const locationSubRef = useRef<Location.LocationSubscription | null>(null);
const lastConfigRef = useRef("");
const modeChangeCountRef = useRef<Map<MovementMode, number>>(new Map());
const stableModeRef = useRef<MovementMode>("idle");
const processLocationUpdate = useCallback(
(location: Location.LocationObject) => {
const { latitude, longitude, speed, heading } = location.coords;
const speedMps = speed ?? 0;
const prev = useLocationStore.getState();
const updates: Record<string, unknown> = {};
const locChanged =
!prev.currentLocation ||
Math.abs(prev.currentLocation.latitude - latitude) > 0.0000005 ||
Math.abs(prev.currentLocation.longitude - longitude) > 0.0000005;
Eif (locChanged) {
updates.currentLocation = { latitude, longitude };
}
if (Math.abs(prev.currentSpeed - speedMps) > 0.3) {
updates.currentSpeed = speedMps;
}
if (heading !== null && heading >= 0 && prev.currentHeading !== heading) {
updates.currentHeading = heading;
}
const detectedMode = determineMovementMode(speedMps);
const stableMode = getStableMovementMode(
detectedMode,
stableModeRef,
modeChangeCountRef,
MODE_STABILITY_THRESHOLD,
);
if (stableMode) {
updates.movementMode = stableMode;
}
Eif (locChanged) {
Object.assign(
updates,
getNearbyBuildingUpdates(prev, { latitude, longitude }),
);
}
Eif (Object.keys(updates).length > 0) {
useLocationStore.setState(updates);
}
},
[getNearbyBuildingUpdates],
);
const stopWatching = useCallback(() => {
if (!locationSubRef.current) return;
locationSubRef.current.remove();
locationSubRef.current = null;
lastConfigRef.current = "";
Eif (useLocationStore.getState().isWatchingLocation) {
setIsWatchingLocation(false);
}
}, [setIsWatchingLocation]);
const startWatching = useCallback(async () => {
if (permissionStatus !== "granted") {
return;
}
const config = getWatcherConfigForMode(movementMode, isNavigating);
const configKey = JSON.stringify(config);
if (locationSubRef.current && lastConfigRef.current === configKey) {
return;
}
if (locationSubRef.current) {
locationSubRef.current.remove();
locationSubRef.current = null;
}
try {
locationSubRef.current = await Location.watchPositionAsync(
{
accuracy: config.accuracy,
timeInterval: config.timeInterval,
distanceInterval: config.distanceInterval,
},
processLocationUpdate,
);
lastConfigRef.current = configKey;
if (!useLocationStore.getState().isWatchingLocation) {
setIsWatchingLocation(true);
}
} catch (error) {
console.error("Error starting location watcher:", error);
}
}, [
isNavigating,
movementMode,
permissionStatus,
processLocationUpdate,
setIsWatchingLocation,
]);
const getCurrentPosition = useCallback(async () => {
if (permissionStatus !== "granted") {
throw new Error("Location permission not granted");
}
const location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
});
processLocationUpdate(location);
return {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
};
}, [permissionStatus, processLocationUpdate]);
return { startWatching, stopWatching, getCurrentPosition };
}
|