|
1 | 1 | /**
|
2 |
| - * |
3 | 2 | * @format
|
4 | 3 | * @flow
|
5 | 4 | */
|
6 | 5 |
|
7 |
| -import React from 'react'; |
| 6 | +import React, {useState} from 'react'; |
8 | 7 | import {
|
9 | 8 | StyleSheet,
|
10 | 9 | View,
|
11 | 10 | SafeAreaView,
|
12 |
| - Button, |
13 | 11 | Text,
|
14 |
| - TouchableOpacity, |
| 12 | + Switch, |
| 13 | + TextInput, |
15 | 14 | } from 'react-native';
|
16 | 15 |
|
17 |
| -import {Colors} from 'react-native/Libraries/NewAppScreen'; |
| 16 | +import LegacyStorage from './src/legacy'; |
| 17 | +import Button from './src/components/Button'; |
| 18 | +import LegacyAsyncStorage from '@react-native-community/async-storage-backend-legacy'; |
18 | 19 |
|
19 |
| -import LegacyExample from './src/legacy'; |
20 |
| - |
21 |
| -const Examples = { |
22 |
| - legacy: { |
23 |
| - title: 'Legacy', |
24 |
| - screen: LegacyExample, |
25 |
| - }, |
| 20 | +const storagesAvailable = { |
| 21 | + Legacy: LegacyStorage, |
26 | 22 | };
|
27 | 23 |
|
28 |
| -class AsyncStorageExampleApp extends React.Component { |
29 |
| - state = { |
30 |
| - page: Examples.legacy, |
31 |
| - }; |
| 24 | +function AsyncStorageExampleApp() { |
| 25 | + const [selectedStorageName, updateStorage] = useState('Legacy'); |
| 26 | + const [multiValueMode, updateMultiValueMode] = useState(false); |
| 27 | + const [key, updateKey] = useState(''); |
| 28 | + const [value, updateValue] = useState(''); |
| 29 | + const [savedKeys, updatedSavedKeys] = useState([]); |
| 30 | + |
| 31 | + const storage = storagesAvailable[selectedStorageName]; |
| 32 | + |
| 33 | + async function setValue() { |
| 34 | + if (multiValueMode) { |
| 35 | + const keys = key.split(',').map(k => k.trim()); |
| 36 | + const values = value.split(',').map(v => v.trim()); |
| 37 | + |
| 38 | + const keyValues = keys.map((k, index) => ({[k]: values[index]})); |
| 39 | + await storage.setMultiple(keyValues); |
| 40 | + } else { |
| 41 | + await storage.set(key, value); |
| 42 | + } |
| 43 | + } |
| 44 | + async function readValue() { |
| 45 | + if (multiValueMode) { |
| 46 | + const keys = key.split(',').map(k => k.trim()); |
| 47 | + |
| 48 | + const values = await storage.getMultiple(keys); |
32 | 49 |
|
33 |
| - render() { |
34 |
| - const Example = this.state.page; |
| 50 | + const val = Object.keys(values).map(k => values[k]); |
| 51 | + updateValue(val.join(', ')); |
| 52 | + } else { |
| 53 | + const val = await storage.get(key); |
| 54 | + updateValue(val); |
| 55 | + } |
| 56 | + } |
| 57 | + async function deleteValue() { |
| 58 | + if (multiValueMode) { |
| 59 | + const keys = key.split(',').map(k => k.trim()); |
| 60 | + await storage.removeMultiple(keys); |
| 61 | + } else { |
| 62 | + await storage.remove(key); |
| 63 | + } |
| 64 | + } |
| 65 | + async function getKeys() { |
| 66 | + const keys = await storage.getKeys(); |
| 67 | + updatedSavedKeys(keys || []); |
| 68 | + } |
| 69 | + async function drop() { |
| 70 | + await storage.clearStorage(); |
| 71 | + } |
35 | 72 |
|
36 |
| - return ( |
37 |
| - <SafeAreaView style={styles.sectionContainer}> |
38 |
| - <TouchableOpacity |
39 |
| - style={styles.resetButton} |
40 |
| - onPress={() => { |
41 |
| - this.setState({page: null}); |
42 |
| - }}> |
43 |
| - <Text style={{color: '#fff'}}>Reset</Text> |
44 |
| - </TouchableOpacity> |
| 73 | + const buttons = [ |
| 74 | + { |
| 75 | + name: multiValueMode ? 'get many' : 'get single', |
| 76 | + func: readValue, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: multiValueMode ? 'save many' : 'save single', |
| 80 | + func: setValue, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: multiValueMode ? 'delete many' : 'delete single', |
| 84 | + func: deleteValue, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'get keys', |
| 88 | + func: getKeys, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: 'clear', |
| 92 | + func: drop, |
| 93 | + }, |
| 94 | + ]; |
45 | 95 |
|
46 |
| - <View style={styles.buttonContainer}> |
47 |
| - {Object.keys(Examples).map(pageKey => { |
48 |
| - const example = Examples[pageKey]; |
49 |
| - return ( |
50 |
| - <View style={styles.sectionButton} key={example.title}> |
| 96 | + return ( |
| 97 | + <SafeAreaView style={styles.flexOne}> |
| 98 | + <View style={styles.container}> |
| 99 | + <Text style={styles.header}>Async Storage - Mobile example</Text> |
| 100 | + <View style={[styles.flexOne, styles.optionsContainer]}> |
| 101 | + <View style={styles.alignCenter}> |
| 102 | + <Text>Multi-value mode:</Text> |
| 103 | + <Switch |
| 104 | + style={styles.switch} |
| 105 | + onValueChange={updateMultiValueMode} |
| 106 | + value={multiValueMode} |
| 107 | + /> |
| 108 | + </View> |
| 109 | + <View style={styles.optionsButtonContainer}> |
| 110 | + {Object.keys(storagesAvailable).map(storageName => { |
| 111 | + return ( |
51 | 112 | <Button
|
52 |
| - title={example.title} |
53 |
| - onPress={() => { |
54 |
| - this.setState({page: example}); |
55 |
| - }} |
| 113 | + key={storageName} |
| 114 | + title={storageName} |
| 115 | + onPress={() => updateStorage(storageName)} |
| 116 | + active={storageName === selectedStorageName} |
56 | 117 | />
|
57 |
| - </View> |
58 |
| - ); |
59 |
| - })} |
| 118 | + ); |
| 119 | + })} |
| 120 | + </View> |
60 | 121 | </View>
|
61 |
| - {Example ? ( |
62 |
| - <> |
63 |
| - <Text style={styles.sectionTitle}>{Example.title} Example</Text> |
64 |
| - <Example.screen /> |
65 |
| - </> |
66 |
| - ) : ( |
67 |
| - <Text>Please select an example</Text> |
68 |
| - )} |
69 |
| - </SafeAreaView> |
70 |
| - ); |
71 |
| - } |
| 122 | + </View> |
| 123 | + <View style={styles.inputsContainer}> |
| 124 | + {multiValueMode ? ( |
| 125 | + <Text>Note: keys and values should be separated by a coma</Text> |
| 126 | + ) : null} |
| 127 | + <TextInput |
| 128 | + autoCapitalize="none" |
| 129 | + placeholder={multiValueMode ? 'keys' : 'key'} |
| 130 | + style={styles.input} |
| 131 | + onChangeText={text => updateKey(text)} |
| 132 | + value={key} |
| 133 | + /> |
| 134 | + <TextInput |
| 135 | + autoCapitalize="none" |
| 136 | + placeholder={multiValueMode ? 'values' : 'value'} |
| 137 | + style={styles.input} |
| 138 | + onChangeText={text => updateValue(text)} |
| 139 | + value={value} |
| 140 | + /> |
| 141 | + </View> |
| 142 | + <View style={styles.savedKeysContainer}> |
| 143 | + <Text>Saved keys: {savedKeys.join(', ')}</Text> |
| 144 | + </View> |
| 145 | + <View style={styles.buttonsContainer}> |
| 146 | + {buttons.map(({name, func}) => ( |
| 147 | + <Button key={name} title={name} onPress={func} active /> |
| 148 | + ))} |
| 149 | + </View> |
| 150 | + </SafeAreaView> |
| 151 | + ); |
72 | 152 | }
|
73 | 153 |
|
74 | 154 | const styles = StyleSheet.create({
|
75 |
| - sectionContainer: { |
76 |
| - marginTop: 16, |
77 |
| - alignItems: 'center', |
| 155 | + flexOne: { |
78 | 156 | flex: 1,
|
79 |
| - paddingHorizontal: 16, |
80 | 157 | },
|
81 |
| - buttonContainer: { |
82 |
| - width: '100%', |
| 158 | + alignCenter: { |
| 159 | + alignItems: 'center', |
83 | 160 | flexDirection: 'row',
|
| 161 | + width: '100%', |
| 162 | + }, |
| 163 | + container: { |
| 164 | + marginTop: 16, |
| 165 | + paddingHorizontal: 12, |
| 166 | + flex: 1 / 4, |
| 167 | + }, |
| 168 | + header: { |
| 169 | + fontSize: 18, |
| 170 | + color: '#020202', |
84 | 171 | marginVertical: 12,
|
85 |
| - justifyContent: 'flex-start', |
86 | 172 | },
|
87 |
| - sectionTitle: { |
88 |
| - fontSize: 24, |
89 |
| - marginBottom: 8, |
90 |
| - fontWeight: '600', |
91 |
| - color: Colors.black, |
| 173 | + optionsContainer: { |
| 174 | + flexDirection: 'row', |
| 175 | + flexWrap: 'wrap', |
| 176 | + }, |
| 177 | + optionsButtonContainer: { |
| 178 | + marginTop: 8, |
| 179 | + flexDirection: 'row', |
| 180 | + justifyContent: 'space-around', |
| 181 | + alignItems: 'center', |
| 182 | + }, |
| 183 | + switch: { |
| 184 | + marginLeft: 10, |
| 185 | + }, |
| 186 | + savedKeysContainer: { |
| 187 | + paddingHorizontal: 12, |
| 188 | + marginVertical: 10, |
| 189 | + }, |
| 190 | + inputsContainer: { |
| 191 | + flex: 1 / 4, |
| 192 | + paddingHorizontal: 12, |
92 | 193 | },
|
93 |
| - sectionButton: { |
94 |
| - marginRight: 15, |
| 194 | + input: { |
| 195 | + backgroundColor: '#e8e8e8', |
| 196 | + marginVertical: 8, |
| 197 | + padding: 8, |
| 198 | + borderRadius: 4, |
95 | 199 | },
|
96 |
| - resetButton: { |
97 |
| - backgroundColor: '#ffb340', |
98 |
| - paddingHorizontal: 8, |
99 |
| - paddingVertical: 4, |
100 |
| - elevation: 3, |
101 |
| - alignSelf: 'flex-end', |
| 200 | + buttonsContainer: { |
| 201 | + flex: 2 / 4, |
| 202 | + justifyContent: 'center', |
| 203 | + paddingHorizontal: 12, |
102 | 204 | },
|
103 | 205 | });
|
104 | 206 |
|
|
0 commit comments