CompletionCallbackWrapper.ts 1.09 KB
Newer Older
yogevbd's avatar
yogevbd committed
1
import { NativeCommandsSender } from './NativeCommandsSender';
2 3 4
import { Notification } from '../interfaces/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';

yogevbd's avatar
yogevbd committed
5
import { Platform } from 'react-native';
yogevbd's avatar
yogevbd committed
6 7 8 9 10 11 12 13
export class CompletionCallbackWrapper {
  constructor(
    private readonly nativeCommandsSender: NativeCommandsSender
  ) {}

  public wrapReceivedCallback(callback: Function): (notification: Notification) => void {
    return (notification) => {
      const completion = (response: NotificationCompletion) => {
yogevbd's avatar
yogevbd committed
14 15 16
        if (Platform.OS === 'ios') {
          this.nativeCommandsSender.finishPresentingNotification(notification.identifier, response);
        }
yogevbd's avatar
yogevbd committed
17 18 19 20 21 22
      };

      callback(notification, completion);
    }
  }

yogevbd's avatar
yogevbd committed
23 24
  public wrapOpenedCallback(callback: Function): (notification: Notification) => void {
    return (notification) => {
yogevbd's avatar
yogevbd committed
25
      const completion = () => {
yogevbd's avatar
yogevbd committed
26 27 28
        if (Platform.OS === 'ios') {
          this.nativeCommandsSender.finishHandlingAction(notification.identifier);
        }
yogevbd's avatar
yogevbd committed
29 30
      };

yogevbd's avatar
yogevbd committed
31
      callback(notification, completion);
yogevbd's avatar
yogevbd committed
32 33 34
    }
  }
}