diff --git a/iphone/Classes/TiUIShortcutProxy.m b/iphone/Classes/TiUIShortcutProxy.m index c178772eac7..e417ef15c07 100644 --- a/iphone/Classes/TiUIShortcutProxy.m +++ b/iphone/Classes/TiUIShortcutProxy.m @@ -28,7 +28,7 @@ - (void)_destroy { TiThreadPerformOnMainThread( ^{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; + [NSNotificationCenter.defaultCenter removeObserver:self]; }, YES); [super _destroy]; @@ -37,11 +37,11 @@ - (void)_destroy - (void)_listenerAdded:(NSString *)type count:(int)count { if (count == 1 && [type isEqualToString:@"click"]) { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector - (didReceiveShortcutNotification:) - name:kTiApplicationShortcut - object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector + (didReceiveShortcutNotification:) + name:kTiApplicationShortcut + object:nil]; [self retain]; } } @@ -56,9 +56,8 @@ - (void)_listenerRemoved:(NSString *)type count:(int)count - (NSArray *)items { - NSMutableArray *shortcutsToReturn = [NSMutableArray array]; - NSArray *shortcuts = [UIApplication sharedApplication].shortcutItems; - + NSArray *shortcuts = UIApplication.sharedApplication.shortcutItems; + NSMutableArray *shortcutsToReturn = [NSMutableArray arrayWithCapacity:shortcuts.count]; for (UIApplicationShortcutItem *item in shortcuts) { [shortcutsToReturn addObject:[[[TiUIShortcutItemProxy alloc] initWithShortcutItem:item] autorelease]]; } @@ -68,13 +67,12 @@ - (void)_listenerRemoved:(NSString *)type count:(int)count - (NSArray *)staticItems { - NSMutableArray *shortcutsToReturn = [NSMutableArray array]; - NSArray *shortcuts = [NSBundle mainBundle].infoDictionary[@"UIApplicationShortcutItems"]; - - if (shortcuts == nil || [shortcuts count] == 0) { + NSArray *shortcuts = NSBundle.mainBundle.infoDictionary[@"UIApplicationShortcutItems"]; + if (shortcuts == nil || shortcuts.count == 0) { return @[]; } + NSMutableArray *shortcutsToReturn = [NSMutableArray arrayWithCapacity:shortcuts.count]; for (NSDictionary *item in shortcuts) { // We need to map the plist-keys manually for static shortcuts NSString *type = item[@"UIApplicationShortcutItemType"]; @@ -97,56 +95,66 @@ - (void)_listenerRemoved:(NSString *)type count:(int)count - (TiUIShortcutItemProxy *)getById:(NSString *)identifier { - NSArray *shortcuts = [UIApplication sharedApplication].shortcutItems; - for (UIApplicationShortcutItem *item in shortcuts) { - if ([item.type isEqualToString:[TiUtils stringValue:identifier]]) { - return [[[TiUIShortcutItemProxy alloc] initWithShortcutItem:item] autorelease]; + NSArray *shortcuts = UIApplication.sharedApplication.shortcutItems; + if (shortcuts != nil && shortcuts.count > 0) { + NSString *type = [TiUtils stringValue:identifier]; + for (UIApplicationShortcutItem *item in shortcuts) { + if ([item.type isEqualToString:type]) { + return [[[TiUIShortcutItemProxy alloc] initWithShortcutItem:item] autorelease]; + } } } - return nil; } - (void)remove:(TiUIShortcutItemProxy *)shortcut { - NSString *key = [shortcut shortcutItem].type; - - NSMutableArray *shortcuts = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems; - for (UIApplicationShortcutItem *item in shortcuts) { - if ([item.type isEqualToString:[shortcut shortcutItem].type]) { - [shortcuts removeObject:item]; - break; + NSArray *shortcuts = UIApplication.sharedApplication.shortcutItems; + if (shortcuts != nil && shortcuts.count > 0) { + NSString *key = shortcut.shortcutItem.type; + NSMutableArray *shortcutsCopy = [shortcuts mutableCopy]; + for (UIApplicationShortcutItem *item in shortcutsCopy) { + if ([item.type isEqualToString:key]) { + [shortcutsCopy removeObject:item]; + break; + } } + UIApplication.sharedApplication.shortcutItems = shortcutsCopy; } - [UIApplication sharedApplication].shortcutItems = shortcuts; } - (void)removeAll { - [UIApplication sharedApplication].shortcutItems = nil; + UIApplication.sharedApplication.shortcutItems = nil; } - (void)add:(TiUIShortcutItemProxy *)shortcut { - NSMutableArray *shortcuts = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems; - - // Remove previous shortcutitem of same id if exists - __block NSUInteger index = shortcuts.count; - [shortcuts enumerateObjectsUsingBlock:^(UIApplicationShortcutItem *_Nonnull item, NSUInteger idx, BOOL *_Nonnull stop) { - if ([item.type isEqualToString:[shortcut shortcutItem].type]) { - index = idx; - [shortcuts removeObject:item]; - *stop = true; - } - }]; - [shortcuts insertObject:[shortcut shortcutItem] atIndex:index]; - [UIApplication sharedApplication].shortcutItems = shortcuts; + NSArray *shortcuts = UIApplication.sharedApplication.shortcutItems; + if (shortcuts != nil && shortcuts.count > 0) { + NSString *key = shortcut.shortcutItem.type; + NSMutableArray *shortcutsCopy = [shortcuts mutableCopy]; + // Remove previous shortcutitem of same id if exists + __block NSUInteger index = shortcuts.count; + [shortcutsCopy enumerateObjectsUsingBlock:^(UIApplicationShortcutItem *_Nonnull item, NSUInteger idx, BOOL *_Nonnull stop) { + if ([item.type isEqualToString:key]) { + index = idx; + [shortcutsCopy removeObject:item]; + *stop = true; + } + }]; + [shortcutsCopy insertObject:shortcut.shortcutItem atIndex:index]; + shortcuts = shortcutsCopy; + } else { + shortcuts = @[ shortcut.shortcutItem ]; + } + UIApplication.sharedApplication.shortcutItems = shortcuts; } - (void)didReceiveShortcutNotification:(NSNotification *)info { if ([self _hasListeners:@"click"]) { - NSDictionary *userInfo = [info userInfo]; + NSDictionary *userInfo = info.userInfo; UIApplicationShortcutItem *shortcut = [[[UIApplicationShortcutItem alloc] initWithType:userInfo[@"type"] localizedTitle:userInfo[@"title"] localizedSubtitle:userInfo[@"subtitle"]