1// Copyright 2021-present 650 Industries. All rights reserved. 2 3#import "EXScopedNotificationCategoryMigrator.h" 4#import "EXScopedNotificationsUtils.h" 5 6@implementation EXScopedNotificationCategoryMigrator 7 8+ (void)migrateLegacyScopedCategoryIdentifiersForProjectWithScopeKey:(NSString *)scopeKey 9{ 10 [EXScopedNotificationCategoryMigrator renameLegacyCategoryIdentifiersForExperienceWithScopeKey:scopeKey withBlock:^(UNNotificationCategory *oldCategory) { 11 NSString *unscopedLegacyCategoryId = [EXScopedNotificationsUtils unscopedLegacyCategoryIdentifierWithId:oldCategory.identifier forScopeKey:scopeKey]; 12 NSString *newCategoryId = [EXScopedNotificationsUtils scopedIdentifierFromId:unscopedLegacyCategoryId 13 forExperience:scopeKey]; 14 UNNotificationCategory *newCategory = [EXScopedNotificationCategoryMigrator createNewCategoryFrom:oldCategory withNewIdentifier:newCategoryId]; 15 return newCategory; 16 }]; 17} 18 19+ (void)unscopeLegacyCategoryIdentifiersForProjectWithScopeKey:(NSString *)scopeKey 20{ 21 [EXScopedNotificationCategoryMigrator renameLegacyCategoryIdentifiersForExperienceWithScopeKey:scopeKey withBlock:^(UNNotificationCategory *oldCategory) { 22 NSString *unscopedCategoryId = [EXScopedNotificationsUtils unscopedLegacyCategoryIdentifierWithId:oldCategory.identifier forScopeKey:scopeKey]; 23 UNNotificationCategory *newCategory = [EXScopedNotificationCategoryMigrator createNewCategoryFrom:oldCategory withNewIdentifier:unscopedCategoryId]; 24 return newCategory; 25 }]; 26} 27 28+ (void)renameLegacyCategoryIdentifiersForExperienceWithScopeKey:(NSString *)scopeKey withBlock:(UNNotificationCategory *(^)(UNNotificationCategory *category))renameCategoryBlock 29{ 30 [[UNUserNotificationCenter currentNotificationCenter] getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> *categories) { 31 NSMutableSet<UNNotificationCategory *> *newCategories = [categories mutableCopy]; 32 BOOL didChangeCategories = NO; 33 for (UNNotificationCategory *previousCategory in categories) { 34 if ([EXScopedNotificationsUtils isLegacyCategoryId:previousCategory.identifier scopedByScopeKey:scopeKey]) { 35 UNNotificationCategory *newCategory = renameCategoryBlock(previousCategory); 36 [newCategories removeObject:previousCategory]; 37 [newCategories addObject:newCategory]; 38 didChangeCategories = YES; 39 } 40 } 41 if (didChangeCategories) { 42 [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:newCategories]; 43 } 44 }]; 45} 46 47+ (UNNotificationCategory *)createNewCategoryFrom:(UNNotificationCategory *)originalCategory withNewIdentifier:(NSString *)newCategoryId 48{ 49 if (@available(iOS 12, *)) { 50 return [UNNotificationCategory categoryWithIdentifier:newCategoryId 51 actions:originalCategory.actions 52 intentIdentifiers:originalCategory.intentIdentifiers 53 hiddenPreviewsBodyPlaceholder:originalCategory.hiddenPreviewsBodyPlaceholder 54 categorySummaryFormat:originalCategory.categorySummaryFormat 55 options:originalCategory.options]; 56 } else if (@available(iOS 11, *)) { 57 return [UNNotificationCategory categoryWithIdentifier:newCategoryId 58 actions:originalCategory.actions 59 intentIdentifiers:originalCategory.intentIdentifiers 60 hiddenPreviewsBodyPlaceholder:originalCategory.hiddenPreviewsBodyPlaceholder 61 options:originalCategory.options]; 62 } else { 63 return [UNNotificationCategory categoryWithIdentifier:newCategoryId 64 actions:originalCategory.actions 65 intentIdentifiers:originalCategory.intentIdentifiers 66 options:originalCategory.options]; 67 } 68} 69 70@end 71