-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
133 lines (113 loc) · 3.81 KB
/
App.tsx
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
import 'react-native-gesture-handler';
import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
import {
MD3DarkTheme as DefaultTheme,
Provider as PaperProvider,
} from 'react-native-paper';
import {StatusBar} from 'expo-status-bar';
import {DrawerNav} from './src/components/DrawerNav';
import {useEffect, useState} from 'react';
import {
snapshotWorker,
simulateRealtimeDataWorker,
getOrCreateRealtimeRecord,
hrService,
powerService,
} from './src/lib/data';
import Loading from './src/components/Loading';
import {locationWorker} from './src/lib/location';
import {Alert} from 'react-native';
import {NavigationContainer, DarkTheme} from '@react-navigation/native';
import {navigationRef} from './src/lib/navigation';
import {StravaProvider} from './src/lib/StravaContext';
import * as strava from './src/lib/strava';
import {isDevice} from 'expo-device';
import {useKeepAwake} from 'expo-keep-awake';
import * as Sentry from 'sentry-expo';
import 'react-native-gesture-handler';
import Constants from './src/constants';
import Mapbox from '@rnmapbox/maps';
import {ble} from './src/lib/sensor';
Mapbox.setAccessToken(Constants.mapboxPublicToken);
Sentry.init({
dsn: 'https://[email protected]/4504984718934016',
// only enable on device in development.
// don't want emulator errors
enableInExpoDevelopment:
process.env.APP_VARIANT === 'preview' ||
process.env.APP_VARIANT === 'production',
debug: true, // If `true`, Sentry will try to print out useful debugging information if something goes wrong with sending the event. Set it to `false` in production
});
function App() {
// hasBooted is a flag for all required vars
// needed before mounting app.
const [hasBooted, setHasBooted] = useState(false);
// Set shouldTrack based on if we want GPS location trackin on or not
// keep the screen awake so our services can run.
useKeepAwake();
const [stravaToken, setStravaToken] = useState<strava.Token | null>(null);
useEffect(() => {
const startServicesAndTasks = async () => {
try {
await ble.requestPermissions();
await ble.start();
} catch (err) {
console.log(err);
}
console.log('ble', await ble.checkState());
try {
// todo we should check settings
// before launching if shouldTrack = false
await locationWorker.start();
} catch (e) {
Alert.alert('Error getting location');
}
// Ensure that the realtime row is setup
// before we run any async services.
await getOrCreateRealtimeRecord();
if (!isDevice) {
// this only runs in the emulator
await simulateRealtimeDataWorker.start(3000);
}
await snapshotWorker.start(1000);
const token = await strava.loadToken();
setStravaToken(token);
setHasBooted(true);
// Start our bluetooth services
try {
await hrService.start();
} catch (err) {
console.log(err);
}
try {
await powerService.start();
} catch (err) {
console.log(err);
}
console.log('App has booted successfully.');
};
startServicesAndTasks(); // run it
return () => {
simulateRealtimeDataWorker.stop();
snapshotWorker.stop();
hrService.stop();
powerService.stop();
// this now gets called when the component unmounts
};
}, []);
if (!hasBooted) {
// TODO style splash screen
return <Loading />;
}
return (
<PaperProvider theme={DefaultTheme}>
<StatusBar hidden />
<StravaProvider stravaToken={stravaToken}>
<NavigationContainer theme={DarkTheme} ref={navigationRef}>
<DrawerNav />
</NavigationContainer>
</StravaProvider>
</PaperProvider>
);
}
export default Sentry.Native.wrap(gestureHandlerRootHOC(App));