-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
264 lines (235 loc) · 7.68 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
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
// Import React core components
import React from 'react';
import {
View,
Text,
StyleSheet,
} from 'react-native';
// Import Contexts
import { AuthContext } from './contexts/AuthContext.js';
import {StateContext} from './contexts/StateContext.js';
import {UserContext} from './contexts/UserContext.js';
// Import for navigation
import { NavigationContainer } from '@react-navigation/native';
import { RootStackScreen } from './component/Navigation';
// Import screens
import Splash from './screens/Splash.js';
// Import for Firebase Auth
import { useState, useEffect, useMemo } from 'react';
import auth from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-community/google-signin';
// Import for Firestore
import firestore from '@react-native-firebase/firestore';
// Import for geolocation
import Radar from 'react-native-radar';
// Import for push notification
import {LocalNotification} from './android/app/src/services/LocalPushController.js';
let timeID = null;
let flag = false;
let rzTimeID = null;
let rzTime = 0;
const App = () => {
const [initializingUser, setInitializingUser] = useState(true);
const [user, setUser] = useState(null);
const [danger, setDanger] = useState(false);
const [warning, setWarning] = useState(false);
const [safe, setSafe] = useState(true);
const [isNewUser, setIsNewUser] = useState(false);
const authContext = useMemo(() => {
return {
signInWithGmail: async () => {
const { idToken, accessToken } = await GoogleSignin.signIn();
const googleCredential = auth.GoogleAuthProvider.credential(idToken, accessToken);
return auth().signInWithCredential(googleCredential).then((userInfo) =>{
console.log('User signed in with Gmail')
if (userInfo.additionalUserInfo.isNewUser) {
console.log("User is new!")
setIsNewUser(true);
flag = false;
}
}).catch;
},
signOutAcc: () => {
clearInterval(timeID);
console.log('Stopped warning')
Radar.stopTracking();
console.log('Stopped Tracking')
setUser(null);
auth()
.signOut()
.then(() => console.log('User signed out!'));
},
}
})
function onAuthStateChanged(user){
setUser(user);
if (user) {
Radar.startTrackingResponsive();
console.log('Started Tracking');
}
if (initializingUser) {setInitializingUser(false);}
}
useEffect(() => {
GoogleSignin.configure({
webClientId: '380907738035-l391j339sa11ka5d20jujjc3727k1tsc.apps.googleusercontent.com',
});
const subscriber = auth().onAuthStateChanged(onAuthStateChanged)
return subscriber;
}, [onAuthStateChanged]);
useEffect(() => {
Radar.getPermissionsStatus().then((status) => {
if (status !== 'GRANTED_BACKGROUND') {
Radar.requestPermissions(true);
}
});
}, [])
let clientLocation = null;
Radar.on('clientLocation', (result) => {
clientLocation = {
latitude: result.location.latitude,
longitude: result.location.longitude,
}
});
async function getLatLongRad(externalId) {
let LatLongRad;
await firestore()
.collection('regions')
.doc(externalId)
.get()
.then((documentSnapshot) => {
LatLongRad = documentSnapshot.data();
})
return LatLongRad;
}
function calculateDistance(clLat, clLong, rzLat, rzLong){
console.log()
console.log('-------------------------------')
console.log('CL: ', clLat, clLong);
console.log('RZ', rzLat, rzLong);
function toRadians(degree) {
let one_deg = (Math.PI) / 180;
return (one_deg * degree);
}
clLat = toRadians(clLat);
clLong = toRadians(clLong);
rzLat = toRadians(rzLat);
rzLong = toRadians(rzLong);
let dLong = rzLong - clLong;
let dLat = rzLat - clLat;
let ans = Math.pow( Math.sin(dLat / 2), 2) + Math.cos(clLat) * Math.cos(rzLat) * Math.pow( Math.sin(dLong / 2), 2);
ans = 2 * Math.asin(Math.sqrt(ans));
let R = 6371;
ans = ans * R;
ans = ans * 1000;
return ans;
}
useEffect(() => {
if (user) {
if (danger) {
console.log('Timer started');
rzTimeID = setInterval(() => {
rzTime += 1;
}, 1000)
LocalNotification('You are in a red zone, be careful!', 'Corona-Card Alert', 'Danger: You Are In A Red Zone', 'YOU ARE IN A RED ZONE')
} else {
console.log('RZ TIME:', rzTime);
clearInterval(rzTimeID);
firestore()
.collection('Users')
.doc(user.uid)
.get()
.then(documentSnapshot => documentSnapshot.get('rzTime'))
.then(currentRzTime => {
let updatedRzTime = rzTime + currentRzTime;
firestore()
.collection('Users')
.doc(user.uid)
.update({
rzTime: updatedRzTime,
})
.then(() => {
firestore()
.collection('Users')
.doc(user.uid)
.get()
.then(documentSnapshot => documentSnapshot.get('rzTime'))
.then(currentRzTime => {
console.log('Updated Rz Time:', currentRzTime)
})
})
.then(() => {
rzTime = 0;
})
})
}
}
}, [danger])
useEffect(() => {
if (warning) {
LocalNotification('You are near a red zone, be careful!', 'Corona-Card Alert', 'Warning: You Are Near A Red Zone', 'YOU ARE NEAR A RED ZONE')
}
}, [warning])
if (initializingUser) {return <Splash text="Loading User"/>;}
if (!flag) {
timeID = setInterval( () => {
Radar.searchGeofences({
radius: 500,
tags: ['RedZone'],
limit: 10,
}).then((result) => {
if (Object.keys(result.geofences).length > 0) {
result.geofences.forEach(async (region) => {
await getLatLongRad(region.externalId).then((result) => {
let distance = calculateDistance(clientLocation.latitude, clientLocation.longitude, result.lat, result.long);
console.log('Distance:', distance);
if (distance < result.radius) {
console.log('In the Red zone: DANGER')
console.log('-------------------------------')
console.log()
setDanger(true);
setSafe(false);
setWarning(false);
} else if (distance > result.radius){
console.log('Near red zone: Warning')
console.log('-------------------------------')
console.log()
setWarning(true);
setSafe(false);
setDanger(false);
}
});
})
} else {
console.log('Not near red zone: Safe')
console.log('-------------------------------')
console.log()
setSafe(true);
setWarning(false);
setDanger(false);
}
}).catch((err) => {
console.log('ERROR SEACRH GEOFENCE: ', err);
});
}, 5000);
flag = true;
}
return (
<UserContext.Provider value={{user: user, isNewUser: isNewUser}}>
<AuthContext.Provider value={authContext}>
<StateContext.Provider value={{warning: warning,safe: safe, danger: danger}}>
<NavigationContainer>
<RootStackScreen userToken={user} isNewUser={isNewUser}/>
</NavigationContainer>
</StateContext.Provider>
</AuthContext.Provider>
</UserContext.Provider>
)
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});