From b322af0f4026c2f807e404583505b2c70b6e18b0 Mon Sep 17 00:00:00 2001 From: Salakar Date: Thu, 17 Aug 2017 16:25:29 +0100 Subject: [PATCH] [android][database] final cleanup of RNFirebaseDatabaseReference for v3. --- .../database/RNFirebaseDatabaseReference.java | 308 +++++++++++------- 1 file changed, 195 insertions(+), 113 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java b/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java index e45eea25..d38659e8 100644 --- a/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java +++ b/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java @@ -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 childEventListeners; private HashMap 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(); - valueEventListeners = new HashMap(); - 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 modifiersList = Utils.recursivelyDeconstructReadableArray(modifiers); - // todo cleanup into utils for (Object m : modifiersList) { - Map 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); + } + } } }