-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
94 lines (84 loc) · 2.16 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
import 'react-native-gesture-handler';
import React, { useState } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { Provider as PaperProvider, DefaultTheme, DarkTheme } from 'react-native-paper';
import { StatusBar } from 'expo-status-bar';
import Main from './components/Main';
import ThemeContext from './utils/SettingsContext';
import AsyncStorage from '@react-native-community/async-storage';
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
import { AppLoading } from 'expo';
function reducer(state: {
isDark: boolean,
keepAwake: boolean
}, action) {
if (action.type == "TOGGLE_DARK_MODE") {
return {
isDark: action.value,
keepAwake: state.keepAwake
}
} else if (action.type == "TOGGLE_KEEP_AWAKE") {
if (action.value) {
activateKeepAwake();
} else {
deactivateKeepAwake();
}
return {
isDark: state.isDark,
keepAwake: action.value
};
}
}
export default function App() {
const [state, dispatch] = React.useReducer(reducer, {
isDark: false,
keepAwake: false
});
const [isLoading, setIsLoading] = useState(true);
async function loadSettings() {
let theme = await AsyncStorage.getItem("@theme")
if (theme == "dark" && !state.isDark) {
dispatch({ type: "TOGGLE_DARK_MODE", value: true })
}
let keepAwake = await AsyncStorage.getItem("@keepAwake")
if (keepAwake == "on" && !state.keepAwake) {
dispatch({ type: "TOGGLE_KEEP_AWAKE", value: false })
}
}
if (isLoading) {
return (
<AppLoading
startAsync={loadSettings}
onFinish={() => setIsLoading(false)}
onError={console.warn}
/>
);
}
return (
<ThemeContext.Provider value={{ state, dispatch }}>
<PaperProvider theme={state.isDark ? darkTheme : lightTheme}>
<StatusBar style={state.isDark ? "light" : "dark"} />
<NavigationContainer>
<Main />
</NavigationContainer>
</PaperProvider>
</ThemeContext.Provider>
);
}
const lightTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
primary: '#fad196',
},
};
const darkTheme = {
...DarkTheme,
colors: {
...DarkTheme.colors,
primary: '#202020',
surface: '#202020',
text: '#fff',
onSurface: '#fff'
},
};