2011-10-27 09:44:58 -07:00
|
|
|
#import "BaseViewController.h"
|
|
|
|
#import "NewsBlurAppDelegate.h"
|
|
|
|
|
|
|
|
@implementation BaseViewController
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark HTTP requests
|
|
|
|
|
|
|
|
- (ASIHTTPRequest*) requestWithURL:(NSString*) s {
|
|
|
|
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:s]];
|
|
|
|
[self addRequest:request];
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (ASIFormDataRequest*) formRequestWithURL:(NSString*) s {
|
|
|
|
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:s]];
|
|
|
|
[self addRequest:request];
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addRequest:(ASIHTTPRequest*)request {
|
|
|
|
[request setDelegate:self];
|
|
|
|
if (!requests) {
|
|
|
|
requests = [[NSMutableArray alloc] initWithCapacity:3];
|
|
|
|
} else {
|
|
|
|
[self clearFinishedRequests];
|
|
|
|
}
|
|
|
|
[requests addObject:request];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) clearFinishedRequests {
|
|
|
|
NSMutableArray* toremove = [[NSMutableArray alloc] initWithCapacity:[requests count]];
|
|
|
|
for (ASIHTTPRequest* r in requests) {
|
|
|
|
if ([r isFinished]) {
|
|
|
|
[toremove addObject:r];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ASIHTTPRequest* r in toremove) {
|
|
|
|
[requests removeObject:r];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelRequests {
|
|
|
|
for (ASIHTTPRequest* r in requests) {
|
|
|
|
r.delegate = nil;
|
|
|
|
[r cancel];
|
|
|
|
}
|
|
|
|
[requests removeAllObjects];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark View methods
|
|
|
|
|
2011-11-30 09:38:31 -08:00
|
|
|
- (void)informError:(id)error {
|
2014-11-17 14:43:36 -08:00
|
|
|
[self informError:error details:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)informError:(id)error details:(NSString *)details {
|
2013-09-05 17:07:21 -07:00
|
|
|
NSLog(@"informError: %@", error);
|
2011-11-30 09:38:31 -08:00
|
|
|
NSString *errorMessage;
|
|
|
|
if ([error isKindOfClass:[NSString class]]) {
|
|
|
|
errorMessage = error;
|
|
|
|
} else {
|
|
|
|
errorMessage = [error localizedDescription];
|
|
|
|
if ([error code] == 4 &&
|
|
|
|
[errorMessage rangeOfString:@"cancelled"].location != NSNotFound) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-10-27 09:44:58 -07:00
|
|
|
|
|
|
|
[MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
|
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
2012-07-15 15:06:06 -07:00
|
|
|
[HUD setCustomView:[[UIImageView alloc]
|
|
|
|
initWithImage:[UIImage imageNamed:@"warning.gif"]]];
|
2011-10-27 09:44:58 -07:00
|
|
|
[HUD setMode:MBProgressHUDModeCustomView];
|
2014-11-17 14:43:36 -08:00
|
|
|
if (details) {
|
|
|
|
[HUD setDetailsLabelText:details];
|
|
|
|
}
|
2011-11-30 09:38:31 -08:00
|
|
|
HUD.labelText = errorMessage;
|
2014-11-17 14:43:36 -08:00
|
|
|
[HUD hide:YES afterDelay:(details ? 3 : 1)];
|
2011-11-30 09:38:31 -08:00
|
|
|
|
2011-10-27 09:44:58 -07:00
|
|
|
// UIAlertView* alertView = [[UIAlertView alloc]
|
|
|
|
// initWithTitle:@"Error"
|
|
|
|
// message:localizedDescription delegate:nil
|
|
|
|
// cancelButtonTitle:@"OK"
|
|
|
|
// otherButtonTitles:nil];
|
|
|
|
// [alertView show];
|
|
|
|
// [alertView release];
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:07:19 -07:00
|
|
|
- (void)informMessage:(NSString *)message {
|
2012-10-17 15:07:53 -07:00
|
|
|
[MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
|
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
|
|
|
HUD.mode = MBProgressHUDModeText;
|
|
|
|
HUD.labelText = message;
|
|
|
|
[HUD hide:YES afterDelay:.75];
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:07:19 -07:00
|
|
|
- (void)informLoadingMessage:(NSString *)message {
|
|
|
|
[MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
|
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
|
|
|
HUD.labelText = message;
|
|
|
|
[HUD hide:YES afterDelay:2];
|
|
|
|
}
|
|
|
|
|
2011-10-27 09:44:58 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark UIViewController
|
|
|
|
|
|
|
|
- (void) viewDidLoad {
|
|
|
|
[super viewDidLoad];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) viewDidUnload {
|
|
|
|
[super viewDidUnload];
|
|
|
|
}
|
|
|
|
|
2015-09-23 13:02:25 -07:00
|
|
|
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
|
|
|
|
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
|
|
|
|
|
|
|
|
if ([self presentedViewController]) {
|
|
|
|
[[self presentedViewController] viewWillTransitionToSize:size
|
|
|
|
withTransitionCoordinator:coordinator];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 09:44:58 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Memory management
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[self cancelRequests];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|