23
23
public class FirestackUtils {
24
24
private static final String TAG = "FirestackUtils" ;
25
25
26
- // TODO NOTE
26
+ // TODO NOTE
27
27
public static void todoNote (final String tag , final String name , final Callback callback ) {
28
28
Log .e (tag , "The method " + name + " has not yet been implemented." );
29
29
Log .e (tag , "Feel free to contribute to finish the method in the source." );
@@ -34,11 +34,11 @@ public static void todoNote(final String tag, final String name, final Callback
34
34
}
35
35
36
36
/**
37
- * send a JS event
38
- **/
37
+ * send a JS event
38
+ **/
39
39
public static void sendEvent (final ReactContext context ,
40
- final String eventName ,
41
- final WritableMap params ) {
40
+ final String eventName ,
41
+ final WritableMap params ) {
42
42
if (context .hasActiveCatalystInstance ()) {
43
43
context
44
44
.getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
@@ -49,57 +49,61 @@ public static void sendEvent(final ReactContext context,
49
49
}
50
50
51
51
// snapshot
52
- public static WritableMap dataSnapshotToMap (String name ,
53
- String path ,
54
- DataSnapshot dataSnapshot ) {
55
- WritableMap data = Arguments .createMap ();
56
-
57
- data .putString ("key" , dataSnapshot .getKey ());
58
- data .putBoolean ("exists" , dataSnapshot .exists ());
59
- data .putBoolean ("hasChildren" , dataSnapshot .hasChildren ());
60
-
61
- data .putDouble ("childrenCount" , dataSnapshot .getChildrenCount ());
62
- if (!dataSnapshot .hasChildren ()) {
63
- Object value = dataSnapshot .getValue ();
64
- String type = value !=null ? value .getClass ().getName () : "" ;
65
- switch (type ) {
66
- case "java.lang.Boolean" :
67
- data .putBoolean ("value" , (Boolean )value );
68
- break ;
69
- case "java.lang.Long" :
70
- Long longVal = (Long ) value ;
71
- data .putDouble ("value" , (double )longVal );
72
- break ;
73
- case "java.lang.Double" :
74
- data .putDouble ("value" , (Double ) value );
75
- break ;
76
- case "java.lang.String" :
77
- data .putString ("value" ,(String ) value );
78
- break ;
79
- default :
80
- data .putString ("value" , null );
81
- }
82
- } else {
83
- WritableMap valueMap = FirestackUtils .castSnapshotValue (dataSnapshot );
84
- data .putMap ("value" , valueMap );
52
+ public static WritableMap dataSnapshotToMap (
53
+ String name ,
54
+ String path ,
55
+ String modifiersString ,
56
+ DataSnapshot dataSnapshot
57
+ ) {
58
+ WritableMap data = Arguments .createMap ();
59
+
60
+ data .putString ("key" , dataSnapshot .getKey ());
61
+ data .putBoolean ("exists" , dataSnapshot .exists ());
62
+ data .putBoolean ("hasChildren" , dataSnapshot .hasChildren ());
63
+
64
+ data .putDouble ("childrenCount" , dataSnapshot .getChildrenCount ());
65
+ if (!dataSnapshot .hasChildren ()) {
66
+ Object value = dataSnapshot .getValue ();
67
+ String type = value != null ? value .getClass ().getName () : "" ;
68
+ switch (type ) {
69
+ case "java.lang.Boolean" :
70
+ data .putBoolean ("value" , (Boolean ) value );
71
+ break ;
72
+ case "java.lang.Long" :
73
+ Long longVal = (Long ) value ;
74
+ data .putDouble ("value" , (double ) longVal );
75
+ break ;
76
+ case "java.lang.Double" :
77
+ data .putDouble ("value" , (Double ) value );
78
+ break ;
79
+ case "java.lang.String" :
80
+ data .putString ("value" , (String ) value );
81
+ break ;
82
+ default :
83
+ data .putString ("value" , null );
85
84
}
85
+ } else {
86
+ WritableMap valueMap = FirestackUtils .castSnapshotValue (dataSnapshot );
87
+ data .putMap ("value" , valueMap );
88
+ }
86
89
87
- // Child keys
88
- WritableArray childKeys = FirestackUtils .getChildKeys (dataSnapshot );
89
- data .putArray ("childKeys" , childKeys );
90
+ // Child keys
91
+ WritableArray childKeys = FirestackUtils .getChildKeys (dataSnapshot );
92
+ data .putArray ("childKeys" , childKeys );
90
93
91
- Object priority = dataSnapshot .getPriority ();
92
- if (priority == null ) {
93
- data .putString ("priority" , null );
94
- } else {
95
- data .putString ("priority" , priority .toString ());
96
- }
94
+ Object priority = dataSnapshot .getPriority ();
95
+ if (priority == null ) {
96
+ data .putString ("priority" , null );
97
+ } else {
98
+ data .putString ("priority" , priority .toString ());
99
+ }
97
100
98
- WritableMap eventMap = Arguments .createMap ();
99
- eventMap .putString ("eventName" , name );
100
- eventMap .putMap ("snapshot" , data );
101
- eventMap .putString ("path" , path );
102
- return eventMap ;
101
+ WritableMap eventMap = Arguments .createMap ();
102
+ eventMap .putString ("eventName" , name );
103
+ eventMap .putMap ("snapshot" , data );
104
+ eventMap .putString ("path" , path );
105
+ eventMap .putString ("modifiersString" , modifiersString );
106
+ return eventMap ;
103
107
}
104
108
105
109
public static <Any > Any castSnapshotValue (DataSnapshot snapshot ) {
@@ -113,7 +117,7 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
113
117
break ;
114
118
case "java.lang.Long" :
115
119
Long longVal = (Long ) castedChild ;
116
- data .putDouble (child .getKey (), (double )longVal );
120
+ data .putDouble (child .getKey (), (double ) longVal );
117
121
break ;
118
122
case "java.lang.Double" :
119
123
data .putDouble (child .getKey (), (Double ) castedChild );
@@ -135,15 +139,15 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
135
139
String type = snapshot .getValue ().getClass ().getName ();
136
140
switch (type ) {
137
141
case "java.lang.Boolean" :
138
- return (Any )(snapshot .getValue ());
142
+ return (Any ) (snapshot .getValue ());
139
143
case "java.lang.Long" :
140
- return (Any )(snapshot .getValue ());
144
+ return (Any ) (snapshot .getValue ());
141
145
case "java.lang.Double" :
142
- return (Any )(snapshot .getValue ());
146
+ return (Any ) (snapshot .getValue ());
143
147
case "java.lang.String" :
144
- return (Any )(snapshot .getValue ());
148
+ return (Any ) (snapshot .getValue ());
145
149
default :
146
- Log .w (TAG , "Invalid type: " + type );
150
+ Log .w (TAG , "Invalid type: " + type );
147
151
return (Any ) null ;
148
152
}
149
153
}
@@ -164,65 +168,65 @@ public static WritableArray getChildKeys(DataSnapshot snapshot) {
164
168
}
165
169
166
170
public static Map <String , Object > recursivelyDeconstructReadableMap (ReadableMap readableMap ) {
167
- ReadableMapKeySetIterator iterator = readableMap .keySetIterator ();
168
- Map <String , Object > deconstructedMap = new HashMap <>();
169
- while (iterator .hasNextKey ()) {
170
- String key = iterator .nextKey ();
171
- ReadableType type = readableMap .getType (key );
172
- switch (type ) {
173
- case Null :
174
- deconstructedMap .put (key , null );
175
- break ;
176
- case Boolean :
177
- deconstructedMap .put (key , readableMap .getBoolean (key ));
178
- break ;
179
- case Number :
180
- deconstructedMap .put (key , readableMap .getDouble (key ));
181
- break ;
182
- case String :
183
- deconstructedMap .put (key , readableMap .getString (key ));
184
- break ;
185
- case Map :
186
- deconstructedMap .put (key , FirestackUtils .recursivelyDeconstructReadableMap (readableMap .getMap (key )));
187
- break ;
188
- case Array :
189
- deconstructedMap .put (key , FirestackUtils .recursivelyDeconstructReadableArray (readableMap .getArray (key )));
190
- break ;
191
- default :
192
- throw new IllegalArgumentException ("Could not convert object with key: " + key + "." );
193
- }
194
-
171
+ ReadableMapKeySetIterator iterator = readableMap .keySetIterator ();
172
+ Map <String , Object > deconstructedMap = new HashMap <>();
173
+ while (iterator .hasNextKey ()) {
174
+ String key = iterator .nextKey ();
175
+ ReadableType type = readableMap .getType (key );
176
+ switch (type ) {
177
+ case Null :
178
+ deconstructedMap .put (key , null );
179
+ break ;
180
+ case Boolean :
181
+ deconstructedMap .put (key , readableMap .getBoolean (key ));
182
+ break ;
183
+ case Number :
184
+ deconstructedMap .put (key , readableMap .getDouble (key ));
185
+ break ;
186
+ case String :
187
+ deconstructedMap .put (key , readableMap .getString (key ));
188
+ break ;
189
+ case Map :
190
+ deconstructedMap .put (key , FirestackUtils .recursivelyDeconstructReadableMap (readableMap .getMap (key )));
191
+ break ;
192
+ case Array :
193
+ deconstructedMap .put (key , FirestackUtils .recursivelyDeconstructReadableArray (readableMap .getArray (key )));
194
+ break ;
195
+ default :
196
+ throw new IllegalArgumentException ("Could not convert object with key: " + key + "." );
195
197
}
196
- return deconstructedMap ;
198
+
199
+ }
200
+ return deconstructedMap ;
197
201
}
198
202
199
203
public static List <Object > recursivelyDeconstructReadableArray (ReadableArray readableArray ) {
200
- List <Object > deconstructedList = new ArrayList <>(readableArray .size ());
201
- for (int i = 0 ; i < readableArray .size (); i ++) {
202
- ReadableType indexType = readableArray .getType (i );
203
- switch (indexType ) {
204
- case Null :
205
- deconstructedList .add (i , null );
206
- break ;
207
- case Boolean :
208
- deconstructedList .add (i , readableArray .getBoolean (i ));
209
- break ;
210
- case Number :
211
- deconstructedList .add (i , readableArray .getDouble (i ));
212
- break ;
213
- case String :
214
- deconstructedList .add (i , readableArray .getString (i ));
215
- break ;
216
- case Map :
217
- deconstructedList .add (i , FirestackUtils .recursivelyDeconstructReadableMap (readableArray .getMap (i )));
218
- break ;
219
- case Array :
220
- deconstructedList .add (i , FirestackUtils .recursivelyDeconstructReadableArray (readableArray .getArray (i )));
221
- break ;
222
- default :
223
- throw new IllegalArgumentException ("Could not convert object at index " + i + "." );
224
- }
204
+ List <Object > deconstructedList = new ArrayList <>(readableArray .size ());
205
+ for (int i = 0 ; i < readableArray .size (); i ++) {
206
+ ReadableType indexType = readableArray .getType (i );
207
+ switch (indexType ) {
208
+ case Null :
209
+ deconstructedList .add (i , null );
210
+ break ;
211
+ case Boolean :
212
+ deconstructedList .add (i , readableArray .getBoolean (i ));
213
+ break ;
214
+ case Number :
215
+ deconstructedList .add (i , readableArray .getDouble (i ));
216
+ break ;
217
+ case String :
218
+ deconstructedList .add (i , readableArray .getString (i ));
219
+ break ;
220
+ case Map :
221
+ deconstructedList .add (i , FirestackUtils .recursivelyDeconstructReadableMap (readableArray .getMap (i )));
222
+ break ;
223
+ case Array :
224
+ deconstructedList .add (i , FirestackUtils .recursivelyDeconstructReadableArray (readableArray .getArray (i )));
225
+ break ;
226
+ default :
227
+ throw new IllegalArgumentException ("Could not convert object at index " + i + "." );
225
228
}
226
- return deconstructedList ;
229
+ }
230
+ return deconstructedList ;
227
231
}
228
232
}
0 commit comments