All files / hooks locationData.ts

88% Statements 22/25
62.5% Branches 5/8
100% Functions 3/3
88% Lines 22/25

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                                                                15x 15x 15x 15x   15x 5x   5x 5x   5x     5x   4x 4x 4x 2x     2x 2x                     1x     5x 5x     5x       15x           15x    
 
 
 
import iLocationData from "@/interfaces/iLocationData";
import { LocationAccessError } from "@/utils/ErrorTypes";
import { useState, useEffect } from 'react';
import { isEnabled } from "react-native/Libraries/Performance/Systrace";
 
 
 
/**
 * Data about the user's location.
 */
class LocationResponse {
    /** The [Latitude, Longitude of the device */
    location?:[number, number] | null;
    /** If an error occurs it is stored here */
    error?: string | null;
    /** If the location is in the process of loading it is true, otherwise false */
    loading?: boolean;
    /** true if location access is enabled, false otherwise */
    isEnabled?: boolean;
 
}
 
 
/**
 * A hook that gets the location of the user.
 * @param locationService The service used to manage location.
 * @returns Data about the current status of the location.
 */
export default function useLocation(locationService: iLocationData): LocationResponse {
    const [location, setLocation] = useState<[number, number] | null>(null);
    const [error, setError] = useState<string | null>(null);
    const [loading, setLoading] = useState<boolean>(false);
    const [isEnabled, setIsEnabled] = useState<boolean>(false);
 
    useEffect(() => {
        const fetchLocation = async () => {
 
            setLoading(true);
            setError(null);
 
            let enabled = await locationService.isEnabled();
 
            
            if (enabled || (await locationService.requestPermission())) {
                /* if location permision is granted */
                enabled = true;
                try {
                    const loc = await locationService.getLocation();
                    setLocation(loc);
                } 
                catch (err) {
                    if (err instanceof LocationAccessError) {
                        setError(err.message);
                    } 
                    else Eif (err instanceof Error) {
                        setError(`uncaught error: ${err.message}`);
                    } 
                    else {
                        setError(`unexpected error: ${err}`);
                    }
                }
            } 
            else {
                setError("location access disabled");
            }
 
            setIsEnabled(enabled);
            setLoading(false);
        };
 
        fetchLocation();
    }, []);
 
 
    let response =  {
        location: location,
        error: error,
        loading: loading,
        isEnabled: isEnabled
    }
    return response;
    
}