all files / firebase/modules/notifications/ AndroidRemoteInput.js

18.75% Statements 6/32
0% Branches 0/10
16.67% Functions 2/12
18.75% Lines 6/32
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 116 117 118 119 120 121 122 123 124                                                                                                                                                                                                                                           
/**
 * @flow
 * AndroidRemoteInput representation wrapper
 */
 
import type { AndroidAllowDataType, NativeAndroidRemoteInput } from './types';
 
export default class AndroidRemoteInput {
  _allowedDataTypes: AndroidAllowDataType[];
  _allowFreeFormInput: boolean | void;
  _choices: string[];
  _label: string | void;
  _resultKey: string;
 
  constructor(resultKey: string) {
    this._allowedDataTypes = [];
    this._choices = [];
    this._resultKey = resultKey;
  }
 
  get allowedDataTypes(): AndroidAllowDataType[] {
    return this._allowedDataTypes;
  }
 
  get allowFreeFormInput(): ?boolean {
    return this._allowFreeFormInput;
  }
 
  get choices(): string[] {
    return this._choices;
  }
 
  get label(): ?string {
    return this._label;
  }
 
  get resultKey(): string {
    return this._resultKey;
  }
 
  /**
   *
   * @param mimeType
   * @param allow
   * @returns {AndroidRemoteInput}
   */
  setAllowDataType(mimeType: string, allow: boolean): AndroidRemoteInput {
    this._allowedDataTypes.push({
      allow,
      mimeType,
    });
    return this;
  }
 
  /**
   *
   * @param allowFreeFormInput
   * @returns {AndroidRemoteInput}
   */
  setAllowFreeFormInput(allowFreeFormInput: boolean): AndroidRemoteInput {
    this._allowFreeFormInput = allowFreeFormInput;
    return this;
  }
 
  /**
   *
   * @param choices
   * @returns {AndroidRemoteInput}
   */
  setChoices(choices: string[]): AndroidRemoteInput {
    this._choices = choices;
    return this;
  }
 
  /**
   *
   * @param label
   * @returns {AndroidRemoteInput}
   */
  setLabel(label: string): AndroidRemoteInput {
    this._label = label;
    return this;
  }
 
  build(): NativeAndroidRemoteInput {
    if (!this._resultKey) {
      throw new Error(
        'AndroidRemoteInput: Missing required `resultKey` property'
      );
    }
 
    return {
      allowedDataTypes: this._allowedDataTypes,
      allowFreeFormInput: this._allowFreeFormInput,
      choices: this._choices,
      label: this._label,
      resultKey: this._resultKey,
    };
  }
}
 
export const fromNativeAndroidRemoteInput = (
  nativeRemoteInput: NativeAndroidRemoteInput
): AndroidRemoteInput => {
  const remoteInput = new AndroidRemoteInput(nativeRemoteInput.resultKey);
  if (nativeRemoteInput.allowDataType) {
    for (let i = 0; i < nativeRemoteInput.allowDataType.length; i++) {
      const allowDataType = nativeRemoteInput.allowDataType[i];
      remoteInput.setAllowDataType(allowDataType.mimeType, allowDataType.allow);
    }
  }
  if (nativeRemoteInput.allowFreeFormInput) {
    remoteInput.setAllowFreeFormInput(nativeRemoteInput.allowFreeFormInput);
  }
  if (nativeRemoteInput.choices) {
    remoteInput.setChoices(nativeRemoteInput.choices);
  }
  if (nativeRemoteInput.label) {
    remoteInput.setLabel(nativeRemoteInput.label);
  }
 
  return remoteInput;
};