Skip to content

Commit cbf6304

Browse files
committed
manual merge of @BlooJeans pr #132 - needed a major cleanup though, unused variables & functions, duplications, unused imports, unnecessary variable creations etc - added TODO notes for everything else that needs cleaning up here
1 parent 51a323f commit cbf6304

File tree

7 files changed

+783
-647
lines changed

7 files changed

+783
-647
lines changed

android/src/main/java/io/fullstack/firestack/FirestackDatabase.java

+88-130
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.fullstack.firestack;
22

33
import android.content.Context;
4+
import android.text.TextUtils;
45
import android.util.Log;
56
import java.util.HashMap;
67
import java.util.List;
@@ -13,6 +14,7 @@
1314
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1415
import com.facebook.react.bridge.ReactMethod;
1516
import com.facebook.react.bridge.Callback;
17+
import com.facebook.react.bridge.WritableArray;
1618
import com.facebook.react.bridge.WritableMap;
1719
import com.facebook.react.bridge.ReadableMap;
1820
import com.facebook.react.bridge.ReadableMapKeySetIterator;
@@ -49,29 +51,29 @@ public void setModifiers(final ReadableArray modifiers) {
4951
mModifiers = modifiers;
5052
}
5153

52-
public void addChildEventListener(final String name, final ReadableArray modifiers) {
54+
public void addChildEventListener(final String name, final ReadableArray modifiersArray, final String modifiersString) {
5355
final FirestackDBReference self = this;
5456

5557
if (mEventListener == null) {
5658
mEventListener = new ChildEventListener() {
5759
@Override
5860
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
59-
self.handleDatabaseEvent("child_added", mPath, dataSnapshot);
61+
self.handleDatabaseEvent("child_added", mPath, modifiersString, dataSnapshot);
6062
}
6163

6264
@Override
6365
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
64-
self.handleDatabaseEvent("child_changed", mPath, dataSnapshot);
66+
self.handleDatabaseEvent("child_changed", mPath, modifiersString, dataSnapshot);
6567
}
6668

6769
@Override
6870
public void onChildRemoved(DataSnapshot dataSnapshot) {
69-
self.handleDatabaseEvent("child_removed", mPath, dataSnapshot);
71+
self.handleDatabaseEvent("child_removed", mPath, modifiersString, dataSnapshot);
7072
}
7173

7274
@Override
7375
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
74-
self.handleDatabaseEvent("child_moved", mPath, dataSnapshot);
76+
self.handleDatabaseEvent("child_moved", mPath, modifiersString, dataSnapshot);
7577
}
7678

7779
@Override
@@ -81,18 +83,18 @@ public void onCancelled(DatabaseError error) {
8183
};
8284
}
8385

84-
Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiers);
86+
Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiersArray);
8587
ref.addChildEventListener(mEventListener);
86-
this.setListeningTo(mPath, name);
88+
//this.setListeningTo(mPath, modifiersString, name);
8789
}
8890

89-
public void addValueEventListener(final String name, final ReadableArray modifiers) {
91+
public void addValueEventListener(final String name, final ReadableArray modifiersArray, final String modifiersString) {
9092
final FirestackDBReference self = this;
9193

9294
mValueListener = new ValueEventListener() {
9395
@Override
9496
public void onDataChange(DataSnapshot dataSnapshot) {
95-
self.handleDatabaseEvent("value", mPath, dataSnapshot);
97+
self.handleDatabaseEvent("value", mPath, modifiersString, dataSnapshot);
9698
}
9799

98100
@Override
@@ -101,19 +103,20 @@ public void onCancelled(DatabaseError error) {
101103
}
102104
};
103105

104-
Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiers);
106+
Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiersArray);
105107
ref.addValueEventListener(mValueListener);
106-
this.setListeningTo(mPath, "value");
108+
//this.setListeningTo(mPath, modifiersString, "value");
107109
}
108110

109-
public void addOnceValueEventListener(final ReadableArray modifiers,
111+
public void addOnceValueEventListener(final ReadableArray modifiersArray,
112+
final String modifiersString,
110113
final Callback callback) {
111114
final FirestackDBReference self = this;
112115

113116
mOnceValueListener = new ValueEventListener() {
114117
@Override
115118
public void onDataChange(DataSnapshot dataSnapshot) {
116-
WritableMap data = FirestackUtils.dataSnapshotToMap("value", mPath, dataSnapshot);
119+
WritableMap data = FirestackUtils.dataSnapshotToMap("value", mPath, modifiersString, dataSnapshot);
117120
callback.invoke(null, data);
118121
}
119122

@@ -127,31 +130,31 @@ public void onCancelled(DatabaseError error) {
127130
}
128131
};
129132

130-
Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiers);
133+
Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiersArray);
131134
ref.addListenerForSingleValueEvent(mOnceValueListener);
132135
}
133136

134-
public Boolean isListeningTo(final String path, final String evtName) {
135-
String key = this.pathListeningKey(path, evtName);
136-
return mListeners.containsKey(key);
137-
}
137+
//public Boolean isListeningTo(final String path, String modifiersString, final String evtName) {
138+
// String key = this.pathListeningKey(path, modifiersString, evtName);
139+
// return mListeners.containsKey(key);
140+
//}
138141

139142
/**
140143
* Note: these path/eventType listeners only get removed when javascript calls .off() and cleanup is run on the entire path
141144
*/
142-
public void setListeningTo(final String path, final String evtName) {
143-
String key = this.pathListeningKey(path, evtName);
144-
mListeners.put(key, true);
145-
}
145+
//public void setListeningTo(final String path, String modifiersString, final String evtName) {
146+
// String key = this.pathListeningKey(path, modifiersString, evtName);
147+
// mListeners.put(key, true);
148+
//}
146149

147-
public void notListeningTo(final String path, final String evtName) {
148-
String key = this.pathListeningKey(path, evtName);
149-
mListeners.remove(key);
150-
}
150+
//public void notListeningTo(final String path, String modifiersString, final String evtName) {
151+
// String key = this.pathListeningKey(path, modifiersString, evtName);
152+
// mListeners.remove(key);
153+
//}
151154

152-
private String pathListeningKey(final String path, final String eventName) {
153-
return "listener/" + path + "/" + eventName;
154-
}
155+
//private String pathListeningKey(final String path, String modifiersString, final String eventName) {
156+
//return "listener/" + path + "/" + modifiersString + "/" + eventName;
157+
//}
155158

156159
public void cleanup() {
157160
Log.d(TAG, "cleaning up database reference " + this);
@@ -163,10 +166,10 @@ public void removeChildEventListener() {
163166
if (mEventListener != null) {
164167
DatabaseReference ref = this.getDatabaseRef();
165168
ref.removeEventListener(mEventListener);
166-
this.notListeningTo(mPath, "child_added");
167-
this.notListeningTo(mPath, "child_changed");
168-
this.notListeningTo(mPath, "child_removed");
169-
this.notListeningTo(mPath, "child_moved");
169+
//this.notListeningTo(mPath, "child_added");
170+
//this.notListeningTo(mPath, "child_changed");
171+
//this.notListeningTo(mPath, "child_removed");
172+
//this.notListeningTo(mPath, "child_moved");
170173
mEventListener = null;
171174
}
172175
}
@@ -175,7 +178,7 @@ public void removeValueEventListener() {
175178
DatabaseReference ref = this.getDatabaseRef();
176179
if (mValueListener != null) {
177180
ref.removeEventListener(mValueListener);
178-
this.notListeningTo(mPath, "value");
181+
//this.notListeningTo(mPath, "value");
179182
mValueListener = null;
180183
}
181184
if (mOnceValueListener != null) {
@@ -184,16 +187,17 @@ public void removeValueEventListener() {
184187
}
185188
}
186189

187-
private void handleDatabaseEvent(final String name, final String path, final DataSnapshot dataSnapshot) {
188-
if (!FirestackDBReference.this.isListeningTo(path, name)) {
189-
return;
190-
}
191-
WritableMap data = FirestackUtils.dataSnapshotToMap(name, path, dataSnapshot);
190+
private void handleDatabaseEvent(final String name, final String path, final String modifiersString, final DataSnapshot dataSnapshot) {
191+
//if (!FirestackDBReference.this.isListeningTo(path, modifiersString, name)) {
192+
//return;
193+
//}
194+
WritableMap data = FirestackUtils.dataSnapshotToMap(name, path, modifiersString, dataSnapshot);
192195
WritableMap evt = Arguments.createMap();
193196
evt.putString("eventName", name);
194197
evt.putString("path", path);
198+
evt.putString("modifiersString", modifiersString);
195199
evt.putMap("body", data);
196-
200+
197201
FirestackUtils.sendEvent(mReactContext, "database_event", evt);
198202
}
199203

@@ -304,8 +308,12 @@ public String getName() {
304308
public void enablePersistence(
305309
final Boolean enable,
306310
final Callback callback) {
307-
FirebaseDatabase.getInstance()
311+
try {
312+
FirebaseDatabase.getInstance()
308313
.setPersistenceEnabled(enable);
314+
} catch (Throwable t) {
315+
Log.e(TAG, "FirebaseDatabase setPersistenceEnabled exception", t);
316+
}
309317

310318
WritableMap res = Arguments.createMap();
311319
res.putString("status", "success");
@@ -429,35 +437,37 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
429437

430438
@ReactMethod
431439
public void on(final String path,
432-
final ReadableArray modifiers,
440+
final String modifiersString,
441+
final ReadableArray modifiersArray,
433442
final String name,
434443
final Callback callback) {
435-
FirestackDBReference ref = this.getDBHandle(path);
444+
FirestackDBReference ref = this.getDBHandle(path, modifiersString);
436445

437446
WritableMap resp = Arguments.createMap();
438447

439448
if (name.equals("value")) {
440-
ref.addValueEventListener(name, modifiers);
449+
ref.addValueEventListener(name, modifiersArray, modifiersString);
441450
} else {
442-
ref.addChildEventListener(name, modifiers);
451+
ref.addChildEventListener(name, modifiersArray, modifiersString);
443452
}
444453

445-
this.saveDBHandle(path, ref);
454+
this.saveDBHandle(path, modifiersString, ref);
446455
resp.putString("result", "success");
447-
Log.d(TAG, "Added listener " + name + " for " + ref);
448-
456+
Log.d(TAG, "Added listener " + name + " for " + ref + "with modifiers: "+ modifiersString);
457+
449458
resp.putString("handle", path);
450459
callback.invoke(null, resp);
451460
}
452461

453462
@ReactMethod
454463
public void onOnce(final String path,
455-
final ReadableArray modifiers,
464+
final String modifiersString,
465+
final ReadableArray modifiersArray,
456466
final String name,
457467
final Callback callback) {
458468
Log.d(TAG, "Setting one-time listener on event: " + name + " for path " + path);
459-
FirestackDBReference ref = this.getDBHandle(path);
460-
ref.addOnceValueEventListener(modifiers, callback);
469+
FirestackDBReference ref = this.getDBHandle(path, modifiersString);
470+
ref.addOnceValueEventListener(modifiersArray, modifiersString, callback);
461471
}
462472

463473
/**
@@ -467,9 +477,13 @@ public void onOnce(final String path,
467477
* off() should therefore clean *everything* up
468478
*/
469479
@ReactMethod
470-
public void off(final String path, @Deprecated final String name, final Callback callback) {
471-
this.removeDBHandle(path);
472-
Log.d(TAG, "Removed listener " + path);
480+
public void off(
481+
final String path,
482+
final String modifiersString,
483+
@Deprecated final String name,
484+
final Callback callback) {
485+
this.removeDBHandle(path, modifiersString);
486+
Log.d(TAG, "Removed listener " + path + "/" + modifiersString);
473487
WritableMap resp = Arguments.createMap();
474488
resp.putString("handle", path);
475489
resp.putString("result", "success");
@@ -569,24 +583,31 @@ private void handleCallback(
569583
}
570584
}
571585

572-
private FirestackDBReference getDBHandle(final String path) {
573-
if (!mDBListeners.containsKey(path)) {
586+
private FirestackDBReference getDBHandle(final String path, final String modifiersString) {
587+
String key = this.getDBListenerKey(path, modifiersString);
588+
if (!mDBListeners.containsKey(key)) {
574589
ReactContext ctx = getReactApplicationContext();
575-
mDBListeners.put(path, new FirestackDBReference(ctx, path));
590+
mDBListeners.put(key, new FirestackDBReference(ctx, path));
576591
}
577592

578-
return mDBListeners.get(path);
593+
return mDBListeners.get(key);
579594
}
580595

581-
private void saveDBHandle(final String path, final FirestackDBReference dbRef) {
582-
mDBListeners.put(path, dbRef);
596+
private void saveDBHandle(final String path, String modifiersString, final FirestackDBReference dbRef) {
597+
String key = this.getDBListenerKey(path, modifiersString);
598+
mDBListeners.put(key, dbRef);
583599
}
584600

585-
private void removeDBHandle(final String path) {
586-
if (mDBListeners.containsKey(path)) {
587-
FirestackDBReference r = mDBListeners.get(path);
601+
private String getDBListenerKey(String path, String modifiersString) {
602+
return path + " | " + modifiersString;
603+
}
604+
605+
private void removeDBHandle(final String path, String modifiersString) {
606+
String key = this.getDBListenerKey(path, modifiersString);
607+
if (mDBListeners.containsKey(key)) {
608+
FirestackDBReference r = mDBListeners.get(key);
588609
r.cleanup();
589-
mDBListeners.remove(path);
610+
mDBListeners.remove(key);
590611
}
591612
}
592613

@@ -600,73 +621,10 @@ private DatabaseReference getDatabaseReferenceAtPath(final String path) {
600621
return mDatabase;
601622
}
602623

603-
private Query getDatabaseQueryAtPathAndModifiers(
604-
final String path,
605-
final ReadableArray modifiers) {
606-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
607-
608-
List<Object> strModifiers = FirestackUtils.recursivelyDeconstructReadableArray(modifiers);
609-
ListIterator<Object> it = strModifiers.listIterator();
610-
Query query = ref.orderByKey();
611-
612-
while(it.hasNext()) {
613-
String str = (String) it.next();
614-
String[] strArr = str.split(":");
615-
String methStr = strArr[0];
616-
617-
if (methStr.equalsIgnoreCase("orderByKey")) {
618-
query = ref.orderByKey();
619-
} else if (methStr.equalsIgnoreCase("orderByValue")) {
620-
query = ref.orderByValue();
621-
} else if (methStr.equalsIgnoreCase("orderByPriority")) {
622-
query = ref.orderByPriority();
623-
} else if (methStr.contains("orderByChild")) {
624-
String key = strArr[1];
625-
Log.d(TAG, "orderByChild: " + key);
626-
query = ref.orderByChild(key);
627-
} else if (methStr.contains("limitToLast")) {
628-
String key = strArr[1];
629-
int limit = Integer.parseInt(key);
630-
Log.d(TAG, "limitToLast: " + limit);
631-
query = query.limitToLast(limit);
632-
} else if (methStr.contains("limitToFirst")) {
633-
String key = strArr[1];
634-
int limit = Integer.parseInt(key);
635-
Log.d(TAG, "limitToFirst: " + limit);
636-
query = query.limitToFirst(limit);
637-
} else if (methStr.contains("equalTo")) {
638-
String value = strArr[1];
639-
String key = strArr.length >= 3 ? strArr[2] : null;
640-
if (key == null) {
641-
query = query.equalTo(value);
642-
} else {
643-
query = query.equalTo(value, key);
644-
}
645-
} else if (methStr.contains("endAt")) {
646-
String value = strArr[1];
647-
String key = strArr.length >= 3 ? strArr[2] : null;
648-
if (key == null) {
649-
query = query.endAt(value);
650-
} else {
651-
query = query.endAt(value, key);
652-
}
653-
} else if (methStr.contains("startAt")) {
654-
String value = strArr[1];
655-
String key = strArr.length >= 3 ? strArr[2] : null;
656-
if (key == null) {
657-
query = query.startAt(value);
658-
} else {
659-
query = query.startAt(value, key);
660-
}
661-
}
662-
}
663-
664-
return query;
665-
}
666624

667-
private WritableMap dataSnapshotToMap(String name, String path, DataSnapshot dataSnapshot) {
668-
return FirestackUtils.dataSnapshotToMap(name, path, dataSnapshot);
669-
}
625+
//private WritableMap dataSnapshotToMap(String name, String path, DataSnapshot dataSnapshot) {
626+
// return FirestackUtils.dataSnapshotToMap(name, path, dataSnapshot);
627+
//}
670628

671629
private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
672630
if (snapshot.hasChildren()) {

0 commit comments

Comments
 (0)