-
Notifications
You must be signed in to change notification settings - Fork 6
/
CBRColorCache.m
102 lines (80 loc) · 2.37 KB
/
CBRColorCache.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
#import "CBRColorCache.h"
#import "ColorBadges.h"
#import "Defines.h"
#import <objc/runtime.h>
#define DEFAULT_COUNT_LIMIT 100
static id bucket_Class = nil;
static id cb_Class = nil;
static BOOL prettierBanners_isInstalled = NO;
static void proxy_init() {
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
bucket_Class = objc_getClass("CBRBucket");
cb_Class = objc_getClass("ColorBadges");
NSString *pBPath = @"/Library/MobileSubstrate/DynamicLibraries/PrettierBanners.dylib";
prettierBanners_isInstalled = [[NSFileManager defaultManager] fileExistsAtPath:pBPath];
});
}
static int proxy_colorForImage(UIImage *image) {
proxy_init();
if (bucket_Class) {
return [bucket_Class colorForImage:image];
}
return [[cb_Class sharedInstance] colorForImage:image];
}
static BOOL proxy_isDarkColor(int color) {
proxy_init();
if (bucket_Class) {
return [bucket_Class isDarkColor:color];
}
return [cb_Class isDarkColor:color];
}
@implementation CBRColorCache
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken;
static CBRColorCache *cache;
dispatch_once(&onceToken, ^{ cache = [[CBRColorCache alloc] init]; } );
return cache;
}
+ (BOOL)isDarkColor:(int)color {
return proxy_isDarkColor(color);
}
- (instancetype)init {
self = [super init];
if (self) {
_cache = [[NSCache alloc] init];
[_cache setCountLimit:DEFAULT_COUNT_LIMIT];
}
return self;
}
- (int)colorForIdentifier:(NSString *)identifier image:(UIImage *)image {
if (!identifier) {
CBRLOG(@"No identifier given for image %@", image);
return proxy_colorForImage(image);
}
if (prettierBanners_isInstalled && [identifier isEqualToString:@"com.apple.MobileSMS"]) {
CBRLOG(@"PrettierBanners is installed! Avoiding the cache.");
return proxy_colorForImage(image);
}
NSNumber *colorNum = [_cache objectForKey:identifier];
if (colorNum) {
CBRLOG(@"Cache hit for identifier %@", identifier);
return [colorNum intValue];
} else {
CBRLOG(@"Cache miss for identifier %@", identifier);
int color = proxy_colorForImage(image);
[_cache setObject:@(color) forKey:identifier];
return color;
}
}
- (int)colorForImage:(UIImage *)image {
if (!image) {
CBRLOG(@"No image given when requesting analysis!");
}
return proxy_colorForImage(image);
}
- (void)dealloc {
[_cache release];
[super dealloc];
}
@end