forked from PapillonApp/papillon-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
120 lines (107 loc) · 3.74 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
import React, { useEffect, useState, useMemo } from 'react';
import { View } from 'react-native';
import { AppContextProvider } from './utils/AppContext';
import * as appContext from './utils/AppContext';
import { IndexDataInstance } from './fetch';
import AsyncStorage from '@react-native-async-storage/async-storage';
import loadFonts from './utils/Fonts';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
import AppStack from './stacks/AppStack';
import AuthStack from './stacks/AuthStack';
import LoadingScreen from './stacks/LoadingScreen';
import { startNetworkLogging } from 'react-native-network-logger';
import FlashMessage from 'react-native-flash-message';
startNetworkLogging();
const provider = new IndexDataInstance();
import {
BottomSheetModalProvider,
} from '@gorhom/bottom-sheet';
function App() {
const [loggedIn, setLoggedIn] = useState(false);
const [loading, setLoading] = useState(true);
const dataProvider = useMemo(() => provider, [provider]); // Use memo for dataProvider
useEffect(() => {
const prepare = async () => {
try {
await loadFonts();
const serviceName = await AsyncStorage.getItem('service');
if (!serviceName) {
setLoading(false);
return;
}
if (serviceName === 'pronote') {
setLoggedIn(true);
await provider.init(serviceName);
} else if (serviceName === 'skolengo') {
await provider.init(serviceName);
setLoggedIn(Boolean(provider.pronoteInstance || provider.skolengoInstance || provider.isNetworkFailing));
} else if (serviceName === 'ecoledirecte') {
await provider.init(serviceName);
setLoggedIn(true);
}
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
prepare();
}, []);
const ctxValue = useMemo(() => ({
loggedIn,
setLoggedIn,
dataProvider,
}), [loggedIn, dataProvider]); // Use memo for ctxValue
appContext.setContextValues(ctxValue);
const AltScreens = [
{
name: 'NetworkLoggerScreen',
component: require('./views/Settings/NetworkLogger').default,
options: {
presentation: 'modal',
headerTitle: 'Historique réseau',
},
},
{
name: 'ConsentScreen',
component: require('./views/NewAuthStack/ConsentScreen').default,
options: {
presentation: 'modal',
headerTitle: 'Termes & conditions',
headerBackVisible: false,
},
},
{
name: 'ConsentScreenWithoutAcceptation',
component: require('./views/ConsentScreenWithoutAcceptation').default,
options: {
presentation: 'modal',
headerTitle: 'Termes & conditions',
headerBackVisible: false,
},
},
];
return (
<View style={{ flex: 1 }}>
<AppContextProvider state={ctxValue}>
<BottomSheetModalProvider>
<Stack.Navigator>
{loading ? (
<Stack.Screen name="Loading" component={LoadingScreen} options={{ headerShown: false }} />
) : loggedIn ? (
<Stack.Screen name="AppStack" component={AppStack} options={{ headerShown: false, animation: 'fade', animationDuration: 150 }} />
) : (
<Stack.Screen name="AuthStack" component={AuthStack} options={{ headerShown: false }} />
)}
{AltScreens.map((screen, index) => (
<Stack.Screen key={index} name={screen.name} component={screen.component} options={screen.options} />
))}
</Stack.Navigator>
</BottomSheetModalProvider>
</AppContextProvider>
<FlashMessage position="top" />
</View>
);
}
export default App;