-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathindex.native.tsx
More file actions
207 lines (183 loc) · 6.19 KB
/
index.native.tsx
File metadata and controls
207 lines (183 loc) · 6.19 KB
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
import * as React from 'react';
import { I18nManager } from 'react-native';
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { InitialState, NavigationContainer } from '@react-navigation/native';
import { useFonts } from 'expo-font';
import { useKeepAwake } from 'expo-keep-awake';
import { StatusBar } from 'expo-status-bar';
import * as Updates from 'expo-updates';
import { PaperProvider, MD3DarkTheme, MD3LightTheme } from 'react-native-paper';
import { SafeAreaInsetsContext } from 'react-native-safe-area-context';
import DrawerItems from './DrawerItems';
import { PreferencesContext } from './PreferencesContext';
import App from './RootNavigator';
import { deviceColorsSupported } from '../utils';
import {
CombinedDefaultTheme,
CombinedDarkTheme,
createConfiguredFontTheme,
createConfiguredFontNavigationTheme,
} from '../utils/themes';
const PERSISTENCE_KEY = 'NAVIGATION_STATE';
const PREFERENCES_KEY = 'APP_PREFERENCES';
const Drawer = createDrawerNavigator<{ Home: undefined }>();
export default function PaperExample() {
useKeepAwake();
const [fontsLoaded] = useFonts({
Abel: require('../assets/fonts/Abel-Regular.ttf'),
});
const [isReady, setIsReady] = React.useState(false);
const [initialState, setInitialState] = React.useState<
InitialState | undefined
>();
const [shouldUseDeviceColors, setShouldUseDeviceColors] =
React.useState(true);
const [isDarkMode, setIsDarkMode] = React.useState(false);
const [rtl, setRtl] = React.useState<boolean>(
I18nManager.getConstants().isRTL
);
const [collapsed, setCollapsed] = React.useState(false);
const [customFontLoaded, setCustomFont] = React.useState(false);
const [rippleEffectEnabled, setRippleEffectEnabled] = React.useState(true);
const { theme: mdTheme } = useMaterial3Theme();
const theme = React.useMemo(() => {
if (!deviceColorsSupported || !shouldUseDeviceColors) {
return isDarkMode ? MD3DarkTheme : MD3LightTheme;
}
return isDarkMode
? { ...MD3DarkTheme, colors: { ...MD3DarkTheme.colors, ...mdTheme.dark } }
: {
...MD3LightTheme,
colors: { ...MD3LightTheme.colors, ...mdTheme.light },
};
}, [isDarkMode, mdTheme, shouldUseDeviceColors]);
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) {
setIsDarkMode(preferences.theme === 'dark');
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: isDarkMode ? 'dark' : 'light',
rtl,
})
);
} catch (e) {
// ignore error
}
if (I18nManager.getConstants().isRTL !== rtl) {
I18nManager.forceRTL(rtl);
Updates.reloadAsync();
}
};
savePrefs();
}, [rtl, isDarkMode]);
const preferences = React.useMemo(
() => ({
toggleShouldUseDeviceColors: () =>
setShouldUseDeviceColors((oldValue) => !oldValue),
toggleTheme: () => setIsDarkMode((oldValue) => !oldValue),
toggleRtl: () => setRtl((rtl) => !rtl),
toggleCollapsed: () => setCollapsed(!collapsed),
toggleCustomFont: () => setCustomFont(!customFontLoaded),
toggleRippleEffect: () => setRippleEffectEnabled(!rippleEffectEnabled),
customFontLoaded,
rippleEffectEnabled,
collapsed,
rtl,
theme,
shouldUseDeviceColors,
}),
[
rtl,
theme,
collapsed,
customFontLoaded,
shouldUseDeviceColors,
rippleEffectEnabled,
]
);
if (!isReady && !fontsLoaded) {
return null;
}
const combinedTheme = isDarkMode ? CombinedDarkTheme : CombinedDefaultTheme;
const configuredFontTheme = createConfiguredFontTheme(combinedTheme);
const configuredFontNavigationTheme =
createConfiguredFontNavigationTheme(combinedTheme);
return (
<PaperProvider
settings={{ rippleEffectEnabled: preferences.rippleEffectEnabled }}
theme={customFontLoaded ? configuredFontTheme : theme}
>
<PreferencesContext.Provider value={preferences}>
<NavigationContainer
theme={
customFontLoaded ? configuredFontNavigationTheme : combinedTheme
}
initialState={initialState}
onStateChange={(state) =>
AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state))
}
>
<SafeAreaInsetsContext.Consumer>
{(insets) => {
const { left, right } = insets || { left: 0, right: 0 };
const collapsedDrawerWidth = 100 + Math.max(left, right);
return (
<Drawer.Navigator
screenOptions={{
drawerStyle: collapsed && {
width: collapsedDrawerWidth,
},
}}
drawerContent={() => <DrawerItems />}
>
<Drawer.Screen
name="Home"
component={App}
options={{ headerShown: false }}
/>
</Drawer.Navigator>
);
}}
</SafeAreaInsetsContext.Consumer>
<StatusBar style={!theme.isV3 || theme.dark ? 'light' : 'dark'} />
</NavigationContainer>
</PreferencesContext.Provider>
</PaperProvider>
);
}