-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_layout.tsx
83 lines (75 loc) · 2.89 KB
/
_layout.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
import { SplashScreen, Stack } from "expo-router";
import { useFonts } from "expo-font";
import { useEffect, useState } from "react";
import NetInfo from '@react-native-community/netinfo';
import { Alert, View, Text } from 'react-native';
import { MapThemeProvider } from '../context/MapThemeContext';
import { LocationProvider } from '../context/LocationContext';
import { ErrorBoundary } from 'react-error-boundary';
import { OfflineDataProvider } from '../context/OfflineDataContext';
function ErrorFallback({ error }: { error: Error }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Something went wrong:</Text>
<Text>{error.message}</Text>
</View>
);
}
export default function RootLayout() {
const [fontsLoaded, error] = useFonts({
"Poppins-Black": require("../assets/fonts/Poppins-Black.ttf"),
"Poppins-Bold": require("../assets/fonts/Poppins-Bold.ttf"),
"Poppins-ExtraBold": require("../assets/fonts/Poppins-ExtraBold.ttf"),
"Poppins-ExtraLight": require("../assets/fonts/Poppins-ExtraLight.ttf"),
"Poppins-Light": require("../assets/fonts/Poppins-Light.ttf"),
"Poppins-Medium": require("../assets/fonts/Poppins-Medium.ttf"),
"Poppins-Regular": require("../assets/fonts/Poppins-Regular.ttf"),
"Poppins-SemiBold": require("../assets/fonts/Poppins-SemiBold.ttf"),
"Poppins-Thin": require("../assets/fonts/Poppins-Thin.ttf"),
});
const [isOfflineMode, setIsOfflineMode] = useState(false);
useEffect(() => {
if (error) throw error;
if (fontsLoaded) SplashScreen.hideAsync();
const unsubscribe = NetInfo.addEventListener(state => {
if (!state.isConnected && !isOfflineMode) {
Alert.alert(
"No Internet Connection",
"Would you like to continue in offline mode? Some features may be limited.",
[
{
text: "Enable Offline Mode",
onPress: () => setIsOfflineMode(true),
style: "default"
},
{
text: "Try Again",
onPress: () => NetInfo.fetch(),
style: "cancel"
}
],
{ cancelable: false }
);
}
});
return () => unsubscribe();
}, [fontsLoaded, error, isOfflineMode]);
if (!fontsLoaded && !error) return null;
return (
<ErrorBoundary FallbackComponent={ErrorFallback}>
<OfflineDataProvider>
<LocationProvider>
<MapThemeProvider>
<Stack screenOptions={{
headerShown: false,
}}>
<Stack.Screen name="(auth)" options={{ headerShown: false }} />
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
<Stack.Screen name="index" options={{ headerShown: false }} />
</Stack>
</MapThemeProvider>
</LocationProvider>
</OfflineDataProvider>
</ErrorBoundary>
);
}