diff --git a/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java b/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java index 3e1e0c41..85e8bda9 100644 --- a/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java +++ b/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java @@ -14,6 +14,7 @@ import com.facebook.react.bridge.WritableArray; import com.google.android.gms.tasks.OnCompleteListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.google.firebase.FirebaseApp; import com.google.firebase.remoteconfig.FirebaseRemoteConfig; import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue; import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings; @@ -47,10 +48,12 @@ class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule { return TAG; } - @ReactMethod - public void enableDeveloperMode(String appName) { + @ReactMethod public void enableDeveloperMode(String appName) { + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + FirebaseRemoteConfigSettings.Builder settings = new FirebaseRemoteConfigSettings.Builder(); settings.setDeveloperModeEnabled(true); + FirebaseRemoteConfig.getInstance().setConfigSettings(settings.build()); } diff --git a/ios/RNFirebase/config/RNFirebaseRemoteConfig.h b/ios/RNFirebase/config/RNFirebaseConfig.h similarity index 64% rename from ios/RNFirebase/config/RNFirebaseRemoteConfig.h rename to ios/RNFirebase/config/RNFirebaseConfig.h index 98b65028..e9e37850 100644 --- a/ios/RNFirebase/config/RNFirebaseRemoteConfig.h +++ b/ios/RNFirebase/config/RNFirebaseConfig.h @@ -3,7 +3,7 @@ #import -@interface RNFirebaseRemoteConfig : NSObject +@interface RNFirebaseConfig : NSObject @end diff --git a/ios/RNFirebase/config/RNFirebaseRemoteConfig.m b/ios/RNFirebase/config/RNFirebaseConfig.m similarity index 60% rename from ios/RNFirebase/config/RNFirebaseRemoteConfig.m rename to ios/RNFirebase/config/RNFirebaseConfig.m index e2e6951e..acf9ecc1 100644 --- a/ios/RNFirebase/config/RNFirebaseRemoteConfig.m +++ b/ios/RNFirebase/config/RNFirebaseConfig.m @@ -1,12 +1,12 @@ -#import "RNFirebaseRemoteConfig.h" +#import "RNFirebaseConfig.h" #if __has_include() + #import "FirebaseRemoteConfig/FirebaseRemoteConfig.h" #import -NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus value) -{ - switch(value){ +NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus value) { + switch (value) { case FIRRemoteConfigFetchStatusNoFetchYet: return @"config/no_fetch_yet"; case FIRRemoteConfigFetchStatusFailure: @@ -18,9 +18,8 @@ NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus } } -NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) -{ - switch(value) { +NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) { + switch (value) { case FIRRemoteConfigSourceDefault: return @"default"; case FIRRemoteConfigSourceRemote: @@ -30,33 +29,29 @@ NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) } } -NSDictionary *convertFIRRemoteConfigValueToNSDictionary(FIRRemoteConfigValue *value) -{ - return @{ - @"stringValue" : value.stringValue ?: [NSNull null], - @"numberValue" : value.numberValue ?: [NSNull null], - @"dataValue" : value.dataValue ? [value.dataValue base64EncodedStringWithOptions:0] : [NSNull null], - @"boolValue" : @(value.boolValue), - @"source" : convertFIRRemoteConfigSourceToNSString(value.source) - }; +NSDictionary *convertFIRRemoteConfigValueToNSDictionary(FIRRemoteConfigValue *value) { + return @{@"stringValue": value.stringValue ?: [NSNull null], @"numberValue": value.numberValue ?: [NSNull null], @"dataValue": value.dataValue ? [value.dataValue base64EncodedStringWithOptions:0] : [NSNull null], @"boolValue": @(value.boolValue), @"source": convertFIRRemoteConfigSourceToNSString(value.source)}; } -@interface RNFirebaseRemoteConfig () +@interface RNFirebaseConfig () -@property (nonatomic, readwrite, weak) FIRRemoteConfig *remoteConfig; +@property(nonatomic, readwrite, weak) FIRRemoteConfig *remoteConfig; @end -@implementation RNFirebaseRemoteConfig +@implementation RNFirebaseConfig -RCT_EXPORT_MODULE(RNFirebaseRemoteConfig); +RCT_EXPORT_MODULE(RNFirebaseConfig); RCT_EXPORT_METHOD(enableDeveloperMode) { FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES]; [FIRRemoteConfig remoteConfig].configSettings = remoteConfigSettings; } -RCT_EXPORT_METHOD(fetch:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { +RCT_EXPORT_METHOD(fetch: + (RCTPromiseResolveBlock) resolve + rejecter: + (RCTPromiseRejectBlock) reject) { [[FIRRemoteConfig remoteConfig] fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *__nullable error) { if (error) { RCTLogError(@"\nError: %@", RCTJSErrorFromNSError(error)); @@ -67,10 +62,11 @@ RCT_EXPORT_METHOD(fetch:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseReje }]; } -RCT_EXPORT_METHOD(fetchWithExpirationDuration:(nonnull NSNumber *)expirationDuration - resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) -{ +RCT_EXPORT_METHOD(fetchWithExpirationDuration: + (nonnull + NSNumber *)expirationDuration + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { [[FIRRemoteConfig remoteConfig] fetchWithExpirationDuration:expirationDuration.doubleValue completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *__nullable error) { if (error) { RCTLogError(@"\nError: %@", RCTJSErrorFromNSError(error)); @@ -81,37 +77,44 @@ RCT_EXPORT_METHOD(fetchWithExpirationDuration:(nonnull NSNumber *)expirationDura }]; } -RCT_EXPORT_METHOD(activateFetched:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) -{ +RCT_EXPORT_METHOD(activateFetched: + (RCTPromiseResolveBlock) resolve + rejecter: + (RCTPromiseRejectBlock) reject) { BOOL status = [[FIRRemoteConfig remoteConfig] activateFetched]; resolve(@(status)); } -RCT_EXPORT_METHOD(getValue:(NSString *)key - resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) -{ +RCT_EXPORT_METHOD(getValue: + (NSString *) key + resolver: + (RCTPromiseResolveBlock) resolve + rejecter: + (RCTPromiseRejectBlock) reject) { FIRRemoteConfigValue *value = [[FIRRemoteConfig remoteConfig] configValueForKey:key]; resolve(convertFIRRemoteConfigValueToNSDictionary(value)); } -RCT_EXPORT_METHOD(getValues:(NSArray *)keys - resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) -{ +RCT_EXPORT_METHOD(getValues: + (NSArray *) keys + resolver: + (RCTPromiseResolveBlock) resolve + rejecter: + (RCTPromiseRejectBlock) reject) { NSMutableArray *valuesArray = [[NSMutableArray alloc] init]; for (NSString *key in keys) { - FIRRemoteConfigValue *value = [[FIRRemoteConfig remoteConfig] configValueForKey:key]; + FIRRemoteConfigValue *value = [[FIRRemoteConfig remoteConfig] configValueForKey:key]; [valuesArray addObject:convertFIRRemoteConfigValueToNSDictionary(value)]; } resolve(valuesArray); } -RCT_EXPORT_METHOD(getKeysByPrefix:(NSString *)prefix - resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject) -{ +RCT_EXPORT_METHOD(getKeysByPrefix: + (NSString *) prefix + resolver: + (RCTPromiseResolveBlock) resolve + rejecter: + (RCTPromiseRejectBlock) reject) { NSSet *keys = [[FIRRemoteConfig remoteConfig] keysWithPrefix:prefix]; NSMutableArray *keysArray = [[NSMutableArray alloc] init]; for (NSString *key in keys) { @@ -120,17 +123,19 @@ RCT_EXPORT_METHOD(getKeysByPrefix:(NSString *)prefix resolve(keysArray); } -RCT_EXPORT_METHOD(setDefaults:(NSDictionary *)defaults) { +RCT_EXPORT_METHOD(setDefaults: + (NSDictionary *) defaults) { [[FIRRemoteConfig remoteConfig] setDefaults:defaults]; } -RCT_EXPORT_METHOD(setDefaultsFromResource:(NSString *)fileName) { +RCT_EXPORT_METHOD(setDefaultsFromResource: + (NSString *) fileName) { [[FIRRemoteConfig remoteConfig] setDefaultsFromPlistFileName:fileName]; } @end #else -@implementation RNFirebaseRemoteConfig +@implementation RNFirebaseConfig @end #endif diff --git a/lib/firebase-app.js b/lib/firebase-app.js index 33344136..54264a1d 100644 --- a/lib/firebase-app.js +++ b/lib/firebase-app.js @@ -3,12 +3,16 @@ import { NativeModules } from 'react-native'; import INTERNALS from './internals'; // modules +// todo import Crash from './modules/crash'; import Performance from './modules/perf'; -import RemoteConfig from './modules/config'; import Analytics from './modules/analytics'; -import Auth, { statics as AuthStatics } from './modules/auth'; import AdMob, { statics as AdMobStatics } from './modules/admob'; +// ---- + + +import Auth, { statics as AuthStatics } from './modules/auth'; +import RemoteConfig from './modules/config'; import Storage, { statics as StorageStatics } from './modules/storage'; import Database, { statics as DatabaseStatics } from './modules/database'; import Messaging, { statics as MessagingStatics } from './modules/messaging'; @@ -56,6 +60,10 @@ export default class FirebaseApp { return this._staticsOrModuleInstance('auth', AuthStatics, Auth); } + get config() { + return this._staticsOrModuleInstance('config', {}, RemoteConfig); + } + get database() { return this._staticsOrModuleInstance('database', DatabaseStatics, Database); } diff --git a/lib/firebase.js b/lib/firebase.js index 3ff6d556..7fa09029 100644 --- a/lib/firebase.js +++ b/lib/firebase.js @@ -12,6 +12,7 @@ import FirebaseApp from './firebase-app'; // module imports import Auth, { statics as AuthStatics } from './modules/auth'; +import RemoteConfig from './modules/config'; import Storage, { statics as StorageStatics } from './modules/storage'; import Database, { statics as DatabaseStatics } from './modules/database'; import Messaging, { statics as MessagingStatics } from './modules/messaging'; @@ -110,6 +111,11 @@ class FirebaseCore { return this._appNamespaceOrStatics('auth', AuthStatics, Auth); } + get config() { + return this._appNamespaceOrStatics('config', {}, RemoteConfig); + } + + get database() { return this._appNamespaceOrStatics('database', DatabaseStatics, Database); } diff --git a/lib/utils/ModuleBase.js b/lib/utils/ModuleBase.js index 9999acfe..469e646e 100644 --- a/lib/utils/ModuleBase.js +++ b/lib/utils/ModuleBase.js @@ -8,6 +8,13 @@ import INTERNALS from './../internals'; const logs = {}; +// Firebase Native SDKs that support multiple app instances +const MULTI_APP_MODULES = [ + 'auth', + 'database', + 'storage', +]; + export default class ModuleBase { constructor(firebaseApp, options, moduleName, withEventEmitter = false) { this._options = Object.assign({}, options); @@ -24,7 +31,12 @@ export default class ModuleBase { } // used by the modules that extend ModuleBase to access their native module counterpart - this._native = nativeWithApp(this._appName, nativeModule); + if (!MULTI_APP_MODULES.includes(moduleName.toLowerCase())) { + this._native = nativeModule; + } else { + this._native = nativeWithApp(this._appName, nativeModule); + } + if (withEventEmitter) { this._eventEmitter = new NativeEventEmitter(this._native); }