react-native-firebase/lib/modules/utils/index.js

116 lines
2.8 KiB
JavaScript
Raw Normal View History

// @flow
import { NativeModules } from 'react-native';
import INTERNALS from '../../utils/internals';
import { isIOS } from '../../utils';
2017-11-23 17:29:40 +00:00
import ModuleBase from '../../utils/ModuleBase';
import type App from '../core/app';
const FirebaseCoreModule = NativeModules.RNFirebase;
2017-11-23 17:29:40 +00:00
type GoogleApiAvailabilityType = {
status: number,
isAvailable: boolean,
isUserResolvableError?: boolean,
hasResolution?: boolean,
2018-01-25 18:25:39 +00:00
error?: string,
};
2017-11-23 17:29:40 +00:00
export const MODULE_NAME = 'RNFirebaseUtils';
export const NAMESPACE = 'utils';
export default class RNFirebaseUtils extends ModuleBase {
constructor(app: App) {
super(app, {
moduleName: MODULE_NAME,
multiApp: false,
hasShards: false,
namespace: NAMESPACE,
});
}
/**
*
*/
checkPlayServicesAvailability() {
if (isIOS) return;
2017-10-07 02:09:05 +00:00
const { status } = this.playServicesAvailability;
2017-10-07 02:09:05 +00:00
if (!this.playServicesAvailability.isAvailable) {
2018-01-25 18:25:39 +00:00
if (
INTERNALS.OPTIONS.promptOnMissingPlayServices &&
this.playServicesAvailability.isUserResolvableError
) {
this.promptForPlayServices();
} else {
const error = INTERNALS.STRINGS.ERROR_PLAY_SERVICES(status);
if (INTERNALS.OPTIONS.errorOnMissingPlayServices) {
2018-01-25 18:25:39 +00:00
if (status === 2)
console.warn(error); // only warn if it exists but may need an update
2017-10-07 02:09:05 +00:00
else throw new Error(error);
} else {
console.warn(error);
}
}
}
}
promptForPlayServices() {
if (isIOS) return null;
return FirebaseCoreModule.promptForPlayServices();
}
resolutionForPlayServices() {
if (isIOS) return null;
return FirebaseCoreModule.resolutionForPlayServices();
}
makePlayServicesAvailable() {
if (isIOS) return null;
return FirebaseCoreModule.makePlayServicesAvailable();
}
/**
* Set the global logging level for all logs.
*
* @param logLevel
*/
set logLevel(logLevel: string) {
INTERNALS.OPTIONS.logLevel = logLevel;
}
/**
* Returns props from the android GoogleApiAvailability sdk
* @android
* @return {RNFirebase.GoogleApiAvailabilityType|{isAvailable: boolean, status: number}}
*/
get playServicesAvailability(): GoogleApiAvailabilityType {
2018-01-25 18:25:39 +00:00
return (
FirebaseCoreModule.playServicesAvailability || {
isAvailable: true,
status: 0,
}
);
}
/**
2017-10-07 02:53:20 +00:00
* Enable/Disable throwing an error or warning on detecting a play services problem
* @android
* @param bool
*/
2017-11-23 17:29:40 +00:00
set errorOnMissingPlayServices(bool: boolean) {
INTERNALS.OPTIONS.errorOnMissingPlayServices = bool;
}
/**
* Enable/Disable automatic prompting of the play services update dialog
* @android
* @param bool
*/
2017-11-23 17:29:40 +00:00
set promptOnMissingPlayServices(bool: boolean) {
INTERNALS.OPTIONS.promptOnMissingPlayServices = bool;
}
}
2018-01-09 17:31:54 +00:00
export const statics = {};