Skip to content

Commit

Permalink
fix(ios): handle adding Ti.UI.Shortcut when existing array is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Sep 8, 2020
1 parent 43042cc commit f967cf9
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions iphone/Classes/TiUIShortcutProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (void)_destroy
{
TiThreadPerformOnMainThread(
^{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[NSNotificationCenter.defaultCenter removeObserver:self];
},
YES);
[super _destroy];
Expand All @@ -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];
}
}
Expand All @@ -56,9 +56,8 @@ - (void)_listenerRemoved:(NSString *)type count:(int)count

- (NSArray<TiUIShortcutItemProxy *> *)items
{
NSMutableArray<TiUIShortcutItemProxy *> *shortcutsToReturn = [NSMutableArray array];
NSArray<UIApplicationShortcutItem *> *shortcuts = [UIApplication sharedApplication].shortcutItems;

NSArray<UIApplicationShortcutItem *> *shortcuts = UIApplication.sharedApplication.shortcutItems;
NSMutableArray<TiUIShortcutItemProxy *> *shortcutsToReturn = [NSMutableArray arrayWithCapacity:shortcuts.count];
for (UIApplicationShortcutItem *item in shortcuts) {
[shortcutsToReturn addObject:[[[TiUIShortcutItemProxy alloc] initWithShortcutItem:item] autorelease]];
}
Expand All @@ -68,13 +67,12 @@ - (void)_listenerRemoved:(NSString *)type count:(int)count

- (NSArray<TiUIShortcutItemProxy *> *)staticItems
{
NSMutableArray<TiUIShortcutItemProxy *> *shortcutsToReturn = [NSMutableArray array];
NSArray<NSDictionary *> *shortcuts = [NSBundle mainBundle].infoDictionary[@"UIApplicationShortcutItems"];

if (shortcuts == nil || [shortcuts count] == 0) {
NSArray<NSDictionary *> *shortcuts = NSBundle.mainBundle.infoDictionary[@"UIApplicationShortcutItems"];
if (shortcuts == nil || shortcuts.count == 0) {
return @[];
}

NSMutableArray<TiUIShortcutItemProxy *> *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"];
Expand All @@ -97,56 +95,66 @@ - (void)_listenerRemoved:(NSString *)type count:(int)count

- (TiUIShortcutItemProxy *)getById:(NSString *)identifier
{
NSArray<UIApplicationShortcutItem *> *shortcuts = [UIApplication sharedApplication].shortcutItems;
for (UIApplicationShortcutItem *item in shortcuts) {
if ([item.type isEqualToString:[TiUtils stringValue:identifier]]) {
return [[[TiUIShortcutItemProxy alloc] initWithShortcutItem:item] autorelease];
NSArray<UIApplicationShortcutItem *> *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<UIApplicationShortcutItem *> *shortcuts = (NSMutableArray<UIApplicationShortcutItem *> *)[UIApplication sharedApplication].shortcutItems;
for (UIApplicationShortcutItem *item in shortcuts) {
if ([item.type isEqualToString:[shortcut shortcutItem].type]) {
[shortcuts removeObject:item];
break;
NSArray<UIApplicationShortcutItem *> *shortcuts = UIApplication.sharedApplication.shortcutItems;
if (shortcuts != nil && shortcuts.count > 0) {
NSString *key = shortcut.shortcutItem.type;
NSMutableArray<UIApplicationShortcutItem *> *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<UIApplicationShortcutItem *> *shortcuts = (NSMutableArray<UIApplicationShortcutItem *> *)[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<UIApplicationShortcutItem *> *shortcuts = UIApplication.sharedApplication.shortcutItems;
if (shortcuts != nil && shortcuts.count > 0) {
NSString *key = shortcut.shortcutItem.type;
NSMutableArray<UIApplicationShortcutItem *> *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"]
Expand Down

0 comments on commit f967cf9

Please sign in to comment.