Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Use of AFNetworking in Nimbus #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions WhiteHouseApp/WHPhotoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// WhiteHouseApp
//
//

#import "AFImageRequestOperation.h"
#import "WHPhotoViewController.h"

#import "WHCaptionedPhotoView.h"
Expand Down Expand Up @@ -105,33 +105,27 @@ - (void)loadPhotoAtIndex:(NSInteger)photoIndex
{
WHFeedItem *item = [self.feedItems objectAtIndex:photoIndex];
WHMediaElement *mediaContent = [item bestContentForWidth:[UIScreen mainScreen].bounds.size.width];
NINetworkRequestOperation *loadOp = [[NINetworkRequestOperation alloc] initWithURL:mediaContent.URL];
loadOp.tag = photoIndex;
[loadOp setDidFinishBlock:^(NIOperation *operation) {
UIImage* image = [UIImage imageWithData:((NINetworkRequestOperation *) operation).data];
[self.imageCache storeObject:image withName:[self cacheKeyForPhotoAtIndex:photoIndex]];
[self.photoAlbumView didLoadPhoto:image atIndex:photoIndex photoSize:NIPhotoScrollViewPhotoSizeOriginal];
}];

[self.queue addOperation:loadOp];
NSURLRequest *request = [NSURLRequest requestWithURL:mediaContent.URL];
AFImageRequestOperation *operation = [AFImageRequestOperation
imageRequestOperationWithRequest:request success:^(UIImage *image) {
[self.imageCache storeObject:image withName:[self cacheKeyForPhotoAtIndex:photoIndex]];
[self.photoAlbumView didLoadPhoto:image atIndex:photoIndex photoSize:NIPhotoScrollViewPhotoSizeOriginal];
}];
[operation start];
}


- (void)loadThumbnailAtIndex:(NSInteger)photoIndex
{
WHFeedItem *item = [self.feedItems objectAtIndex:photoIndex];

// get the smallest thumbnail available
WHMediaElement *mediaContent = [item bestThumbnailForWidth:0];
NINetworkRequestOperation *loadOp = [[NINetworkRequestOperation alloc] initWithURL:mediaContent.URL];
loadOp.tag = photoIndex;
[loadOp setDidFinishBlock:^(NIOperation *operation) {
UIImage* image = [UIImage imageWithData:((NINetworkRequestOperation *) operation).data];
[self.imageCache storeObject:image withName:[self cacheKeyForPhotoAtIndex:photoIndex]];
[self.photoScrubberView didLoadThumbnail:image atIndex:photoIndex];
NSURLRequest *request = [NSURLRequest requestWithURL:mediaContent.URL];
AFImageRequestOperation *operation = [AFImageRequestOperation
imageRequestOperationWithRequest:request success:^(UIImage *image) {
[self.imageCache storeObject:image withName:[self cacheKeyForPhotoAtIndex:photoIndex]];
[self.photoScrubberView didLoadThumbnail:image atIndex:photoIndex];
}];

[self.queue addOperation:loadOp];
[operation start];
}


Expand Down
23 changes: 9 additions & 14 deletions WhiteHouseApp/WHSearchController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
//
//

#import "AFJSONRequestOperation.h"
#import "WHSearchController.h"

@interface WHSearchController ()
Expand Down Expand Up @@ -75,20 +76,16 @@ + (NSString *)escapeQueryParameter:(NSString *)param
- (void)fetchResults
{
self.page = self.page + 1;

NSString *formatString = AppConfig(@"SearchURLFormat");
NSString *escaped = [[self class] escapeQueryParameter:query];
NSURL *searchURL = [NSURL URLWithString:[NSString stringWithFormat:formatString, escaped, self.page]];

DebugLog(@"Search URL = %@", searchURL);
NINetworkRequestOperation *op = [[NINetworkRequestOperation alloc] initWithURL:searchURL];

NSURLRequest *request = [NSURLRequest requestWithURL:searchURL];
// this block will be called with the operation itself
op.didFinishBlock = ^(id obj) {
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// cast it to access network-op-specific properties
id result = [NSJSONSerialization JSONObjectWithData:op.data options:0 error:nil];
DebugLog(@"API result = %@", result);
id results = [result objectForKey:@"results"];
id results = [JSON objectForKey:@"results"];
DebugLog(@"API result = %@", results);
if (results && [results respondsToSelector:@selector(objectAtIndex:)]) {
if (self.results) {
self.results = [self.results arrayByAddingObjectsFromArray:results];
Expand All @@ -97,14 +94,12 @@ - (void)fetchResults
}
[self.delegate searchControllerDidFindResults:self];
}
};

op.didFailWithErrorBlock = ^(id obj, NSError *error) {

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,NSError *error,id JSON) {
[self reportError:error];
};

}];
// the operation will start when added to the queue
[self.queue addOperation:op];
[operation start];
}


Expand Down