forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
129 lines (116 loc) · 3.16 KB
/
App.js
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
/* @flow */
import { KeepAwake, Util } from 'expo';
import * as React from 'react';
import { StatusBar, I18nManager, AsyncStorage, Platform } from 'react-native';
import {
Provider as PaperProvider,
DarkTheme,
DefaultTheme,
} from 'react-native-paper';
import createReactContext from 'create-react-context';
import { createDrawerNavigator } from 'react-navigation';
import RootNavigator from './src/RootNavigator';
import DrawerItems from './DrawerItems';
import type { Theme } from 'react-native-paper/types';
type State = {
theme: Theme,
rtl: boolean,
};
const PreferencesContext: any = createReactContext();
const App = createDrawerNavigator(
{ Home: { screen: RootNavigator } },
{
contentComponent: () => (
<PreferencesContext.Consumer>
{preferences => (
<DrawerItems
toggleTheme={preferences.theme}
toggleRTL={preferences.rtl}
isRTL={preferences.isRTL}
isDarkTheme={preferences.isDarkTheme}
/>
)}
</PreferencesContext.Consumer>
),
// set drawerPosition to support rtl toggle on android
drawerPosition:
Platform.OS === 'android' && (I18nManager.isRTL ? 'right' : 'left'),
}
);
export default class PaperExample extends React.Component<{}, State> {
state = {
theme: DefaultTheme,
rtl: I18nManager.isRTL,
};
async componentDidMount() {
StatusBar.setBarStyle('light-content');
try {
const prefString = await AsyncStorage.getItem('preferences');
const preferences = JSON.parse(prefString);
if (preferences) {
// eslint-disable-next-line react/no-did-mount-set-state
this.setState(state => ({
theme: preferences.theme === 'dark' ? DarkTheme : DefaultTheme,
rtl:
typeof preferences.rtl === 'boolean' ? preferences.rtl : state.rtl,
}));
}
} catch (e) {
// ignore error
}
}
_savePreferences = async () => {
try {
AsyncStorage.setItem(
'preferences',
JSON.stringify({
theme: this.state.theme === DarkTheme ? 'dark' : 'light',
rtl: this.state.rtl,
})
);
} catch (e) {
// ignore error
}
};
_toggleTheme = () =>
this.setState(
state => ({
theme: state.theme === DarkTheme ? DefaultTheme : DarkTheme,
}),
this._savePreferences
);
_toggleRTL = () =>
this.setState(
state => ({
rtl: !state.rtl,
}),
async () => {
await this._savePreferences();
I18nManager.forceRTL(this.state.rtl);
Util.reload();
}
);
render() {
return (
<PaperProvider theme={this.state.theme}>
<PreferencesContext.Provider
value={{
theme: this._toggleTheme,
rtl: this._toggleRTL,
isRTL: this.state.rtl,
isDarkTheme: this.state.theme === DarkTheme,
}}
>
<App
persistenceKey={
process.env.NODE_ENV !== 'production'
? 'NavigationStateDEV'
: null
}
/>
</PreferencesContext.Provider>
<KeepAwake />
</PaperProvider>
);
}
}