-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.tsx
211 lines (174 loc) · 6.98 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
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
import React, { useEffect, useRef, useState } from 'react';
import { Provider } from 'react-redux';
import store from './App/Store/store'
import MainPage from './App/MainPage';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { useGetOverviewQuery, useGetFixturesQuery } from './App/Store/fplSlice';
import * as SplashScreen from "expo-splash-screen";
import * as ScreenOrientation from 'expo-screen-orientation';
import { FplBaseDataContext, ManageThemeContext } from './App/AppContext';
import PlayerModal from './App/Modals/PlayerModal';
import SettingsModal from './App/Modals/SettingsModal';
import PlayerDetailedStatsModal from './App/Modals/PlayerDetailedStatsModal';
import PlayerComparisonModal from './App/Modals/PlayerComparisonModal';
import TeamModal from './App/Modals/TeamModal';
import GameweekOverviewModal from './App/Modals/GameweekOverviewModal';
import MutableModal from './App/Modals/MutableModal';
import FilterModal from './App/Modals/FilterModal';
import { FplFixture } from './App/Models/FplFixtures';
import { Appearance, useColorScheme } from 'react-native';
import { darkTheme, defaultTheme } from './App/Global/GlobalConstants';
import { getThemeData, setThemeData } from './App/Helpers/FplDataStorageService';
import TeamFixtureDifficulty from './App/Modals/GameweekOverviewModal/TeamFixtureDifficulty';
import { useFonts } from 'expo-font';
export interface FixturesMap {
[key: number] : FplFixture[];
}
export type RootStackParams = {
Home: any;
SettingsModal: any;
PlayerModal: any;
PlayerDetailedStatsModal: any;
PlayerComparisonModal: any;
TeamModal: any;
GameweekOverview: any;
MutableModal: any;
FilterModal: any;
}
let customFonts = {
SFNSText: require('./assets/fonts/SFNSText-Regular.otf'),
SFNSTextBold: require('./assets/fonts/SFNSText-Bold.otf'),
SFNSTextSemiBold: require('./assets/fonts/SFNSText-Semibold.otf'),
}
const Stack = createStackNavigator<RootStackParams>();
const AppWrapper = () => {
return (
<Provider store={store}>
<App/>
</Provider>
)
}
function App() {
const overview = useGetOverviewQuery();
const fixtures = useGetFixturesQuery();
const [fixtureLists, setFixtureLists] = useState({} as FixturesMap);
const [teamFixtureDifficultyView, setTeamFixtureDifficultyView] = useState({} as JSX.Element);
const errorCount = useRef(0);
const [isFontLoaded] = useFonts(customFonts);
const colorScheme = useColorScheme();
const [theme, setTheme] = useState('light');
const [useDeviceTheme, setUseDeviceTheme] = useState(false);
useEffect( function refetchIfError() {
if (errorCount.current < 3) {
if (overview.isError) {
setTimeout(() => { overview.refetch() }, 30000);
}
if (fixtures.isError) {
setTimeout(() => { fixtures.refetch() }, 30000);
}
}
else {
SplashScreen.hideAsync();
}
errorCount.current = errorCount.current++;
}, [overview.isError, fixtures.isError]);
useEffect( function showSplashScreenWhileFetchingData() {
SplashScreen.preventAutoHideAsync();
}, []);
useEffect(function gettingThemeDataFromAsyncStorage() {
async function getThemeDataFromStorage() {
let themeData = await getThemeData();
if (themeData) {
if (themeData.useDeviceTheme) {
if (colorScheme) {
setTheme(colorScheme);
setUseDeviceTheme(true);
}
}
else {
setTheme(themeData.theme);
}
}
}
getThemeDataFromStorage();
}, []);
useEffect(function lockOrientation() {
async function lockingOrientation() {
await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT);
}
lockingOrientation();
}, [])
useEffect(function useDevicesThemeChanged() {
if (useDeviceTheme) {
let deviceTheme = Appearance.getColorScheme();
if (deviceTheme) {
setThemeData({
theme: deviceTheme,
useDeviceTheme: true,
});
setTheme(deviceTheme);
}
else {
setThemeData({
theme: theme,
useDeviceTheme: false,
});
}
}
else {
setThemeData({
theme: theme,
useDeviceTheme: false,
});
}
}, [useDeviceTheme]);
useEffect(function themeChanged() {
if (!useDeviceTheme) {
setThemeData({
theme: theme,
useDeviceTheme: false,
})
}
}, [theme])
useEffect( function removeSplashScreen() {
if (fixtures.isSuccess && overview.isSuccess && isFontLoaded) {
let liveGameweek = overview.data.events.filter((event) => { return event.is_current === true; })[0]?.id;
let gettingFixtureLists = {} as FixturesMap;
overview.data.teams.map(team => {
gettingFixtureLists[team.id] = fixtures.data.filter((fixture) => (fixture.team_a === team.id || fixture.team_h === team.id) && (fixture.event && fixture.event > liveGameweek));
})
setFixtureLists(gettingFixtureLists);
setTimeout(() => SplashScreen.hideAsync(), 2000);
}
}, [fixtures.isSuccess, overview.isSuccess, isFontLoaded]);
useEffect(function createTeamFixtureDifficultyView() {
setTeamFixtureDifficultyView(<TeamFixtureDifficulty/>);
}, [fixtureLists])
if (overview.isSuccess && fixtures.isSuccess && isFontLoaded)
return (
<FplBaseDataContext.Provider value={{ overview: overview.data, fixtures: fixtures.data, fixtureLists: fixtureLists, TeamFixtureDifficultyView: teamFixtureDifficultyView}}>
<ManageThemeContext.Provider value={{theme: theme, useDeviceTheme: useDeviceTheme, setTheme: setTheme, setUseDeviceTheme: setUseDeviceTheme }}>
<NavigationContainer theme={(theme === 'dark') ? darkTheme : defaultTheme}>
<Stack.Navigator initialRouteName='Home'>
<Stack.Group>
<Stack.Screen name="Home" component={MainPage} options={{headerShown: false}}/>
</Stack.Group>
<Stack.Group screenOptions={{ presentation: 'transparentModal', headerShown: false }}>
<Stack.Screen name="SettingsModal" component={SettingsModal}/>
<Stack.Screen name="PlayerModal" component={PlayerModal}/>
<Stack.Screen name="PlayerDetailedStatsModal" component={PlayerDetailedStatsModal}/>
<Stack.Screen name="PlayerComparisonModal" component={PlayerComparisonModal}/>
<Stack.Screen name="TeamModal" component={TeamModal}/>
<Stack.Screen name="GameweekOverview" component={GameweekOverviewModal}/>
<Stack.Screen name="MutableModal" component={MutableModal}/>
<Stack.Screen name="FilterModal" component={FilterModal}/>
</Stack.Group>
</Stack.Navigator>
</NavigationContainer>
</ManageThemeContext.Provider>
</FplBaseDataContext.Provider>
);
else return null;
}
export default AppWrapper;