Skip to content

Commit 6ae8cf0

Browse files
authored
Merge pull request #6 from hansemannn/fix-api
Attempt to fix crash (#5)
2 parents e2c5e10 + 653f161 commit 6ae8cf0

File tree

3 files changed

+91
-36
lines changed

3 files changed

+91
-36
lines changed

README.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,28 @@ Next, you have to declare the icons that you will use in your by adding the foll
4141

4242
```xml
4343
<ios>
44-
<key>CFBundleIcons</key>
45-
<dict>
46-
<key>CFBundleAlternateIcons</key>
47-
<dict>
48-
<key>alloyIcon</key>
49-
<dict>
50-
<key>CFBundleIconFiles</key>
51-
<array>
52-
<string>alloy</string>
53-
</array>
54-
</dict>
55-
</dict>
56-
<key>CFBundlePrimaryIcon</key>
57-
<dict>
58-
<key>CFBundleIconFiles</key>
59-
<array>
60-
<string>AppIcon</string>
61-
</array>
62-
</dict>
63-
</dict>
64-
</dict>
44+
<dict>
45+
<key>CFBundleIcons</key>
46+
<dict>
47+
<key>CFBundleAlternateIcons</key>
48+
<dict>
49+
<key>alloyIcon</key>
50+
<dict>
51+
<key>CFBundleIconFiles</key>
52+
<array>
53+
<string>alloy</string>
54+
</array>
55+
</dict>
56+
</dict>
57+
<key>CFBundlePrimaryIcon</key>
58+
<dict>
59+
<key>CFBundleIconFiles</key>
60+
<array>
61+
<string>AppIcon</string>
62+
</array>
63+
</dict>
64+
</dict>
65+
</dict>
6566
</ios>
6667
```
6768

@@ -87,11 +88,11 @@ if (AlternateIcons.isSupported()) {
8788
```
8889
#### Methods
8990

90-
- [x] `isSupported`
91-
- [x] `supportsAlternateIcons`
92-
- [x] `alternateIconName`
93-
- [x] `set​Alternate​Icon​Name`
94-
- [x] `setDefaultIconName`
91+
- [x] `isSupported`
92+
- [x] `supportsAlternateIcons`
93+
- [x] `alternateIconName`
94+
- [x] `set​Alternate​Icon​Name`
95+
- [x] `setDefaultIconName`
9596

9697
### Author
9798

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

108109
### Special Thanks
109110

110-
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
111+
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.

iphone/Classes/TiAlternateiconsModule.m

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "TiBase.h"
1010
#import "TiHost.h"
1111
#import "TiUtils.h"
12+
#import "TiFilesystemFileProxy.h"
1213
#import <CommonCrypto/CommonDigest.h>
1314

1415
@implementation TiAlternateiconsModule
@@ -38,6 +39,8 @@ -(void)startup
3839
NSLog(@"[DEBUG] %@ loaded",self);
3940
}
4041

42+
#define fileURLify(foo) [[NSURL fileURLWithPath:foo isDirectory:YES] path]
43+
4144
#pragma Public APIs
4245

4346
- (NSNumber *)isSupported:(id)unused
@@ -72,11 +75,20 @@ - (void)setAlternateIconName:(id)args
7275
{
7376
NSString *iconName;
7477
KrollCallback *callback;
75-
76-
ENSURE_ARG_OR_NIL_AT_INDEX(iconName, args, 0, NSString);
7778

78-
if ([args count] == 2) {
79-
ENSURE_ARG_AT_INDEX(callback, args, 1, KrollCallback);
79+
// FIXME: This is super ugly because of `setDefaultIcon`, need to refactor this whenever possible
80+
if ([args isKindOfClass:[NSArray class]] && [args count] == 2) {
81+
if ([args objectAtIndex:0] == [NSNull null]) {
82+
id _callback = [args objectAtIndex:1];
83+
iconName = nil;
84+
callback = _callback != [NSNull null] && _callback != nil ? (KrollCallback *)[args objectAtIndex:1] : nil;
85+
} else {
86+
ENSURE_ARG_OR_NIL_AT_INDEX(iconName, args, 0, NSString);
87+
ENSURE_ARG_OR_NIL_AT_INDEX(callback, args, 1, KrollCallback);
88+
}
89+
} else {
90+
ENSURE_TYPE_OR_NIL(args, NSString);
91+
iconName = [TiUtils stringValue:args];
8092
}
8193

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

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

91-
if (error) {
103+
if (error != nil) {
92104
[event setObject:[error localizedDescription] forKey:@"error"];
93105
}
94106

@@ -120,7 +132,7 @@ - (NSString *)validatedIconWithName:(NSString * _Nullable)name
120132
return nil;
121133
}
122134

123-
NSString *iconInAssetCatalog = [TiAlternateiconsModule iconInAssetCatalog:name];
135+
NSString *iconInAssetCatalog = [self iconInAssetCatalog:name];
124136

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

133145
// Based on Ti.Filesystem.getAsset()
134-
+ (NSString *)iconInAssetCatalog:(NSString *)icon
146+
- (NSString *)iconInAssetCatalog:(NSString *)icon
135147
{
136148
if (icon == nil) {
137149
return nil;
138150
}
139151

152+
icon = [self pathFromComponents:@[[icon stringByAppendingString:@".png"]]];
153+
140154
if ([icon hasPrefix:[NSString stringWithFormat:@"%@/", [[NSURL fileURLWithPath:[TiHost resourcePath] isDirectory:YES] path]]] && ([icon hasSuffix:@".jpg"] || [icon hasSuffix:@".png"])) {
141155

142156
NSRange range = [icon rangeOfString:@".app"];
@@ -161,7 +175,7 @@ + (NSString *)iconInAssetCatalog:(NSString *)icon
161175
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
162176
[sha appendFormat:@"%02x", digest[i]];
163177
}
164-
[sha appendString:[icon substringFromIndex:[icon length] - 4]];
178+
[sha stringByDeletingLastPathComponent];
165179
return [UIImage imageNamed:sha] == nil ? nil : sha;
166180
}
167181
}
@@ -170,4 +184,44 @@ + (NSString *)iconInAssetCatalog:(NSString *)icon
170184
return nil;
171185
}
172186

187+
- (NSString *)pathFromComponents:(NSArray *)args
188+
{
189+
NSString * newpath;
190+
id first = [args objectAtIndex:0];
191+
if ([first hasPrefix:@"file://"])
192+
{
193+
NSURL * fileUrl = [NSURL URLWithString:first];
194+
//Why not just crop? Because the url may have some things escaped that need to be unescaped.
195+
newpath =[fileUrl path];
196+
}
197+
else if ([first characterAtIndex:0]!='/')
198+
{
199+
NSURL* url = [NSURL URLWithString:[self resourcesDirectory]];
200+
newpath = [[url path] stringByAppendingPathComponent:[self resolveFile:first]];
201+
} else {
202+
newpath = [self resolveFile:first];
203+
}
204+
205+
if ([args count] > 1) {
206+
for (int c=1;c<[args count];c++) {
207+
newpath = [newpath stringByAppendingPathComponent:[self resolveFile:[args objectAtIndex:c]]];
208+
}
209+
}
210+
211+
return [newpath stringByStandardizingPath];
212+
}
213+
214+
- (NSString *)resourcesDirectory
215+
{
216+
return [NSString stringWithFormat:@"%@/", fileURLify([TiHost resourcePath])];
217+
}
218+
219+
- (id)resolveFile:(id)arg
220+
{
221+
if ([arg isKindOfClass:[TiFilesystemFileProxy class]]) {
222+
return [(TiFilesystemFileProxy*)arg path];
223+
}
224+
return [TiUtils stringValue:arg];
225+
}
226+
173227
@end

iphone/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 1.1.0
5+
version: 1.1.1
66
apiversion: 2
77
architectures: armv7 arm64 i386 x86_64
88
description: titanium-alternate-icons

0 commit comments

Comments
 (0)