-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
144 lines (118 loc) · 3.87 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
import React, { Component } from 'react';
import { Platform, Text, ScrollView, RefreshControl } from 'react-native';
import * as Permissions from 'expo-permissions';
import * as Location from 'expo-location';
import KeepAwake from 'expo-keep-awake';
import Constants from 'expo-constants';
import { API_ENDPOINT } from 'react-native-dotenv';
import Compass from './src/components/Compass';
import Timer from './src/components/Timer';
import styles from './src/styles/App';
import getBearing from './src/utils/get-bearing';
import launchPadCoords from './src/utils/launch-pads';
export default class App extends Component {
state = {
location: null,
currentForwardBearing: null,
bearingToLaunchPad: null,
errorMessage: null,
launchPad: 1,
launchTime: null,
name: null,
isRefreshing: false
};
onRefresh = async() => {
this.setState({ refreshing: true });
await this.getLaunchDetails();
this.setState({ refreshing: false });
}
componentDidMount() {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({
errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!'
});
} else {
this.getLaunchDetails();
this.getLocationAsync();
Location.watchHeadingAsync(this.handleHeading);
}
}
getLaunchDetails = async() => {
try {
let response = await fetch(`${API_ENDPOINT}/launch.js`);
let {
date,
launchPad,
name
} = await response.json();
this.setState({ launchTime: date, launchPad, name });
} catch(error) {
// Ignore error if there's already some data that was fetched previously.
if (this.state.launchTime === null) {
this.setState({ errorMessage: 'Could not fetch launch details' });
}
}
};
getLocationAsync = async() => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location is required for this app',
});
}
let location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
this.setState({ location });
this.calculateBearing();
};
handleHeading = ({ trueHeading }) => {
this.setState({ currentForwardBearing: trueHeading });
};
toggleLaunchPad = () => {
let currentPad = this.state.launchPad;
if (currentPad === 1) {
this.setState({ launchPad: 2 });
} else {
this.setState({ launchPad: 1 });
}
this.calculateBearing();
};
calculateBearing = () => {
let { launchPad, location } = this.state;
if (!location) {
return;
}
let { latitude, longitude } = location.coords;
let bearingToLaunchPad = getBearing(
{ latitude, longitude },
launchPadCoords[launchPad - 1]
);
this.setState({ bearingToLaunchPad });
};
render() {
let text = 'Getting your location..';
if (this.state.errorMessage) {
text = this.state.errorMessage;
} else if (this.state.bearingToLaunchPad) {
text = `Launch Pad ${this.state.launchPad}`;
}
let refreshControl = (
<RefreshControl
progressViewOffset={10}
refreshing={this.state.isRefreshing}
onRefresh={this.onRefresh} />
);
return (
<ScrollView contentContainerStyle={styles.container} refreshControl={refreshControl}>
<KeepAwake />
<Timer launchTime={this.state.launchTime} />
<Compass
toggleLaunchPad={this.toggleLaunchPad}
bearingToLaunchPad={this.state.bearingToLaunchPad}
currentForwardBearing={this.state.currentForwardBearing} />
<Text style={styles.rocket}>{this.state.name}</Text>
<Text style={styles.launchPad}>{text}</Text>
<Text style={styles.helperText}>Tap the circle to toggle between the launch pads</Text>
</ScrollView>
);
}
}