44import android .util .Log ;
55import java .util .HashMap ;
66import java .util .Iterator ;
7+ import java .util .List ;
8+ import java .util .ListIterator ;
79import java .util .Map ;
810import android .net .Uri ;
911
1820import com .facebook .react .bridge .WritableMap ;
1921import com .facebook .react .bridge .ReadableMap ;
2022import com .facebook .react .bridge .ReadableMapKeySetIterator ;
23+ import com .facebook .react .bridge .ReadableArray ;
2124import com .facebook .react .bridge .ReactContext ;
2225
2326import com .google .android .gms .tasks .OnCompleteListener ;
2831import com .google .firebase .database .FirebaseDatabase ;
2932import com .google .firebase .database .DatabaseReference ;
3033import com .google .firebase .database .ChildEventListener ;
34+ import com .google .firebase .database .Query ;
3135import com .google .firebase .database .ValueEventListener ;
3236import com .google .firebase .database .DataSnapshot ;
3337import com .google .firebase .database .DatabaseError ;
@@ -54,8 +58,12 @@ public String getName() {
5458 }
5559
5660 @ ReactMethod
57- public void set (final String path , final ReadableMap props , final Callback callback ) {
61+ public void set (
62+ final String path ,
63+ final ReadableMap props ,
64+ final Callback callback ) {
5865 DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
66+
5967 final FirestackDatabaseModule self = this ;
6068 Map <String , Object > m = FirestackUtils .recursivelyDeconstructReadableMap (props );
6169
@@ -80,7 +88,9 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
8088 }
8189
8290 @ ReactMethod
83- public void update (final String path , final ReadableMap props , final Callback callback ) {
91+ public void update (final String path ,
92+ final ReadableMap props ,
93+ final Callback callback ) {
8494 DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
8595 final FirestackDatabaseModule self = this ;
8696 Map <String , Object > m = FirestackUtils .recursivelyDeconstructReadableMap (props );
@@ -106,7 +116,8 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
106116 }
107117
108118 @ ReactMethod
109- public void remove (final String path , final Callback callback ) {
119+ public void remove (final String path ,
120+ final Callback callback ) {
110121 DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
111122 final FirestackDatabaseModule self = this ;
112123 DatabaseReference .CompletionListener listener = new DatabaseReference .CompletionListener () {
@@ -130,7 +141,10 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
130141 }
131142
132143 @ ReactMethod
133- public void push (final String path , final ReadableMap props , final Callback callback ) {
144+ public void push (final String path ,
145+ final ReadableMap props ,
146+ final Callback callback ) {
147+
134148 Log .d (TAG , "Called push with " + path );
135149 DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
136150 DatabaseReference newRef = ref .push ();
@@ -174,9 +188,12 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
174188 }
175189
176190 @ ReactMethod
177- public void on (final String path , final String name , final Callback callback ) {
191+ public void on (final String path ,
192+ final ReadableArray modifiers ,
193+ final String name ,
194+ final Callback callback ) {
178195 Log .d (TAG , "Setting a listener on event: " + name + " for path " + path );
179- DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
196+ Query ref = this .getDatabaseQueryAtPathAndModifiers (path , modifiers );
180197 final FirestackDatabaseModule self = this ;
181198
182199 if (name .equals ("value" )) {
@@ -260,9 +277,11 @@ public void onCancelled(DatabaseError error) {
260277 }
261278
262279 @ ReactMethod
263- public void onOnce (final String path , final String name , final Callback callback ) {
280+ public void onOnce (final String path ,
281+ final ReadableArray modifiers ,
282+ final String name , final Callback callback ) {
264283 Log .d (TAG , "Setting one-time listener on event: " + name + " for path " + path );
265- DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
284+ Query ref = this .getDatabaseQueryAtPathAndModifiers (path , modifiers );
266285 final FirestackDatabaseModule self = this ;
267286
268287 ValueEventListener listener = new ValueEventListener () {
@@ -297,11 +316,70 @@ public void removeListeners(final String path, final String name, final Callback
297316 FirestackUtils .todoNote (TAG , "on" , callback );
298317 }
299318
319+ // Private helpers
300320 private DatabaseReference getDatabaseReferenceAtPath (final String path ) {
301321 DatabaseReference mDatabase = FirebaseDatabase .getInstance ().getReference (path );
302322 return mDatabase ;
303323 }
304324
325+ private Query getDatabaseQueryAtPathAndModifiers (
326+ final String path ,
327+ final ReadableArray modifiers ) {
328+ DatabaseReference ref = this .getDatabaseReferenceAtPath (path );
329+
330+ List <Object > strModifiers = FirestackUtils .recursivelyDeconstructReadableArray (modifiers );
331+ ListIterator <Object > it = strModifiers .listIterator ();
332+ Query query = ref .orderByKey ();
333+
334+ while (it .hasNext ()) {
335+ String str = (String ) it .next ();
336+
337+ Log .d (TAG , "getReference with modifiers: " + str );
338+ String [] strArr = str .split (":" );
339+ String methStr = strArr [0 ];
340+
341+ if (methStr .equalsIgnoreCase ("orderByKey" )) {
342+ query = ref .orderByKey ();
343+ } else if (methStr .equalsIgnoreCase ("orderByValue" )) {
344+ query = ref .orderByValue ();
345+ } else if (methStr .equalsIgnoreCase ("orderByPriority" )) {
346+ query = ref .orderByPriority ();
347+ } else if (methStr .contains ("orderByChild" )) {
348+ String key = strArr [1 ];
349+ Log .d (TAG , "orderByChild: " + key );
350+ query = ref .orderByChild (key );
351+ } else if (methStr .contains ("limitToLast" )) {
352+ String key = strArr [1 ];
353+ int limit = Integer .parseInt (key );
354+ Log .d (TAG , "limitToLast: " + limit );
355+ query = query .limitToLast (limit );
356+ } else if (methStr .contains ("limitToFirst" )) {
357+ String key = strArr [1 ];
358+ int limit = Integer .parseInt (key );
359+ Log .d (TAG , "limitToFirst: " + limit );
360+ query = query .limitToFirst (limit );
361+ } else if (methStr .contains ("equalTo" )) {
362+ String value = strArr [1 ];
363+ String key = strArr [2 ];
364+ if (key == null ) {
365+ query = query .equalTo (value );
366+ } else {
367+ query = query .equalTo (value , key );
368+ }
369+ } else if (methStr .contains ("endAt" )) {
370+ String value = strArr [1 ];
371+ String key = strArr [2 ];
372+ if (key == null ) {
373+ query = query .equalTo (value );
374+ } else {
375+ query = query .equalTo (value , key );
376+ }
377+ }
378+ }
379+
380+ return query ;
381+ }
382+
305383 private WritableMap dataSnapshotToMap (String name , DataSnapshot dataSnapshot ) {
306384 WritableMap data = Arguments .createMap ();
307385
@@ -316,7 +394,7 @@ private WritableMap dataSnapshotToMap(String name, DataSnapshot dataSnapshot) {
316394
317395 Object priority = dataSnapshot .getPriority ();
318396 if (priority == null ) {
319- data .putString ("priority" , " null" );
397+ data .putString ("priority" , null );
320398 } else {
321399 data .putString ("priority" , priority .toString ());
322400 }
0 commit comments