all files / firebase/modules/utils/ index.js

33.33% Statements 10/30
15% Branches 3/20
44.44% Functions 4/9
38.46% Lines 10/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                                                                                                                                                                                 
// @flow
import { NativeModules } from 'react-native';
import INTERNALS from '../../utils/internals';
import { isIOS } from '../../utils';
import ModuleBase from '../../utils/ModuleBase';
import type App from '../core/app';
 
const FirebaseCoreModule = NativeModules.RNFirebase;
 
type GoogleApiAvailabilityType = {
  status: number,
  isAvailable: boolean,
  isUserResolvableError?: boolean,
  hasResolution?: boolean,
  error?: string,
};
 
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,
      namespace: NAMESPACE,
    });
  }
 
  /**
   *
   */
  checkPlayServicesAvailability() {
    Iif (isIOS) return;
 
    const { status } = this.playServicesAvailability;
 
    Iif (!this.playServicesAvailability.isAvailable) {
      if (
        INTERNALS.OPTIONS.promptOnMissingPlayServices &&
        this.playServicesAvailability.isUserResolvableError
      ) {
        this.promptForPlayServices();
      } else {
        const error = INTERNALS.STRINGS.ERROR_PLAY_SERVICES(status);
        if (INTERNALS.OPTIONS.errorOnMissingPlayServices) {
          if (status === 2)
            console.warn(error); // only warn if it exists but may need an update
          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 {
    return (
      FirebaseCoreModule.playServicesAvailability || {
        isAvailable: true,
        status: 0,
      }
    );
  }
 
  /**
   * Enable/Disable throwing an error or warning on detecting a play services problem
   * @android
   * @param bool
   */
  set errorOnMissingPlayServices(bool: boolean) {
    INTERNALS.OPTIONS.errorOnMissingPlayServices = bool;
  }
 
  /**
   * Enable/Disable automatic prompting of the play services update dialog
   * @android
   * @param bool
   */
  set promptOnMissingPlayServices(bool: boolean) {
    INTERNALS.OPTIONS.promptOnMissingPlayServices = bool;
  }
}
 
export const statics = {};