-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
123 lines (107 loc) · 3.12 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
import ApolloClient from 'apollo-boost';
import { AppLoading, Constants, Font, Location, Permissions } from 'expo';
import React from 'react';
import { ApolloProvider } from 'react-apollo';
import { RefreshControl, View } from 'react-native';
import Background from './components/background';
import Error from './components/error';
import Weather from './components/weather';
import Wrapper from './components/wrapper';
import { theme } from './helpers/theme';
const client = new ApolloClient({
uri: 'https://naucast-api.now.sh',
});
interface State {
assetsLoaded: boolean;
refreshing: boolean;
error: string;
coords: { lat: number; lng: number };
}
export default class App extends React.Component<{}, State> {
public state = {
assetsLoaded: false,
refreshing: false,
error: null,
coords: null,
};
public componentDidMount() {
this.getLocation();
}
public render() {
const { assetsLoaded, refreshing, error, coords } = this.state;
if (assetsLoaded) {
return (
<ApolloProvider client={client}>
<Background />
<View
style={{ height: Constants.statusBarHeight }}
accessibilityElementsHidden={true}
importantForAccessibility="no-hide-descendants"
/>
<Wrapper
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={this.onRefresh}
tintColor="#fff"
colors={[theme.colors.lightBlue]}
/>
}
>
{!error && !refreshing && <Weather coords={coords} />}
{error && <Error>{error}</Error>}
</Wrapper>
</ApolloProvider>
);
} else {
return <AppLoading startAsync={this.loadAssets} onFinish={this.setAssetsLoaded} onError={console.warn} />;
}
}
private onRefresh = () => {
this.getLocation();
this.setRefreshing(true);
};
private getLocation = async () => {
this.setRefreshing(true);
// Request location permission
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
error: 'Please allow location access to use this app',
});
}
// Get coords and update state
const { coords } = await Location.getCurrentPositionAsync({ enableHighAccuracy: false });
this.setState({
coords: {
lat: coords.latitude,
lng: coords.longitude,
},
});
this.setRefreshing(false);
};
private loadAssets = async () => {
await this.loadFonts();
return;
};
private setAssetsLoaded = () => {
this.setState({
assetsLoaded: true,
});
};
private clearError() {
this.setState({ error: null });
}
private setRefreshing(val: boolean) {
this.setState({
refreshing: val,
});
}
private loadFonts = async () => {
await Font.loadAsync({
'font-light': require('./assets/fonts/AvenirNext-UltraLight.ttf'),
'font-regular': require('./assets/fonts/AvenirNext-Regular.ttf'),
'font-bold': require('./assets/fonts/AvenirNext-DemiBold.ttf'),
});
};
}