From 46772d253da8bea0f006f0025938e462f27d5785 Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Wed, 19 Jul 2017 18:18:16 +0100 Subject: [PATCH] [android][ios][database] Add missing setPriority and setWithPriority methods --- .../firebase/database/RNFirebaseDatabase.java | 39 ++++++++++++++++- ios/RNFirebase/database/RNFirebaseDatabase.m | 19 ++++++++ lib/modules/database/reference.js | 23 ++++++++++ tests/ios/Podfile.lock | 23 +++++----- tests/src/tests/database/ref/index.js | 2 + tests/src/tests/database/ref/priorityTests.js | 43 +++++++++++++++++++ 6 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 tests/src/tests/database/ref/priorityTests.js diff --git a/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabase.java b/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabase.java index ba2dcca6..23b3d059 100644 --- a/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabase.java +++ b/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabase.java @@ -88,7 +88,6 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule { DatabaseReference ref = mFirebaseDatabase.getReference(path); Map m = Utils.recursivelyDeconstructReadableMap(props); - DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference ref) { @@ -99,6 +98,44 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule { ref.setValue(m.get("value"), listener); } + @ReactMethod + public void priority( + final String path, + final ReadableMap priority, + final Callback callback) { + DatabaseReference ref = mFirebaseDatabase.getReference(path); + Map priorityMap = Utils.recursivelyDeconstructReadableMap(priority); + + DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() { + @Override + public void onComplete(DatabaseError error, DatabaseReference ref) { + handleCallback("priority", callback, error); + } + }; + + ref.setPriority(priorityMap.get("value"), listener); + } + + @ReactMethod + public void withPriority( + final String path, + final ReadableMap data, + final ReadableMap priority, + final Callback callback) { + DatabaseReference ref = mFirebaseDatabase.getReference(path); + Map dataMap = Utils.recursivelyDeconstructReadableMap(data); + Map priorityMap = Utils.recursivelyDeconstructReadableMap(priority); + + DatabaseReference.CompletionListener listener = new DatabaseReference.CompletionListener() { + @Override + public void onComplete(DatabaseError error, DatabaseReference ref) { + handleCallback("withPriority", callback, error); + } + }; + + ref.setValue(dataMap.get("value"), priorityMap.get("value"), listener); + } + @ReactMethod public void update(final String path, final ReadableMap props, diff --git a/ios/RNFirebase/database/RNFirebaseDatabase.m b/ios/RNFirebase/database/RNFirebaseDatabase.m index d2bb6645..e7bbcd00 100644 --- a/ios/RNFirebase/database/RNFirebaseDatabase.m +++ b/ios/RNFirebase/database/RNFirebaseDatabase.m @@ -148,6 +148,25 @@ RCT_EXPORT_METHOD(set: }]; } +RCT_EXPORT_METHOD(priority:(NSString *) path + priorityData:(NSDictionary *) priorityData + callback:(RCTResponseSenderBlock) callback) { + FIRDatabaseReference *ref = [self getPathRef:path]; + [ref setPriority:[priorityData valueForKey:@"value"] withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) { + [self handleCallback:@"priority" callback:callback databaseError:error]; + }]; +} + +RCT_EXPORT_METHOD(withPriority:(NSString *) path + data:(NSDictionary *) data + priorityData:(NSDictionary *) priorityData + callback:(RCTResponseSenderBlock) callback) { + FIRDatabaseReference *ref = [self getPathRef:path]; + [ref setValue:[data valueForKey:@"value"] andPriority:[priorityData valueForKey:@"value"] withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) { + [self handleCallback:@"withPriority" callback:callback databaseError:error]; + }]; +} + RCT_EXPORT_METHOD(update: (NSString *) path value: diff --git a/lib/modules/database/reference.js b/lib/modules/database/reference.js index 9137767f..fcd22b96 100644 --- a/lib/modules/database/reference.js +++ b/lib/modules/database/reference.js @@ -91,6 +91,29 @@ export default class Reference extends ReferenceBase { return promisify('set', FirebaseDatabase)(path, _value); } + /** + * + * @param priority + * @returns {*} + */ + setPriority(priority: string | number | null) { + const path = this.path; + const _priority = this._serializeAnyType(priority); + return promisify('priority', FirebaseDatabase)(path, _priority); + } + + /** + * + * @param priority + * @returns {*} + */ + setWithPriority(value: any, priority: string | number | null) { + const path = this.path; + const _priority = this._serializeAnyType(priority); + const _value = this._serializeAnyType(value); + return promisify('withPriority', FirebaseDatabase)(path, _value, _priority); + } + /** * * @param val diff --git a/tests/ios/Podfile.lock b/tests/ios/Podfile.lock index d72cb893..846f0ca9 100644 --- a/tests/ios/Podfile.lock +++ b/tests/ios/Podfile.lock @@ -90,16 +90,17 @@ PODS: - GoogleToolboxForMac/NSString+URLArguments (2.1.1) - GTMSessionFetcher/Core (1.1.10) - Protobuf (3.3.0) - - React (0.44.3): - - React/Core (= 0.44.3) - - React/Core (0.44.3): + - React (0.44.0): + - React/Core (= 0.44.0) + - React/Core (0.44.0): - React/cxxreact - - Yoga (= 0.44.3.React) - - React/cxxreact (0.44.3): + - Yoga (= 0.44.0.React) + - React/cxxreact (0.44.0): - React/jschelpers - - React/jschelpers (0.44.3) - - RNFirebase (2.0.0) - - Yoga (0.44.3.React) + - React/jschelpers (0.44.0) + - RNFirebase (2.0.4): + - React + - Yoga (0.44.0.React) DEPENDENCIES: - Firebase/AdMob @@ -142,9 +143,9 @@ SPEC CHECKSUMS: GoogleToolboxForMac: 8e329f1b599f2512c6b10676d45736bcc2cbbeb0 GTMSessionFetcher: 30d874b96d0d76028f61fbd122801e3f030d47db Protobuf: d582fecf68201eac3d79ed61369ef45734394b9c - React: 6361345ebeb769a929e10a06baf0c868d6d03ad5 - RNFirebase: dcc4dcb1c9400a9bc01866e50d1351272a7df311 - Yoga: c90474ca3ec1edba44c97b6c381f03e222a9e287 + React: d2077cc20245ccdc8bfe1fdc002f2003318ae8d8 + RNFirebase: 3e5a3ff431c5c8a997152f5f19d354e1ca66f65d + Yoga: a92a5d8e128905bf9f29c82f870192a6e873dd98 PODFILE CHECKSUM: 45666f734ebfc8b3b0f2be0a83bc2680caeb502f diff --git a/tests/src/tests/database/ref/index.js b/tests/src/tests/database/ref/index.js index 8cc11c01..7644947f 100644 --- a/tests/src/tests/database/ref/index.js +++ b/tests/src/tests/database/ref/index.js @@ -17,6 +17,7 @@ import rootTests from './rootTests'; import transactionTests from './transactionTests'; import queryTests from './queryTests'; import issueSpecificTests from './issueSpecificTests'; +import priorityTests from './priorityTests'; import DatabaseContents from '../../support/DatabaseContents'; @@ -24,6 +25,7 @@ const testGroups = [ issueSpecificTests, factoryTests, keyTests, parentTests, childTests, rootTests, pushTests, onTests, onValueTests, onChildAddedTests, offTests, onceTests, updateTests, removeTests, setTests, transactionTests, queryTests, refTests, isEqualTests, + priorityTests, ]; function registerTestSuite(testSuite) { diff --git a/tests/src/tests/database/ref/priorityTests.js b/tests/src/tests/database/ref/priorityTests.js new file mode 100644 index 00000000..cc73ff93 --- /dev/null +++ b/tests/src/tests/database/ref/priorityTests.js @@ -0,0 +1,43 @@ +import DatabaseContents from '../../support/DatabaseContents'; + +function setTests({ describe, it, firebase }) { + describe('ref().priority', () => { + it('setPriority() should correctly set a priority for all non-null values', async () => { + await Promise.map(Object.keys(DatabaseContents.DEFAULT), async (dataRef) => { + // Setup + + const ref = firebase.native.database().ref(`tests/types/${dataRef}`); + + // Test + + await ref.setPriority(1); + + // Assertion + + await ref.once('value').then((snapshot) => { + if (snapshot.val() !== null) { + snapshot.getPriority().should.eql(1); + } + }); + }); + }); + + it('setWithPriority() should correctly set the priority', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/types/number'); + + // Test + + await ref.setWithPriority(DatabaseContents.DEFAULT.number, '2'); + + // Assertion + + await ref.once('value').then((snapshot) => { + snapshot.getPriority().should.eql('2'); + }); + }); + }); +} + +export default setTests;