|
1 |
| -/* @flow */ |
2 |
| -import * as React from 'react'; |
3 |
| -import { StyleSheet, NavigatorIOS, StatusBar } from 'react-native'; |
4 |
| -import { ThemeProvider, DefaultTheme } from 'react-native-ios-kit'; |
| 1 | +// @flow |
| 2 | +import React, { useState, useEffect, useCallback } from 'react'; |
| 3 | +import { AsyncStorage, StatusBar } from 'react-native'; |
| 4 | +import { |
| 5 | + ThemeProvider, |
| 6 | + DefaultTheme, |
| 7 | + DarkTheme, |
| 8 | + Button, |
| 9 | +} from 'react-native-ios-kit'; |
5 | 10 | import type { Theme } from 'react-native-ios-kit/types';
|
| 11 | +import { NavigationContainer } from '@react-navigation/native'; |
6 | 12 |
|
7 |
| -import ExampleList from './src/ExampleList'; |
| 13 | +import { createStackNavigator } from '@react-navigation/stack'; |
| 14 | +import { SafeAreaProvider } from 'react-native-safe-area-context'; |
| 15 | +import ExampleList, { examples } from './src/ExampleList'; |
8 | 16 | import ThemeSelector from './src/ThemeSelector';
|
9 | 17 |
|
10 |
| -type Props = { |
11 |
| - navigator: Object, |
12 |
| -}; |
| 18 | +const Stack = createStackNavigator(); |
13 | 19 |
|
14 |
| -type State = { |
15 |
| - selectedTheme: Theme, |
16 |
| -}; |
| 20 | +const PREFERENCES_KEY = 'APP_PREFERENCES'; |
17 | 21 |
|
18 |
| -export default class App extends React.Component<Props, State> { |
19 |
| - state = { |
20 |
| - selectedTheme: DefaultTheme, |
21 |
| - }; |
22 |
| - _nav: ?Object; |
23 |
| - openThemesScreen = () => { |
24 |
| - if (this._nav) { |
25 |
| - this._nav.push({ |
26 |
| - component: ThemeSelector, |
27 |
| - title: 'Select Theme', |
28 |
| - passProps: { |
29 |
| - selectTheme: this.selectTheme, |
30 |
| - selectedTheme: this.state.selectedTheme, |
31 |
| - }, |
32 |
| - }); |
33 |
| - } |
| 22 | +export default function App() { |
| 23 | + const [theme, setTheme] = useState<Theme>(DefaultTheme); |
| 24 | + |
| 25 | + const selectTheme = (selectedTheme: 'dark' | 'light') => { |
| 26 | + setTheme(selectedTheme === 'dark' ? DarkTheme : DefaultTheme); |
34 | 27 | };
|
35 | 28 |
|
36 |
| - selectTheme = (theme: Theme) => { |
37 |
| - this.setState({ selectedTheme: theme }); |
| 29 | + const openThemesScreen = useCallback(navigation => { |
| 30 | + navigation.navigate('ThemeSelector', { |
| 31 | + selectTheme, |
| 32 | + }); |
| 33 | + }, []); |
| 34 | + |
| 35 | + useEffect(() => { |
38 | 36 | StatusBar.setBarStyle(
|
39 | 37 | theme === DefaultTheme ? 'dark-content' : 'light-content'
|
40 | 38 | );
|
41 |
| - }; |
| 39 | + }, [theme]); |
42 | 40 |
|
43 |
| - render() { |
44 |
| - const { selectedTheme } = this.state; |
45 |
| - const { |
46 |
| - barColor, |
47 |
| - primaryColor, |
48 |
| - textColor, |
49 |
| - backgroundColor, |
50 |
| - } = selectedTheme; |
51 |
| - return ( |
52 |
| - <ThemeProvider theme={selectedTheme}> |
53 |
| - <NavigatorIOS |
54 |
| - ref={ref => { |
55 |
| - this._nav = ref; |
56 |
| - }} |
57 |
| - initialRoute={{ |
58 |
| - component: ExampleList, |
59 |
| - title: 'Example', |
60 |
| - rightButtonTitle: 'Theme', |
61 |
| - onRightButtonPress: this.openThemesScreen, |
62 |
| - passProps: { theme: selectedTheme }, |
63 |
| - }} |
64 |
| - style={styles.container} |
65 |
| - itemWrapperStyle={[styles.scene, { backgroundColor }]} |
66 |
| - barTintColor={barColor} |
67 |
| - tintColor={primaryColor} |
68 |
| - titleTextColor={textColor} |
69 |
| - /> |
70 |
| - </ThemeProvider> |
71 |
| - ); |
72 |
| - } |
73 |
| -} |
| 41 | + useEffect(() => { |
| 42 | + const restorePrefs = async () => { |
| 43 | + try { |
| 44 | + const prefString = await AsyncStorage.getItem(PREFERENCES_KEY); |
| 45 | + const preferences = JSON.parse(prefString || ''); |
| 46 | + if (preferences) { |
| 47 | + setTheme(preferences.theme === 'dark' ? DarkTheme : DefaultTheme); |
| 48 | + } |
| 49 | + } catch (e) { |
| 50 | + // ignore error |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + restorePrefs(); |
| 55 | + }, []); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + const savePrefs = async () => { |
| 59 | + try { |
| 60 | + await AsyncStorage.setItem( |
| 61 | + PREFERENCES_KEY, |
| 62 | + JSON.stringify({ |
| 63 | + theme: theme === DarkTheme ? 'dark' : 'light', |
| 64 | + }) |
| 65 | + ); |
| 66 | + } catch (e) { |
| 67 | + // ignore error |
| 68 | + } |
| 69 | + }; |
74 | 70 |
|
75 |
| -const styles = StyleSheet.create({ |
76 |
| - container: { |
77 |
| - flex: 1, |
78 |
| - }, |
79 |
| - scene: { |
80 |
| - paddingTop: 64, |
81 |
| - }, |
82 |
| -}); |
| 71 | + savePrefs(); |
| 72 | + }, [theme]); |
| 73 | + |
| 74 | + return ( |
| 75 | + <ThemeProvider theme={theme}> |
| 76 | + <SafeAreaProvider> |
| 77 | + <NavigationContainer> |
| 78 | + <Stack.Navigator |
| 79 | + screenOptions={{ |
| 80 | + headerStyle: { |
| 81 | + backgroundColor: theme.barColor, |
| 82 | + }, |
| 83 | + headerTitleStyle: { |
| 84 | + color: theme.textColor, |
| 85 | + }, |
| 86 | + cardStyle: { |
| 87 | + backgroundColor: theme.backgroundColor, |
| 88 | + }, |
| 89 | + }} |
| 90 | + > |
| 91 | + <Stack.Screen |
| 92 | + name="React Native iOS Kit" |
| 93 | + component={ExampleList} |
| 94 | + options={({ navigation }) => ({ |
| 95 | + headerRight: () => ( |
| 96 | + <Button |
| 97 | + color="#007aff" |
| 98 | + onPress={() => openThemesScreen(navigation)} |
| 99 | + style={{ paddingRight: 15 }} |
| 100 | + > |
| 101 | + Theme |
| 102 | + </Button> |
| 103 | + ), |
| 104 | + })} |
| 105 | + /> |
| 106 | + <Stack.Screen name="ThemeSelector" component={ThemeSelector} /> |
| 107 | + {examples.map(example => ( |
| 108 | + <Stack.Screen |
| 109 | + key={example.title} |
| 110 | + name={example.title} |
| 111 | + component={example.component} |
| 112 | + /> |
| 113 | + ))} |
| 114 | + </Stack.Navigator> |
| 115 | + </NavigationContainer> |
| 116 | + </SafeAreaProvider> |
| 117 | + </ThemeProvider> |
| 118 | + ); |
| 119 | +} |
0 commit comments