-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.tsx
211 lines (188 loc) · 5.11 KB
/
index.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
211
import * as React from 'react';
import { I18nManager, Platform } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import { Updates } from 'expo';
import { useKeepAwake } from 'expo-keep-awake';
import { InitialState, NavigationContainer } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import {
Provider as PaperProvider,
DarkTheme,
DefaultTheme,
} from 'react-native-paper';
import App from './RootNavigator';
import DrawerItems from './DrawerItems';
import { SafeAreaProvider } from 'react-native-safe-area-context';
// Add new typescript properties to the theme
declare global {
namespace ReactNativePaper {
interface ThemeFonts {
superLight: ThemeFont;
}
interface ThemeColors {
customColor: string;
}
interface ThemeAnimation {
customProperty: number;
}
interface Theme {
userDefinedThemeProperty: string;
}
}
}
const PERSISTENCE_KEY = 'NAVIGATION_STATE';
const PREFERENCES_KEY = 'APP_PREFERENCES';
const CustomDarkTheme: ReactNativePaper.Theme = {
...DarkTheme,
colors: {
...DarkTheme.colors,
customColor: '#BADA55',
},
fonts: {
...DarkTheme.fonts,
superLight: { ...DarkTheme.fonts['light'] },
},
userDefinedThemeProperty: '',
animation: {
...DarkTheme.animation,
customProperty: 1,
},
};
const CustomDefaultTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
customColor: '#BADA55',
},
fonts: {
...DefaultTheme.fonts,
superLight: { ...DefaultTheme.fonts['light'] },
},
userDefinedThemeProperty: '',
animation: {
...DefaultTheme.animation,
customProperty: 1,
},
};
const PreferencesContext = React.createContext<any>(null);
const DrawerContent = () => {
return (
<PreferencesContext.Consumer>
{(preferences) => (
<DrawerItems
toggleTheme={preferences.toggleTheme}
toggleRTL={preferences.toggleRtl}
isRTL={preferences.rtl}
isDarkTheme={preferences.theme.dark}
/>
)}
</PreferencesContext.Consumer>
);
};
const Drawer = createDrawerNavigator<{ Home: undefined }>();
export default function PaperExample() {
useKeepAwake();
const [isReady, setIsReady] = React.useState(false);
const [initialState, setInitialState] = React.useState<
InitialState | undefined
>();
const [theme, setTheme] = React.useState<ReactNativePaper.Theme>(
CustomDefaultTheme
);
const [rtl, setRtl] = React.useState<boolean>(I18nManager.isRTL);
React.useEffect(() => {
const restoreState = async () => {
try {
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
const state = JSON.parse(savedStateString || '');
setInitialState(state);
} catch (e) {
// ignore error
} finally {
setIsReady(true);
}
};
if (!isReady) {
restoreState();
}
}, [isReady]);
React.useEffect(() => {
const restorePrefs = async () => {
try {
const prefString = await AsyncStorage.getItem(PREFERENCES_KEY);
const preferences = JSON.parse(prefString || '');
if (preferences) {
// eslint-disable-next-line react/no-did-mount-set-state
setTheme(
preferences.theme === 'dark' ? CustomDarkTheme : CustomDefaultTheme
);
if (typeof preferences.rtl === 'boolean') {
setRtl(preferences.rtl);
}
}
} catch (e) {
// ignore error
}
};
restorePrefs();
}, []);
React.useEffect(() => {
const savePrefs = async () => {
try {
await AsyncStorage.setItem(
PREFERENCES_KEY,
JSON.stringify({
theme: theme === DarkTheme ? 'dark' : 'light',
rtl,
})
);
} catch (e) {
// ignore error
}
if (I18nManager.isRTL !== rtl) {
I18nManager.forceRTL(rtl);
Updates.reloadFromCache();
}
};
savePrefs();
}, [rtl, theme]);
const preferences = React.useMemo(
() => ({
toggleTheme: () =>
setTheme((theme) =>
theme === CustomDefaultTheme ? CustomDarkTheme : CustomDefaultTheme
),
toggleRtl: () => setRtl((rtl) => !rtl),
rtl,
theme,
}),
[rtl, theme]
);
if (!isReady) {
return null;
}
return (
<PaperProvider theme={theme}>
<SafeAreaProvider>
<PreferencesContext.Provider value={preferences}>
<React.Fragment>
<NavigationContainer
initialState={initialState}
onStateChange={(state) =>
AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state))
}
>
{Platform.OS === 'web' ? (
<App />
) : (
<Drawer.Navigator drawerContent={() => <DrawerContent />}>
<Drawer.Screen name="Home" component={App} />
</Drawer.Navigator>
)}
</NavigationContainer>
</React.Fragment>
</PreferencesContext.Provider>
</SafeAreaProvider>
</PaperProvider>
);
}