From 3d6fcc35c9cbb9a4275f3c8b72560a32423b2ffc Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sat, 11 Aug 2012 11:16:55 -0400 Subject: [PATCH] Moving error alert logic from models to controllers --- .../project.pbxproj | 2 +- Example/AppDelegate.m | 6 +++++- .../Controllers/PublicTimelineViewController.m | 6 ++++-- Example/Classes/Models/Tweet.h | 2 +- Example/Classes/Models/Tweet.m | 13 ++++--------- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Example/AFNetworking iOS Example.xcodeproj/project.pbxproj b/Example/AFNetworking iOS Example.xcodeproj/project.pbxproj index 7abdbab..96e4c23 100644 --- a/Example/AFNetworking iOS Example.xcodeproj/project.pbxproj +++ b/Example/AFNetworking iOS Example.xcodeproj/project.pbxproj @@ -189,8 +189,8 @@ children = ( F8DA09E31396AC040057D0CC /* main.m */, F8129C3815910830009BFE23 /* Prefix.pch */, - F8129C7215910C37009BFE23 /* AppDelegate.m */, F8129C7315910C37009BFE23 /* AppDelegate.h */, + F8129C7215910C37009BFE23 /* AppDelegate.m */, F8E4696C1395739D00DB05C8 /* iOS-Info.plist */, ); name = "Supporting Files"; diff --git a/Example/AppDelegate.m b/Example/AppDelegate.m index 2e27051..fc2ee78 100644 --- a/Example/AppDelegate.m +++ b/Example/AppDelegate.m @@ -71,7 +71,11 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions [self.window makeKeyAndOrderFront:self]; - [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets) { + [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets, NSError *error) { + if (error) { + [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", nil) defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@",[error localizedDescription]] runModal]; + } + self.tweetsArrayController.content = tweets; }]; diff --git a/Example/Classes/Controllers/PublicTimelineViewController.m b/Example/Classes/Controllers/PublicTimelineViewController.m index 966b607..aad2d63 100644 --- a/Example/Classes/Controllers/PublicTimelineViewController.m +++ b/Example/Classes/Controllers/PublicTimelineViewController.m @@ -41,8 +41,10 @@ [_activityIndicatorView startAnimating]; self.navigationItem.rightBarButtonItem.enabled = NO; - [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets) { - if (tweets) { + [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets, NSError *error) { + if (error) { + [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK", nil), nil] show]; + } else { _tweets = tweets; [self.tableView reloadData]; } diff --git a/Example/Classes/Models/Tweet.h b/Example/Classes/Models/Tweet.h index a08fd3c..20d9054 100644 --- a/Example/Classes/Models/Tweet.h +++ b/Example/Classes/Models/Tweet.h @@ -33,6 +33,6 @@ - (id)initWithAttributes:(NSDictionary *)attributes; -+ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets))block; ++ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets, NSError *error))block; @end diff --git a/Example/Classes/Models/Tweet.m b/Example/Classes/Models/Tweet.m index f9e42f8..d427ae9 100644 --- a/Example/Classes/Models/Tweet.m +++ b/Example/Classes/Models/Tweet.m @@ -52,7 +52,7 @@ #pragma mark - -+ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets))block { ++ (void)publicTimelineTweetsWithBlock:(void (^)(NSArray *tweets, NSError *error))block { [[AFTwitterAPIClient sharedClient] getPath:@"statuses/public_timeline.json" parameters:[NSDictionary dictionaryWithObject:@"false" forKey:@"include_entities"] success:^(AFHTTPRequestOperation *operation, id JSON) { NSMutableArray *mutableTweets = [NSMutableArray arrayWithCapacity:[JSON count]]; for (NSDictionary *attributes in JSON) { @@ -61,16 +61,11 @@ } if (block) { - block([NSArray arrayWithArray:mutableTweets]); + block([NSArray arrayWithArray:mutableTweets], nil); } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { -#if __IPHONE_OS_VERSION_MIN_REQUIRED - [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK", nil), nil] show]; -#else - [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", nil) defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@",[error localizedDescription]] runModal]; -#endif + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (block) { - block(nil); + block([NSArray array], error); } }]; }