1- import React , { useContext } from 'react' ;
2- import { StatusBar , StyleSheet , View , Alert } from 'react-native' ;
1+ import React , { useCallback , useContext } from 'react' ;
2+ import { StatusBar , StyleSheet , View , Alert , Text } from 'react-native' ;
33import { NavigationContainer } from '@react-navigation/native' ;
44import Discover from './components/Discover' ;
55import DiscoverSection from './components/DiscoverSection' ;
@@ -21,7 +21,7 @@ import SignUp from './components/SignUp';
2121import { DefaultTheme , Provider as PaperProvider } from 'react-native-paper' ;
2222import * as Keychain from 'react-native-keychain' ;
2323import MessageSection from './components/MessageSection' ;
24-
24+ import NetInfo from '@react-native-community/netinfo' ;
2525import axios from 'axios' ;
2626import { Android_Local_ADDRESS , IOS_Local_ADDRESS } from '@env' ;
2727import { ChatContext } from './components/ChatContext' ;
@@ -78,6 +78,7 @@ const App = () => {
7878 const [ isSignedIn , setIsSignedIn ] = React . useState ( false ) ;
7979 const [ user , setUser ] = React . useState ( { } ) ;
8080 const [ axiosInst , setaxiosInstance ] = React . useState ( { } ) ;
81+ const [ isConnected , setIsConnected ] = React . useState ( true ) ;
8182
8283 const BASE_ADDRESS = 'https://campusspace.herokuapp.com' ;
8384 // Platform.OS === 'android' ? Android_Local_ADDRESS : IOS_Local_ADDRESS;
@@ -129,6 +130,14 @@ const App = () => {
129130 }
130131 } ;
131132
133+ const checkConnection = useCallback ( state => {
134+ if ( state . isConnected ) {
135+ setIsConnected ( true ) ;
136+ } else {
137+ setIsConnected ( false ) ;
138+ }
139+ } , [ ] ) ;
140+
132141 React . useEffect ( ( ) => {
133142 async function init ( ) {
134143 try {
@@ -137,7 +146,6 @@ const App = () => {
137146 GoogleSignin . configure ( { } ) ;
138147 await GoogleSignin . hasPlayServices ( ) ;
139148
140- // TODO: store user in storage
141149 const credentials = await Keychain . getGenericPassword ( ) ;
142150
143151 if ( credentials ) {
@@ -153,10 +161,9 @@ const App = () => {
153161
154162 setaxiosInstance ( { axiosInstance} ) ;
155163
156- // TODO: Remove this fake splash screen
157- setTimeout ( ( ) => {
158- setIsLoading ( false ) ;
159- } , 2000 ) ;
164+ NetInfo . addEventListener ( checkConnection ) ;
165+
166+ setIsLoading ( false ) ;
160167 } catch ( error ) {
161168 console . log ( error ) ;
162169 if ( error . code === statusCodes . SIGN_IN_CANCELLED ) {
@@ -177,10 +184,13 @@ const App = () => {
177184 }
178185
179186 init ( ) ;
187+
188+ return ( ) => {
189+ NetInfo . removeEventListener ( checkConnection ) ;
190+ } ;
180191 } , [ ] ) ;
181192
182193 if ( isLoading ) {
183- console . log ( 'Loading' ) ;
184194 return < SplashScreen > </ SplashScreen > ;
185195 }
186196
@@ -208,6 +218,26 @@ const App = () => {
208218 backgroundColor = "transparent"
209219 translucent = { true }
210220 />
221+ { ! isConnected && (
222+ < View
223+ style = { {
224+ display : 'flex' ,
225+ alignItems : 'center' ,
226+ justifyContent : 'center' ,
227+ paddingTop : StatusBar . currentHeight + 4 ,
228+ paddingBottom : 4 ,
229+ backgroundColor : theme . colors . danger ,
230+ } } >
231+ < Text
232+ style = { {
233+ fontSize : 14 ,
234+ color : theme . colors . textHeadBlack ,
235+ fontWeight : 'bold' ,
236+ } } >
237+ No network connection
238+ </ Text >
239+ </ View >
240+ ) }
211241 { ! isSignedIn && (
212242 < LoggedOutStack . Navigator >
213243 < LoggedOutStack . Screen
0 commit comments