Skip to content

Commit aa5b3fa

Browse files
authored
Merge pull request #8 from chrisbianca/master
Update Java database module to simplify reference handling
2 parents 8c5458a + 5a95fb3 commit aa5b3fa

File tree

2 files changed

+103
-188
lines changed

2 files changed

+103
-188
lines changed

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

Lines changed: 42 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
public class FirestackDatabase extends ReactContextBaseJavaModule {
2828
private static final String TAG = "FirestackDatabase";
2929
private HashMap<String, FirestackDatabaseReference> mDBListeners = new HashMap<String, FirestackDatabaseReference>();
30+
private FirebaseDatabase mFirebaseDatabase;
3031

3132
public FirestackDatabase(ReactApplicationContext reactContext) {
3233
super(reactContext);
34+
mFirebaseDatabase = FirebaseDatabase.getInstance();
3335
}
3436

3537
@Override
@@ -43,8 +45,7 @@ public void enablePersistence(
4345
final Boolean enable,
4446
final Callback callback) {
4547
try {
46-
FirebaseDatabase.getInstance()
47-
.setPersistenceEnabled(enable);
48+
mFirebaseDatabase.setPersistenceEnabled(enable);
4849
} catch (Throwable t) {
4950
Log.e(TAG, "FirebaseDatabase setPersistenceEnabled exception", t);
5051
}
@@ -59,7 +60,7 @@ public void keepSynced(
5960
final String path,
6061
final Boolean enable,
6162
final Callback callback) {
62-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
63+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
6364
ref.keepSynced(enable);
6465

6566
WritableMap res = Arguments.createMap();
@@ -74,15 +75,13 @@ public void set(
7475
final String path,
7576
final ReadableMap props,
7677
final Callback callback) {
77-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
78-
79-
final FirestackDatabase self = this;
78+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
8079
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(props);
8180

8281
DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() {
8382
@Override
8483
public void onComplete(DatabaseError error, DatabaseReference ref) {
85-
handleCallback("set", callback, error, ref);
84+
handleCallback("set", callback, error);
8685
}
8786
};
8887

@@ -93,14 +92,13 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
9392
public void update(final String path,
9493
final ReadableMap props,
9594
final Callback callback) {
96-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
97-
final FirestackDatabase self = this;
95+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
9896
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(props);
9997

10098
DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() {
10199
@Override
102100
public void onComplete(DatabaseError error, DatabaseReference ref) {
103-
handleCallback("update", callback, error, ref);
101+
handleCallback("update", callback, error);
104102
}
105103
};
106104

@@ -110,12 +108,11 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
110108
@ReactMethod
111109
public void remove(final String path,
112110
final Callback callback) {
113-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
114-
final FirestackDatabase self = this;
111+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
115112
DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() {
116113
@Override
117114
public void onComplete(DatabaseError error, DatabaseReference ref) {
118-
handleCallback("remove", callback, error, ref);
115+
handleCallback("remove", callback, error);
119116
}
120117
};
121118

@@ -128,7 +125,7 @@ public void push(final String path,
128125
final Callback callback) {
129126

130127
Log.d(TAG, "Called push with " + path);
131-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
128+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
132129
DatabaseReference newRef = ref.push();
133130

134131
final Uri url = Uri.parse(newRef.toString());
@@ -138,7 +135,6 @@ public void push(final String path,
138135
if (iterator.hasNextKey()) {
139136
Log.d(TAG, "Passed value to push");
140137
// lame way to check if the `props` are empty
141-
final FirestackDatabase self = this;
142138
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(props);
143139

144140
DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() {
@@ -175,20 +171,16 @@ public void on(final String path,
175171
final ReadableArray modifiersArray,
176172
final String name,
177173
final Callback callback) {
178-
FirestackDatabaseReference ref = this.getDBHandle(path, modifiersString);
179-
180-
WritableMap resp = Arguments.createMap();
174+
FirestackDatabaseReference ref = this.getDBHandle(path, modifiersArray, modifiersString);
181175

182176
if (name.equals("value")) {
183-
ref.addValueEventListener(name, modifiersArray, modifiersString);
177+
ref.addValueEventListener();
184178
} else {
185-
ref.addChildEventListener(name, modifiersArray, modifiersString);
179+
ref.addChildEventListener(name);
186180
}
187181

188-
this.saveDBHandle(path, modifiersString, ref);
182+
WritableMap resp = Arguments.createMap();
189183
resp.putString("result", "success");
190-
Log.d(TAG, "Added listener " + name + " for " + ref + "with modifiers: "+ modifiersString);
191-
192184
resp.putString("handle", path);
193185
callback.invoke(null, resp);
194186
}
@@ -199,9 +191,8 @@ public void onOnce(final String path,
199191
final ReadableArray modifiersArray,
200192
final String name,
201193
final Callback callback) {
202-
Log.d(TAG, "Setting one-time listener on event: " + name + " for path " + path);
203-
FirestackDatabaseReference ref = this.getDBHandle(path, modifiersString);
204-
ref.addOnceValueEventListener(modifiersArray, modifiersString, callback);
194+
FirestackDatabaseReference ref = this.getDBHandle(path, modifiersArray, modifiersString);
195+
ref.addOnceValueEventListener(callback);
205196
}
206197

207198
/**
@@ -227,82 +218,60 @@ public void off(
227218
// On Disconnect
228219
@ReactMethod
229220
public void onDisconnectSetObject(final String path, final ReadableMap props, final Callback callback) {
230-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
221+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
231222
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(props);
232223

233224
OnDisconnect od = ref.onDisconnect();
234225
od.setValue(m, new DatabaseReference.CompletionListener() {
235226
@Override
236-
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
237-
handleCallback("onDisconnectSetObject", callback, databaseError, databaseReference);
227+
public void onComplete(DatabaseError error, DatabaseReference ref) {
228+
handleCallback("onDisconnectSetObject", callback, error);
238229
}
239230
});
240231
}
241232

242233
@ReactMethod
243234
public void onDisconnectSetString(final String path, final String value, final Callback callback) {
244-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
235+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
245236

246237
OnDisconnect od = ref.onDisconnect();
247238
od.setValue(value, new DatabaseReference.CompletionListener() {
248239
@Override
249-
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
250-
handleCallback("onDisconnectSetString", callback, databaseError, databaseReference);
240+
public void onComplete(DatabaseError error, DatabaseReference ref) {
241+
handleCallback("onDisconnectSetString", callback, error);
251242
}
252243
});
253244
}
254245

255246
@ReactMethod
256247
public void onDisconnectRemove(final String path, final Callback callback) {
257-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
248+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
258249

259250
OnDisconnect od = ref.onDisconnect();
260251
od.removeValue(new DatabaseReference.CompletionListener() {
261252
@Override
262-
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
263-
handleCallback("onDisconnectRemove", callback, databaseError, databaseReference);
253+
public void onComplete(DatabaseError error, DatabaseReference ref) {
254+
handleCallback("onDisconnectRemove", callback, error);
264255
}
265256
});
266257
}
267258
@ReactMethod
268259
public void onDisconnectCancel(final String path, final Callback callback) {
269-
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
260+
DatabaseReference ref = mFirebaseDatabase.getReference(path);
270261

271262
OnDisconnect od = ref.onDisconnect();
272263
od.cancel(new DatabaseReference.CompletionListener() {
273264
@Override
274-
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
275-
handleCallback("onDisconnectCancel", callback, databaseError, databaseReference);
265+
public void onComplete(DatabaseError error, DatabaseReference ref) {
266+
handleCallback("onDisconnectCancel", callback, error);
276267
}
277268
});
278269
}
279270

280-
// Private helpers
281-
// private void handleDatabaseEvent(final String name, final DataSnapshot dataSnapshot) {
282-
// WritableMap data = this.dataSnapshotToMap(name, dataSnapshot);
283-
// WritableMap evt = Arguments.createMap();
284-
// evt.putString("eventName", name);
285-
// evt.putMap("body", data);
286-
// Utils.sendEvent(mReactContext, "database_event", evt);
287-
// }
288-
289-
// private void handleDatabaseError(final String name, final DatabaseError error) {
290-
// WritableMap err = Arguments.createMap();
291-
// err.putInt("errorCode", error.getCode());
292-
// err.putString("errorDetails", error.getDetails());
293-
// err.putString("description", error.getMessage());
294-
295-
// WritableMap evt = Arguments.createMap();
296-
// evt.putString("eventName", name);
297-
// evt.putMap("body", err);
298-
// Utils.sendEvent(mReactContext, "database_error", evt);
299-
// }
300-
301271
private void handleCallback(
302272
final String methodName,
303273
final Callback callback,
304-
final DatabaseError databaseError,
305-
final DatabaseReference databaseReference) {
274+
final DatabaseError databaseError) {
306275
if (databaseError != null) {
307276
WritableMap err = Arguments.createMap();
308277
err.putInt("errorCode", databaseError.getCode());
@@ -317,19 +286,19 @@ private void handleCallback(
317286
}
318287
}
319288

320-
private FirestackDatabaseReference getDBHandle(final String path, final String modifiersString) {
289+
private FirestackDatabaseReference getDBHandle(final String path,
290+
final ReadableArray modifiersArray,
291+
final String modifiersString) {
321292
String key = this.getDBListenerKey(path, modifiersString);
322-
if (!mDBListeners.containsKey(key)) {
293+
FirestackDatabaseReference r = mDBListeners.get(key);
294+
295+
if (r == null) {
323296
ReactContext ctx = getReactApplicationContext();
324-
mDBListeners.put(key, new FirestackDatabaseReference(ctx, path));
297+
r = new FirestackDatabaseReference(ctx, mFirebaseDatabase, path, modifiersArray, modifiersString);
298+
mDBListeners.put(key, r);
325299
}
326300

327-
return mDBListeners.get(key);
328-
}
329-
330-
private void saveDBHandle(final String path, String modifiersString, final FirestackDatabaseReference dbRef) {
331-
String key = this.getDBListenerKey(path, modifiersString);
332-
mDBListeners.put(key, dbRef);
301+
return r;
333302
}
334303

335304
private String getDBListenerKey(String path, String modifiersString) {
@@ -338,20 +307,11 @@ private String getDBListenerKey(String path, String modifiersString) {
338307

339308
private void removeDBHandle(final String path, String modifiersString) {
340309
String key = this.getDBListenerKey(path, modifiersString);
341-
if (mDBListeners.containsKey(key)) {
342-
FirestackDatabaseReference r = mDBListeners.get(key);
310+
FirestackDatabaseReference r = mDBListeners.get(key);
311+
312+
if (r != null) {
343313
r.cleanup();
344314
mDBListeners.remove(key);
345315
}
346316
}
347-
348-
private String keyPath(final String path, final String eventName) {
349-
return path + "-" + eventName;
350-
}
351-
352-
// TODO: move to FirestackDatabaseReference?
353-
private DatabaseReference getDatabaseReferenceAtPath(final String path) {
354-
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(path);
355-
return mDatabase;
356-
}
357317
}

0 commit comments

Comments
 (0)