summaryrefslogtreecommitdiffstats
path: root/modules/Main.js
blob: 18457b0ff3d3dcdfb73ccfb2faf060bdab2d565b (plain) (blame)
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
import * as React from 'react';
import { useState, useEffect } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, FlatList, Switch, ActivityIndicator, Image } from 'react-native';
import * as Location from 'expo-location';
import { useFonts } from "expo-font";
import AsyncStorage from "@react-native-async-storage/async-storage";

import Fira from "../assets/Fira.ttf";
import LocIcon from "../assets/LocIcon.png";

const L_KEY = 'locs_key'

export default function Main(props) {
  const [fontLoaded] = useFonts({
    Fira,
  });
  const [isLoading, setIsLoading] = useState(false);
  const [locs, _setLocs] = useState([]);
  const setLocs = async (nls) => {
    await AsyncStorage.setItem(L_KEY, JSON.stringify(nls))
    _setLocs(nls)
  }
  const saveLocs = async () => {
    await AsyncStorage.setItem(L_KEY, JSON.stringify(locs))
  }
  useEffect(() => {
    (async () => {
      const x = JSON.parse(await AsyncStorage.getItem(L_KEY))
      if (x) _setLocs(x);

      const { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== "granted") {
        alert("Permission to access location was denied");
      }
    })();
  }, []);
  // useEffect(() => console.log(locs))
  const getPosition = async () => {
    let loc = { c: false, l: await Location.getCurrentPositionAsync(), }
    setLocs([...locs, loc])
  };
  const [switchState, setSwitchState] = useState(false);
  const pins = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  if (isLoading) {
    return (
      <ActivityIndicator style={{ top: "50%" }} color="#00F" size="large" animating={isLoading} />
    )
  }
  return (
    <View style={styles.cont}>
      <View style={styles.nav}>
        <View style={styles.navEl}>
          <TouchableOpacity onPress={getPosition} style={[styles.navBtn, { flex: 2 }]}><Text style={[styles.navTxtSm, fontLoaded ? { fontFamily: "Fira" } : { backgroundColor: "red" }]}>LOAD{'\n'}AND SAVE{'\n'}POSITION</Text></TouchableOpacity>
          <TouchableOpacity onPress={() => setLocs([])} style={styles.navBtn}><Text style={styles.navTxtSm}>REMOVE{'\n'}ALL DATA</Text></TouchableOpacity>
        </View>
        <View style={styles.map}>
          <TouchableOpacity onPress={async () => { await saveLocs(); props.navigation.navigate("mapV") }} style={styles.navBtnB}><Text style={styles.navTxt}>GO TO THE MAP</Text></TouchableOpacity>
          <Switch
            style={styles.mapSwitch}
            trackColor={{ true: "#AEEDF0", false: "lightgray" }}
            thumbColor={switchState ? "#00BFC5" : "white"}
            value={switchState}
            onValueChange={(a) => {
              setLocs(locs.map(l => { l.c = a; return l }))
              setSwitchState(a)
            }}
          />
        </View>
      </View>
      <FlatList
        data={locs}
        renderItem={({ item }) => {
          console.log("item", item)
          return (
            <View style={{ height: 130, width: "100%", position: "relative" }}>
              <Image source={LocIcon} style={{ width: 90, height: 90, top: 13 }} />
              <View style={{ top: -66, left: 100 }}>
                <Text style={[styles.itemTxt, { color: "#3D54BB" }]}>- Timestamp {item.l.timestamp}</Text>
                <Text style={[styles.itemTxt]}>- Latitude {item.l.coords.latitude}</Text>
                <Text style={[styles.itemTxt]}>- Longitude {item.l.coords.longitude}</Text>
              </View>
              <Switch
                style={{ top: -120 }}
                trackColor={{ true: "#AEEDF0", false: "lightgray" }}
                thumbColor={item.c ? "#00BFC5" : "white"}
                value={item.c}
                onValueChange={(a) => {
                  setLocs(locs.map(l => {
                    if (l.l.timestamp === item.l.timestamp)
                      l.c = a
                    return l
                  }
                  ))
                }}
              />
            </View>
          )
        }}
        keyExtractor={p => {
          console.log('p', p);
          p.l.timestamp
        }}
        style={styles.list}
      />
    </View >
  )
}

const styles = StyleSheet.create({
  cont: {
    flex: 1,
    margin: 30,
  },
  nav: {
    flex: 1,
    flexDirection: "column",
    flexWrap: "wrap",
    // backgroundColor: "red",
  },
  navEl: {
    flex: 1,
    flexDirection: "row",
    // backgroundColor: "blue",
  },
  navTxt: {
    fontSize: 26,
    textAlign: "center",
    fontWeight: "bold",
  },
  navTxtSm: {
    fontSize: 26,
    fontWeight: "bold",
  },
  navBtn: {
    flex: 1,
  },
  map: {
    // backgroundColor: "orange",
    flex: 1,
    flexDirection: "row",
  },
  mapSwitch: {
    flex: 1,
    height: 65,
  },
  navBtnB: {
    flex: 1,
  },
  list: {
    // height: 414,
    flex: 1,
    // margin: 9,
    paddingBottom: 170,
    // height: "100%",
    // marginBottom: -20,
  },
  itemTxt: {
    fontSize: 15,
    fontWeight: "bold",
  },
})