|
| 1 | +import React from 'react'; |
| 2 | +import { Platform, StatusBar, StyleSheet, View } from 'react-native'; |
| 3 | +import { AppLoading, Font, Icon } from 'expo'; |
| 4 | +import AppNavigator from './navigation/AppNavigator'; |
| 5 | +import DummyNetworkContext from './DummyNetworkContext'; |
| 6 | + |
| 7 | +const onlineUrl = 'https://www.google.com/'; |
| 8 | +const offlineUrl = 'https://www.weifhweopfhwioehfiwoephfpweoifhewifhpewoif.com'; |
| 9 | + |
| 10 | +export default class App extends React.Component { |
| 11 | + constructor(props) { |
| 12 | + super(props); |
| 13 | + this.state = { |
| 14 | + isLoadingComplete: false, |
| 15 | + network: { |
| 16 | + pingUrl: onlineUrl, |
| 17 | + toggleConnection: this.toggleConnection, |
| 18 | + }, |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + toggleConnection = () => { |
| 23 | + this.setState(prevState => ({ |
| 24 | + network: { |
| 25 | + ...prevState.network, |
| 26 | + pingUrl: |
| 27 | + prevState.network.pingUrl === onlineUrl ? offlineUrl : onlineUrl, |
| 28 | + }, |
| 29 | + })); |
| 30 | + }; |
| 31 | + |
| 32 | + loadResourcesAsync = async () => |
| 33 | + Font.loadAsync({ |
| 34 | + ...Icon.Ionicons.font, |
| 35 | + 'space-mono': require('./assets/fonts/SpaceMono-Regular.ttf'), |
| 36 | + }); |
| 37 | + |
| 38 | + handleFinishLoading = () => { |
| 39 | + this.setState({ isLoadingComplete: true }); |
| 40 | + }; |
| 41 | + |
| 42 | + render() { |
| 43 | + const { isLoadingComplete, network } = this.state; |
| 44 | + const { skipLoadingScreen } = this.props; |
| 45 | + if (!isLoadingComplete && !skipLoadingScreen) { |
| 46 | + return ( |
| 47 | + <AppLoading |
| 48 | + startAsync={this.loadResourcesAsync} |
| 49 | + onFinish={this.handleFinishLoading} |
| 50 | + /> |
| 51 | + ); |
| 52 | + } |
| 53 | + return ( |
| 54 | + <DummyNetworkContext.Provider value={network}> |
| 55 | + <View style={styles.container}> |
| 56 | + {Platform.OS === 'ios' && <StatusBar barStyle="default" />} |
| 57 | + <AppNavigator /> |
| 58 | + </View> |
| 59 | + </DummyNetworkContext.Provider> |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const styles = StyleSheet.create({ |
| 65 | + container: { |
| 66 | + flex: 1, |
| 67 | + backgroundColor: '#fff', |
| 68 | + }, |
| 69 | +}); |
0 commit comments