1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import "EXScopedNotificationCategoriesModule.h"
4#import "EXScopedNotificationCategoryMigrator.h"
5#import "EXScopedNotificationsUtils.h"
6
7@interface EXScopedNotificationCategoriesModule ()
8
9@property (nonatomic, strong) NSString *scopeKey;
10
11@end
12
13@implementation EXScopedNotificationCategoriesModule
14
15- (instancetype)initWithScopeKey:(NSString *)scopeKey
16{
17  if (self = [super init]) {
18    _scopeKey = scopeKey;
19  }
20  return self;
21}
22
23- (void)getNotificationCategoriesAsyncWithResolver:(EXPromiseResolveBlock)resolve reject:(EXPromiseRejectBlock)reject
24{
25  [[UNUserNotificationCenter currentNotificationCenter] getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> *categories) {
26    NSMutableArray* existingCategories = [NSMutableArray new];
27    for (UNNotificationCategory *category in categories) {
28      if ([EXScopedNotificationsUtils isId:category.identifier scopedByExperience:self->_scopeKey]) {
29        [existingCategories addObject:[self serializeCategory:category]];
30      }
31    }
32    resolve(existingCategories);
33  }];
34}
35
36- (void)setNotificationCategoryWithCategoryId:(NSString *)categoryId
37                                      actions:(NSArray *)actions
38                                      options:(NSDictionary *)options
39                                      resolve:(EXPromiseResolveBlock)resolve
40                                       reject:(EXPromiseRejectBlock)reject
41{
42  NSString *scopedCategoryIdentifier = [EXScopedNotificationsUtils scopedIdentifierFromId:categoryId
43                                                                            forExperience:_scopeKey];
44  [super setNotificationCategoryWithCategoryId:scopedCategoryIdentifier
45                                       actions:actions
46                                       options:options
47                                       resolve:resolve
48                                        reject:reject];
49}
50
51- (void)deleteNotificationCategoryWithCategoryId:(NSString *)categoryId
52                                         resolve:(EXPromiseResolveBlock)resolve
53                                          reject:(EXPromiseRejectBlock)reject
54{
55  NSString *scopedCategoryIdentifier = [EXScopedNotificationsUtils scopedIdentifierFromId:categoryId
56                                                                            forExperience:_scopeKey];
57  [super deleteNotificationCategoryWithCategoryId:scopedCategoryIdentifier
58                                          resolve:resolve
59                                           reject:reject];
60}
61
62- (NSMutableDictionary *)serializeCategory:(UNNotificationCategory *)category
63{
64  NSMutableDictionary* serializedCategory = [super serializeCategory:category];
65  serializedCategory[@"identifier"] = [EXScopedNotificationsUtils getScopeAndIdentifierFromScopedIdentifier:serializedCategory[@"identifier"]].identifier;
66
67  return serializedCategory;
68}
69
70#pragma mark - static method for migrating categories in both Expo Go and standalones. Added in SDK 41. TODO(Cruzan): Remove in SDK 47
71
72+ (void)maybeMigrateLegacyCategoryIdentifiersForProjectWithExperienceStableLegacyId:(NSString *)experienceStableLegacyId
73                                                                 scopeKey:(NSString *)scopeKey
74                                                                         isInExpoGo:(BOOL)isInExpoGo __deprecated_msg("To be removed once SDK 43 is phased out")
75{
76  if (isInExpoGo) {
77    // Changed scoping prefix in SDK 41 FROM "experienceId-" to ESCAPED "experienceId/"
78    [EXScopedNotificationCategoryMigrator migrateLegacyScopedCategoryIdentifiersForProjectWithScopeKey:scopeKey];
79  } else {
80    // Used to prefix with "experienceId-" even in standalone apps in SDKs <= 40, so we need to unscope those
81    [EXScopedNotificationCategoryMigrator unscopeLegacyCategoryIdentifiersForProjectWithScopeKey:scopeKey];
82  }
83}
84
85@end
86