Skip to content

Commit

Permalink
Merge pull request #6 from hansemannn/fix-api
Browse files Browse the repository at this point in the history
Attempt to fix crash (#5)
  • Loading branch information
ccavazos authored Jun 14, 2017
2 parents e2c5e10 + 653f161 commit 6ae8cf0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 36 deletions.
55 changes: 28 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,28 @@ Next, you have to declare the icons that you will use in your by adding the foll

```xml
<ios>
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>alloyIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>alloy</string>
</array>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
</dict>
</dict>
</dict>
<dict>
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>alloyIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>alloy</string>
</array>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
</dict>
</dict>
</dict>
</ios>
```

Expand All @@ -87,11 +88,11 @@ if (AlternateIcons.isSupported()) {
```
#### Methods

- [x] `isSupported`
- [x] `supportsAlternateIcons`
- [x] `alternateIconName`
- [x] `set​Alternate​Icon​Name`
- [x] `setDefaultIconName`
- [x] `isSupported`
- [x] `supportsAlternateIcons`
- [x] `alternateIconName`
- [x] `set​Alternate​Icon​Name`
- [x] `setDefaultIconName`

### Author

Expand All @@ -107,4 +108,4 @@ Code contributions are greatly appreciated, please submit a new [pull request](h

### Special Thanks

To [Hans Knoechel](https://github.com/hansemannn) because this module was created using [titanium-review-dialog](https://github.com/hansemannn/titanium-review-dialog) as a reference
To [Hans Knoechel](https://github.com/hansemannn) because this module was created using [titanium-review-dialog](https://github.com/hansemannn/titanium-review-dialog) as a reference.
70 changes: 62 additions & 8 deletions iphone/Classes/TiAlternateiconsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiFilesystemFileProxy.h"
#import <CommonCrypto/CommonDigest.h>

@implementation TiAlternateiconsModule
Expand Down Expand Up @@ -38,6 +39,8 @@ -(void)startup
NSLog(@"[DEBUG] %@ loaded",self);
}

#define fileURLify(foo) [[NSURL fileURLWithPath:foo isDirectory:YES] path]

#pragma Public APIs

- (NSNumber *)isSupported:(id)unused
Expand Down Expand Up @@ -72,11 +75,20 @@ - (void)setAlternateIconName:(id)args
{
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);
// FIXME: This is super ugly because of `setDefaultIcon`, need to refactor this whenever possible
if ([args isKindOfClass:[NSArray class]] && [args count] == 2) {
if ([args objectAtIndex:0] == [NSNull null]) {
id _callback = [args objectAtIndex:1];
iconName = nil;
callback = _callback != [NSNull null] && _callback != nil ? (KrollCallback *)[args objectAtIndex:1] : nil;
} else {
ENSURE_ARG_OR_NIL_AT_INDEX(iconName, args, 0, NSString);
ENSURE_ARG_OR_NIL_AT_INDEX(callback, args, 1, KrollCallback);
}
} else {
ENSURE_TYPE_OR_NIL(args, NSString);
iconName = [TiUtils stringValue:args];
}

#ifdef __IPHONE_10_3
Expand All @@ -88,7 +100,7 @@ - (void)setAlternateIconName:(id)args

NSMutableDictionary *event = [NSMutableDictionary dictionaryWithDictionary:@{@"success": NUMBOOL(error == nil)}];

if (error) {
if (error != nil) {
[event setObject:[error localizedDescription] forKey:@"error"];
}

Expand Down Expand Up @@ -120,7 +132,7 @@ - (NSString *)validatedIconWithName:(NSString * _Nullable)name
return nil;
}

NSString *iconInAssetCatalog = [TiAlternateiconsModule iconInAssetCatalog:name];
NSString *iconInAssetCatalog = [self iconInAssetCatalog:name];

// This will return nil if asset catalog is not used
if (iconInAssetCatalog == nil) {
Expand All @@ -131,12 +143,14 @@ - (NSString *)validatedIconWithName:(NSString * _Nullable)name
}

// Based on Ti.Filesystem.getAsset()
+ (NSString *)iconInAssetCatalog:(NSString *)icon
- (NSString *)iconInAssetCatalog:(NSString *)icon
{
if (icon == nil) {
return nil;
}

icon = [self pathFromComponents:@[[icon stringByAppendingString:@".png"]]];

if ([icon hasPrefix:[NSString stringWithFormat:@"%@/", [[NSURL fileURLWithPath:[TiHost resourcePath] isDirectory:YES] path]]] && ([icon hasSuffix:@".jpg"] || [icon hasSuffix:@".png"])) {

NSRange range = [icon rangeOfString:@".app"];
Expand All @@ -161,7 +175,7 @@ + (NSString *)iconInAssetCatalog:(NSString *)icon
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
[sha appendFormat:@"%02x", digest[i]];
}
[sha appendString:[icon substringFromIndex:[icon length] - 4]];
[sha stringByDeletingLastPathComponent];
return [UIImage imageNamed:sha] == nil ? nil : sha;
}
}
Expand All @@ -170,4 +184,44 @@ + (NSString *)iconInAssetCatalog:(NSString *)icon
return nil;
}

- (NSString *)pathFromComponents:(NSArray *)args
{
NSString * newpath;
id first = [args objectAtIndex:0];
if ([first hasPrefix:@"file://"])
{
NSURL * fileUrl = [NSURL URLWithString:first];
//Why not just crop? Because the url may have some things escaped that need to be unescaped.
newpath =[fileUrl path];
}
else if ([first characterAtIndex:0]!='/')
{
NSURL* url = [NSURL URLWithString:[self resourcesDirectory]];
newpath = [[url path] stringByAppendingPathComponent:[self resolveFile:first]];
} else {
newpath = [self resolveFile:first];
}

if ([args count] > 1) {
for (int c=1;c<[args count];c++) {
newpath = [newpath stringByAppendingPathComponent:[self resolveFile:[args objectAtIndex:c]]];
}
}

return [newpath stringByStandardizingPath];
}

- (NSString *)resourcesDirectory
{
return [NSString stringWithFormat:@"%@/", fileURLify([TiHost resourcePath])];
}

- (id)resolveFile:(id)arg
{
if ([arg isKindOfClass:[TiFilesystemFileProxy class]]) {
return [(TiFilesystemFileProxy*)arg path];
}
return [TiUtils stringValue:arg];
}

@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.1.0
version: 1.1.1
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: titanium-alternate-icons
Expand Down

0 comments on commit 6ae8cf0

Please sign in to comment.