-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
186 lines (168 loc) · 5.94 KB
/
App.js
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
import { StatusBar } from "expo-status-bar";
import { AppRegistry } from "react-native";
import { PaperProvider } from "react-native-paper";
import { name as appName } from "./app.json";
import Dashboard from "./Dashboard";
import { SafeAreaProvider } from "react-native-safe-area-context";
import * as Location from "expo-location";
import * as TaskManager from "expo-task-manager";
import React, { useState, useEffect } from "react";
import { GlobalContext } from "./GlobalContext";
import Default from "./components/Default";
import Map from "./Map";
import TestLogin from "./components/TestLogin";
import {
isInEastArchitecture,
isInInstructionalCenter,
buildingList,
} from "./BuildingFunctions";
import { collection, addDoc, setDoc, doc, getDoc } from "firebase/firestore";
import { db } from "./firebaseConfig";
// medium article
const LOCATION_TRACKING = "location-tracking";
/**
* Taken from the following medium article:
* https://arnav25.medium.com/background-location-tracking-in-react-native-d03bb7652602
*/
export default function App() {
// test data to test insights page
const [insights, setInsights] = useState([
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 3 hours at Brittan Dining Hall",
"You spent 4 hours at the Georgia Tech Library",
"You spent 2 hours at Clough",
"You spent 5 hours at Klaus",
]);
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [user, setUser] = useState();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
let globalObj = {
profile: [
[user, setUser],
[email, setEmail],
[password, setPassword],
[insights, setInsights],
],
};
TaskManager.defineTask(LOCATION_TRACKING, async ({ data, error }) => {
if (error) {
console.log("LOCATION_TRACKING task ERROR:", error);
return;
}
if (data) {
const { locations } = data;
let lat = locations[0].coords.latitude;
let long = locations[0].coords.longitude;
//console.log(`${new Date(Date.now()).toLocaleString()}: ${lat},${long}`);
if (user) {
try {
const docRef = await addDoc(collection(db, user.email), {
latitude: lat,
longitude: long,
timeStamp: new Date(Date.now()),
});
console.log("Document written with ID: ", docRef.id);
const userDocRef = doc(db, "BUILDINGS", user.email);
const userDocSnapshot = await getDoc(userDocRef);
const userBuildingData = userDocSnapshot.data();
let inAnyBuilding = false;
for (const building in buildingList) {
if (building.isInBuilding(lat, long)) {
const previousTime =
userBuildingData[building.firestoreAttribute] || 0;
const newDoc = {
...userBuildingData,
email: user.email,
[building.firestoreAttribute]: previousTime + 5,
};
const docRef2 = await setDoc(userDocRef, newDoc);
console.log(
`Updated location for ${building.name}: `,
previousTime + 5
);
inAnyBuilding = true;
break; // Exit the loop after finding the building
}
}
if (!inAnyBuilding) {
const previousTime = userBuildingData.timeOutside || 0;
const newDoc = {
...userBuildingData,
email: user.email,
timeOutside: previousTime + 5,
};
const docRef2 = await setDoc(userDocRef, newDoc);
console.log("Updated location for Outside: ", previousTime);
}
} catch (e) {
console.error("Error adding document: ", e);
}
} else {
console.log("User not connected...");
}
}
});
/**
* Taken from the following medium article:
* https://arnav25.medium.com/background-location-tracking-in-react-native-d03bb7652602
*/
const startLocationTracking = async () => {
await Location.startLocationUpdatesAsync(LOCATION_TRACKING, {
accuracy: Location.Accuracy.Highest,
timeInterval: 5000,
distanceInterval: 0,
});
const hasStarted = await Location.hasStartedLocationUpdatesAsync(
LOCATION_TRACKING
);
console.log("Has this started working:", hasStarted);
};
useEffect(() => {
(async () => {
var { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
setErrorMsg("Permission to access foreground location was denied");
return;
}
var { status } = await Location.requestBackgroundPermissionsAsync();
if (status !== "granted") {
setErrorMsg("Permission to access background location was denied");
return;
}
await startLocationTracking();
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
let text = "Waiting..";
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
console.log(text);
return (
<GlobalContext.Provider value={globalObj}>
<SafeAreaProvider>
<PaperProvider>
<Dashboard />
</PaperProvider>
</SafeAreaProvider>
</GlobalContext.Provider>
);
}
AppRegistry.registerComponent(appName, () => Main);