Skip to content

Commit

Permalink
Merge pull request #3 from hansemannn/support-callbacks
Browse files Browse the repository at this point in the history
Support callbacks for improved error-handling
  • Loading branch information
ccavazos authored Apr 19, 2017
2 parents 6b4e26b + 6c77e95 commit 028e187
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 36 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions iphone/Classes/TiAlternateiconsModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 37 additions & 15 deletions iphone/Classes/TiAlternateiconsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -58,34 +58,56 @@ -(NSNumber*)supportsAlternateIcons:(id)unused
#endif
}

-(NSString*)alternateIconName:(id)unused
- (NSString *)alternateIconName:(id)unused
{
#ifdef __IPHONE_10_3
return [[UIApplication sharedApplication] alternateIconName];
#else
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
2 changes: 1 addition & 1 deletion iphone/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions ti.alternateicons.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Titanium Alternate Icons (Hyperloop)
* @version 1.0.1
* @version 1.1.0
* @author ccavazos
*/
var UIApplication = require('UIKit/UIApplication');
Expand All @@ -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);
};

0 comments on commit 028e187

Please sign in to comment.