[android][app] apps initialized natively now automatically initialized js side - via react module getConstants

This commit is contained in:
Salakar 2017-07-17 17:36:50 +01:00
parent ef259fbbc0
commit 9a3e22431b
5 changed files with 52 additions and 13 deletions

View File

@ -2,6 +2,8 @@ package io.invertase.firebase;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
@ -102,7 +104,28 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
FirebaseApp firebaseApp;
Map<String, Object> constants = new HashMap<>();
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(getReactApplicationContext());
List<Map<String, Object>> appMapsList = new ArrayList<Map<String, Object>>();
for (FirebaseApp app : firebaseAppList) {
String appName = app.getName();
FirebaseOptions appOptions = app.getOptions();
Map<String, Object> appProps = new HashMap<>();
appProps.put("name", appName);
appProps.put("apiKey", appOptions.getApiKey());
appProps.put("applicationId", appOptions.getApplicationId());
appProps.put("databaseUrl", appOptions.getDatabaseUrl());
appProps.put("messagingSenderId", appOptions.getGcmSenderId());
appProps.put("projectId", appOptions.getProjectId());
appProps.put("storageBucket", appOptions.getStorageBucket());
// TODO no way to get client id currently from app options
appMapsList.add(appProps);
}
constants.put("apps", appMapsList);
constants.put("googleApiAvailability", getPlayServicesStatus());
return constants;
}

View File

@ -12,10 +12,8 @@ import Storage, { statics as StorageStatics } from './modules/storage';
import Database, { statics as DatabaseStatics } from './modules/database';
import Messaging, { statics as MessagingStatics } from './modules/messaging';
const FirebaseCoreModule = NativeModules.RNFirebase;
export default class FirebaseApp {
constructor(name: string, options: Object = {}) {
this._name = name;
@ -26,14 +24,27 @@ export default class FirebaseApp {
this._intialized = false;
}
_initializeApp() {
FirebaseCoreModule.initializeApp(this._name, this._options, (error, result) => {
// todo check error/result
_initializeApp(local = false) {
if (local) {
// for apps already initialized natively that we have info
// from RN constants.
this._initialized = true;
});
} else {
FirebaseCoreModule.initializeApp(this._name, this._options, (error, result) => {
// todo check error/result
this._initialized = true;
});
}
}
get name() {
if (this._name === INTERNALS.STRINGS.DEFAULT_APP_NAME) {
// ios and android firebase sdk's return different
// app names - so we just return what the web sdk
// would if it was default.
return '[DEFAULT]';
}
return this._name;
}

View File

@ -27,6 +27,14 @@ class FirebaseCore {
constructor() {
this._nativeEmitters = {};
this._nativeSubscriptions = {};
for (let i = 0, len = FirebaseCoreModule.apps.length; i < len; i++) {
const app = FirebaseCoreModule.apps[i];
const options = Object.assign({}, app);
delete options.name;
INTERNALS.APPS[app.name] = new FirebaseApp(app.name, options);
INTERNALS.APPS[app.name]._initializeApp(true);
}
}
/**

View File

@ -31,7 +31,7 @@ export default class ModuleBase {
this._options = Object.assign({}, options);
this._module = moduleName;
this._firebaseApp = firebaseApp;
this._appName = firebaseApp.name;
this._appName = firebaseApp._name;
this._namespace = `${this._appName}:${this._module}`;
// check if native module exists as all native

View File

@ -13,13 +13,10 @@ const config = {
const instances = {
web: firebase.initializeApp(config),
native: RNfirebase.initializeApp({
debug: __DEV__ ? '*' : false,
errorOnMissingPlayServices: false,
persistence: true,
}),
native: RNfirebase.app(),
};
console.log('RNApps -->', RNfirebase.apps);
instances.web.database().ref('tests/types').set(DatabaseContents.DEFAULT);