Skip to content

Commit

Permalink
Merge pull request #4 from hansemannn/support-asset-catalog
Browse files Browse the repository at this point in the history
Support asset catalog (Proof of Concept)
  • Loading branch information
ccavazos authored Apr 19, 2017
2 parents 028e187 + cdc843a commit e2c5e10
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 17 deletions.
82 changes: 71 additions & 11 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 <CommonCrypto/CommonDigest.h>

@implementation TiAlternateiconsModule

Expand Down Expand Up @@ -79,20 +80,21 @@ - (void)setAlternateIconName:(id)args
}

#ifdef __IPHONE_10_3
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (callback == nil) {
return;
}
[[UIApplication sharedApplication] setAlternateIconName:[self validatedIconWithName:iconName]
completionHandler:^(NSError * _Nullable error) {
if (callback == nil) {
return;
}

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

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

NSArray *invocationArray = [NSArray arrayWithObjects:&event count:1];
[callback call:invocationArray thisObject:self];
}];
NSArray *invocationArray = [NSArray arrayWithObjects:&event count:1];
[callback call:invocationArray thisObject:self];
}];
#else
if (callback != nil) {
NSDictionary *event = @{@"success": NUMBOOL(NO), @"error": @"This feature is only available on iOS 10.3 and later."};
Expand All @@ -110,4 +112,62 @@ - (void)setDefaultIconName:(id)args
[self setAlternateIconName:@[[NSNull null], [args count] == 1 ? [args objectAtIndex:0] : [NSNull null]]];
}

#pragma mark Utilities

- (NSString *)validatedIconWithName:(NSString * _Nullable)name
{
if (name == nil) {
return nil;
}

NSString *iconInAssetCatalog = [TiAlternateiconsModule iconInAssetCatalog:name];

// This will return nil if asset catalog is not used
if (iconInAssetCatalog == nil) {
return name;
}

return iconInAssetCatalog;
}

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

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

NSRange range = [icon rangeOfString:@".app"];
NSString *imageArg = nil;

if (range.location != NSNotFound) {
imageArg = [icon substringFromIndex:range.location + 5];
}

//remove suffixes.
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@3x" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~iphone" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~ipad" withString:@""];

if (imageArg != nil) {
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
NSData *stringBytes = [imageArg dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([stringBytes bytes], (CC_LONG)[stringBytes length], digest)) {
// SHA-1 hash has been calculated and stored in 'digest'.
NSMutableString *sha = [[NSMutableString alloc] init];
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
[sha appendFormat:@"%02x", digest[i]];
}
[sha appendString:[icon substringFromIndex:[icon length] - 4]];
return [UIImage imageNamed:sha] == nil ? nil : sha;
}
}
}

return nil;
}

@end
43 changes: 37 additions & 6 deletions iphone/titanium-alternate-icons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
0867D690FE84028FC02AAC07 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0600;
LastUpgradeCheck = 0830;
};
buildConfigurationList = 1DEB922208733DC00010E9CD /* Build configuration list for PBXProject "titanium-alternate-icons" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -233,7 +233,7 @@
GCC_WARN_UNUSED_VALUE = NO;
GCC_WARN_UNUSED_VARIABLE = NO;
INSTALL_PATH = /usr/local/lib;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LIBRARY_SEARCH_PATHS = "";
OTHER_CFLAGS = (
"-DDEBUG",
Expand Down Expand Up @@ -272,7 +272,7 @@
GCC_WARN_UNUSED_VALUE = NO;
GCC_WARN_UNUSED_VARIABLE = NO;
INSTALL_PATH = /usr/local/lib;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LIBRARY_SEARCH_PATHS = "";
OTHER_CFLAGS = "-DTI_POST_1_2";
OTHER_LDFLAGS = "-ObjC";
Expand All @@ -287,21 +287,37 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 24DD6D1B1134B66800162E58 /* titanium.xcconfig */;
buildSettings = {
CLANG_CXX_LIBRARY = "libstdc++";
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LIBRARY = "compiler-default";
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DSTROOT = /tmp/TiAlternateicons.dst;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = c99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = TiAlternateicons_Prefix.pch;
GCC_PREPROCESSOR_DEFINITIONS = "TI_VERSION=$(TI_VERSION)";
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
GCC_VERSION = "";
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = NO;
GCC_WARN_MISSING_PARENTHESES = NO;
GCC_WARN_SHADOW = NO;
GCC_WARN_STRICT_SELECTOR_MATCH = NO;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_PARAMETER = NO;
GCC_WARN_UNUSED_VALUE = NO;
Expand All @@ -327,25 +343,40 @@
baseConfigurationReference = 24DD6D1B1134B66800162E58 /* titanium.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LIBRARY = "libstdc++";
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LIBRARY = "compiler-default";
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
DSTROOT = /tmp/TiAlternateicons.dst;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = c99;
GCC_MODEL_TUNING = G5;
GCC_NO_COMMON_BLOCKS = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = TiAlternateicons_Prefix.pch;
GCC_PREPROCESSOR_DEFINITIONS = "TI_VERSION=$(TI_VERSION)";
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
GCC_VERSION = "";
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = NO;
GCC_WARN_MISSING_PARENTHESES = NO;
GCC_WARN_SHADOW = NO;
GCC_WARN_STRICT_SELECTOR_MATCH = NO;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_PARAMETER = NO;
GCC_WARN_UNUSED_VALUE = NO;
GCC_WARN_UNUSED_VARIABLE = NO;
INSTALL_PATH = /usr/local/lib;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
OTHER_CFLAGS = "-DTI_POST_1_2";
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = ti.alternateicons;
Expand Down

0 comments on commit e2c5e10

Please sign in to comment.