diff --git a/README.md b/README.md index 2e6ba28..42fadd5 100644 --- a/README.md +++ b/README.md @@ -75,10 +75,14 @@ You can simple require the `ti.alternateicons.js` in your application and run it #### Example ```javascript -var AlternateIcon = require('ti.alternateicons'); - -if (AlternateIcon.isSupported()) { - AlternateIcon.set​Alternate​Icon​Name('alloyIcon'); +var AlternateIcons = require('ti.alternateicons'); + +if (AlternateIcons.isSupported()) { + AlternateIcons.set​Alternate​Icon​Name('alloyIcon', function(e) { + if (!e.success) { + Ti.API.error(e.error); + } + }); } ``` #### Methods diff --git a/iphone/Classes/TiAlternateiconsModule.h b/iphone/Classes/TiAlternateiconsModule.h index 8becd51..59d2f79 100644 --- a/iphone/Classes/TiAlternateiconsModule.h +++ b/iphone/Classes/TiAlternateiconsModule.h @@ -9,10 +9,14 @@ @interface TiAlternateiconsModule : TiModule -- (NSNumber*) isSupported:(id)unused; -- (NSNumber*) supportsAlternateIcons:(id)unused; -- (NSString*) alternateIconName:(id)unused; -- (void) setAlternateIconName:(NSString*)iconName; -- (void) setDefaultIconName:(id)unused; +- (NSNumber *)isSupported:(id)unused; + +- (NSNumber *)supportsAlternateIcons:(id)unused; + +- (NSString *)alternateIconName:(id)unused; + +- (void)setAlternateIconName:(id)args; + +- (void)setDefaultIconName:(id)args; @end diff --git a/iphone/Classes/TiAlternateiconsModule.m b/iphone/Classes/TiAlternateiconsModule.m index 8282d76..30e72dd 100644 --- a/iphone/Classes/TiAlternateiconsModule.m +++ b/iphone/Classes/TiAlternateiconsModule.m @@ -15,13 +15,13 @@ @implementation TiAlternateiconsModule #pragma mark Internal // this is generated for your module, please do not change it --(id)moduleGUID +- (id)moduleGUID { return @"1730e6ec-327b-4f48-8705-461178f9db12"; } // this is generated for your module, please do not change it --(NSString*)moduleId +- (NSString *)moduleId { return @"ti.alternateicons"; } @@ -39,7 +39,7 @@ -(void)startup #pragma Public APIs --(NSNumber*)isSupported:(id)unused +- (NSNumber *)isSupported:(id)unused { #ifdef __IPHONE_10_3 return NUMBOOL([[[UIDevice currentDevice] systemVersion] compare:@"10.3" options:NSNumericSearch] != NSOrderedAscending); @@ -48,7 +48,7 @@ -(NSNumber*)isSupported:(id)unused #endif } --(NSNumber*)supportsAlternateIcons:(id)unused +- (NSNumber *)supportsAlternateIcons:(id)unused { ENSURE_ARG_COUNT(unused, 0); #ifdef __IPHONE_10_3 @@ -58,7 +58,7 @@ -(NSNumber*)supportsAlternateIcons:(id)unused #endif } --(NSString*)alternateIconName:(id)unused +- (NSString *)alternateIconName:(id)unused { #ifdef __IPHONE_10_3 return [[UIApplication sharedApplication] alternateIconName]; @@ -66,26 +66,48 @@ -(NSString*)alternateIconName:(id)unused NSLog(@"[ERROR] Ti.AlternateIcons: This feature is only available on iOS 10.3 and later."); #endif } --(void)setAlternateIconName:(NSString*)iconName + +- (void)setAlternateIconName:(id)args { - ENSURE_STRING_OR_NIL(iconName) + NSString *iconName; + KrollCallback *callback; + + ENSURE_ARG_OR_NIL_AT_INDEX(iconName, args, 0, NSString); + + if ([args count] == 2) { + ENSURE_ARG_AT_INDEX(callback, args, 1, KrollCallback); + } + #ifdef __IPHONE_10_3 [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) { + if (callback == nil) { + return; + } + + NSMutableDictionary *event = [NSMutableDictionary dictionaryWithDictionary:@{@"success": NUMBOOL(error == nil)}]; + if (error) { - NSLog(@"[ERROR] Ti.AlternateIcons: %@", [error localizedDescription]); - } else { - // TODO: Pass optional callback as second parameter - NSLog(@"[DEBUG] Ti.AlternateIcons: Icon has been changed to %@.", iconName); + [event setObject:[error localizedDescription] forKey:@"error"]; } + + NSArray *invocationArray = [NSArray arrayWithObjects:&event count:1]; + [callback call:invocationArray thisObject:self]; }]; #else - NSLog(@"[ERROR] Ti.AlternateIcons: This feature is only available on iOS 10.3 and later."); + if (callback != nil) { + NSDictionary *event = @{@"success": NUMBOOL(NO), @"error": @"This feature is only available on iOS 10.3 and later."}; + + NSArray *invocationArray = [NSArray arrayWithObjects:&event count:1]; + [callback call:invocationArray thisObject:self]; + } else { + NSLog(@"[ERROR] Ti.AlternateIcons: This feature is only available on iOS 10.3 and later."); + } #endif } --(void)setDefaultIconName:(id)unused + +- (void)setDefaultIconName:(id)args { - ENSURE_ARG_COUNT(unused, 0); - [self setAlternateIconName:nil]; + [self setAlternateIconName:@[[NSNull null], [args count] == 1 ? [args objectAtIndex:0] : [NSNull null]]]; } @end diff --git a/iphone/manifest b/iphone/manifest index 2ed3527..b127881 100644 --- a/iphone/manifest +++ b/iphone/manifest @@ -2,7 +2,7 @@ # this is your module manifest and used by Titanium # during compilation, packaging, distribution, etc. # -version: 1.0.1 +version: 1.1.0 apiversion: 2 architectures: armv7 arm64 i386 x86_64 description: titanium-alternate-icons diff --git a/ti.alternateicons.js b/ti.alternateicons.js index 996bc18..3fe4c21 100644 --- a/ti.alternateicons.js +++ b/ti.alternateicons.js @@ -1,6 +1,6 @@ /** * Titanium Alternate Icons (Hyperloop) - * @version 1.0.1 + * @version 1.1.0 * @author ccavazos */ var UIApplication = require('UIKit/UIApplication'); @@ -25,16 +25,24 @@ exports.alternateIconName = function() { } }; -exports.setAlternateIconName = function(iconName) { - UIApplication.sharedApplication.setAlternateIconNameCompletionHandler(iconName, function(error){ - if (error) { - console.error('Ti.AlternateIcons', error); - } else { - console.log('Ti.AlternateIcons: Icon has been changed to', (iconName) ? iconName : 'Default'); - } - }); +exports.setAlternateIconName = function(iconName, cb) { + UIApplication.sharedApplication.setAlternateIconNameCompletionHandler(iconName, function(error) { + if (!cb) { + return; + } + + var event = { + success: error == null + }; + + if (error != null) { + event.error = error.localizedDescription; + } + + cb(event); + }); }; -exports.setDefaultIconName = function() { - exports.setAlternateIconName(null); +exports.setDefaultIconName = function(cb) { + exports.setAlternateIconName(null, cb); };