From dd940b953bfd641f9fe1b5645c8d3fb166e52df5 Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Tue, 27 Mar 2018 17:31:25 +0100 Subject: [PATCH] [firestore] Add support for enable / disable network --- .../firestore/RNFirebaseFirestore.java | 35 +++++++++++++++++-- .../firestore/RNFirebaseFirestore.m | 25 +++++++++++++ lib/index.d.ts | 2 ++ lib/modules/firestore/index.js | 27 +++++--------- tests/src/tests/firestore/firestoreTests.js | 21 +++-------- 5 files changed, 72 insertions(+), 38 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/firestore/RNFirebaseFirestore.java b/android/src/main/java/io/invertase/firebase/firestore/RNFirebaseFirestore.java index 48191f53..d2efa140 100644 --- a/android/src/main/java/io/invertase/firebase/firestore/RNFirebaseFirestore.java +++ b/android/src/main/java/io/invertase/firebase/firestore/RNFirebaseFirestore.java @@ -18,6 +18,7 @@ import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.Task; import com.google.firebase.FirebaseApp; +import com.google.firebase.firestore.FirebaseFirestoreSettings; import com.google.firebase.firestore.Transaction; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.FieldValue; @@ -47,14 +48,42 @@ public class RNFirebaseFirestore extends ReactContextBaseJavaModule { * REACT NATIVE METHODS */ - /** - * @param enabled - */ + @ReactMethod + public void disableNetwork(String appName, final Promise promise) { + getFirestoreForApp(appName).disableNetwork().addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + Log.d(TAG, "disableNetwork:onComplete:success"); + promise.resolve(null); + } else { + Log.e(TAG, "disableNetwork:onComplete:failure", task.getException()); + RNFirebaseFirestore.promiseRejectException(promise, (FirebaseFirestoreException)task.getException()); + } + } + }); + } + @ReactMethod public void enableLogging(Boolean enabled) { FirebaseFirestore.setLoggingEnabled(enabled); } + @ReactMethod + public void enableNetwork(String appName, final Promise promise) { + getFirestoreForApp(appName).enableNetwork().addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + Log.d(TAG, "enableNetwork:onComplete:success"); + promise.resolve(null); + } else { + Log.e(TAG, "enableNetwork:onComplete:failure", task.getException()); + RNFirebaseFirestore.promiseRejectException(promise, (FirebaseFirestoreException)task.getException()); + } + } + }); + } @ReactMethod public void collectionGet(String appName, String path, ReadableArray filters, diff --git a/ios/RNFirebase/firestore/RNFirebaseFirestore.m b/ios/RNFirebase/firestore/RNFirebaseFirestore.m index 5bd71328..6bfe58a9 100644 --- a/ios/RNFirebase/firestore/RNFirebaseFirestore.m +++ b/ios/RNFirebase/firestore/RNFirebaseFirestore.m @@ -185,11 +185,36 @@ RCT_EXPORT_METHOD(transactionBegin:(NSString *)appDisplayName * TRANSACTIONS END */ +RCT_EXPORT_METHOD(disableNetwork:(NSString *)appDisplayName + resolver:(RCTPromiseResolveBlock) resolve + rejecter:(RCTPromiseRejectBlock) reject) { + FIRFirestore *firestore = [RNFirebaseFirestore getFirestoreForApp:appDisplayName]; + [firestore disableNetworkWithCompletion:^(NSError * _Nullable error) { + if (error) { + [RNFirebaseFirestore promiseRejectException:reject error:error]; + } else { + resolve(nil); + } + }]; +} RCT_EXPORT_METHOD(enableLogging:(BOOL)enabled) { [FIRFirestore enableLogging:enabled]; } +RCT_EXPORT_METHOD(enableNetwork:(NSString *)appDisplayName + resolver:(RCTPromiseResolveBlock) resolve + rejecter:(RCTPromiseRejectBlock) reject) { + FIRFirestore *firestore = [RNFirebaseFirestore getFirestoreForApp:appDisplayName]; + [firestore enableNetworkWithCompletion:^(NSError * _Nullable error) { + if (error) { + [RNFirebaseFirestore promiseRejectException:reject error:error]; + } else { + resolve(nil); + } + }]; +} + RCT_EXPORT_METHOD(collectionGet:(NSString *)appDisplayName path:(NSString *)path filters:(NSArray *)filters diff --git a/lib/index.d.ts b/lib/index.d.ts index 7e4175fc..c04bef1b 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1149,7 +1149,9 @@ declare module "react-native-firebase" { readonly app: App; batch(): WriteBatch; collection(collectionPath: string): CollectionReference; + disableNetwork(): Promise doc(documentPath: string): DocumentReference; + enableNetwork(): Promise /** NOT SUPPORTED YET */ // enablePersistence(): Promise; diff --git a/lib/modules/firestore/index.js b/lib/modules/firestore/index.js index e8047894..08438585 100644 --- a/lib/modules/firestore/index.js +++ b/lib/modules/firestore/index.js @@ -16,6 +16,7 @@ import WriteBatch from './WriteBatch'; import TransactionHandler from './TransactionHandler'; import Transaction from './Transaction'; import INTERNALS from '../../utils/internals'; +import { getNativeModule } from '../../utils/native'; import type DocumentSnapshot from './DocumentSnapshot'; import type App from '../core/app'; @@ -110,6 +111,10 @@ export default class Firestore extends ModuleBase { return new CollectionReference(this, path); } + disableNetwork(): void { + return getNativeModule(this).disableNetwork(); + } + /** * Gets a DocumentReference instance that refers to the document at the specified path. * @@ -125,6 +130,10 @@ export default class Firestore extends ModuleBase { return new DocumentReference(this, path); } + enableNetwork(): Promise { + return getNativeModule(this).enableNetwork(); + } + /** * Executes the given updateFunction and then attempts to commit the * changes applied within the transaction. If any document read within @@ -156,24 +165,6 @@ export default class Firestore extends ModuleBase { ); } - enableNetwork(): void { - throw new Error( - INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( - 'firestore', - 'enableNetwork' - ) - ); - } - - disableNetwork(): void { - throw new Error( - INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD( - 'firestore', - 'disableNetwork' - ) - ); - } - /** * ------------- * MISC diff --git a/tests/src/tests/firestore/firestoreTests.js b/tests/src/tests/firestore/firestoreTests.js index a6dc4e7c..10915f21 100644 --- a/tests/src/tests/firestore/firestoreTests.js +++ b/tests/src/tests/firestore/firestoreTests.js @@ -126,23 +126,10 @@ function firestoreTests({ before, describe, it, context, firebase }) { }); }); - context('disableNetwork()', () => { - it('should throw an unsupported error', () => { - (() => { - firebase.native.firestore().disableNetwork(); - }).should.throw( - 'firebase.firestore().disableNetwork() is unsupported by the native Firebase SDKs.' - ); - }); - }); - - context('enableNetwork()', () => { - it('should throw an unsupported error', () => { - (() => { - firebase.native.firestore().enableNetwork(); - }).should.throw( - 'firebase.firestore().enableNetwork() is unsupported by the native Firebase SDKs.' - ); + context('disable/enableNetwork()', () => { + it('should work without error', async () => { + await firebase.native.firestore().disableNetwork(); + await firebase.native.firestore().enableNetwork(); }); });