-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
84 lines (72 loc) · 2.77 KB
/
App.jsx
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
import 'react-native-gesture-handler';
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import { useColorScheme } from 'react-native';
import { useFonts } from 'expo-font';
import { enableScreens } from 'react-native-screens';
import { Provider as PaperProvider } from 'react-native-paper';
import { NavigationContainer } from '@react-navigation/native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { BatteryMonitor } from './components';
import { UserContextProvider, LocationContextProvider } from './contexts';
import { CombinedDarkTheme, CombinedDefaultTheme } from './theme';
import { initDB, initNotifications, initTasks, initAnimations } from './config';
import { themeNames } from './utils/constants';
import AppNavigator from './navigation/AppNavigator';
import combineProviders from './combineProviders';
// Good for performance @ https://reactnavigation.org/docs/community-libraries-and-navigators/#react-native-screens<
enableScreens();
// Initialize the Database
initDB();
// Initialize notifications
initNotifications();
// Initialize background tasks
initTasks();
// Initialize custom animations
initAnimations();
export default function App() {
const [theme, setTheme] = useState(null);
const [fontsLoaded] = useFonts({
'open-sans': require('./assets/fonts/OpenSans-Regular.ttf'),
'open-sans-bold': require('./assets/fonts/OpenSans-Bold.ttf'),
'open-sans-light': require('./assets/fonts/OpenSans-Light.ttf'),
'open-sans-medium': require('./assets/fonts/OpenSans-Medium.ttf'),
});
const scheme = useColorScheme();
useEffect(() => {
(async () => {
const themeFromStorage = await AsyncStorage.getItem('theme');
if (themeFromStorage) {
setTheme(themeFromStorage === themeNames.DARK ? CombinedDarkTheme : CombinedDefaultTheme);
return;
}
setTheme(scheme === themeNames.DARK ? CombinedDarkTheme : CombinedDefaultTheme);
})();
}, [scheme]);
const toggleTheme = useCallback(() => {
setTheme((currentTheme) => {
AsyncStorage.setItem('theme', currentTheme.dark ? themeNames.LIGHT : themeNames.DARK);
return currentTheme.dark ? CombinedDefaultTheme : CombinedDarkTheme;
});
}, [theme]);
const Providers = useMemo(
() =>
combineProviders([
{ name: PaperProvider, props: { theme } },
{ name: UserContextProvider, props: { toggleTheme } },
{ name: LocationContextProvider },
{ name: NavigationContainer, props: { theme } },
]),
[theme],
);
if (!fontsLoaded || !theme) return null;
return (
<>
<StatusBar style={theme.dark ? 'light' : 'dark'} />
<BatteryMonitor />
<Providers>
<AppNavigator />
</Providers>
</>
);
}