All files / atlas-mobile-ts/src/components/Map/MainMapComponent IndoorMap.tsx

0% Statements 0/88
0% Branches 0/21
0% Functions 0/25
0% Lines 0/86

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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import React, { useState, useEffect, Children } from 'react';
import { View, Text, StatusBar, StyleSheet, Dimensions, Button, ActivityIndicator, Alert, Modal } from 'react-native';
import { Svg, Defs, Rect, Mask, Circle, Marker, Path, Polyline, Image } from 'react-native-svg';
import { RadioButton } from 'react-native-paper';
import { Picker } from '@react-native-picker/picker';
import ReactNativeZoomableView from '@openspacelabs/react-native-zoomable-view/src/ReactNativeZoomableView';
import Spinner from 'react-native-spinkit'
import { colors } from "../../../utils/GlobalUtils";
import { MapStackNavigationProp } from "../../../navigation/MapNavigator"
import CustomModal from './modal';
 
import BasementC from './images/BasementC.svg';
import FirstFloorC from './images/FirstFloorC.svg'
import SecondFloorC from './images/SecondFloorC.svg'
import ThirdFloorC from './images/ThirdFloorC.svg'
import FourthFloorC from './images/FourthFloorC.svg'
import FifthFloorC from './images/FifthFloorC.svg'
import { Landmark } from '../../../hooks/useLandmarks';
 
 
interface IndoorMapProps {
  navigation: MapStackNavigationProp
  landmarks: Landmark[]
  promptAddLandmark: (longitude?: number, latitude?: number) => void
  focusLandmark: (landmark: Landmark) => void
  applyFilter: (landmarks: Landmark[]) => Landmark[]
}
 
interface IndoorMarker {
  key: number,
  coordx: number,
  coordy: number,
  description: string,
  landmark: string
}
 
const IndoorMap: React.FC<IndoorMapProps> = ({navigation, landmarks, promptAddLandmark, focusLandmark, applyFilter}) => {
  const [floor, setFloor] = useState(0);
  const [showME, setShowME] = useState(false);
  const [showDots, setShowDots] = useState(false);
  const [showAddedDot, setShowAddedDot] = useState(false)
  const [showModal2, setShowModal] = useState(false)
  const [checked, setChecked] = React.useState('information');
  const [coords, setCoords] = useState([0, 0])
  const [SVGdim, setSVGdim] = useState([1, 1])
  const [firstTime, setfirstTime] = useState(true)
 
 
  // Main issue is that I need to first load the page to retrieve dimensions of SVG, THEN I can actually use the 
  // real proper coordinate values. Before, I used raw numbers, which is why it could load immediately. However here,
  // my state starts with ratio values (0.5 , 0.25, etc), retrieves SVG coordinates, then finally gets the real positioning.
 
 
  let indoorCircles: IndoorMarker[][] = [ //first to fifth floor, last element is basement
    [{ key: 1, coordx: 0.5 * SVGdim[0], coordy: 0.5 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 2, coordx: 0.25 * SVGdim[0], coordy: 0.25 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
 
    [{ key: 3, coordx: 0.35 * SVGdim[0], coordy: 0.35 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 4, coordx: 0.15 * SVGdim[0], coordy: 0.15 * SVGdim[1], description: "nothing yet2", landmark: "power" }],
 
    [{ key: 5, coordx: 0.5 * SVGdim[0], coordy: 0.5 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 6, coordx: 0.25 * SVGdim[0], coordy: 0.25 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
 
    [{ key: 7, coordx: 0.35 * SVGdim[0], coordy: 0.35 * SVGdim[1], description: "nothing yet1", landmark: "power" },
    { key: 8, coordx: 0.15 * SVGdim[0], coordy: 0.15 * SVGdim[1], description: "nothing yet2", landmark: "power" }],
 
    [{ key: 9, coordx: 0.5 * SVGdim[0], coordy: 0.5 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 10, coordx: 0.25 * SVGdim[0], coordy: 0.25 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
 
    [{ key: 11, coordx: 0.35 * SVGdim[0], coordy: 0.35 * SVGdim[1], description: "nothing yet1", landmark: "power" },
    { key: 12, coordx: 0.15 * SVGdim[0], coordy: 0.15 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
  ]
 
  const [indoorMarkers, setIndoorMarkers] = useState([ //first to fifth floor, last element is basement
    [{ key: 1, coordx: 0.5 * SVGdim[0], coordy: 0.5 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 2, coordx: 0.25 * SVGdim[0], coordy: 0.25 * SVGdim[1], description: "nothing yet2", landmark: "power" }],
 
    [{ key: 3, coordx: 0.35 * SVGdim[0], coordy: 0.35 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 4, coordx: 0.15 * SVGdim[0], coordy: 0.15 * SVGdim[1], description: "nothing yet2", landmark: "power" }],
 
    [{ key: 5, coordx: 0.5 * SVGdim[0], coordy: 0.5 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 6, coordx: 0.25 * SVGdim[0], coordy: 0.25 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
 
    [{ key: 7, coordx: 0.35 * SVGdim[0], coordy: 0.35 * SVGdim[1], description: "nothing yet1", landmark: "power" },
    { key: 8, coordx: 0.15 * SVGdim[0], coordy: 0.15 * SVGdim[1], description: "nothing yet2", landmark: "power" }],
 
    [{ key: 9, coordx: 0.5 * SVGdim[0], coordy: 0.5 * SVGdim[1], description: "nothing yet1", landmark: "stairs" },
    { key: 10, coordx: 0.25 * SVGdim[0], coordy: 0.25 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
 
    [{ key: 11, coordx: 0.35 * SVGdim[0], coordy: 0.35 * SVGdim[1], description: "nothing yet1", landmark: "power" },
    { key: 12, coordx: 0.15 * SVGdim[0], coordy: 0.15 * SVGdim[1], description: "nothing yet2", landmark: "stairs" }],
  ])
 
  const loadCircles = (indoorMarkers[floor]).map(item => {
    while (SVGdim[0] != 1 && SVGdim[1] != 1) {
      console.log("important numbers are " + SVGdim[0] + " AND " + SVGdim[1])
      if (item.landmark == "stairs") {
        return (
          <Image onPress={() => handleDelete(item.coordx, item.coordy)} key={item.key} x={item.coordx} y={item.coordy} width={0.05 * Dimensions.get("window").width} height={0.05 * Dimensions.get("window").width} href={require('./landmark_images/stairs.png')} />
          // <Circle onPress={() => handleDelete(item.coordx, item.coordy)} key={item.key} cx={item.coordx} cy={item.coordy} r="4" fill="black" />)
        )
      }
      else if (item.landmark == "power") {
        return (
          <Image onPress={() => handleDelete(item.coordx, item.coordy)} key={item.key} x={item.coordx} y={item.coordy} width={0.05 * Dimensions.get("window").width} height={0.05 * Dimensions.get("window").width} href={require('./landmark_images/power.png')} />
        )
      }
      else Iif (item.landmark == "information") {
        return (
          <Image onPress={() => handleDelete(item.coordx, item.coordy)} key={item.key} x={item.coordx} y={item.coordy} width={0.05 * Dimensions.get("window").width} height={0.05 * Dimensions.get("window").width} href={require('./landmark_images/information.png')} />
        )
      }
    }
  }
  )
 
  function addCircle(evt: any) {
    Alert.alert("Are you sure you want to add a landmark here?", undefined,
      [{ text: "Cancel", onPress: () => console.log("Cancelled") }
        ,
      {
        text: "Confirm", onPress: () => {
          setShowModal(true)
          setCoords([evt.nativeEvent.locationX, evt.nativeEvent.locationY])
        }
      }])
  }
 
  function addCircleConfirmed() {
    let newKey: number
    if (indoorMarkers[floor].length == 0) {
      newKey = floor * 100
    }
    else {
      newKey = indoorMarkers[floor][indoorMarkers[floor].length - 1].key + 1
    }
    const newDot = { key: newKey, coordx: coords[0], coordy: coords[1], description: "filler", landmark: checked }
    console.log(checked)
    console.log(newDot)
    indoorMarkers[floor].push(newDot)
    setIndoorMarkers(indoorMarkers)
    setShowAddedDot(true)
    Alert.alert("Added Circle: coordinates are " + coords[0].toFixed(3) + " and " + coords[1].toFixed(3))
  }
 
  function handleDelete(coordx: number, coordy: number) {
    Alert.alert("Are you sure you want to delete this landmark?", undefined,
      [{ text: "Cancel", onPress: () => console.log("Cancelled") }
        ,
      { text: "Confirm", onPress: () => handleDeleteConfirmed() }])
 
 
    function handleDeleteConfirmed() {
      const result = indoorMarkers[floor].filter(indoorMarker => indoorMarker['coordx'] != coordx && indoorMarker['coordy'] != coordy)
      indoorMarkers[floor] = result
      setIndoorMarkers(indoorMarkers);
      setShowDots(true)
      Alert.alert("Delete: Coordinates of deleted circle were " + coordx.toFixed(3) + " and " + coordy.toFixed(3))
      console.log(result)
      console.log(indoorMarkers[floor])
    }
  }
 
  useEffect(() => {
    // Alert.alert("useEffect has been triggered")
    setShowAddedDot(false)
    setShowDots(false)
    setTimeout(() => setShowME(true), 100);
  })
 
 
  const compArray = [
    <FirstFloorC style={{ borderColor: "red", borderWidth: 0 }} height={"100%"} width={"100%"} />,
    <SecondFloorC style={{ borderColor: "red", borderWidth: 0 }} height={"100%"} width={"100%"} />,
    <ThirdFloorC viewBox='250 131 310 310' style={{ borderColor: "red", borderWidth: 0 }} height={"100%"} width={"100%"} />,
    <FourthFloorC viewBox='250 140 300 300' style={{ borderColor:"blue" , borderWidth:0, height:"100%" , width:"100%",}}/>,
    <FifthFloorC viewBox='257 155 270 290' style={{ borderColor: "red", borderWidth: 0 }} height={"100%"} width={"100%"} />,
    <BasementC style={{ borderColor: "red", borderWidth: 0 }} height={"100%"} width={"100%"} />
  ]
 
 
  // TODO: wire up promptaddlandmark, applyfilters, and focuslandmark methods passed from MapNavigator
  return (
    <View style={{ height: '100%', width: '100%', padding: 5 }}>
      {console.log("THE STATE IS NOW " + floor)}
      <StatusBar backgroundColor="#121212" />
      <CustomModal/>
      <Text style={{ fontSize: 16, marginBottom: 5 }}>Please select a floor you would like to go to.</Text>
      <Picker
        style={{ backgroundColor: '#d9d9d9' }}
        selectedValue={floor} // the text of what gets displayed on the dropdown header
        onValueChange={(itemValue, itemIndex: number) => {
          setFloor(itemIndex)
          setShowME(false)
        }}>
        {/* The value in Picker.Item refers to selectedValue in Picker, which refers to the state "floor" */}
        <Picker.Item label="First Floor" value={0} />
        <Picker.Item label="Second Floor" value={1} />
        <Picker.Item label="Third Floor" value={2} />
        <Picker.Item label="Fourth Floor" value={3} />
        <Picker.Item label="Fifth Floor" value={4} />
        <Picker.Item label="Basement" value={5} />
      </Picker>
 
      <View style={{ flex: 1, alignItems: "center", height: '100%', width: '100%' }}>
        <View style={styles.container}>
          {showME === false ?
            <View style={{ display: 'flex', flexDirection: 'row', justifyContent: "center", }}>
              <Spinner size={200} color={colors.red} type="9CubeGrid" />
            </View> :
 
            <ReactNativeZoomableView
              zoomStep={2.8}
              bindToBorders={true}
              // initialZoom={2.2}
              maxZoom={2.8}
              minZoom={1}
              initialOffsetY={5}
              onLongPress={(event) => { addCircle(event) }}
            >
              <Modal transparent={true} visible={showModal2}>
                <View style={{ backgroundColor: "#000000aa", flex: 1 }}>
                  <View style={{ backgroundColor: "#ffffff", margin: 50, padding: 20, borderRadius: 10, }}>
                    <Text style={{ fontSize: 18, marginBottom: 10 }}>Type of Landmark to Add:</Text>
 
                    <View style={{ flexDirection: "row", alignItems: "center" }}>
                      <RadioButton
                        value="information"
                        status={checked === 'information' ? 'checked' : 'unchecked'}
                        onPress={() => setChecked('information')}
                      />
                      <Text style={{ fontSize: 16 }}>Information</Text>
                    </View>
 
                    <View style={{ flexDirection: "row", alignItems: "center" }}>
                      <RadioButton
                        value="power"
                        status={checked === 'power' ? 'checked' : 'unchecked'}
                        onPress={() => setChecked('power')}
                      />
                      <Text style={{ fontSize: 16 }}>Power</Text>
                    </View>
 
                    <View style={{ flexDirection: "row", alignItems: "center", marginBottom: 11 }}>
                      <RadioButton
                        value="stairs"
                        status={checked === 'stairs' ? 'checked' : 'unchecked'}
                        onPress={() => setChecked('stairs')}
                      />
                      <Text style={{ fontSize: 16 }}>Stairs</Text>
                    </View>
 
                    <Button color={"red"} title='OK' onPress={() => {
                      addCircleConfirmed()
                      setShowModal(false)
                    }}></Button>
                  </View>
                </View>
              </Modal>
 
              <Svg onLayout={event => {
                // console.log("OFFICIAL: " + event.nativeEvent.layout.width + " , " + event.nativeEvent.layout.height)
                console.log(SVGdim[0] + " AND " + SVGdim[1])
                setSVGdim([event.nativeEvent.layout.width, event.nativeEvent.layout.height])
 
                // TODO: change this mapping to apply to Landmark type
                let ans = indoorCircles[floor].map(item => {
                  return { ...item, coordx: item.coordx * event.nativeEvent.layout.width, coordy: item.coordy * event.nativeEvent.layout.height }
                })
                indoorCircles[floor] = ans
                // setIndoorMarkers(indoorCircles)
              }}>
 
                {firstTime==true? undefined : loadCircles}
 
                {compArray[floor]}
              </Svg>
 
            </ReactNativeZoomableView>
 
          }
 
        </View>
      </View>
      <Button title="load coordinates" onPress={() => {
        setfirstTime(false) 
        setIndoorMarkers(indoorCircles) }} />
      {/* <Button title="Go back to map" onPress={() => props.navigation.goBack()} /> */}
 
    </View>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    // backgroundColor: "#fff",
    justifyContent: "center",
    borderColor: "black",
    borderWidth: 2,
    marginVertical: 7,
    aspectRatio: 9/10,  // (caters to portrait mode) (width is 66% the value of height dimension)
    // flex: 1,
    // // backgroundColor: "#fff",
    // justifyContent: "center",
    // borderColor: "black",
    // borderWidth: 2,
    // width: '100%',
    // height: '85%',
    // marginVertical: 15,
  },
  image: {
    alignItems: 'center',
    justifyContent: 'center',
  },
  png: {
    borderColor: "red",
    borderWidth: 3
  }
});
 
 
export default IndoorMap;