-
Notifications
You must be signed in to change notification settings - Fork 1
/
RGHelpers.m
178 lines (136 loc) · 5.26 KB
/
RGHelpers.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// RGHelpers.m
//
// Created by Rok Gregorič on 2/5/13.
// Copyright (c) 2013 Rok Gregorič. All rights reserved.
//
#include "RGHelpers.h"
#pragma mark - UIColor (IntegerConversion)
@implementation UIColor (IntegerConversion)
+ (UIColor *)colorWithInt:(NSInteger)number {
return [UIColor colorWithRed:((number & 0xFF0000) >> 16) / 255.0
green:((number & 0xFF00) >> 8) / 255.0
blue:(number & 0xFF) / 255.0
alpha:1];
}
- (BOOL)isEqualToColor:(UIColor *)otherColor {
if (self == otherColor) return YES;
CGColorSpaceRef colorSpaceRGB = CGColorSpaceCreateDeviceRGB();
UIColor *(^convertColorToRGBSpace)(UIColor *) = ^(UIColor *color) {
if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) == kCGColorSpaceModelMonochrome) {
const CGFloat *oldComponents = CGColorGetComponents(color.CGColor);
CGFloat components[4] = {oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1]};
CGColorRef colorRef = CGColorCreate(colorSpaceRGB, components);
UIColor *color = [UIColor colorWithCGColor:colorRef];
CGColorRelease(colorRef);
return color;
}
return color;
};
UIColor *selfColor = convertColorToRGBSpace(self);
otherColor = convertColorToRGBSpace(otherColor);
CGColorSpaceRelease(colorSpaceRGB);
return [selfColor isEqual:otherColor];
}
@end
#pragma mark - UIColor (LighterAndDarker)
@implementation UIColor (LighterAndDarker)
- (UIColor *)colorWithBrightnessComponent:(CGFloat)brightness {
CGFloat h, s, b, a;
if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
return [UIColor colorWithHue:h
saturation:s
brightness:MIN(b * brightness, 1)
alpha:a];
return nil;
}
- (UIColor *)lighterColor {
return [self colorWithBrightnessComponent:1.3];
}
- (UIColor *)darkerColor {
return [self colorWithBrightnessComponent:0.85];
}
@end
#pragma mark - NSString (UrlEncoding)
@implementation NSString (UrlEncoding)
- (NSString *)urlEncoded {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8));
}
- (NSString *)urlDecoded {
return [[self stringByReplacingOccurrencesOfString:@"+" withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@end
#pragma mark - UIApplication (NetworkActivity)
@implementation UIApplication (NetworkActivity)
- (void)setNetworkActivityIndicatorVisibleWithCounter:(BOOL)setVisible {
static int numberOfCallsToSetVisible = 0;
numberOfCallsToSetVisible += setVisible ? +1 : -1;
UIApplication.sharedApplication.networkActivityIndicatorVisible = (numberOfCallsToSetVisible > 0);
}
@end
#pragma mark - NSObject (NetworkRequest)
@implementation NSObject (NetworkRequest)
- (void)asyncNetworkRequestForUrl:(NSString *)url withCompletion:(void (^)(NSData *response))completion {
UIApplication.sharedApplication.networkActivityIndicatorVisibleWithCounter = YES;
runOnQueue(@"networkRequestQueue", ^{
for (int retries = 0; retries < 3; retries++) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
if (data.length) {
runInForegroundAsync(^{
UIApplication.sharedApplication.networkActivityIndicatorVisibleWithCounter = NO;
completion(data);
});
return;
}
}
runInForegroundAsync(^{
UIApplication.sharedApplication.networkActivityIndicatorVisibleWithCounter = NO;
completion(nil);
});
});
}
@end
#pragma mark - GCD helpers
void runInForegroundSync(void (^block)(void)) {
if (NSThread.isMainThread) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}
void runInForegroundAsync(void (^block)(void)) {
dispatch_async(dispatch_get_main_queue(), block);
}
void runInBackground(void (^block)(void)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block);
}
void runOnQueue(NSString *queue, void (^block)(void)) {
dispatch_async(dispatch_queue_create(queue.UTF8String, DISPATCH_QUEUE_CONCURRENT), block);
}
void runDelayed(double delayInSeconds, void (^block)(void)) {
#if TARGET_IPHONE_SIMULATOR
CGFloat UIAnimationDragCoefficient(void);
delayInSeconds *= MAX(UIAnimationDragCoefficient(), 1);
#endif
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), block);
}
#pragma mark - CGRect helpers
CGRect CGRectSetWidth(CGRect rect, CGFloat width) {
return CGRectMake(rect.origin.x, rect.origin.y, width, rect.size.height);
}
CGRect CGRectSetHeight(CGRect rect, CGFloat height) {
return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, height);
}
CGRect CGRectSetSize(CGRect rect, CGSize size) {
return CGRectMake(rect.origin.x, rect.origin.y, size.width, size.height);
}
CGRect CGRectSetX(CGRect rect, CGFloat x) {
return CGRectMake(x, rect.origin.y, rect.size.width, rect.size.height);
}
CGRect CGRectSetY(CGRect rect, CGFloat y) {
return CGRectMake(rect.origin.x, y, rect.size.width, rect.size.height);
}
CGRect CGRectSetOrigin(CGRect rect, CGPoint origin) {
return CGRectMake(origin.x, origin.y, rect.size.width, rect.size.height);
}