-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
401 lines (363 loc) · 13 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import React, { useEffect, useRef, useState } from "react";
import {
StyleSheet,
View,
Platform,
StatusBar,
RefreshControl,
ScrollView,
AppState,
} from "react-native";
import { WebView } from "react-native-webview";
import Constants from "expo-constants";
import * as BackgroundFetch from "expo-background-fetch";
import * as TaskManager from "expo-task-manager";
import useGoBackHandler from "../../hooks/useGoBackHandler";
import * as Device from "expo-device";
import useRefresh from "../../hooks/useRefresh";
import useStorage from "../../hooks/useStorage";
import axios from "axios";
import BackgroundGeolocation from "react-native-background-geolocation";
// import useSendData from "../../hooks/useSendData";
import locationInstance from "../../hooks/useLocations";
import settingsInstance, { useSettings } from "../../hooks/useSettings";
import moment from "moment";
import AsyncStorage from "@react-native-async-storage/async-storage";
const pwaUri = Constants.expoConfig.extra.EXPO_PUBLIC_PWA_URI;
const appUrl = Constants.expoConfig.extra.EXPO_PUBLIC_API_URL;
const appVariant = Constants.expoConfig.extra.APP_VARIANT;
let WEBVIEW = null;
const sendMessageToWebView = async (message) => {
// if (message.type === "locationUpdates") {
// let user = await (("user");
// message = { ...message, user: user.id };
// }
let messageString = JSON.stringify(message);
const injectedJavaScript = `(function() {
window.postMessage(${messageString});
})();`;
WEBVIEW?.injectJavaScript(injectedJavaScript);
};
const BACKGROUND_FETCH_TASK = "background-fetch";
// const RESTART_BACKGROUND_FETCH_TASK = "restart-background-fetch";
const NUMBER_LOCATIONS_TO_SEND = 100;
async function sendLocations() {
let user = await AsyncStorage.getItem("user");
user = JSON.parse(user);
let providerState = await BackgroundGeolocation.getProviderState();
const locations = locationInstance.getState().getLocations();
const numOfIterations = locations.length / NUMBER_LOCATIONS_TO_SEND;
const numOfIterationsWithoutDec = Math.floor(numOfIterations);
for (let i = 0; i < numOfIterationsWithoutDec + 1; i++) {
let locationsToSend = [];
if (i === 0) {
locationsToSend = Array.from(
locations.slice(i * NUMBER_LOCATIONS_TO_SEND, NUMBER_LOCATIONS_TO_SEND)
);
} else {
locationsToSend = Array.from(
locations.slice(
i * NUMBER_LOCATIONS_TO_SEND,
(i + 1) * NUMBER_LOCATIONS_TO_SEND
)
);
}
const a = await axios.post(
`${appUrl}location/track`,
{
type: "locationUpdates",
userId: user?.id,
timezone: user?.timezone,
foreground_permission:
providerState.status === 4 || providerState.status === 3,
background_permission: providerState.status === 3,
device_brand: Device.brand,
device_model: Device.modelName,
locations: locationsToSend,
},
{
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
}
);
console.log(a.data);
}
}
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
try {
console.log("Envio task 1");
await sendLocations(); //Logic to send data to API
locationInstance.getState().removeAllLocations();
await registerRestarBackgroundFetchAsync();
return BackgroundFetch.BackgroundFetchResult.NewData;
} catch (error) {
console.error("Error: ", error);
console.log(JSON.stringify(error), "STRINGI");
return BackgroundFetch.BackgroundFetchResult.Failed;
}
});
TaskManager.defineTask(RESTART_BACKGROUND_FETCH_TASK, async () => {
try {
console.log("Envio task 2");
await sendLocations();
return BackgroundFetch.BackgroundFetchResult.NewData;
} catch (error) {
console.error("Error: ", error);
return BackgroundFetch.BackgroundFetchResult.Failed;
}
});
async function registerBackgroundFetchAsync(hour, minute) {
const targetTime = moment().set({ hour, minute, second: 0 });
const now = moment();
let nextExecution;
if (now.isBefore(targetTime)) {
nextExecution = targetTime.diff(now, "seconds");
} else {
const nextOccurrence = targetTime.clone().add(1, "day").startOf("hour");
nextExecution = nextOccurrence.diff(now, "seconds");
}
return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: nextExecution,
stopOnTerminate: false,
startOnBoot: true,
});
}
async function registerRestarBackgroundFetchAsync() {
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
const TwentyFourHoursInSeconds = 86400;
return BackgroundFetch.registerTaskAsync(RESTART_BACKGROUND_FETCH_TASK, {
minimumInterval: TwentyFourHoursInSeconds,
stopOnTerminate: false,
startOnBoot: true,
})
.then((a) => console.log("Work: ", a))
.catch((e) => console.log("Error: ", e));
}
const MainWebViewWrapper = () => {
const statusBarHeight = Constants.statusBarHeight;
// const WEBVIEW = useRef();
const [webViewKey, setWebViewKey] = useState(0);
const { distanceFilter, hasSettings, hour, minutes } = useSettings();
const { addLocation } = locationInstance.getState();
const { refreshing, refresherEnabled, onRefresh, handleScroll } =
useRefresh(WEBVIEW);
const [locationForegroundPermission, setLocationForegroundPermission] =
useState(false);
const [locationBackgroundPermission, setLocationBackgroundPermission] =
useState(false);
const { storeData, getData } = useStorage("user");
const [error, setError] = useState(null);
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
const [isMounted, setMounted] = useState(false);
async function onMessage(event) {
// Handle messages received from the PWA
const message = JSON.parse(event.nativeEvent.data);
if (message?.type === "user") {
await storeData("user", message.user);
}
}
useGoBackHandler(() => {
WEBVIEW?.goBack();
return true;
}, []);
useEffect(() => {
/// 1. Subscribe to events.
const onLocation = BackgroundGeolocation.onLocation(
({ coords, timestamp }) => {
console.log("[onLocation]", coords.latitude, coords.longitude);
addLocation({
coords: {
...coords,
altitudeAccuracy: coords.altitude_accuracy,
},
timestamp,
});
}
);
const onMotionChange = BackgroundGeolocation.onMotionChange((event) => {
// console.log("[onMotionChange]", event);
});
const onActivityChange = BackgroundGeolocation.onActivityChange((event) => {
// console.log("[onActivityChange]", event);
});
const onProviderChange = BackgroundGeolocation.onProviderChange((event) => {
// console.log("[onProviderChange]", event);
if (
event.accuracyAuthorization ==
BackgroundGeolocation.ACCURACY_AUTHORIZATION_REDUCED
) {
// Supply "Purpose" key from Info.plist as 1st argument.
BackgroundGeolocation.requestTemporaryFullAccuracy("Delivery")
.then((accuracyAuthorization) => {
if (
accuracyAuthorization ==
BackgroundGeolocation.ACCURACY_AUTHORIZATION_FULL
) {
console.log(
"[requestTemporaryFullAccuracy] GRANTED: ",
accuracyAuthorization
);
} else {
console.log(
"[requestTemporaryFullAccuracy] DENIED: ",
accuracyAuthorization
);
}
})
.catch((error) => {
console.warn(
"[requestTemporaryFullAccuracy] FAILED TO SHOW DIALOG: ",
error
);
});
}
});
const onHttp = BackgroundGeolocation.onHttp((response) => {
console.log("[onHttp] ", response);
});
const initBackgroundGeolocation = async () => {
registerBackgroundFetchAsync(hour, minutes);
// registerRestarBackgroundFetchAsync();
let user = await getData("user");
/// 2. ready the plugin.
BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: distanceFilter,
// Activity Recognition
stopTimeout: 1,
// Application config
debug: appVariant === "development" ? true : false, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
startOnBoot: true, // <-- Auto start tracking when device is powered-up.
// HTTP / SQLite config
url: appUrl + "location/track",
batchSync: true, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
// maxBatchSize: 50,
// autoSync: false, // <-- [Default: true] Set true to sync each location to server as it arrives.
autoSyncThreshold: 5,
// <-- Optional HTTP headers
headers: {
Accept: "application/json",
},
httpRootProperty: "locations",
locationTemplate: [
"{",
'"timestamp": "<%= timestamp %>",',
'"coords": {"altitude": <%= altitude %>, "heading": <%= heading %>, "altitudeAccuracy": <%= altitude_accuracy %>, "latitude": <%= latitude %>, "speed": <%= speed %>, "longitude": <%= longitude %>, "accuracy": <%= accuracy %>}',
"}",
].join(""),
autoSync: !hasSettings,
// Android only
locationAuthorizationRequest: "Always",
backgroundPermissionRationale: {
title:
"Allow {applicationName} to access to this device's location in the background?",
message:
"In order to track your activity in the background, please enable {backgroundPermissionOptionLabel} location permission",
positiveAction: "Change to {backgroundPermissionOptionLabel}",
negativeAction: "Cancel",
},
}).then(async (state) => {
console.log("- BackgroundGeolocation is configured and ready");
let providerState = await BackgroundGeolocation.getProviderState();
setLocationForegroundPermission(
providerState.status === 4 || providerState.status === 3
);
setLocationBackgroundPermission(providerState.status === 3);
BackgroundGeolocation.setConfig({
params: {
// <-- Optional HTTP params
type: "locationUpdates",
userId: user?.id,
timezone: user?.timezone,
foregroundPermission:
providerState.status === 4 || providerState.status === 3,
backgroundPermission: providerState.status === 3,
deviceBrand: Device.brand,
deviceModel: Device.modelName,
},
}).then((state) => {
// console.log("[setConfig] success: ", state);
});
console.log(state);
if (!state.enabled) BackgroundGeolocation.start();
// let changePace = await BackgroundGeolocation.changePace(true); // uncomment for testing
});
};
initBackgroundGeolocation();
return () => {
// Remove BackgroundGeolocation event-subscribers when the View is removed or refreshed
// during development live-reload. Without this, event-listeners will accumulate with
// each refresh during live-reload.
onLocation.remove();
onMotionChange.remove();
onActivityChange.remove();
onProviderChange.remove();
onHttp.remove();
};
}, []);
return (
<ScrollView
contentContainerStyle={styles.scrollView}
refreshControl={
<RefreshControl
refreshing={refreshing}
enabled={refresherEnabled}
onRefresh={onRefresh}
/>
}
>
<View
style={{
flex: 1,
height: Platform.OS === "android" ? statusBarHeight : 0,
backgroundColor: "#ffffff",
}}
>
<StatusBar style="auto" />
<WebView
key={webViewKey}
ref={(ref) => (WEBVIEW = ref)}
source={{ uri: pwaUri }}
javaScriptEnabled={true}
cacheEnabled={false}
incognito={true}
style={{ flex: 1 }}
onMessage={onMessage}
onScroll={handleScroll}
// startInLoadingState={true}
// renderLoading={() => (
// <ActivityIndicator
// style={styles.activityIndicator}
// color="#ffffff"
// size="large"
// />
// )}
/>
</View>
</ScrollView>
);
};
export default MainWebViewWrapper;
const styles = StyleSheet.create({
scrollView: {
flex: 1,
},
// activityIndicator: {
// backgroundColor: "#E02F85",
// flex: 1,
// position: "absolute",
// left: 0,
// right: 0,
// top: 0,
// bottom: 0,
// alignItems: "center",
// justifyContent: "center",
// transform: [{ scaleX: 2 }, { scaleY: 2 }],
// },
});