-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
233 lines (201 loc) · 5.66 KB
/
main.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
/**
* @providesModule main
* @flow
*/
import Exponent, {
Components,
} from 'exponent';
import React from 'react';
import {
AppRegistry,
ActivityIndicator,
DeviceEventEmitter,
Linking,
NativeModules,
Platform,
StatusBar,
StyleSheet,
TouchableOpacity,
View,
Alert,
} from 'react-native';
import {
withNavigation,
NavigationProvider,
StackNavigation,
} from '@exponent/ex-navigation';
import {
MaterialIcons,
Ionicons,
} from '@exponent/vector-icons';
import {
connect,
Provider as ReduxProvider,
} from 'react-redux';
import PlaygroundStore from 'PlaygroundStore';
import Actions from 'Actions';
import Colors from 'Colors';
import Layout from 'Layout';
import LocalStorage from 'LocalStorage';
import Router from 'Router';
import {
cacheFonts,
cacheImages,
} from './utilities/cacheHelpers';
class AppContainer extends React.Component {
render() {
return (
<ReduxProvider store={PlaygroundStore}>
<NavigationProvider router={Router}>
<App {...this.props} />
</NavigationProvider>
</ReduxProvider>
);
}
}
@withNavigation
@connect(data => App.getDataProps)
class App extends React.Component {
static getDataProps(data) {
return {
currentUser: data.currentUser,
isGlobalLoadingVisible: data.apiState.isLoading,
};
};
state = {
bootstrapIsComplete: false,
};
componentWillMount() {
this._bootstrap();
}
componentDidUpdate(prevProps) {
if (!this.state.bootstrapIsComplete) {
return;
}
const rootNavigator = this.props.navigation.getNavigator('root');
if (!isLoggedIn(prevProps.currentUser) && isLoggedIn(this.props.currentUser)) {
rootNavigator.replace(Router.getRoute(Layout.navigationLayoutRoute));
} else if (isLoggedIn(prevProps.currentUser) && !isLoggedIn(this.props.currentUser)) {
rootNavigator.replace(Router.getRoute('authentication'));
}
}
componentDidMount() {
let { exp } = this.props;
// Was the app opened from a rnplay:// link?
if (exp.initialUri) {
this._handleOpenUrl(exp.initialUri);
}
// Was a rnplay:// link pressed while the app was backgrounded?
Linking.addEventListener('url', (event) => {
let { url } = event;
this._handleOpenUrl(url);
});
// Was the app opened from a push notification?
if (exp.notification) {
this._handleNotification({data: exp.notification});
}
// Watch for push notifications while open
DeviceEventEmitter.addListener('Exponent.notification', this._handleNotification);
}
_handleNotification = ({data}) => {
if (typeof data === 'string') {
data = JSON.parse(data);
}
if (data && data.url_token) {
this.props.dispatch(Actions.openApp(`rnplay://rnplay.org/apps/${data.url_token}`));
}
}
_handleOpenUrl = (url) => {
if (!url || url.indexOf('rnplay://') === -1) {
return;
}
if (url.indexOf('+reload') > 0) {
console.log('Reload!');
NativeModules.ExponentUtil.reload && NativeModules.ExponentUtil.reload();
} else {
this.props.dispatch(Actions.openApp(url));
}
}
render() {
if (!this.state.bootstrapIsComplete) {
return <Components.AppLoading />;
}
return (
<View style={styles.container}>
{Platform.OS === 'ios' && <StatusBar barStyle="light-content" />}
{Platform.OS === 'android' && <View style={styles.statusBarUnderlay} />}
<StackNavigation
id="root"
initialRoute={getInitialRoute(this.props.currentUser)}
/>
{this.props.isGlobalLoadingVisible && this._renderGlobalLoadingOverlay()}
</View>
);
}
_renderGlobalLoadingOverlay = () => {
return (
<View style={[StyleSheet.absoluteFill, styles.loadingOverlay]}>
<ActivityIndicator color={Colors.midGrey} />
</View>
);
}
async _bootstrap() {
try {
let fetchUser = LocalStorage.getUserAsync();
let fetchHistory = LocalStorage.getHistoryAsync();
let fontAssets = cacheFonts([
Platform.OS === 'ios' ? Ionicons.font : MaterialIcons.font,
{'open-sans-light': require('./assets/fonts/opensans-light.ttf')},
{'open-sans': require('./assets/fonts/opensans-regular.ttf')},
{'open-sans-bold': require('./assets/fonts/opensans-bold.ttf')},
]);
let imageAssets = cacheImages([
require('./assets/images/logo-white.png'),
require('./assets/images/logo-with-sand.png'),
require('./assets/images/network-error.png'),
require('./assets/images/exponent-wordmark.png'),
require('./assets/images/exponent-icon.png'),
]);
let [ user, history, ...rest ] = await Promise.all([
fetchUser,
fetchHistory,
...fontAssets,
...imageAssets
]);
user && this.props.dispatch(Actions.setCurrentUser(user));
history && this.props.dispatch(Actions.setHistory(history));
this.setState({bootstrapIsComplete: true});
} catch (e) {
Alert.alert('Error on bootstrap!', e.message);
}
}
}
function isLoggedIn(userState) {
return !!userState.authToken || userState.isGuest;
}
function getInitialRoute(currentUser) {
if (isLoggedIn(currentUser)) {
return Router.getRoute(Layout.navigationLayoutRoute);
} else {
return Router.getRoute('authentication');
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
statusBarUnderlay: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: 25,
backgroundColor: 'rgba(0,0,0,0.5)',
},
loadingOverlay: {
backgroundColor: 'rgba(255,255,255,0.5)',
alignItems: 'center',
justifyContent: 'center',
},
});
Exponent.registerRootComponent(AppContainer);