-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonUtility.m
More file actions
362 lines (301 loc) · 16.1 KB
/
CommonUtility.m
File metadata and controls
362 lines (301 loc) · 16.1 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// CommonUtility.m
// EMAN
//
// Created by Martin Darma Kusuma Tjandra on 1/22/16.
// Copyright © 2016 Danny Raharja. All rights reserved.
//
#import "CommonUtility.h"
@implementation Util
+(void) showMessageInViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonOKTitle:(NSString *)buttonOKTitle completion:(completionBlock)completion {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:buttonOKTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
if (completion != nil) completion();
}]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[viewController presentViewController:alertController animated:YES completion:nil];
}
+(void) showMessageInViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message completion:(completionBlock)completion {
[self showMessageInViewController:viewController title:title message:message buttonOKTitle:@"Ok" completion:completion];
}
+(void) showMessageInViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonOKTitle:(NSString *)buttonOKTitle nextFirstResponder:(NSObject *)nextFirstResponder {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:buttonOKTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (nextFirstResponder != nil && [nextFirstResponder isKindOfClass:[UITextField class]]) {
UITextField *t = (UITextField *)nextFirstResponder;
[t becomeFirstResponder];
}
}]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[viewController presentViewController:alertController animated:YES completion:nil];
}
+(void) showMessageInViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message nextFirstResponder:(NSObject *)nextFirstResponder{
[self showMessageInViewController:viewController title:title message:message buttonOKTitle:@"Ok" nextFirstResponder:nextFirstResponder];
}
+ (void) showConfirmationInViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message buttonYESTitle:(NSString *)buttonYESTitle buttonNOTitle:(NSString*)buttonNOTitle chooseYES:(completionBlock)chooseYES chooseNO:(completionBlock)chooseNO {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionYES = [UIAlertAction actionWithTitle:buttonYESTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
if (chooseYES != nil) chooseYES();
}];
[alertController addAction:actionYES];
UIAlertAction *actionNO = [UIAlertAction actionWithTitle:buttonNOTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
if (chooseNO != nil) chooseNO();
}];
[alertController addAction:actionNO];
[viewController presentViewController:alertController animated:YES completion:nil];
}
+ (void)showIndicatorAtView:(UIView *)view withNetworkActivityIndicator:(BOOL)withNetworkActivityIndicator {
UIActivityIndicatorView *indicator;
if ([view viewWithTag:15372] != nil)
indicator = [view viewWithTag:15372];
else
indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = view.center;
if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UITextField class]]) {
indicator.center = CGPointMake(view.bounds.size.width - (view.bounds.size.height / 2), view.bounds.size.height / 2);
}
[indicator bringSubviewToFront:view];
[indicator setTag:15372];
[indicator startAnimating];
[view addSubview:indicator];
[UIApplication sharedApplication].networkActivityIndicatorVisible = withNetworkActivityIndicator;
}
+ (void)showIndicatorAtView:(UIView *)view {
[Util showIndicatorAtView:view withNetworkActivityIndicator:YES];
}
+ (void)stopIndicatorAtView:(UIView *)view {
UIActivityIndicatorView *indicator = [view viewWithTag:15372];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[indicator stopAnimating];
[indicator removeFromSuperview];
}
+(UILabel *)copyLabelFrom:(UILabel *) label withText:(NSString *)text tag:(int)tag {
UILabel *result = [[UILabel alloc] initWithFrame:[label frame]];
[result setBackgroundColor:[label backgroundColor]];
[result setNumberOfLines:[label numberOfLines]];
[result setFont:[label font]];
[result setTextAlignment:[label textAlignment]];
[result setTag:[label tag]];
[result setAdjustsFontSizeToFitWidth:[label adjustsFontSizeToFitWidth]];
[result setTextColor:[label textColor]];
[result setText:text];
[result setTag:tag];
return result;
}
+(UILabel *)createLabelWithFrame:(CGRect)frame fontSize:(int)size weight:(float)weight text:(NSString *)text textColor:(UIColor *)textColor tag:(int)tag
{
UILabel *result = [[UILabel alloc] initWithFrame:frame];
[result setBackgroundColor:[UIColor clearColor]];
[result setNumberOfLines:0];
[result setFont:[UIFont systemFontOfSize:size weight:weight]];
[result setTextAlignment:NSTextAlignmentLeft];
[result setTag:tag];
[result setAdjustsFontSizeToFitWidth:NO];
[result setTextColor:textColor];
[result setText:text];
return result;
}
+ (UIButton *)createButtonWithFrame:(CGRect)frame fontSize:(int)size weight:(float)weight text:(NSString *)text backgroundColor:(UIColor *)backgroundColor tag:(int)tag {
// UIButton *result = [[UIButton alloc] initWithFrame:frame];
UIButton *result = [UIButton buttonWithType:UIButtonTypeSystem];
[result setFrame:frame];
[result setTitle:text forState:UIControlStateNormal];
[[result titleLabel] setFont:[UIFont systemFontOfSize:size weight:weight]];
// [result setTitleColor:[Global colorButtonFaceNormal] forState:UIControlStateNormal];
// [result setTitleColor:[Global colorButtonFaceHighlight] forState:UIControlStateHighlighted];
// [result setTitleColor:[Global colorButtonFaceDisabled] forState:UIControlStateDisabled];
[result setBackgroundColor:backgroundColor];
[result setTag:tag];
return result;
}
+ (UIButton *)createButtonWithFrame:(CGRect)frame fontSize:(int)size weight:(float)weight text:(NSString *)text tag:(int)tag {
return [self createButtonWithFrame:frame fontSize:size weight:weight text:text backgroundColor:[UIColor clearColor] tag:tag];
}
+(NSDateComponents *) stringToDateComponents:(NSString *)dateStr withFormat:(NSString *)dateTimeFormat
{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:dateTimeFormat];//@"yyyy-MM-dd HH:mm:ss";
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to date component object
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
return components;
}
+(NSDateComponents *) stringToDateComponents:(NSString *)dateStr {
return [self stringToDateComponents:dateStr withFormat:@"yyyy-MM-dd HH:mm:ss"];
}
+ (NSString *) monthName:(NSInteger)monthIndex {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *monthName = [[df monthSymbols] objectAtIndex:monthIndex];
return monthName;
}
+(float)scaledHeightFromWidth:(float)oldWidth height:(float)oldHeight targetWidth:(float)newWidth
{
if (oldWidth == 0) return 155;
float scaleFactor = newWidth / oldWidth;
float newHeight = oldHeight * scaleFactor;
return newHeight;
}
+(float)textHeightFromWidth:(float)swidth text:(NSString *)text size:(NSUInteger)textSize
{
UILabel *desc = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, swidth, 150)];
[desc setFont:[UIFont systemFontOfSize:textSize]];
[desc setNumberOfLines:0];
[desc setText:text];
[desc sizeToFit];
return desc.bounds.size.height;
}
+(NSUInteger)countRowsFromString:(NSString *)string {
if (string == nil) return 0;
NSUInteger numberOfLines, index, stringLength = [string length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
index = NSMaxRange([string lineRangeForRange:NSMakeRange(index, 0)]);
return numberOfLines;
}
+ (NSString *) URLRequestForAction:(URLRequestAction)requestAction {
switch (requestAction) {
case ACTION_POST_USER_REGISTRATION:
return [NSString stringWithFormat:@"%@/%@/user/reg", [Global baseURL], [Global tokenAccess]];
case ACTION_POST_USER_LOGIN:
return [NSString stringWithFormat:@"%@/%@/user/login", [Global baseURL], [Global tokenAccess]];
default:
break;
}
return @"";
}
+ (NSString *) URLRequestForAction:(URLRequestAction)requestAction userID:(NSInteger)userID page:(long)page {
switch (requestAction) {
default:
break;
}
return @"";
}
+ (NSString *) URLRequestForAction:(URLRequestAction)requestAction userID:(NSInteger)userID {
switch (requestAction) {
case ACTION_GET_USER_DETAIL:
return [NSString stringWithFormat:@"%@/%@/user/%ld", [Global baseURL], [Global tokenAccess], (long) userID];
break;
default:
break;
}
return [self URLRequestForAction:requestAction userID:userID page:0];
}
+ (NSString *) URLRequestForAction:(URLRequestAction)requestAction userID:(NSInteger)userID requesterID:(NSInteger)requesterID {
switch (requestAction) {
default:
break;
}
return @"";
}
+ (NSString *) URLRequestForAction:(URLRequestAction)requestAction pictureFilename:(NSString *)pictureFilename {
if (pictureFilename == nil) return @"";
NSString *thumb = @"";
if (requestAction == ACTION_GET_PROFILEPICTURE_THUMB && ![pictureFilename isEqualToString:@"default.jpg"]) thumb = @"thumb_";
return [NSString stringWithFormat:@"%@/%@%@", [Global ppbaseURL], thumb, pictureFilename];
}
+ (BOOL) isNull:(id)object {
if (object == nil || object == NULL || object == [NSNull null]) return YES; else return NO;
}
+(NSString *) getSafeString: (NSString *)nullableString {
if ([self isNull:nullableString]) {
return @"";
}
else {
return nullableString;
}
}
+(NSString *) getSafeString: (NSString *)nullableString withFallbackString:(NSString *)fallbackString {
if ([self isNull:nullableString] || [nullableString isEqualToString:@""]) {
return fallbackString;
}
else {
return nullableString;
}
}
+ (UITableViewCell *) emptyCellWithBackgroundColor:(UIColor*)backgroundColor {
UITableViewCell *cell = [[UITableViewCell alloc] init];
[cell setBackgroundColor:backgroundColor];
return cell;
}
+ (void) globalResignFirstResponderRec:(UIView*) view {
if ([view respondsToSelector:@selector(resignFirstResponder)]){
[view resignFirstResponder];
}
for (UIView * subview in [view subviews]){
[self globalResignFirstResponderRec:subview];
}
}
+(OSTextField *)copyTextFieldFrom:(OSTextField *) textField withText:(NSString *)text placeholder:(NSString *)placeholder tag:(int)tag{
OSTextField *result = [[OSTextField alloc] initWithFrame:[textField frame]];
[result setBackgroundColor:[textField backgroundColor]];
[result setBorderStyle:[textField borderStyle]];
[result setFont:[textField font]];
[result setTextAlignment:[textField textAlignment]];
[result setAdjustsFontSizeToFitWidth:[textField adjustsFontSizeToFitWidth]];
[result setTextColor:[textField textColor]];
[result setTag:tag];
result.returnKeyType = textField.returnKeyType;
result.edgeInsets = textField.edgeInsets;
if (text != nil)
[result setText:text];
if (placeholder != nil)
[result setPlaceholder:placeholder];
return result;
}
+(OSTextField *)createTextFieldWithFrame:(CGRect)frame inset:(UIEdgeInsets)inset fontSize:(int)size weight:(float)weight text:(NSString *)text placeholder:(NSString *)placeholder textColor:(UIColor *)textColor border:(UITextBorderStyle)borderStyle returnKeyType:(UIReturnKeyType)returnKeyType tag:(int)tag
{
OSTextField *result = [[OSTextField alloc] initWithFrame:frame];
[result setBackgroundColor:[UIColor clearColor]];
[result setBorderStyle:borderStyle];
[result setFont:[UIFont systemFontOfSize:size weight:weight]];
[result setTextAlignment:NSTextAlignmentLeft];
[result setTag:tag];
[result setAdjustsFontSizeToFitWidth:NO];
[result setTextColor:textColor];
[result setText:text];
[result setPlaceholder:placeholder];
result.returnKeyType = returnKeyType;
return result;
}
+ (UIViewController*) getViewControllerID:(NSString *)vcname {
return [self getViewControllerID:vcname fromStoryboard:@"Main"];
}
+ (UIViewController*) getViewControllerID:(NSString *)vcname fromStoryboard:(NSString *)storyboardName {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
return [storyboard instantiateViewControllerWithIdentifier:vcname];
}
//+ (AppDelegate *) aDelegate {
// return (AppDelegate*) [[UIApplication sharedApplication] delegate];
//}
+ (void) phoneCall:(NSString *)number {
NSString *cleanedString = [[number componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
// NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", cleanedString];
NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", cleanedString];
// NSString *phoneNumber = [@"tel://" stringByAppendingString:number];
NSLog (@"%@", phoneURLString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneURLString]];
}
+ (void) delay:(double)delay closure:(completionBlock)closure {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
closure();
});
}
@end