From b2aed3d9588e89f6ad87ccbce6027d5da122b369 Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Mon, 4 Apr 2016 09:57:20 +0200 Subject: [PATCH] dropbox errors now display error summary --- FMC Planner 2/DropboxV2ObjC.m | 129 ++++++++++++++++++++++++---- FMC Planner 2/SharedDeclerations.m | 3 +- FMC Planner 2/TableViewController.m | 4 +- 3 files changed, 114 insertions(+), 22 deletions(-) diff --git a/FMC Planner 2/DropboxV2ObjC.m b/FMC Planner 2/DropboxV2ObjC.m index 3b3bc3c..8d2bebd 100644 --- a/FMC Planner 2/DropboxV2ObjC.m +++ b/FMC Planner 2/DropboxV2ObjC.m @@ -102,16 +102,22 @@ options:0 error:nil] isEqualToDictionary:@{}]) { - [SharedDeclerations - presentErrorAlert: - [NSError errorWithDomain:DropboxErrorDomain - code:100 - userInfo:@{ - DropboxErrorUserInfo : - NSLocalizedDescriptionKey - }] + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError + errorWithDomain:DropboxErrorDomain + code:100 + userInfo: + [[NSJSONSerialization + JSONObjectWithData:data + options:0 + error:nil] + valueForKey: + DropboxErrorUserInfo]] - presenter:presenter]; + presenter:presenter]; + }]; } }]; @@ -122,7 +128,6 @@ * Pass nil as path for root dir * @param completion executed after completion */ -// TODO: Alert on Dropbox error - (void)contentsOfPath:(NSString *)path completion:(void (^)(NSArray *data))handler presenter:(UIViewController *)presenter @@ -160,7 +165,7 @@ [NSURLConnection sendAsynchronousRequest:request - queue:[NSOperationQueue mainQueue] + queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (data != nil) { @@ -168,7 +173,31 @@ options:0 error:nil]; if ([data isKindOfClass:[NSDictionary class]]) { - handler([data valueForKey:@"entries"]); + NSArray *data2 = [data valueForKey:@"entries"]; + if (data2 == nil) { + [[NSOperationQueue + mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError + errorWithDomain:DropboxErrorDomain + code:100 + userInfo: + [[NSJSONSerialization + JSONObjectWithData: + data + options: + 0 + error: + nil] + valueForKey: + DropboxErrorUserInfo]] + presenter:presenter]; + }]; + } + else { + handler(data2); + } } } else if (error != nil) { @@ -182,7 +211,6 @@ }]; } -// TODO: Alert on Dropbox error - (void)downloadFromDropbox:(NSArray *)files presenter:(UIViewController *)presenter { @@ -221,9 +249,35 @@ completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (error == nil) { - [data writeToFile:[SharedDeclerations - savePathForFile:string] - atomically:YES]; + if ([[NSJSONSerialization JSONObjectWithData:data + options:0 + error:nil] + valueForKey:@"error_summary"] == nil) { + [data writeToFile:[SharedDeclerations + savePathForFile:string] + atomically:YES]; + } + else { + [[NSOperationQueue + mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError + errorWithDomain:DropboxErrorDomain + code:100 + userInfo: + [[NSJSONSerialization + JSONObjectWithData: + data + options: + 0 + error: + nil] + valueForKey: + DropboxErrorUserInfo]] + presenter:presenter]; + }]; + } } else if (error != nil) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ @@ -238,7 +292,6 @@ } } -// TODO: Alert on Dropbox error - (void)uploadToDropbox:(NSArray *)files presenter:(UIViewController *)presenter { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; @@ -287,6 +340,26 @@ }]; } + if ([[NSJSONSerialization JSONObjectWithData:data + options:0 + error:nil] + valueForKey:@"error_summary"] != nil) { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError + errorWithDomain:DropboxErrorDomain + code:100 + userInfo: + [[NSJSONSerialization + JSONObjectWithData:data + options:0 + error:nil] + valueForKey: + DropboxErrorUserInfo]] + presenter:presenter]; + }]; + } }]; } } @@ -294,7 +367,6 @@ #pragma mark - Other operations // !!!: Cannot be asynchronous -// TODO: Alert on Dropbox error - (NSDictionary *)getUserInfoWithPresenter:(UIViewController *)presenter { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; @@ -319,7 +391,26 @@ returningResponse:&response error:&error2]]; if ([data isKindOfClass:[NSDictionary class]]) { - return data; + if ([[NSJSONSerialization JSONObjectWithData:data options:0 error:nil] + valueForKey:@"error_summary"] == nil) { + return data; + } + else { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError errorWithDomain:DropboxErrorDomain + code:100 + userInfo:[[NSJSONSerialization + JSONObjectWithData:data + options:0 + error:nil] + valueForKey: + DropboxErrorUserInfo]] + presenter:presenter]; + }]; + return nil; + } } else if (error != nil) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ diff --git a/FMC Planner 2/SharedDeclerations.m b/FMC Planner 2/SharedDeclerations.m index bc16621..74e8163 100644 --- a/FMC Planner 2/SharedDeclerations.m +++ b/FMC Planner 2/SharedDeclerations.m @@ -13,8 +13,7 @@ NSString *const FMCNEXTRTEPAGE = @"NEXTRTEPAGE"; NSString *const FMCFIRSTRTEPAGE = @"FIRSTRTEPAGE"; NSString *const DropboxErrorDomain = @"com.weebly.alikja.FMC"; -NSString *const DropboxErrorUserInfo = - @"Dropbox API Error. Please try again later!"; +NSString *const DropboxErrorUserInfo = @"error_summary"; #pragma mark - Adaptive font size method diff --git a/FMC Planner 2/TableViewController.m b/FMC Planner 2/TableViewController.m index 9d0386c..553dc6e 100644 --- a/FMC Planner 2/TableViewController.m +++ b/FMC Planner 2/TableViewController.m @@ -55,7 +55,9 @@ DropboxV2ObjC *dbClient = nil; [NSPredicate predicateWithFormat:@"NOT (SELF BEGINSWITH %@)", @"."]] mutableCopy]; - [self.tableView reloadData]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [self.tableView reloadData]; + }]; } presenter:self]; }