2
0
mirror of synced 2025-01-14 08:25:04 +00:00

[android][database] final cleanup of RNFirebaseDatabaseReference for v3.

This commit is contained in:
Salakar 2017-08-17 16:25:29 +01:00
parent 0737f53dea
commit b322af0f40

View File

@ -1,8 +1,8 @@
package io.invertase.firebase.database;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import android.util.Log;
import android.support.annotation.Nullable;
@ -32,12 +32,9 @@ class RNFirebaseDatabaseReference {
private HashMap<String, ChildEventListener> childEventListeners;
private HashMap<String, ValueEventListener> valueEventListeners;
Query getQuery() {
return query;
}
/**
* TODO
* RNFirebase wrapper around FirebaseDatabaseReference,
* handles Query generation and event listeners.
*
* @param context
* @param app
@ -47,11 +44,22 @@ class RNFirebaseDatabaseReference {
*/
RNFirebaseDatabaseReference(ReactContext context, String app, String refKey, String refPath, ReadableArray modifiersArray) {
key = refKey;
query = null;
appName = app;
reactContext = context;
childEventListeners = new HashMap<String, ChildEventListener>();
valueEventListeners = new HashMap<String, ValueEventListener>();
query = buildDatabaseQueryAtPathAndModifiers(refPath, modifiersArray);
childEventListeners = new HashMap<>();
valueEventListeners = new HashMap<>();
buildDatabaseQueryAtPathAndModifiers(refPath, modifiersArray);
}
/**
* Used outside of class for keepSynced etc.
*
* @return Query
*/
Query getQuery() {
return query;
}
/**
@ -91,28 +99,32 @@ class RNFirebaseDatabaseReference {
}
/**
* TODO
* Add a ValueEventListener to the query and internally keep a reference to it.
*
* @param eventRegistrationKey
* @param listener
*/
private void addEventListener(String eventRegistrationKey, ValueEventListener listener) {
valueEventListeners.put(eventRegistrationKey, listener);
query.addValueEventListener(listener);
}
/**
* TODO
* Add a ChildEventListener to the query and internally keep a reference to it.
*
* @param eventRegistrationKey
* @param listener
*/
private void addEventListener(String eventRegistrationKey, ChildEventListener listener) {
childEventListeners.put(eventRegistrationKey, listener);
query.addChildEventListener(listener);
}
/**
* Listen for a single 'value' event from firebase.
* Listen for a single .once('value',..) event from firebase.
*
* @param promise
*/
@ -135,7 +147,7 @@ class RNFirebaseDatabaseReference {
}
/**
* Listen for single 'child_X' event from firebase.
* Listen for single '.once(child_X, ...)' event from firebase.
*
* @param eventName
* @param promise
@ -190,7 +202,7 @@ class RNFirebaseDatabaseReference {
/**
* Handles a React Native JS 'on' request and initializes listeners.
* Handles a React Native JS '.on(..)' request and initializes listeners.
*
* @param database
* @param registration
@ -219,7 +231,7 @@ class RNFirebaseDatabaseReference {
/**
* TODO
* Add a native .on('child_X',.. ) event listener.
*
* @param registration
* @param eventType
@ -267,14 +279,14 @@ class RNFirebaseDatabaseReference {
};
addEventListener(eventRegistrationKey, childEventListener);
query.addChildEventListener(childEventListener);
}
}
/**
* TODO
*
* Add a native .on('value',.. ) event listener.
*
* @param registration
* @param database
*/
private void addValueEventListener(final ReadableMap registration, final RNFirebaseDatabase database) {
final String eventRegistrationKey = registration.getString("eventRegistrationKey");
@ -295,12 +307,12 @@ class RNFirebaseDatabaseReference {
};
addEventListener(eventRegistrationKey, valueEventListener);
query.addValueEventListener(valueEventListener);
}
}
/**
* Handles value/child update events.
*
* @param eventType
* @param dataSnapshot
* @param previousChildName
@ -332,112 +344,182 @@ class RNFirebaseDatabaseReference {
Utils.sendEvent(reactContext, "database_sync_event", event);
}
private Query buildDatabaseQueryAtPathAndModifiers(String path, ReadableArray modifiers) {
/**
* @param path
* @param modifiers
* @return
*/
private void buildDatabaseQueryAtPathAndModifiers(String path, ReadableArray modifiers) {
FirebaseDatabase firebaseDatabase = RNFirebaseDatabase.getDatabaseForApp(appName);
Query query = firebaseDatabase.getReference(path);
query = firebaseDatabase.getReference(path);
List<Object> modifiersList = Utils.recursivelyDeconstructReadableArray(modifiers);
// todo cleanup into utils
for (Object m : modifiersList) {
Map<String, Object> modifier = (Map) m;
Map modifier = (Map) m;
String type = (String) modifier.get("type");
String name = (String) modifier.get("name");
if ("orderBy".equals(type)) {
if ("orderByKey".equals(name)) {
query = query.orderByKey();
} else if ("orderByPriority".equals(name)) {
query = query.orderByPriority();
} else if ("orderByValue".equals(name)) {
query = query.orderByValue();
} else if ("orderByChild".equals(name)) {
String key = (String) modifier.get("key");
query = query.orderByChild(key);
}
applyOrderByModifier(name, type, modifier);
} else if ("limit".equals(type)) {
int limit = ((Double) modifier.get("limit")).intValue();
if ("limitToLast".equals(name)) {
query = query.limitToLast(limit);
} else if ("limitToFirst".equals(name)) {
query = query.limitToFirst(limit);
}
applyLimitModifier(name, type, modifier);
} else if ("filter".equals(type)) {
String valueType = (String) modifier.get("valueType");
String key = (String) modifier.get("key");
if ("equalTo".equals(name)) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
}
} else if ("endAt".equals(name)) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.endAt(value);
} else {
query = query.endAt(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.endAt(value);
} else {
query = query.endAt(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.endAt(value);
} else {
query = query.endAt(value, key);
}
}
} else if ("startAt".equals(name)) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.startAt(value);
} else {
query = query.startAt(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.startAt(value);
} else {
query = query.startAt(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.startAt(value);
} else {
query = query.startAt(value, key);
}
}
}
applyFilterModifier(name, modifier);
}
}
}
return query;
/* =================
* QUERY MODIFIERS
* =================
*/
/**
* @param name
* @param type
* @param modifier
*/
private void applyOrderByModifier(String name, String type, Map modifier) {
switch (name) {
case "orderByKey":
query = query.orderByKey();
break;
case "orderByPriority":
query = query.orderByPriority();
break;
case "orderByValue":
query = query.orderByValue();
break;
case "orderByChild":
String key = (String) modifier.get("key");
query = query.orderByChild(key);
}
}
/**
* @param name
* @param type
* @param modifier
*/
private void applyLimitModifier(String name, String type, Map modifier) {
int limit = ((Double) modifier.get("limit")).intValue();
if ("limitToLast".equals(name)) {
query = query.limitToLast(limit);
} else if ("limitToFirst".equals(name)) {
query = query.limitToFirst(limit);
}
}
/**
* @param name
* @param modifier
*/
private void applyFilterModifier(String name, Map modifier) {
String valueType = (String) modifier.get("valueType");
String key = (String) modifier.get("key");
if ("equalTo".equals(name)) {
applyEqualToFilter(key, valueType, modifier);
} else if ("endAt".equals(name)) {
applyEndAtFilter(key, valueType, modifier);
} else if ("startAt".equals(name)) {
applyStartAtFilter(key, valueType, modifier);
}
}
/* ===============
* QUERY FILTERS
* ===============
*/
/**
* @param key
* @param valueType
* @param modifier
*/
private void applyEqualToFilter(String key, String valueType, Map modifier) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
}
}
/**
* @param key
* @param valueType
* @param modifier
*/
private void applyEndAtFilter(String key, String valueType, Map modifier) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.endAt(value);
} else {
query = query.endAt(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.endAt(value);
} else {
query = query.endAt(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.endAt(value);
} else {
query = query.endAt(value, key);
}
}
}
/**
* @param key
* @param valueType
* @param modifier
*/
private void applyStartAtFilter(String key, String valueType, Map modifier) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.startAt(value);
} else {
query = query.startAt(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.startAt(value);
} else {
query = query.startAt(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.startAt(value);
} else {
query = query.startAt(value, key);
}
}
}
}