All files / src/hooks useNavigationSettings.ts

98% Statements 49/50
50% Branches 1/2
100% Functions 23/23
100% Lines 48/48

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                                                  1x   1x 1x                     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x     2x 2x 2x 2x 1x                                           9x   9x 9x                                       1x    
import { create } from "zustand";
import AsyncStorage from "@react-native-async-storage/async-storage";
 
interface NavigationSettingsState {
  avoidStairs: boolean;
  indoorNavigation: boolean;
  autoRerouting: boolean;
  showCompass: boolean;
  showPedestrianTraffic: boolean;
  mapTiltAngle: number; // degrees
  distanceUnits: "Meters" | "Feet";
  mapStyle: "Standard" | "Satellite";
  northOrientation: "Fixed" | "Compass";
  setAvoidStairs: (value: boolean) => void;
  setIndoorNavigation: (value: boolean) => void;
  setAutoRerouting: (value: boolean) => void;
  setShowCompass: (value: boolean) => void;
  setShowPedestrianTraffic: (value: boolean) => void;
  setMapTiltAngle: (value: number) => void;
  setDistanceUnits: (value: "Meters" | "Feet") => void;
  setMapStyle: (value: "Standard" | "Satellite") => void;
  setNorthOrientation: (value: "Fixed" | "Compass") => void;
  hydrateFromStorage: () => Promise<void>;
}
 
const STORAGE_KEY = "navigationSettings.v1";
 
export const useNavigationSettingsStore = create<NavigationSettingsState>(
  (set, get) => ({
    avoidStairs: false,
    indoorNavigation: true,
    autoRerouting: true,
    showCompass: true,
    showPedestrianTraffic: false,
    mapTiltAngle: 45,
    distanceUnits: "Meters",
    mapStyle: "Standard",
    northOrientation: "Fixed",
    setAvoidStairs: (avoidStairs) =>
      set((state) => {
        const next = { ...state, avoidStairs };
        void persist(next);
        return next;
      }),
    setIndoorNavigation: (indoorNavigation) =>
      set((state) => {
        const next = { ...state, indoorNavigation };
        void persist(next);
        return next;
      }),
    setAutoRerouting: (autoRerouting) =>
      set((state) => {
        const next = { ...state, autoRerouting };
        void persist(next);
        return next;
      }),
    setShowCompass: (showCompass) =>
      set((state) => {
        const next = { ...state, showCompass };
        void persist(next);
        return next;
      }),
    setShowPedestrianTraffic: (showPedestrianTraffic) =>
      set((state) => {
        const next = { ...state, showPedestrianTraffic };
        void persist(next);
        return next;
      }),
    setMapTiltAngle: (mapTiltAngle) =>
      set((state) => {
        const next = { ...state, mapTiltAngle };
        void persist(next);
        return next;
      }),
    setDistanceUnits: (distanceUnits) =>
      set((state) => {
        const next = { ...state, distanceUnits };
        void persist(next);
        return next;
      }),
    setMapStyle: (mapStyle) =>
      set((state) => {
        const next = { ...state, mapStyle };
        void persist(next);
        return next;
      }),
    setNorthOrientation: (northOrientation) =>
      set((state) => {
        const next = { ...state, northOrientation };
        void persist(next);
        return next;
      }),
    hydrateFromStorage: async () => {
      try {
        const raw = await AsyncStorage.getItem(STORAGE_KEY);
        Iif (!raw) return;
        const parsed = JSON.parse(raw);
        set((state) => ({
          ...state,
          ...parsed,
        }));
      } catch {
        // ignore
      }
    },
  }),
);
 
async function persist(state: NavigationSettingsState) {
  const {
    avoidStairs,
    indoorNavigation,
    autoRerouting,
    showCompass,
    showPedestrianTraffic,
    mapTiltAngle,
    distanceUnits,
    mapStyle,
    northOrientation,
  } = state;
 
  try {
    await AsyncStorage.setItem(
      STORAGE_KEY,
      JSON.stringify({
        avoidStairs,
        indoorNavigation,
        autoRerouting,
        showCompass,
        showPedestrianTraffic,
        mapTiltAngle,
        distanceUnits,
        mapStyle,
        northOrientation,
      }),
    );
  } catch {
    // best-effort
  }
}
 
export function useNavigationSettings() {
  return useNavigationSettingsStore();
}