forked from a3tweaks/Flipswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSBundle+Flipswitch.m
133 lines (122 loc) · 5.22 KB
/
NSBundle+Flipswitch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#import "NSBundle+Flipswitch.h"
#import "ControlStateVariants.h"
#import "FSSwitchState.h"
@implementation NSBundle (Flipswitch)
- (NSBundle *)flipswitchThemedBundle
{
NSString *path = [[self pathForResource:@"Theme" ofType:@"plist"] stringByDeletingLastPathComponent];
return (!path || [path isEqualToString:[self bundlePath]]) ? self : [NSBundle bundleWithPath:path];
}
- (NSDictionary *)flipswitchThemedInfoDictionary
{
NSString *path = [self pathForResource:@"Theme" ofType:@"plist"];
if (path) {
NSDictionary *result = [NSDictionary dictionaryWithContentsOfFile:path];
if (result)
return result;
}
return self.infoDictionary;
}
static inline NSString *MD5OfString(NSString *string)
{
return MD5OfData([string dataUsingEncoding:NSUTF8StringEncoding] ?: [NSData data]);
}
- (NSString *)flipswitchImageCacheBasePath
{
return [@"/tmp/FlipswitchCache/" stringByAppendingString:MD5OfString([self bundlePath])];
}
- (NSArray *)FSImageImageFileTypes
{
return [NSArray arrayWithObjects:@"pdf", @"png", nil];
}
- (NSUInteger)imageSizeForFlipswitchImageName:(NSString *)imageName closestToSize:(CGFloat)sourceSize inDirectory:(NSString *)directory
{
NSMutableIndexSet *sizes = [[NSMutableIndexSet alloc] init];
for (NSString *fileType in self.FSImageImageFileTypes) {
NSArray *images = [self pathsForResourcesOfType:fileType inDirectory:directory];
for (NSString *fullPath in images) {
NSString *fileName = [fullPath lastPathComponent];
NSInteger location = [fileName rangeOfString:@"-" options:NSLiteralSearch | NSBackwardsSearch].location;
if (location == NSNotFound) {
if ([[fileName stringByDeletingPathExtension] isEqualToString:imageName])
[sizes addIndex:0];
} else {
NSInteger lastPartInteger = [[fileName substringFromIndex:location + 1] integerValue];
if (lastPartInteger == 0) {
if ([[fileName stringByDeletingPathExtension] isEqualToString:imageName])
[sizes addIndex:0];
} else if (lastPartInteger > 9) {
if ([[fileName substringToIndex:location] isEqualToString:imageName])
[sizes addIndex:(NSUInteger)lastPartInteger];
}
}
}
NSUInteger closestSize = [sizes indexGreaterThanOrEqualToIndex:(NSUInteger)sourceSize];
if (closestSize == NSNotFound)
closestSize = [sizes indexLessThanIndex:(NSUInteger)sourceSize];
if (closestSize != NSNotFound) {
[sizes release];
return closestSize;
}
}
[sizes release];
return NSNotFound;
}
- (NSString *)imagePathForFlipswitchImageName:(NSString *)imageName imageSize:(NSUInteger)imageSize preferredScale:(CGFloat)preferredScale controlState:(UIControlState)controlState inDirectory:(NSString *)directory loadedControlState:(UIControlState *)outImageControlState
{
if (!imageName)
return nil;
if (imageSize == NSNotFound)
return nil;
NSString *suffix = imageSize ? [NSString stringWithFormat:@"-%lu", (unsigned long)imageSize] : @"";
NSString *scaleSuffix = preferredScale > 1.0f ? [NSString stringWithFormat:@"@%.0fx", preferredScale] : nil;
for (NSString *fileType in self.FSImageImageFileTypes) {
for (size_t i = 0; i < sizeof(ControlStateVariantMasks) / sizeof(*ControlStateVariantMasks); i++) {
UIControlState newState = controlState & ControlStateVariantMasks[i];
NSString *name = [ControlStateVariantApply(imageName, newState) stringByAppendingString:suffix];
NSString *filePath = scaleSuffix ? [self pathForResource:[name stringByAppendingString:scaleSuffix] ofType:fileType inDirectory:directory] : nil;
if (!filePath)
filePath = directory ? [self pathForResource:name ofType:fileType inDirectory:directory] : [self pathForResource:name ofType:fileType];
if (filePath) {
if (outImageControlState) {
*outImageControlState = newState;
}
return filePath;
}
}
}
return nil;
}
- (NSString *)imagePathForFlipswitchImageName:(NSString *)imageName imageSize:(NSUInteger)imageSize preferredScale:(CGFloat)preferredScale controlState:(UIControlState)controlState inDirectory:(NSString *)directory
{
return [self imagePathForFlipswitchImageName:imageName imageSize:imageSize preferredScale:preferredScale controlState:controlState inDirectory:directory loadedControlState:NULL];
}
- (id)objectForResolvedInfoDictionaryKey:(NSString *)name withSwitchState:(FSSwitchState)state controlState:(UIControlState)controlState resolvedKeyName:(NSString **)outKeyName
{
NSDictionary *themedInfoDictionary = self.flipswitchThemedInfoDictionary;
NSString *stateName = [NSString stringWithFormat:@"%@-%@", name, NSStringFromFSSwitchState(state)];
for (size_t i = 0; i < sizeof(ControlStateVariantMasks) / sizeof(*ControlStateVariantMasks); i++) {
UIControlState newState = controlState & ControlStateVariantMasks[i];
NSString *key = ControlStateVariantApply(stateName, newState);
id value = [themedInfoDictionary objectForKey:key];
if (value) {
if (outKeyName)
*outKeyName = key;
return value;
}
}
for (size_t i = 0; i < sizeof(ControlStateVariantMasks) / sizeof(*ControlStateVariantMasks); i++) {
UIControlState newState = controlState & ControlStateVariantMasks[i];
NSString *key = ControlStateVariantApply(name, newState);
id value = [themedInfoDictionary objectForKey:key];
if (value) {
if (outKeyName)
*outKeyName = key;
return value;
}
}
if (outKeyName)
*outKeyName = nil;
return nil;
}
@end