-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.m
190 lines (134 loc) · 4.84 KB
/
Tools.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
179
180
//
// Tools.m
// Weather
//
// Created by Егор Иванов on 15/01/15.
// Copyright (c) 2015 organization. All rights reserved.
//
#import "Tools.h"
@implementation Tools
#pragma mark - Threads
+(void)runInBackground:(void (^)(void))block{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block);
}
+(void)runLater:(NSTimeInterval)delay block:(void (^)(void))block{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}
+(void)runOnMainThread:(void (^)(void))block{
dispatch_async(dispatch_get_main_queue(), block);
}
#pragma mark - Networking
+(void)jsonRequest:(NSString*)request withCompletion:(void (^)(NSDictionary *json))completion{
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:request]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"JSON request failed: %@", error);
return;
}
if (response){
}
if (data){
NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments error:&jsonError];
if (jsonError){
NSLog(@"Invalid JSON: %@", jsonError);
return;
}
completion(json);
}
}] resume];
}
#pragma mark - Alerts
+(void)showAlertWithTitle:(NSString*)title message:(NSString*)message actions:(NSArray*)actions{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
message:message preferredStyle:UIAlertControllerStyleAlert];
for (UIAlertAction *action in actions) {
if (![action isKindOfClass:[UIAlertAction class]]){
return;
}
[alert addAction:action];
}
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[vc presentViewController:alert animated:YES completion:nil];
}
+(UIAlertAction *)alertActionWithTitle:(NSString*)title block:(void (^)(UIAlertAction *action))handler{
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:handler];
return alertAction;
}
#pragma mark - Core Graphics
CGSize CGSizeAspectFit(CGSize aspectRatio, CGSize boundingSize){
CGFloat mW = boundingSize.width / aspectRatio.width;
CGFloat mH = boundingSize.height / aspectRatio.height;
if( mH < mW ){
boundingSize.width = boundingSize.height / aspectRatio.height * aspectRatio.width;
}
else if( mW < mH ){
boundingSize.height = boundingSize.width / aspectRatio.width * aspectRatio.height;
}
return boundingSize;
}
CGSize CGSizeAspectFill(CGSize aspectRatio, CGSize minimumSize){
CGFloat mW = minimumSize.width / aspectRatio.width;
CGFloat mH = minimumSize.height / aspectRatio.height;
if( mH > mW ){
minimumSize.width = minimumSize.height / aspectRatio.height * aspectRatio.width;
}
else if( mW > mH ){
minimumSize.height = minimumSize.width / aspectRatio.width * aspectRatio.height;
}
return minimumSize;
}
#pragma mark Collections
+(void)findObjectForKey:(NSString*)desiredKey inDict:(NSDictionary*)dict withResultBlock:(void (^)(id result))resultBlock{
[self enumerateJSONToFindKeys:dict forKeyNamed:nil desiredKey:desiredKey resultBlock:resultBlock];
}
+(void)enumerateJSONToFindKeys:(id)object forKeyNamed:(NSString *)keyName desiredKey:(NSString*)desiredKey resultBlock:(void (^)(id result))resultBlock{
if ([object isKindOfClass:[NSDictionary class]]) {
[object enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
[self enumerateJSONToFindKeys:value forKeyNamed:key desiredKey:desiredKey resultBlock:resultBlock];
}];
}
else if ([object isKindOfClass:[NSArray class]]) {
[object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self enumerateJSONToFindKeys:obj forKeyNamed:nil desiredKey:desiredKey resultBlock:resultBlock];
}];
}
else {
if ([keyName isEqualToString:desiredKey]){
if (resultBlock){
resultBlock(object);
}
}
}
}
@end
@implementation UIView (Metrics)
-(CGPoint)centerForSubviews{
return CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
}
-(CGFloat)width{
return CGRectGetWidth(self.bounds);
}
-(CGFloat)height{
return CGRectGetHeight(self.bounds);
}
-(void)roundToNearestPixelEdges{
CGRect frame = self.frame;
frame.origin = CGPointMake(roundf(frame.origin.x), roundf(frame.origin.y));
self.frame = frame;
}
-(CGFloat)minX{
return CGRectGetMinX(self.frame);
}
-(CGFloat)maxX{
return CGRectGetMaxX(self.frame);
}
-(CGFloat)minY{
return CGRectGetMinY(self.frame);
}
-(CGFloat)maxY{
return CGRectGetMaxY(self.frame);
}
@end