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

18.6% Statements 8/43
5.56% Branches 1/18
12.5% Functions 2/16
18.6% Lines 8/43
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                                                                                                                                                                                                                                                                             
/**
 * @flow
 * AndroidAction representation wrapper
 */
import RemoteInput, {
  fromNativeAndroidRemoteInput,
} from './AndroidRemoteInput';
import { SemanticAction } from './types';
import type { NativeAndroidAction, SemanticActionType } from './types';
 
export default class AndroidAction {
  _action: string;
  _allowGeneratedReplies: boolean | void;
  _icon: string;
  _remoteInputs: RemoteInput[];
  _semanticAction: SemanticActionType | void;
  _showUserInterface: boolean | void;
  _title: string;
 
  constructor(action: string, icon: string, title: string) {
    this._action = action;
    this._icon = icon;
    this._remoteInputs = [];
    this._title = title;
  }
 
  get action(): string {
    return this._action;
  }
 
  get allowGeneratedReplies(): ?boolean {
    return this._allowGeneratedReplies;
  }
 
  get icon(): string {
    return this._icon;
  }
 
  get remoteInputs(): RemoteInput[] {
    return this._remoteInputs;
  }
 
  get semanticAction(): ?SemanticActionType {
    return this._semanticAction;
  }
 
  get showUserInterface(): ?boolean {
    return this._showUserInterface;
  }
 
  get title(): string {
    return this._title;
  }
 
  /**
   *
   * @param remoteInput
   * @returns {AndroidAction}
   */
  addRemoteInput(remoteInput: RemoteInput): AndroidAction {
    Iif (!(remoteInput instanceof RemoteInput)) {
      throw new Error(
        `AndroidAction:addRemoteInput expects an 'RemoteInput' but got type ${typeof remoteInput}`
      );
    }
    this._remoteInputs.push(remoteInput);
    return this;
  }
 
  /**
   *
   * @param allowGeneratedReplies
   * @returns {AndroidAction}
   */
  setAllowGenerateReplies(allowGeneratedReplies: boolean): AndroidAction {
    this._allowGeneratedReplies = allowGeneratedReplies;
    return this;
  }
 
  /**
   *
   * @param semanticAction
   * @returns {AndroidAction}
   */
  setSemanticAction(semanticAction: SemanticActionType): AndroidAction {
    if (!Object.values(SemanticAction).includes(semanticAction)) {
      throw new Error(
        `AndroidAction:setSemanticAction Invalid Semantic Action: ${semanticAction}`
      );
    }
    this._semanticAction = semanticAction;
    return this;
  }
 
  /**
   *
   * @param showUserInterface
   * @returns {AndroidAction}
   */
  setShowUserInterface(showUserInterface: boolean): AndroidAction {
    this._showUserInterface = showUserInterface;
    return this;
  }
 
  build(): NativeAndroidAction {
    if (!this._action) {
      throw new Error('AndroidAction: Missing required `action` property');
    } else if (!this._icon) {
      throw new Error('AndroidAction: Missing required `icon` property');
    } else if (!this._title) {
      throw new Error('AndroidAction: Missing required `title` property');
    }
 
    return {
      action: this._action,
      allowGeneratedReplies: this._allowGeneratedReplies,
      icon: this._icon,
      remoteInputs: this._remoteInputs.map(remoteInput => remoteInput.build()),
      semanticAction: this._semanticAction,
      showUserInterface: this._showUserInterface,
      title: this._title,
    };
  }
}
 
export const fromNativeAndroidAction = (
  nativeAction: NativeAndroidAction
): AndroidAction => {
  const action = new AndroidAction(
    nativeAction.action,
    nativeAction.icon,
    nativeAction.title
  );
  if (nativeAction.allowGeneratedReplies) {
    action.setAllowGenerateReplies(nativeAction.allowGeneratedReplies);
  }
  if (nativeAction.remoteInputs) {
    nativeAction.remoteInputs.forEach(remoteInput => {
      action.addRemoteInput(fromNativeAndroidRemoteInput(remoteInput));
    });
  }
  if (nativeAction.semanticAction) {
    action.setSemanticAction(nativeAction.semanticAction);
  }
  if (nativeAction.showUserInterface) {
    action.setShowUserInterface(nativeAction.showUserInterface);
  }
 
  return action;
};