Merge branch 'omer_links' of github.com:rmrs/react-native-firebase into omer_links
This commit is contained in:
commit
17558162fc
|
@ -9,6 +9,7 @@ import android.util.Log;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.facebook.react.bridge.ActivityEventListener;
|
import com.facebook.react.bridge.ActivityEventListener;
|
||||||
|
import com.facebook.react.bridge.LifecycleEventListener;
|
||||||
import com.facebook.react.bridge.Promise;
|
import com.facebook.react.bridge.Promise;
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
|
@ -27,14 +28,14 @@ import com.google.android.gms.tasks.OnFailureListener;
|
||||||
|
|
||||||
import io.invertase.firebase.Utils;
|
import io.invertase.firebase.Utils;
|
||||||
|
|
||||||
public class RNFirebaseLinks extends ReactContextBaseJavaModule implements ActivityEventListener {
|
public class RNFirebaseLinks extends ReactContextBaseJavaModule implements ActivityEventListener ,LifecycleEventListener {
|
||||||
private final static String TAG = RNFirebaseLinks.class.getCanonicalName();
|
private final static String TAG = RNFirebaseLinks.class.getCanonicalName();
|
||||||
private String initialLink = null;
|
private String initialLink = null;
|
||||||
|
|
||||||
public RNFirebaseLinks(ReactApplicationContext reactContext) {
|
public RNFirebaseLinks(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
getReactApplicationContext().addActivityEventListener(this);
|
getReactApplicationContext().addActivityEventListener(this);
|
||||||
registerLinksHandler();
|
getReactApplicationContext().addLifecycleEventListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -52,7 +53,6 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
if (activity == null) {
|
if (activity == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FirebaseDynamicLinks.getInstance()
|
FirebaseDynamicLinks.getInstance()
|
||||||
.getDynamicLink(activity.getIntent())
|
.getDynamicLink(activity.getIntent())
|
||||||
.addOnSuccessListener(activity, new OnSuccessListener<PendingDynamicLinkData>() {
|
.addOnSuccessListener(activity, new OnSuccessListener<PendingDynamicLinkData>() {
|
||||||
|
@ -62,10 +62,10 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
if (pendingDynamicLinkData != null) {
|
if (pendingDynamicLinkData != null) {
|
||||||
Uri deepLinkUri = pendingDynamicLinkData.getLink();
|
Uri deepLinkUri = pendingDynamicLinkData.getLink();
|
||||||
String deepLink = deepLinkUri.toString();
|
String deepLink = deepLinkUri.toString();
|
||||||
// TODO: Validate that this is called when opening from a deep link
|
|
||||||
if (initialLink == null) {
|
if (initialLink == null) {
|
||||||
initialLink = deepLink;
|
initialLink = deepLink;
|
||||||
}
|
}
|
||||||
|
Log.d(TAG, "sending a dynamic_link_received event!");
|
||||||
Utils.sendEvent(getReactApplicationContext(), "dynamic_link_received", deepLink);
|
Utils.sendEvent(getReactApplicationContext(), "dynamic_link_received", deepLink);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,20 +84,32 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewIntent(Intent intent) {
|
public void onNewIntent(Intent intent) {}
|
||||||
// TODO: Do I need to re-register the links handler for each new intent?
|
|
||||||
|
@Override
|
||||||
|
public void onHostResume() {
|
||||||
|
registerLinksHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHostPause() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHostDestroy() {
|
||||||
|
initialLink = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void createDynamicLink(final ReadableMap parameters, final Promise promise) {
|
public void createDynamicLink(final ReadableMap parameters, final Promise promise) {
|
||||||
try {
|
try {
|
||||||
DynamicLink.Builder builder = setDynamicLinkBuilderFromMap(parameters);
|
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(parameters);
|
||||||
Uri link = builder.buildDynamicLink().getUri();
|
|
||||||
|
|
||||||
Log.d(TAG, "created dynamic link: " + link.toString());
|
DynamicLink.Builder builder = getDynamicLinkBuilderFromMap(m);
|
||||||
promise.resolve(link.toString());
|
Uri link = builder.buildDynamicLink().getUri();
|
||||||
}
|
|
||||||
catch(Exception ex) {
|
Log.d(TAG, "created dynamic link: " + link.toString());
|
||||||
|
promise.resolve(link.toString());
|
||||||
|
} catch (Exception ex) {
|
||||||
Log.e(TAG, "create dynamic link failure " + ex.getMessage());
|
Log.e(TAG, "create dynamic link failure " + ex.getMessage());
|
||||||
promise.reject("links/failure", ex.getMessage(), ex);
|
promise.reject("links/failure", ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
|
@ -105,49 +117,54 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void createShortDynamicLink(final ReadableMap parameters, final Promise promise) {
|
public void createShortDynamicLink(final ReadableMap parameters, final Promise promise) {
|
||||||
try {
|
try {
|
||||||
DynamicLink.Builder builder = setDynamicLinkBuilderFromMap(parameters);
|
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(parameters);
|
||||||
Task<ShortDynamicLink> shortLinkTask = builder.buildShortDynamicLink()
|
|
||||||
.addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
|
DynamicLink.Builder builder = getDynamicLinkBuilderFromMap(m);
|
||||||
@Override
|
|
||||||
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
|
Task<ShortDynamicLink> shortLinkTask = getShortDynamicLinkTask(builder, m)
|
||||||
if (task.isSuccessful()) {
|
.addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
|
||||||
Uri shortLink = task.getResult().getShortLink();
|
@Override
|
||||||
Log.d(TAG, "created short dynamic link: " + shortLink.toString());
|
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
|
||||||
promise.resolve(shortLink.toString());
|
if (task.isSuccessful()) {
|
||||||
} else {
|
Uri shortLink = task.getResult().getShortLink();
|
||||||
Log.e(TAG, "create shot dynamic link failure " + task.getException().getMessage());
|
Log.d(TAG, "created short dynamic link: " + shortLink.toString());
|
||||||
promise.reject("links/failure", task.getException().getMessage(), task.getException());
|
promise.resolve(shortLink.toString());
|
||||||
}
|
} else {
|
||||||
|
Log.e(TAG, "create short dynamic link failure " + task.getException().getMessage());
|
||||||
|
promise.reject("links/failure", task.getException().getMessage(), task.getException());
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
catch(Exception ex) {
|
} catch (Exception ex) {
|
||||||
Log.e(TAG, "create short dynamic link failure " + ex.getMessage());
|
Log.e(TAG, "create short dynamic link failure " + ex.getMessage());
|
||||||
promise.reject("links/failure", ex.getMessage(), ex);
|
promise.reject("links/failure", ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private Task<ShortDynamicLink> getShortDynamicLinkTask(final DynamicLink.Builder builder, final Map<String, Object> m) {
|
||||||
* Converts a RN ReadableMap into a set DynamicLink.Builder instance
|
Map<String, Object> suffixParameters = (Map<String, Object>) m.get("suffix");
|
||||||
*
|
if (suffixParameters != null) {
|
||||||
* @param parameters
|
String option = (String) suffixParameters.get("option");
|
||||||
* @return
|
if ("SHORT".equals(option)) {
|
||||||
*/
|
return builder.buildShortDynamicLink(ShortDynamicLink.Suffix.SHORT);
|
||||||
private DynamicLink.Builder setDynamicLinkBuilderFromMap(ReadableMap parameters) {
|
} else if ("UNGUESSABLE".equals(option)) {
|
||||||
|
return builder.buildShortDynamicLink(ShortDynamicLink.Suffix.UNGUESSABLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.buildShortDynamicLink();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DynamicLink.Builder getDynamicLinkBuilderFromMap(final Map<String, Object> m) {
|
||||||
DynamicLink.Builder parametersBuilder = FirebaseDynamicLinks.getInstance().createDynamicLink();
|
DynamicLink.Builder parametersBuilder = FirebaseDynamicLinks.getInstance().createDynamicLink();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(parameters);
|
parametersBuilder.setLink(Uri.parse((String) m.get("link")));
|
||||||
|
parametersBuilder.setDynamicLinkDomain((String) m.get("dynamicLinkDomain"));
|
||||||
parametersBuilder.setLink(Uri.parse((String)m.get("link")));
|
|
||||||
parametersBuilder.setDynamicLinkDomain((String)m.get("dynamicLinkDomain"));
|
|
||||||
|
|
||||||
setAndroidParameters(m, parametersBuilder);
|
setAndroidParameters(m, parametersBuilder);
|
||||||
setIosParameters(m, parametersBuilder);
|
setIosParameters(m, parametersBuilder);
|
||||||
//setNavigationInfoParameters(m, parametersBuilder);
|
|
||||||
setSocialMetaTagParameters(m, parametersBuilder);
|
setSocialMetaTagParameters(m, parametersBuilder);
|
||||||
setAnalyticsParameters(m, parametersBuilder);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "error while building parameters " + e.getMessage());
|
Log.e(TAG, "error while building parameters " + e.getMessage());
|
||||||
|
@ -161,14 +178,14 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
if (androidParameters != null) {
|
if (androidParameters != null) {
|
||||||
DynamicLink.AndroidParameters.Builder androidParametersBuilder =
|
DynamicLink.AndroidParameters.Builder androidParametersBuilder =
|
||||||
androidParameters.containsKey("androidPackageName") ?
|
androidParameters.containsKey("androidPackageName") ?
|
||||||
new DynamicLink.AndroidParameters.Builder((String)androidParameters.get("androidPackageName")) :
|
new DynamicLink.AndroidParameters.Builder((String) androidParameters.get("androidPackageName")) :
|
||||||
new DynamicLink.AndroidParameters.Builder();
|
new DynamicLink.AndroidParameters.Builder();
|
||||||
|
|
||||||
if (androidParameters.containsKey("androidFallbackLink")) {
|
if (androidParameters.containsKey("androidFallbackLink")) {
|
||||||
androidParametersBuilder.setFallbackUrl(Uri.parse((String)androidParameters.get("androidFallbackLink")));
|
androidParametersBuilder.setFallbackUrl(Uri.parse((String) androidParameters.get("androidFallbackLink")));
|
||||||
}
|
}
|
||||||
if (androidParameters.containsKey("androidMinPackageVersionCode")) {
|
if (androidParameters.containsKey("androidMinPackageVersionCode")) {
|
||||||
androidParametersBuilder.setMinimumVersion(((Double)androidParameters.get("androidMinPackageVersionCode")).intValue());
|
androidParametersBuilder.setMinimumVersion(((Double) androidParameters.get("androidMinPackageVersionCode")).intValue());
|
||||||
}
|
}
|
||||||
parametersBuilder.setAndroidParameters(androidParametersBuilder.build());
|
parametersBuilder.setAndroidParameters(androidParametersBuilder.build());
|
||||||
}
|
}
|
||||||
|
@ -178,42 +195,29 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
Map<String, Object> iosParameters = (Map<String, Object>) m.get("iosInfo");
|
Map<String, Object> iosParameters = (Map<String, Object>) m.get("iosInfo");
|
||||||
//TODO: see what happens if bundleId is missing
|
//TODO: see what happens if bundleId is missing
|
||||||
if (iosParameters != null && iosParameters.containsKey("iosBundleId")) {
|
if (iosParameters != null && iosParameters.containsKey("iosBundleId")) {
|
||||||
DynamicLink.IosParameters.Builder iosParametersBuilder = new DynamicLink.IosParameters.Builder((String)iosParameters.get("iosBundleId"));
|
DynamicLink.IosParameters.Builder iosParametersBuilder = new DynamicLink.IosParameters.Builder((String) iosParameters.get("iosBundleId"));
|
||||||
if (iosParameters.containsKey("iosAppStoreId")) {
|
if (iosParameters.containsKey("iosAppStoreId")) {
|
||||||
iosParametersBuilder.setAppStoreId((String)iosParameters.get("iosAppStoreId"));
|
iosParametersBuilder.setAppStoreId((String) iosParameters.get("iosAppStoreId"));
|
||||||
}
|
}
|
||||||
if (iosParameters.containsKey("iosCustomScheme")) {
|
if (iosParameters.containsKey("iosCustomScheme")) {
|
||||||
iosParametersBuilder.setCustomScheme((String)iosParameters.get("iosCustomScheme"));
|
iosParametersBuilder.setCustomScheme((String) iosParameters.get("iosCustomScheme"));
|
||||||
}
|
}
|
||||||
if (iosParameters.containsKey("iosFallbackLink")) {
|
if (iosParameters.containsKey("iosFallbackLink")) {
|
||||||
iosParametersBuilder.setFallbackUrl(Uri.parse((String)iosParameters.get("iosFallbackLink")));
|
iosParametersBuilder.setFallbackUrl(Uri.parse((String) iosParameters.get("iosFallbackLink")));
|
||||||
}
|
}
|
||||||
if (iosParameters.containsKey("iosIpadBundleId")) {
|
if (iosParameters.containsKey("iosIpadBundleId")) {
|
||||||
iosParametersBuilder.setIpadBundleId((String)iosParameters.get("iosIpadBundleId"));
|
iosParametersBuilder.setIpadBundleId((String) iosParameters.get("iosIpadBundleId"));
|
||||||
}
|
}
|
||||||
if (iosParameters.containsKey("iosIpadFallbackLink")) {
|
if (iosParameters.containsKey("iosIpadFallbackLink")) {
|
||||||
iosParametersBuilder.setIpadFallbackUrl(Uri.parse((String)iosParameters.get("iosIpadFallbackLink")));
|
iosParametersBuilder.setIpadFallbackUrl(Uri.parse((String) iosParameters.get("iosIpadFallbackLink")));
|
||||||
}
|
}
|
||||||
if (iosParameters.containsKey("iosMinPackageVersionCode")) {
|
if (iosParameters.containsKey("iosMinPackageVersionCode")) {
|
||||||
iosParametersBuilder.setMinimumVersion((String)iosParameters.get("iosMinPackageVersionCode"));
|
iosParametersBuilder.setMinimumVersion((String) iosParameters.get("iosMinPackageVersionCode"));
|
||||||
}
|
}
|
||||||
parametersBuilder.setIosParameters(iosParametersBuilder.build());
|
parametersBuilder.setIosParameters(iosParametersBuilder.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// private void setNavigationInfoParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
|
||||||
// Map<String, Object> navigationInfoParameters = (Map<String, Object>) m.get("navigationInfo");
|
|
||||||
// if (navigationInfoParameters != null) {
|
|
||||||
// DynamicLink.NavigationInfoParameters.Builder navigationInfoParametersBuilder =
|
|
||||||
// new DynamicLink.NavigationInfoParameters.Builder();
|
|
||||||
//
|
|
||||||
// if (navigationInfoParameters.containsKey("enableForcedRedirect")) {
|
|
||||||
// navigationInfoParametersBuilder.setForcedRedirectEnabled((boolean)navigationInfoParameters.get("enableForcedRedirect"));
|
|
||||||
// }
|
|
||||||
// parametersBuilder.setNavigationInfoParameters(navigationInfoParametersBuilder.build());
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private void setSocialMetaTagParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
private void setSocialMetaTagParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
||||||
Map<String, Object> socialMetaTagParameters = (Map<String, Object>) m.get("socialMetaTagInfo");
|
Map<String, Object> socialMetaTagParameters = (Map<String, Object>) m.get("socialMetaTagInfo");
|
||||||
if (socialMetaTagParameters != null) {
|
if (socialMetaTagParameters != null) {
|
||||||
|
@ -221,67 +225,15 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||||
new DynamicLink.SocialMetaTagParameters.Builder();
|
new DynamicLink.SocialMetaTagParameters.Builder();
|
||||||
|
|
||||||
if (socialMetaTagParameters.containsKey("socialDescription")) {
|
if (socialMetaTagParameters.containsKey("socialDescription")) {
|
||||||
socialMetaTagParametersBuilder.setDescription((String)socialMetaTagParameters.get("socialDescription"));
|
socialMetaTagParametersBuilder.setDescription((String) socialMetaTagParameters.get("socialDescription"));
|
||||||
}
|
}
|
||||||
if (socialMetaTagParameters.containsKey("socialImageLink")) {
|
if (socialMetaTagParameters.containsKey("socialImageLink")) {
|
||||||
socialMetaTagParametersBuilder.setImageUrl(Uri.parse((String)socialMetaTagParameters.get("socialImageLink")));
|
socialMetaTagParametersBuilder.setImageUrl(Uri.parse((String) socialMetaTagParameters.get("socialImageLink")));
|
||||||
}
|
}
|
||||||
if (socialMetaTagParameters.containsKey("socialTitle")) {
|
if (socialMetaTagParameters.containsKey("socialTitle")) {
|
||||||
socialMetaTagParametersBuilder.setTitle((String)socialMetaTagParameters.get("socialTitle"));
|
socialMetaTagParametersBuilder.setTitle((String) socialMetaTagParameters.get("socialTitle"));
|
||||||
}
|
}
|
||||||
parametersBuilder.setSocialMetaTagParameters(socialMetaTagParametersBuilder.build());
|
parametersBuilder.setSocialMetaTagParameters(socialMetaTagParametersBuilder.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setAnalyticsParameters (final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
|
||||||
Map<String, Object> analyticsParameters = (Map<String, Object>) m.get("analyticsInfo");
|
|
||||||
if (analyticsParameters != null) {
|
|
||||||
setGoogleAnalyticsParameters(analyticsParameters, parametersBuilder);
|
|
||||||
setItunesConnectAnalyticsParameters(analyticsParameters, parametersBuilder);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setGoogleAnalyticsParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
|
||||||
Map<String, Object> googleAnalyticsParameters = (Map<String, Object>) m.get("googlePlayAnalytics");
|
|
||||||
if (googleAnalyticsParameters != null) {
|
|
||||||
DynamicLink.GoogleAnalyticsParameters.Builder googleAnalyticsParametersBuilder =
|
|
||||||
new DynamicLink.GoogleAnalyticsParameters.Builder();
|
|
||||||
|
|
||||||
if (googleAnalyticsParameters.containsKey("utmCampaign")) {
|
|
||||||
googleAnalyticsParametersBuilder.setCampaign((String)googleAnalyticsParameters.get("utmCampaign"));
|
|
||||||
}
|
|
||||||
if (googleAnalyticsParameters.containsKey("utmContent")) {
|
|
||||||
googleAnalyticsParametersBuilder.setContent((String)googleAnalyticsParameters.get("utmContent"));
|
|
||||||
}
|
|
||||||
if (googleAnalyticsParameters.containsKey("utmMedium")) {
|
|
||||||
googleAnalyticsParametersBuilder.setMedium((String)googleAnalyticsParameters.get("utmMedium"));
|
|
||||||
}
|
|
||||||
if (googleAnalyticsParameters.containsKey("utmSource")) {
|
|
||||||
googleAnalyticsParametersBuilder.setSource((String)googleAnalyticsParameters.get("utmSource"));
|
|
||||||
}
|
|
||||||
if (googleAnalyticsParameters.containsKey("utmTerm")) {
|
|
||||||
googleAnalyticsParametersBuilder.setTerm((String)googleAnalyticsParameters.get("utmTerm"));
|
|
||||||
}
|
|
||||||
parametersBuilder.setGoogleAnalyticsParameters(googleAnalyticsParametersBuilder.build());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setItunesConnectAnalyticsParameters(final Map<String, Object> m, final DynamicLink.Builder parametersBuilder) {
|
|
||||||
Map<String, Object> itunesConnectAnalyticsParameters = (Map<String, Object>) m.get("itunesConnectAnalytics");
|
|
||||||
if (itunesConnectAnalyticsParameters != null) {
|
|
||||||
DynamicLink.ItunesConnectAnalyticsParameters.Builder itunesConnectAnalyticsParametersBuilder =
|
|
||||||
new DynamicLink.ItunesConnectAnalyticsParameters.Builder();
|
|
||||||
|
|
||||||
if (itunesConnectAnalyticsParameters.containsKey("at")) {
|
|
||||||
itunesConnectAnalyticsParametersBuilder.setAffiliateToken((String)itunesConnectAnalyticsParameters.get("at"));
|
|
||||||
}
|
|
||||||
if (itunesConnectAnalyticsParameters.containsKey("ct")) {
|
|
||||||
itunesConnectAnalyticsParametersBuilder.setCampaignToken((String)itunesConnectAnalyticsParameters.get("ct"));
|
|
||||||
}
|
|
||||||
if (itunesConnectAnalyticsParameters.containsKey("pt")) {
|
|
||||||
itunesConnectAnalyticsParametersBuilder.setProviderToken((String)itunesConnectAnalyticsParameters.get("pt"));
|
|
||||||
}
|
|
||||||
parametersBuilder.setItunesConnectAnalyticsParameters(itunesConnectAnalyticsParametersBuilder.build());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
179
lib/firebase.js
179
lib/firebase.js
|
@ -2,121 +2,106 @@
|
||||||
* @providesModule Firebase
|
* @providesModule Firebase
|
||||||
* @flow
|
* @flow
|
||||||
*/
|
*/
|
||||||
import { NativeModules } from 'react-native'
|
import { NativeModules } from 'react-native';
|
||||||
|
|
||||||
import Log from './utils/log'
|
import Log from './utils/log';
|
||||||
import { isObject } from './utils'
|
import { isObject } from './utils';
|
||||||
|
|
||||||
// modules
|
// modules
|
||||||
import Auth, { statics as AuthStatics } from './modules/auth'
|
import Auth, { statics as AuthStatics } from './modules/auth';
|
||||||
import Storage, { statics as StorageStatics } from './modules/storage'
|
import Storage, { statics as StorageStatics } from './modules/storage';
|
||||||
import Database, { statics as DatabaseStatics } from './modules/database'
|
import Database, { statics as DatabaseStatics } from './modules/database';
|
||||||
import Messaging, { statics as MessagingStatics } from './modules/messaging'
|
import Messaging, { statics as MessagingStatics } from './modules/messaging';
|
||||||
import Analytics from './modules/analytics'
|
import Analytics from './modules/analytics';
|
||||||
import Crash from './modules/crash'
|
import Crash from './modules/crash';
|
||||||
import RemoteConfig from './modules/config'
|
import RemoteConfig from './modules/config';
|
||||||
import Performance from './modules/perf'
|
import Performance from './modules/perf';
|
||||||
import AdMob, { statics as AdMobStatics } from './modules/admob'
|
import AdMob, { statics as AdMobStatics } from './modules/admob';
|
||||||
import Links, { statics as LinksStatics } from './modules/links'
|
import Links, { statics as LinksStatics } from './modules/links';
|
||||||
|
|
||||||
const instances: Object = { default: null }
|
const instances: Object = { default: null };
|
||||||
const FirebaseModule = NativeModules.RNFirebase
|
const FirebaseModule = NativeModules.RNFirebase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Firebase
|
* @class Firebase
|
||||||
*/
|
*/
|
||||||
export default class Firebase {
|
export default class Firebase {
|
||||||
_log: ?Object
|
_log: ?Object;
|
||||||
_auth: ?Object
|
_auth: ?Object;
|
||||||
_store: ?Object
|
_store: ?Object;
|
||||||
_storage: ?Object
|
_storage: ?Object;
|
||||||
_database: ?Object
|
_database: ?Object;
|
||||||
_presence: ?Object
|
_presence: ?Object;
|
||||||
_analytics: ?Object
|
_analytics: ?Object;
|
||||||
_constants: ?Object
|
_constants: ?Object;
|
||||||
_messaging: ?Object
|
_messaging: ?Object;
|
||||||
_config: ?Object
|
_config: ?Object;
|
||||||
_crash: ?Object
|
_crash: ?Object;
|
||||||
_perf: ?Object
|
_perf: ?Object;
|
||||||
_admob: ?Object
|
_admob: ?Object;
|
||||||
_links: ?Object
|
_links: ?Object;
|
||||||
|
|
||||||
auth: Function
|
auth: Function;
|
||||||
crash: Function
|
crash: Function;
|
||||||
storage: Function
|
storage: Function;
|
||||||
database: Function
|
database: Function;
|
||||||
analytics: Function
|
analytics: Function;
|
||||||
messaging: Function
|
messaging: Function;
|
||||||
config: Function
|
config: Function;
|
||||||
perf: Function
|
perf: Function;
|
||||||
admob: Function
|
admob: Function;
|
||||||
links: Function
|
links: Function;
|
||||||
|
|
||||||
eventHandlers: Object
|
eventHandlers: Object;
|
||||||
debug: boolean
|
debug: boolean;
|
||||||
options: {
|
options: {
|
||||||
errorOnMissingPlayServices: boolean,
|
errorOnMissingPlayServices: boolean,
|
||||||
debug?: boolean,
|
debug?: boolean,
|
||||||
persistence?: boolean,
|
persistence?: boolean
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param options
|
* @param options
|
||||||
*/
|
*/
|
||||||
constructor(options: Object = {}) {
|
constructor(options: Object = {}) {
|
||||||
this.eventHandlers = {}
|
this.eventHandlers = {};
|
||||||
this.debug = options.debug || false
|
this.debug = options.debug || false;
|
||||||
this.options = Object.assign(
|
this.options = Object.assign({ errorOnMissingPlayServices: true, promptOnMissingPlayServices: true }, options);
|
||||||
{ errorOnMissingPlayServices: true, promptOnMissingPlayServices: true },
|
|
||||||
options
|
|
||||||
)
|
|
||||||
|
|
||||||
if (this.debug) {
|
if (this.debug) {
|
||||||
Log.enable(this.debug)
|
Log.enable(this.debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._log = new Log('firebase')
|
this._log = new Log('firebase');
|
||||||
|
|
||||||
if (!this.googleApiAvailability.isAvailable) {
|
if (!this.googleApiAvailability.isAvailable) {
|
||||||
if (
|
if (this.options.promptOnMissingPlayServices && this.googleApiAvailability.isUserResolvableError) {
|
||||||
this.options.promptOnMissingPlayServices &&
|
FirebaseModule.promptPlayServices();
|
||||||
this.googleApiAvailability.isUserResolvableError
|
|
||||||
) {
|
|
||||||
FirebaseModule.promptPlayServices()
|
|
||||||
} else {
|
} else {
|
||||||
const error = `Google Play Services is required to run this application but no valid installation was found (Code ${this
|
const error = `Google Play Services is required to run this application but no valid installation was found (Code ${this.googleApiAvailability.status}).`;
|
||||||
.googleApiAvailability.status}).`
|
|
||||||
if (this.options.errorOnMissingPlayServices) {
|
if (this.options.errorOnMissingPlayServices) {
|
||||||
throw new Error(error)
|
throw new Error(error);
|
||||||
} else {
|
} else {
|
||||||
console.warn(error)
|
console.warn(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.auth = this._staticsOrInstance('auth', AuthStatics, Auth)
|
this.auth = this._staticsOrInstance('auth', AuthStatics, Auth);
|
||||||
this.storage = this._staticsOrInstance('storage', StorageStatics, Storage)
|
this.storage = this._staticsOrInstance('storage', StorageStatics, Storage);
|
||||||
this.database = this._staticsOrInstance(
|
this.database = this._staticsOrInstance('database', DatabaseStatics, Database);
|
||||||
'database',
|
this.messaging = this._staticsOrInstance('messaging', MessagingStatics, Messaging);
|
||||||
DatabaseStatics,
|
this.analytics = this._staticsOrInstance('analytics', {}, Analytics);
|
||||||
Database
|
this.crash = this._staticsOrInstance('crash', {}, Crash);
|
||||||
)
|
this.config = this._staticsOrInstance('config', {}, RemoteConfig);
|
||||||
this.messaging = this._staticsOrInstance(
|
this.perf = this._staticsOrInstance('perf', {}, Performance);
|
||||||
'messaging',
|
this.admob = this._staticsOrInstance('admob', AdMobStatics, AdMob);
|
||||||
MessagingStatics,
|
this.links = this._staticsOrInstance('links', LinksStatics, Links);
|
||||||
Messaging
|
|
||||||
)
|
|
||||||
this.analytics = this._staticsOrInstance('analytics', {}, Analytics)
|
|
||||||
this.crash = this._staticsOrInstance('crash', {}, Crash)
|
|
||||||
this.config = this._staticsOrInstance('config', {}, RemoteConfig)
|
|
||||||
this.perf = this._staticsOrInstance('perf', {}, Performance)
|
|
||||||
this.admob = this._staticsOrInstance('admob', AdMobStatics, AdMob)
|
|
||||||
this.links = this._staticsOrInstance('links', LinksStatics, Links)
|
|
||||||
|
|
||||||
// init auth to start listeners
|
// init auth to start listeners
|
||||||
if (NativeModules.RNFirebaseAuth) {
|
if (NativeModules.RNFirebaseAuth) {
|
||||||
this.auth()
|
this.auth();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,29 +113,23 @@ export default class Firebase {
|
||||||
*/
|
*/
|
||||||
static initializeApp(options: Object = {}, name: string = 'default') {
|
static initializeApp(options: Object = {}, name: string = 'default') {
|
||||||
if (!isObject(options)) {
|
if (!isObject(options)) {
|
||||||
throw new Error(
|
throw new Error('Firebase.initializeApp(options <- requires a configuration object');
|
||||||
'Firebase.initializeApp(options <- requires a configuration object'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof name !== 'string') {
|
if (typeof name !== 'string') {
|
||||||
throw new Error(
|
throw new Error('Firebase.initializeApp(options, name <- requires a string value');
|
||||||
'Firebase.initializeApp(options, name <- requires a string value'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name !== 'default') {
|
if (name !== 'default') {
|
||||||
throw new Error(
|
throw new Error('RNFirebase currently only supports one instance of firebase - the default one.');
|
||||||
'RNFirebase currently only supports one instance of firebase - the default one.'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!instances[name]) instances[name] = new Firebase(options)
|
if (!instances[name]) instances[name] = new Firebase(options);
|
||||||
return instances[name]
|
return instances[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
get apps(): Array<string> {
|
get apps(): Array<string> {
|
||||||
return Object.keys(instances)
|
return Object.keys(instances);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -159,16 +138,14 @@ export default class Firebase {
|
||||||
*/
|
*/
|
||||||
get googleApiAvailability(): GoogleApiAvailabilityType {
|
get googleApiAvailability(): GoogleApiAvailabilityType {
|
||||||
// if not available then return a fake object for ios - saves doing platform specific logic.
|
// if not available then return a fake object for ios - saves doing platform specific logic.
|
||||||
return (
|
return FirebaseModule.googleApiAvailability || { isAvailable: true, status: 0 };
|
||||||
FirebaseModule.googleApiAvailability || { isAvailable: true, status: 0 }
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger
|
* Logger
|
||||||
*/
|
*/
|
||||||
get log(): Log {
|
get log(): Log {
|
||||||
return this._log
|
return this._log;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -181,19 +158,19 @@ export default class Firebase {
|
||||||
*/
|
*/
|
||||||
_staticsOrInstance(name, statics, InstanceClass): Function {
|
_staticsOrInstance(name, statics, InstanceClass): Function {
|
||||||
const getInstance = () => {
|
const getInstance = () => {
|
||||||
const internalPropName = `_${name}`
|
const internalPropName = `_${name}`;
|
||||||
|
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
if (!this[internalPropName]) {
|
if (!this[internalPropName]) {
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
this[internalPropName] = new InstanceClass(this)
|
this[internalPropName] = new InstanceClass(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
return this[internalPropName]
|
return this[internalPropName];
|
||||||
}
|
};
|
||||||
|
|
||||||
Object.assign(getInstance, statics || {})
|
Object.assign(getInstance, statics || {});
|
||||||
return getInstance
|
return getInstance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue