1
1
import React from 'react' ;
2
- import { View , Text , Button , StyleSheet , ScrollView , TextInput , NativeModules } from 'react-native' ;
2
+ import { View , Text , Button , StyleSheet , ScrollView , TextInput } from 'react-native' ;
3
3
import AsyncStorage from '@react-native-async-storage/async-storage' ;
4
4
5
- const RCTAsyncStorage = NativeModules . RNC_AsyncSQLiteDBStorage ||
6
- NativeModules . RNCAsyncStorage ;
7
5
6
+ const mergeInitialValue = {
7
+ initial : 'keep' ,
8
+ override1 : 'override' ,
9
+ nested : {
10
+ nestedValue : 'keep' ,
11
+ override2 : 'override' ,
12
+ deeper : {
13
+ deeperValue : 'keep' ,
14
+ override3 : 'override' ,
15
+ } ,
16
+ } ,
17
+ } ;
8
18
9
19
function NextExample ( ) {
10
20
const [ keys , setKeys ] = React . useState ( [ ] ) ;
11
21
const [ error , setError ] = React . useState ( null ) ;
12
22
const [ inputKey , setInputKey ] = React . useState ( ) ;
13
23
const [ inputValue , setInputValue ] = React . useState ( ) ;
14
24
const [ value , setValue ] = React . useState ( ) ;
25
+ const [ mergedValue , setMergedValue ] = React . useState ( ) ;
26
+ const [ overrideValue , setOverrideValue ] = React . useState ( {
27
+ override1 : '' ,
28
+ override2 : '' ,
29
+ override3 : '' ,
30
+ } ) ;
15
31
16
32
17
33
function runWithCatch ( block ) {
@@ -59,14 +75,58 @@ function NextExample() {
59
75
await AsyncStorage . clear ( ) ;
60
76
}
61
77
78
+ async function resetMergedValue ( ) {
79
+ await AsyncStorage . setItem ( 'MERGER' , JSON . stringify ( mergeInitialValue ) ) ;
80
+ const saved = await AsyncStorage . getItem ( 'MERGER' ) ;
81
+ setMergedValue ( JSON . parse ( saved ) ) ;
82
+ }
83
+
84
+ async function readMergedValue ( ) {
85
+ const saved = await AsyncStorage . getItem ( 'MERGER' ) ;
86
+ setMergedValue ( saved ? JSON . parse ( saved ) : { } ) ;
87
+ }
88
+
89
+ async function mergeValues ( ) {
90
+ const { override1, override2, override3} = overrideValue ;
91
+
92
+ // leave out empty inputs
93
+ const toMerge = { } ;
94
+ if ( override1 ) {
95
+ toMerge . override1 = override1 ;
96
+ }
97
+ if ( override2 ) {
98
+ toMerge . nested = {
99
+ override2 : override2 ,
100
+ } ;
101
+ }
102
+ if ( override3 ) {
103
+ if ( ! toMerge . nested ) {
104
+ toMerge . nested = {
105
+ deeper : {
106
+ override3 : override3 ,
107
+ } ,
108
+ } ;
109
+ } else {
110
+ toMerge . nested . deeper = {
111
+ override3 : override3 ,
112
+ } ;
113
+ }
114
+ }
115
+
116
+
117
+ await AsyncStorage . mergeItem ( 'MERGER' , JSON . stringify ( toMerge ) ) ;
118
+ }
119
+
120
+
62
121
return < ScrollView contentContainerStyle = { { flexGrow : 1 } } >
63
122
64
- { error ? < Text style = { { fontSize : 18 , color : 'red' } } > { error } </ Text > : null }
123
+ { error ? < Text style = { styles . error } > { error } </ Text > : null }
65
124
66
125
< View style = { styles . example } >
126
+ < Text style = { styles . title } > Basic operations</ Text >
67
127
< TextInput onChangeText = { setInputKey } value = { inputKey } style = { styles . input } placeholder = "key" />
68
128
< TextInput onChangeText = { setInputValue } value = { inputValue } style = { styles . input } placeholder = "value" />
69
- < View style = { { flexDirection : ' row' , justifyContent : 'space-around' } } >
129
+ < View style = { styles . row } >
70
130
< Button title = "Read" onPress = { runWithCatch ( readValue ) } />
71
131
< Button title = "Save" onPress = { runWithCatch ( saveValue ) } />
72
132
< Button title = "Delete" onPress = { runWithCatch ( removeValue ) } />
@@ -75,24 +135,54 @@ function NextExample() {
75
135
</ View >
76
136
77
137
< View style = { styles . example } >
78
- < Text style = { { fontSize : 16 , fontWeight : '700' } } > Crash scenarios</ Text >
79
- < View style = { { flexDirection : ' row' , justifyContent : 'space-around' } } >
138
+ < Text style = { styles . title } > Crash scenarios</ Text >
139
+ < View style = { styles . row } >
80
140
< Button title = "Key null" onPress = { runWithCatch ( crashKeyNull ) } />
81
141
< Button title = "Key not string" onPress = { runWithCatch ( crashKeyNotString ) } />
82
142
< Button title = "Wrong value type" onPress = { runWithCatch ( crashValueType ) } />
83
143
</ View >
84
144
</ View >
85
145
86
146
< View style = { styles . example } >
87
- < View style = { { flexDirection : 'row' , justifyContent : 'space-around' } } >
147
+ < Text style = { styles . title } > Merging</ Text >
148
+ < View style = { styles . row } >
149
+ < Text > { `Value:\n\n${ JSON . stringify ( mergedValue , null , 2 ) } ` } </ Text >
150
+ < Text > { `Merge with:\n\n${ JSON . stringify ( overrideValue , null , 2 ) } ` } </ Text >
151
+ </ View >
152
+ < View style = { styles . row } >
153
+ < Button title = "Read" onPress = { runWithCatch ( readMergedValue ) } />
154
+ < Button title = "Reset" onPress = { runWithCatch ( resetMergedValue ) } />
155
+ < Button title = "Merge" onPress = { runWithCatch ( mergeValues ) } />
156
+ </ View >
157
+ < View >
158
+ < TextInput
159
+ onChangeText = { t => setOverrideValue ( c => ( { ...c , override1 : t } ) ) }
160
+ value = { overrideValue . override1 }
161
+ style = { styles . input } placeholder = "override1"
162
+ />
163
+ < TextInput
164
+ onChangeText = { t => setOverrideValue ( c => ( { ...c , override2 : t } ) ) }
165
+ value = { overrideValue . override2 }
166
+ style = { styles . input } placeholder = "override2"
167
+ />
168
+ < TextInput
169
+ onChangeText = { t => setOverrideValue ( c => ( { ...c , override3 : t } ) ) }
170
+ value = { overrideValue . override3 }
171
+ style = { styles . input } placeholder = "override3"
172
+ />
173
+ </ View >
174
+ </ View >
175
+
176
+ < View style = { styles . example } >
177
+ < Text style = { styles . title } > Display all keys</ Text >
178
+ < View style = { styles . row } >
88
179
< Button title = "Get all keys" onPress = { runWithCatch ( getAllKeys ) } />
89
180
</ View >
90
- < Text style = { { fontWeight : '700' } } > Keys:</ Text >
91
181
< Text > { keys . join ( ', ' ) } </ Text >
92
182
</ View >
93
183
94
184
< View style = { styles . example } >
95
- < Text style = { { fontSize : 16 } } > Clear database entries</ Text >
185
+ < Text style = { styles . title } > Clear database entries</ Text >
96
186
< Button title = "clear" onPress = { runWithCatch ( clearDb ) } />
97
187
</ View >
98
188
</ ScrollView > ;
@@ -101,7 +191,8 @@ function NextExample() {
101
191
102
192
const styles = StyleSheet . create ( {
103
193
example : {
104
- paddingVertical : 24 ,
194
+ paddingBottom : 24 ,
195
+ paddingTop : 8 ,
105
196
borderBottomWidth : 1 ,
106
197
borderBottomColor : '#e3e3e3' ,
107
198
borderStyle : 'solid' ,
@@ -112,6 +203,19 @@ const styles = StyleSheet.create({
112
203
borderColor : '#eee' ,
113
204
borderStyle : 'solid' ,
114
205
} ,
206
+ error : {
207
+ fontSize : 18 ,
208
+ color : 'red' ,
209
+ } ,
210
+ row : {
211
+ flexDirection : 'row' ,
212
+ justifyContent : 'space-around' ,
213
+ } ,
214
+ title : {
215
+ fontSize : 16 ,
216
+ fontWeight : '700' ,
217
+ paddingBottom : 12 ,
218
+ } ,
115
219
} ) ;
116
220
117
221
0 commit comments