forked from MohGovIL/hamagen-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·60 lines (51 loc) · 1.8 KB
/
index.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
import 'react-native-gesture-handler'; // required to fix an unhandled event due to the asynchronous router
import { AppRegistry } from 'react-native';
import moment from 'moment';
import BackgroundFetch from 'react-native-background-fetch';
import BackgroundGeolocation from 'react-native-background-geolocation';
import App from './src/App';
import { name as appName } from './app.json';
import { updateDBAccordingToSampleVelocity } from './src/services/SampleService';
import { checkSickPeople } from './src/services/Tracker';
import { onError } from './src/services/ErrorService';
import { initConfig } from './src/config/config';
const onLocationReceived = async (location) => {
// ignore non-distinct locations from the SDK
if (location.sample) {
return;
}
try {
location.timestamp = moment(location.timestamp).valueOf();
await initConfig();
await updateDBAccordingToSampleVelocity(location);
} catch (error) {
onError({ error });
}
};
BackgroundGeolocation.onLocation(
async (location) => {
await onLocationReceived(location);
}, (error) => {
onError({ error });
}
);
const BackgroundFetchHeadlessTask = async (event) => {
try {
const { taskId } = event;
console.log('[BackgroundFetch HeadlessTask] start: ', taskId);
await initConfig();
await checkSickPeople();
BackgroundFetch.finish(taskId);
} catch (error) {
onError({ error });
}
};
const BackgroundGeolocationHeadlessTask = async (event) => {
console.log('[BackgroundGeolocation HeadlessTask] -', event.name);
if (event.name === 'location') {
await onLocationReceived(event.params);
}
};
AppRegistry.registerComponent(appName, () => App);
BackgroundFetch.registerHeadlessTask(BackgroundFetchHeadlessTask);
BackgroundGeolocation.registerHeadlessTask(BackgroundGeolocationHeadlessTask);