Notifications.ts 3.03 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2 3 4
import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { Commands } from './commands/Commands';
import { EventsRegistry } from './events/EventsRegistry';
5
import { EventsRegistryIOS } from './events/EventsRegistryIOS';
6
import { Notification } from './DTO/Notification';
7
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
yogevbd's avatar
yogevbd committed
8
import { CompletionCallbackWrapper } from './adapters/CompletionCallbackWrapper';
9
import { NotificationCategory } from './interfaces/NotificationCategory';
10 11
import { NotificationsIOS } from './NotificationsIOS';
import { NotificationsAndroid } from './NotificationsAndroid';
yogevbd's avatar
yogevbd committed
12 13

export class NotificationsRoot {
14 15 16
  public readonly ios: NotificationsIOS;
  public readonly android: NotificationsAndroid;

yogevbd's avatar
yogevbd committed
17 18 19 20
  private readonly nativeEventsReceiver: NativeEventsReceiver;
  private readonly nativeCommandsSender: NativeCommandsSender;
  private readonly commands: Commands;
  private readonly eventsRegistry: EventsRegistry;
21
  private readonly eventsRegistryIOS: EventsRegistryIOS;
22
  private readonly uniqueIdProvider: UniqueIdProvider;
yogevbd's avatar
yogevbd committed
23
  private readonly completionCallbackWrapper: CompletionCallbackWrapper;
yogevbd's avatar
yogevbd committed
24 25 26 27

  constructor() {
    this.nativeEventsReceiver = new NativeEventsReceiver();
    this.nativeCommandsSender = new NativeCommandsSender();
yogevbd's avatar
yogevbd committed
28
    this.completionCallbackWrapper = new CompletionCallbackWrapper(this.nativeCommandsSender);
29
    this.uniqueIdProvider = new UniqueIdProvider();
yogevbd's avatar
yogevbd committed
30
    this.commands = new Commands(
31 32
      this.nativeCommandsSender,
      this.uniqueIdProvider
yogevbd's avatar
yogevbd committed
33
    );
yogevbd's avatar
yogevbd committed
34
    this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.completionCallbackWrapper);
35
    this.eventsRegistryIOS = new EventsRegistryIOS(this.nativeEventsReceiver);
yogevbd's avatar
yogevbd committed
36

37
    this.ios = new NotificationsIOS(this.commands, this.eventsRegistryIOS);
38
    this.android = new NotificationsAndroid(this.commands);
yogevbd's avatar
yogevbd committed
39 40
  }

41 42 43 44 45 46 47 48
  /**
   * registerRemoteNotifications
   */
  public registerRemoteNotifications() {
    this.ios.registerRemoteNotifications();
    this.android.registerRemoteNotifications();
  }

yogevbd's avatar
yogevbd committed
49
  /**
yogevbd's avatar
yogevbd committed
50
   * postLocalNotification
yogevbd's avatar
yogevbd committed
51
   */
yogevbd's avatar
yogevbd committed
52 53
  public postLocalNotification(notification: Notification, id: number) {
    return this.commands.postLocalNotification(notification, id);
yogevbd's avatar
yogevbd committed
54 55 56
  }

  /**
yogevbd's avatar
yogevbd committed
57
   * getInitialNotification
yogevbd's avatar
yogevbd committed
58
   */
59
  public getInitialNotification(): Promise<Notification | undefined> {
yogevbd's avatar
yogevbd committed
60 61 62
    return this.commands.getInitialNotification();
  }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  /**
   * setCategories
   */
  public setCategories(categories: [NotificationCategory?]) {
    this.commands.setCategories(categories);
  }

  /**
   * cancelLocalNotification
  */
  public cancelLocalNotification(notificationId: string) {
    return this.commands.cancelLocalNotification(notificationId);
  }

  /**
   * isRegisteredForRemoteNotifications
   */
80 81
  public isRegisteredForRemoteNotifications(): Promise<boolean> {
    return this.commands.isRegisteredForRemoteNotifications();
82 83
  }

yogevbd's avatar
yogevbd committed
84 85 86 87 88 89 90
  /**
   * Obtain the events registry instance
   */
  public events(): EventsRegistry {
    return this.eventsRegistry;
  }
}