more work on multiple apps support

This commit is contained in:
Salakar 2017-07-04 13:05:19 +01:00
parent ec22e510bc
commit f97db8eba1
6 changed files with 84 additions and 50 deletions

View File

@ -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());
}

View File

@ -3,7 +3,7 @@
#import <React/RCTBridgeModule.h>
@interface RNFirebaseRemoteConfig : NSObject <RCTBridgeModule>
@interface RNFirebaseConfig : NSObject <RCTBridgeModule>
@end

View File

@ -1,12 +1,12 @@
#import "RNFirebaseRemoteConfig.h"
#import "RNFirebaseConfig.h"
#if __has_include(<FirebaseRemoteConfig/FirebaseRemoteConfig.h>)
#import "FirebaseRemoteConfig/FirebaseRemoteConfig.h"
#import <React/RCTConvert.h>
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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}