1
2// Copyright 2018-present 650 Industries. All rights reserved.
3
4#import <EXNotifications/EXRemoteNotificationPermissionSingletonModule.h>
5#import <ExpoModulesCore/EXDefines.h>
6
7@interface EXRemoteNotificationPermissionSingletonModule ()
8
9@property (nonatomic, strong) NSPointerArray *delegates;
10
11@end
12
13@implementation EXRemoteNotificationPermissionSingletonModule
14
15EX_REGISTER_SINGLETON_MODULE(RemoteNotificationPermissionPublisher);
16
17- (instancetype)init
18{
19  if (self = [super init]) {
20    _delegates = [NSPointerArray weakObjectsPointerArray];
21  }
22  return self;
23}
24
25# pragma mark - UIApplicationDelegate
26
27- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
28{
29  // Copying the array in case delegates remove themselves while handling the "did finish" event
30  NSPointerArray *immutableDelegates = [_delegates copy];
31  for (int i = 0; i < immutableDelegates.count; i++) {
32    id pointer = [immutableDelegates pointerAtIndex:i];
33    [pointer handleDidFinishRegisteringForRemoteNotifications];
34  }
35}
36
37- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
38{
39  // Copying the array in case delegates remove themselves while handling the "did finish" event
40  NSPointerArray *immutableDelegates = [_delegates copy];
41  for (int i = 0; i < immutableDelegates.count; i++) {
42    id pointer = [_delegates pointerAtIndex:i];
43    [pointer handleDidFinishRegisteringForRemoteNotifications];
44  }
45}
46
47# pragma mark - EXNotificationCenterDelegate
48
49- (void)addDelegate:(id<EXRemoteNotificationPermissionDelegate>)delegate
50{
51  [_delegates addPointer:(__bridge void * _Nullable)(delegate)];
52}
53
54- (void)removeDelegate:(id<EXRemoteNotificationPermissionDelegate>)delegate
55{
56  for (int i = 0; i < _delegates.count; i++) {
57    id pointer = [_delegates pointerAtIndex:i];
58    if (pointer == (__bridge void * _Nullable)(delegate) || !pointer) {
59      [_delegates removePointerAtIndex:i];
60      i--;
61    }
62  }
63  // compact doesn't work, that's why we need the `|| !pointer` above
64  // http://www.openradar.me/15396578
65  [_delegates compact];
66}
67
68@end
69