All files / atlas-mobile-ts/src/navigation MapNavigator.tsx

0% Statements 0/64
0% Branches 0/16
0% Functions 0/17
0% Lines 0/60

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                                                                                                                                                                                                                                                                                                                                                                               
import { createStackNavigator, StackNavigationProp } from "@react-navigation/stack"
import React, { useEffect } from "react"
import { AppState, Platform, ScrollView, View, Image } from "react-native"
import OutdoorMap, { AuthTabsMapRouteProp } from "../components/Map/MainMapComponent/OutdoorMap"
import IndoorMap from "../components/Map/MainMapComponent/IndoorMap"
import { AuthTabsNavigationProp, AuthTabsParamList } from "./AuthorizedNavigator"
import { RouteProp, useNavigation, useRoute } from "@react-navigation/native"
import AddLandmarkPanel from "../components/Map/Panels/AddLandmarkPanel"
import LandmarkDetails from "../components/Map/Panels/LandmarkDetailsPanel/LandmarkDetails"
import { FilterPanel } from "../components/Map/Panels/FilterPanel/FilterPanel"
import NearbyLandmarksPanel from "../components/Map/Panels/NearbyLandmarksPanel"
import { useMapState } from "../components/Map/MainMapComponent/useMapState"
import { Landmark, useLandmarks } from "../hooks/useLandmarks"
import { checkVoicePermissions, colors, getMapPermissions, lmTypes } from "../utils/GlobalUtils"
import { PERMISSIONS } from "react-native-permissions"
import { authStore } from "../libs/auth/AuthStore"
import { observer } from "mobx-react"
import { LatLng } from "react-native-maps"
import Spokestack from 'react-native-spokestack';
import { IconButton } from "../components/Buttons"
import { Chip } from "react-native-paper"
import { FontAwesome } from "@expo/vector-icons"
import mapStyles from "../components/Map/MainMapComponent/Map.styles"
import { BottomTabBarOptions } from "@react-navigation/bottom-tabs"
 
const MapStackNavigator = createStackNavigator()
 
export type MapStackParamList = {
    Outdoor: {selectedLandmark: string, selectedLandmarks: string[]},
    Indoor: React.FC,
}
 
export type MapStackNavigationProp = StackNavigationProp<MapStackParamList>
 
interface MapNavigatorProps {
    authNavigation: AuthTabsNavigationProp
    route: AuthTabsMapRouteProp
}
 
const tabBarOptions: BottomTabBarOptions = {
    //keyboardHidesTabBar: true,
    activeTintColor: 'white',
    activeBackgroundColor: '#e35555',
    inactiveTintColor: "lightgray",
    
    style: {backgroundColor: colors.red, height: 60, justifyContent: 'center'},
    labelStyle: {marginBottom: 7},
    iconStyle: {marginBottom: 7}
} 
 
 
const MapNavigator: React.FC<MapNavigatorProps> = ({route}) => {
    const mapState = useMapState()
    const authNavigation = useNavigation() as AuthTabsNavigationProp
    const authRoute = useRoute() as AuthTabsMapRouteProp
 
    // bring in all landmarks from useLandmark hook
    const { landmarks, refetchLandmarks } = useLandmarks({});
 
    /**
     * Clear selected landmark when landmark details panel is closed
     */
     useEffect(() => {
        Iif (!mapState.lmDetailsVisible) {
            mapState.setSelectedLandmarkId('')
        }
    }, [mapState.lmDetailsVisible])
 
    useEffect(() => {
        const refetchLandmarksOnFilterOptionsChange = async () => {
            applyFilters(landmarks)
            console.log('[Map]: Filters changed')
        }
        refetchLandmarksOnFilterOptionsChange()
    }, [mapState.lmFilteredTypes, mapState.onlyOwned, mapState.minLmRating])
 
    const applyFilters = (landmarks: Landmark[]): Landmark[] => {
        Iif (landmarks?.length > 0) {
            Iif (mapState.minLmRating > 0) {
                landmarks = landmarks?.filter(lm => lm && lm.rating >= mapState.minLmRating);
            }
        
            Iif (mapState.lmFilteredTypes?.length > 0) {
                Iif (mapState.lmFilteredTypes.length > 0) {
                    landmarks = landmarks?.filter(lm => mapState.lmFilteredTypes.includes(lm?.landmark_type));   
                }
            }
        
            Iif (mapState.onlyOwned) {
                landmarks = landmarks?.filter(lm => lm.user == authStore.userId);
            }
        }
    
        return landmarks
    }
 
    /**
     * 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) => {
        if (mapState.selectedLandmarkId == landmark.id) {
            mapState.toggleLmDetails(true)
        }
        else {
            mapState.setSelectedLandmarkId(landmark.id);
        }
    }
 
        /**
     * 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 (this issue shows up in many other parts of the app where a modal is toggled by a boolean, and is solved in the same way).
     */
    const promptAddLandmark = (longitude?: number, latitude?: number) => {
        console.log('[Map]: Opening add landmark panel...')
        mapState.setNewLandmark({latitude: latitude, longitude: longitude});
        mapState.toggleLmAdd(true)
        mapState.toggleLmDetails(false)   
    }
 
    return (
        <View style={{flex: 1}}>
            <MapStackNavigator.Navigator screenOptions={{ headerShown: false }} initialRouteName="Outdoor">
                <MapStackNavigator.Screen name="Outdoor" >
                    {({navigation}) => 
                    <OutdoorMap 
                        newLandmark={mapState.newLandmark}
                        setNewLandmark={mapState.setNewLandmark}
                        promptAddLandmark={promptAddLandmark}
                        toggleLmAdd={mapState.toggleLmAdd}
                        toggleLmDetails={mapState.toggleLmDetails}
                        applyFilters={applyFilters}
                        focusLandmark={focusLandmark} 
                        route={authRoute} 
                        mapNavigation={navigation} 
                        authNavigation={authNavigation}
                        landmarks={landmarks}
                        selectedLandmarkId={mapState.selectedLandmarkId}
                        setSelectedLandmarkId={mapState.setSelectedLandmarkId} />}
                </MapStackNavigator.Screen>
                <MapStackNavigator.Screen name="Indoor" component={IndoorMap} />
            </MapStackNavigator.Navigator>
            {/*Filter chips and button*/}
            {!mapState.filterVisible ? 
            <View style={{top: 10, marginLeft: 40, marginRight: 20, position: 'absolute', flexDirection: "row-reverse", justifyContent: 'flex-end'}}>
                <IconButton size={20} color={colors.red} style={[mapStyles.filterButton]} icon="filter" onPress={() => mapState.toggleFilter(true)}/>
                <ScrollView horizontal={true} contentContainerStyle={{alignItems: 'center'}} style={{marginHorizontal: 10, flexDirection: 'row'}}>
                {mapState.onlyOwned ? <Chip avatar={(<FontAwesome name="user" size={20} color='gray' style={{textAlign: 'center', textAlignVertical: 'center'}} />)} style={{borderWidth: 1, borderColor: 'lightgray', marginRight: 5, marginLeft: 10}} onClose={() => mapState.toggleOnlyOwned(false)}>My landmarks</Chip> : null}
                {mapState.lmFilteredTypes.map((type, i) => {
                    return (
                        <Chip key={i} avatar={(<Image style={{height: 22, width: 17}} source={lmTypes[type].image} />)} style={{borderWidth: 1, borderColor: 'lightgray', marginHorizontal: 5, justifyContent: 'center'}} onClose={() => mapState.setLmTypeFilter(mapState.lmFilteredTypes.filter(t => t !== type))}>{lmTypes[type].label}</Chip>
                    )
                })}
                {mapState.minLmRating > 0 ? <Chip avatar={(<FontAwesome name="star" size={20} color='gray' style={{textAlign: 'center', textAlignVertical: 'center'}} />)} style={{borderWidth: 1, borderColor: 'lightgray', marginLeft: 5, marginRight: 10}} onClose={() => mapState.setMinLmRating(0)}>Minimum rating: {mapState.minLmRating}</Chip> : null}
                </ScrollView>
            </View> : null}
            <AddLandmarkPanel 
                setNewLandmark={mapState.setNewLandmark} 
                setVisible={mapState.toggleLmAdd} 
                newLandmark={mapState.newLandmark}
                visible={mapState.lmAddVisible} />
            <LandmarkDetails 
                visible={mapState.lmDetailsVisible}
                toggleLmDetails={mapState.toggleLmDetails}
                setLandmark={mapState.setSelectedLandmarkId} 
                toggleDetailsPanel={mapState.toggleLmDetails} 
                setEditing={mapState.toggleLmDetailsEditing} 
                editingEnabled={mapState.lmDetailsEditing} 
                landmarkId={mapState.selectedLandmarkId} />
            <FilterPanel 
                visible={mapState.filterVisible}
                toggleOnlyOwned={mapState.toggleOnlyOwned} 
                onlyOwned={mapState.onlyOwned} toggleFilter={mapState.toggleFilter} 
                setMinLmRating={mapState.setMinLmRating} 
                setLmFilteredTypes={mapState.setLmTypeFilter} 
                lmFilteredTypes={mapState.lmFilteredTypes} 
                minLmRating={mapState.minLmRating} />
        </View>
    )
}
 
export default observer(MapNavigator)