All files / atlas/src/components Map.tsx

50% Statements 24/48
10% Branches 1/10
7.69% Functions 1/13
50% Lines 24/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 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                            4x             4x       2x 2x       2x 2x       2x 2x       2x 2x         2x 2x       2x 2x       2x 2x   2x         2x   2x                                                           2x       2x                                                   2x               2x                                                 2x                                                                                            
import { FontAwesome } from "@expo/vector-icons";
import * as Location from 'expo-location';
import React, { useEffect, useState } from "react";
import { Keyboard, KeyboardAvoidingView, TouchableOpacity, View } from "react-native";
import MapView, { LatLng, Region } from "react-native-maps";
import Modal from "react-native-modal";
import { colors } from "../globals";
import { Landmark, useLandmarks } from "../hooks/useLandmarks";
import AddLandmark from "./AddLandmark";
import LandmarkDetails from "./LandmarkDetails";
import LandmarkPin from "./LandmarkPin";
 
/**
 * An interface representing the user location retrieved from [expo-location]{@link https://docs.expo.dev/versions/latest/sdk/location/}.
 * @category Map
 */
export interface UserLocation {
    latitude: number;
    longitude: number;
    heading?: number;
}
 
const directionsUrl = "https://maps.googleapis.com/maps/api/directions/son?mode=walking&alternatives=true&key=AIzaSyD06YUMazlb4Fu0Q81y_YNyEBz8PmtZyeY";
 
/**
 * The screen component containing the [react-native-maps]{@link https://github.com/react-native-maps/react-native-maps} Map and all related functionality.
 * @category Map
 * @component
 */
export const Map: React.FC = () => {
    /**
     * State that contains the new {@link Landmark} object which is passed down to the {@link AddLandmark} modal.
     */
    const newLandmarkState: Landmark = {};
    const [newLandmark, setNewLandmark] = useState<Landmark>(newLandmarkState);
    /**
     * State that contains the selected {@link Landmark} object which is passed down to the {@link LandmarkDetails} modal.
     */
    const selectedLandmarkState: Landmark = {};
    const [selectedLandmark, setSelectedLandmark] = useState<Landmark>(selectedLandmarkState);
    /**
     * Holds the visibility state of the {@link AddLandmark} modal.
     */
    const lmAddVisibleState = false;
    const [lmAddVisible, toggleLmAdd] = useState<boolean>(false);
    /**
     * Holds the visibility state of the {@link LandmarkDetails} modal.
     */
    const lmDetailsVisibleState = false;
    const [lmDetailsVisible, toggleLmDetails] = useState<boolean>(false);
    /**
     * Flag that toggles whether or not editing is enabled in the {@link LandmarkDetails} modal. 
     * The parent Map component has access to it so that it can disable closing the modal on backdrop press when it is enabled.
     */
    const lmDetailsEditingState = false;
    const [lmDetailsEditing, toggleLmDetailsEditing] = useState<boolean>(false);
    /**
     * State that holds a {@link UserLocation} object retrieved from location services.
     */
    const userLocationState: UserLocation | undefined = undefined;
    const [userLocation, setUserLocation] = useState<UserLocation>(userLocationState);
    /**
     * Flag that determines whether the map should focus and follow the user's location.
     */
    const followUserState = false;
    const [followUser, toggleFollowUser] = useState<boolean>(followUserState);
    //const [prevFetchedBounds, setFetchedBounds] = useState<Region>();
    const { landmarks, getLandmarksStatus, refetchLandmarks } = useLandmarks(undefined);
    
    /**
     * Ref that holds the loaded [MapView]{@link https://github.com/react-native-maps/react-native-maps/blob/master/docs/mapview.md} instance.
     */
    const mapRef = React.createRef<MapView>();
 
    useEffect(() => {
        /**
         * Prompts user to give permission to track their location using [expo-location]{@link https://docs.expo.dev/versions/latest/sdk/location/}.
         * If permission is granted, user location will be retrieved and stored in {@linkcode userLocationState}.
         * @memberOf Map
         */
        const requestLocationPermissions = async () => {
            let { status } = await Location.requestForegroundPermissionsAsync();
            if (status !== 'granted') {
                return;
            }
 
            const location = await Location.getCurrentPositionAsync();
            setUserLocation({latitude: location.coords.latitude, longitude: location.coords.longitude})   
            // setFetchedBounds({
            //     latitude: location.coords.latitude, 
            //     longitude: location.coords.longitude, 
            //     latitudeDelta: 0.01, 
            //     longitudeDelta: 0.01
            // });
        };
 
        requestLocationPermissions();
    }, []);
 
    /**
     * Triggered by long pressing on the map. 
     * Sets {@linkcode newLandmarkState} to a skeleton {@link Landmark} object that only contains the pressed coordinates.
     * Triggers {@link openAddLandmark} via useEffect because the asyncronous nature of useState does not set the coordinates fast enough to toggle the modal directly through this method.
     */
     const promptAddLandmark = (coordinate: LatLng) => {
        setNewLandmark({latitude: coordinate.latitude, longitude: coordinate.longitude});
    }
 
    useEffect(() => {
        /**
         * Opens the {@link AddLandmark} modal when the user creates {@link newLandmarkState} by longpressing the map. 
         * Embedded in a [useEffect]{@link https://reactjs.org/docs/hooks-effect.html} hook that listens to {@linkcode newLandmarkState}.
         * @memberOf Map
         */
        function openAddLandmark() {
            if (newLandmark) {
                toggleLmAdd(true)
                toggleLmDetails(false)   
                console.log(newLandmark)
            }
        }
        openAddLandmark();
    }, [newLandmark]);
 
    // useEffect(() => {
    //     if (prevFetchedBounds) {
    //         refetchLandmarks();
    //     }
    // }, [prevFetchedBounds]);
 
    /**
     * Triggered by on a {@link Landmark} displayed on the map. 
     * Sets {@linkcode selectedLandmark} to the pressed {@link Landmark} object's value and toggles the {@link LandmarkDetails} modal.
     */
    const focusLandmark = (landmark: Landmark) => {
        setSelectedLandmark(landmark);
        toggleLmDetails(true)
    }
 
    /**
     * Animates the map to fly over to and focus on the user's location.
     */
    const flyToUser = () => {
        if (userLocation) {
            mapRef.current?.animateToRegion({latitude: userLocation.latitude, longitude: userLocation.longitude, latitudeDelta: 0.01, longitudeDelta: 0.01})
        }
    }
 
    // const checkBounds = (bounds: Region) => {
    //     if (prevFetchedBounds) {
    //         if (
    //             bounds.latitude < prevFetchedBounds.latitude - prevFetchedBounds.latitudeDelta || // check if new lat exceeds old left bounds
    //             bounds.latitude > prevFetchedBounds.latitude + prevFetchedBounds.latitudeDelta || // check if new lat exceeds old right bounds
    //             bounds.longitude < prevFetchedBounds.longitude - prevFetchedBounds.longitudeDelta || // check if new lat exceeds bottom bounds
    //             bounds.longitude > prevFetchedBounds.longitude + prevFetchedBounds.longitudeDelta || 
    //             bounds.latitudeDelta < prevFetchedBounds.latitudeDelta / 2) // check if user zoomed in to atleast half scale of previous
    //         {
    //             console.log('new bounds')
    //             setFetchedBounds(bounds);
    //         }   
    //     }
    //     else if (bounds.latitudeDelta < 5) {
    //         console.log('initialize bounds')
    //         setFetchedBounds(bounds);
    //     }
    // }
 
    return (
        <View>
            <MapView 
                testID="mapView"
                ref={mapRef} 
                style={{width: "100%", height: "100%"}} 
                initialRegion={userLocation ? {latitude: userLocation.latitude, longitude: userLocation.longitude, latitudeDelta: 0.01, longitudeDelta: 0.01} : undefined} 
                onLongPress={e => promptAddLandmark(e.nativeEvent.coordinate)} 
                // onRegionChangeComplete={bounds => checkBounds(bounds)}
                showsUserLocation={true} 
                onUserLocationChange={e => setUserLocation(e.nativeEvent.coordinate)}
                followsUserLocation={followUser}>
            {landmarks?.map((landmark) => {
                return (
                <LandmarkPin key={landmark.id} landmark={landmark} focusLandmark={focusLandmark}/>)})}
            </MapView>
            <TouchableOpacity style={{position: 'absolute', bottom: 30, right: 30, backgroundColor: colors.red, height: 60, width: 60, borderRadius: 30, justifyContent: "center", alignItems: 'center'}} onPress={flyToUser}>
                <FontAwesome name="location-arrow" size={20} color="white"/>
            </TouchableOpacity>
            {/* <TouchableOpacity style={{position: 'absolute', bottom: 120, right: 30, backgroundColor: colors.red, height: 60, width: 60, borderRadius: 30, justifyContent: "center", alignItems: 'center'}}>
                <FontAwesome name="" size={20} color="white"/>
            </TouchableOpacity> */}
            <Modal
                testID="addLMModal"
                avoidKeyboard={true}
                onBackdropPress={() => toggleLmAdd(false)}
                style={{justifyContent: "flex-end", height: '100%', margin: 0}}
                isVisible={lmAddVisible} >
                <KeyboardAvoidingView>
                    <AddLandmark setVisible={toggleLmAdd} landmark={newLandmark} />
                </KeyboardAvoidingView>
            </Modal>
            <Modal 
                avoidKeyboard={true}
                onBackdropPress={() => {
                    if (lmDetailsEditing) {
                        Keyboard.dismiss();
                    } else {
                        toggleLmDetails(false)   
                    }
                }}
                style={{justifyContent: "flex-end", height: '100%', margin: 0}}
                isVisible={lmDetailsVisible}>
                <LandmarkDetails setVisible={toggleLmDetails} setEditing={toggleLmDetailsEditing} editingEnabled={lmDetailsEditing} landmark={selectedLandmark} />
            </Modal>
        </View> )
}