Skip to content

Commit

Permalink
Make work with Mavericks
Browse files Browse the repository at this point in the history
  • Loading branch information
aussiegeek committed Dec 6, 2014
1 parent 1f1d79a commit 081a206
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 161 deletions.
1 change: 0 additions & 1 deletion External/asi-http-request
Submodule asi-http-request deleted from 1a7ae8
118 changes: 17 additions & 101 deletions FlickrSavr.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0460"
LastUpgradeVersion = "0610"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -39,6 +39,15 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E7C4FB9113FF3B190003A727"
BuildableName = "FlickrSavr.saver"
BlueprintName = "FlickrSavr"
ReferencedContainer = "container:FlickrSavr.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0460"
LastUpgradeVersion = "0610"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
37 changes: 12 additions & 25 deletions FlickrSavr/AFFlickrManager.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#import "AFFlickrManager.h"
#import "AFFlickrPhoto.h"
#include <stdlib.h>
#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h"

#define API_KEY @"4a66fc1a7ab88035aabdeff1f230a971";
#define API_SECRET @"11e7a25980061c0b";

@interface AFFlickrManager ()
@property (strong) NSMutableArray *photos;
Expand All @@ -15,13 +12,6 @@ - (void)parseJson;
@implementation AFFlickrManager
AF_SYNTHESIZE(photos);

- (void)dealloc
{
AF_RELEASE(photos);

[super dealloc];
}

- (id)init
{
self = [super init];
Expand All @@ -35,29 +25,26 @@ - (id)init

- (void)parseJson
{
NSString *interestingURLstring = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=4a66fc1a7ab88035aabdeff1f230a971&format=json&nojsoncallback=1&extras=owner_name,icon_server&per_page=100"];
NSString *interestingURLstring = [NSString stringWithFormat:@"https://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=4a66fc1a7ab88035aabdeff1f230a971&format=json&nojsoncallback=1&extras=owner_name,icon_server&per_page=100"];
NSURL *interestingURL = [NSURL URLWithString:interestingURLstring];
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];
[cache setStoragePath:NSTemporaryDirectory()];
[cache setShouldRespectCacheControlHeaders:NO];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:interestingURL] autorelease];
[request setDownloadCache:cache];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setSecondsToCache:60*60*24];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCompletionBlock:^{
NSData *interestingJson = [request responseData];
NSDictionary *json= [NSJSONSerialization JSONObjectWithData:interestingJson options:0 error:nil];


NSURLRequest *urlRequest = [NSURLRequest requestWithURL:interestingURL];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"connection error: %@", connectionError);
NSLog(@"json: %@", [NSString stringWithUTF8String:[data bytes]]);
NSDictionary *json= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *jsonPhotos = [json objectForKey:@"photos"];
NSArray *jsonPhoto = [jsonPhotos objectForKey:@"photo"];
[jsonPhoto enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
AFFlickrPhoto *flickrPhoto = [[[AFFlickrPhoto alloc] initWithDictionary:obj] autorelease];
AFFlickrPhoto *flickrPhoto = [[AFFlickrPhoto alloc] initWithDictionary:obj];
[flickrPhoto downloadPhotoWithCompletionBlock:^{
[self.photos addObject:flickrPhoto];
[self.photos addObject:flickrPhoto];
}];
}];

}];
[request startAsynchronous];
}

- (AFFlickrPhoto *)randomPhoto
Expand Down
3 changes: 1 addition & 2 deletions FlickrSavr/AFFlickrPhoto.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"

@interface AFFlickrPhoto : NSObject
- (id)initWithDictionary:(NSDictionary *)dict;
- (NSURL *)url;
- (NSString *)title;
- (void)downloadPhotoWithCompletionBlock:(ASIBasicBlock)completionBlock;
- (void)downloadPhotoWithCompletionBlock:(void(^)())block;
- (NSString *)photoPath;
- (NSString *)ownerName;
- (NSURL *)buddyIconURL;
Expand Down
50 changes: 26 additions & 24 deletions FlickrSavr/AFFlickrPhoto.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import "AFFlickrPhoto.h"
#import "ASIHTTPRequest.h"

@interface AFFlickrPhoto ()
@property (strong) NSMutableDictionary *photoAttributes;
Expand All @@ -8,13 +7,6 @@ @interface AFFlickrPhoto ()
@implementation AFFlickrPhoto
AF_SYNTHESIZE(photoAttributes);

- (void)dealloc
{
AF_RELEASE(photoAttributes);

[super dealloc];
}

- (id)initWithDictionary:(NSDictionary *)dict
{
if((self = [super init])) {
Expand All @@ -26,7 +18,7 @@ - (id)initWithDictionary:(NSDictionary *)dict

- (NSURL *)url
{
NSString *urlString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_b.jpg",
NSString *urlString = [NSString stringWithFormat:@"https://farm%@.static.flickr.com/%@/%@_%@_b.jpg",
[self.photoAttributes objectForKey:@"farm"],
[self.photoAttributes objectForKey:@"server"],
[self.photoAttributes objectForKey:@"id"],
Expand All @@ -41,36 +33,46 @@ - (NSString *)title

- (NSString *)photoPath
{
return [NSString stringWithFormat:@"/%@_%@_b.jpg",
return [NSString stringWithFormat:@"%@/%@_%@_b.jpg",
NSTemporaryDirectory(),
[self.photoAttributes objectForKey:@"id"],
[self.photoAttributes objectForKey:@"secret"]];
}

- (void)downloadPhotoWithCompletionBlock:(ASIBasicBlock)completionBlock
- (void)downloadPhotoWithCompletionBlock:(void(^)())completionBlock
{
NSString *fileName = [self photoPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:fileName]) {
// file already exists, so just run completion block
completionBlock();
} else {
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[self url]];
[request setDownloadDestinationPath:fileName];
[request setCompletionBlock:^{
if(![request error]) {
completionBlock();
}
}];
[request startAsynchronous];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[self url]] returningResponse:&response error:&error];
if(error) {
NSLog(@"Error fetching photo: %@", error);
} else {
NSLog(@"Saving image to %@", fileName);
[responseData writeToFile:fileName atomically:YES];
completionBlock();
}
};


NSString *iconFileName = [self buddyIconPath];
if(![fileManager fileExistsAtPath:iconFileName]) {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[self buddyIconURL]];
[request setDownloadDestinationPath:iconFileName];
[request startAsynchronous];
NSError *error = nil;
NSURLResponse *response = nil;
NSLog(@"Fetching buddy icon %@", [self buddyIconURL]);
NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[self buddyIconURL]] returningResponse:&response error:&error];
if(error){
NSLog(@"Error fetching buddy icon: %@", error);
} else {
NSLog(@"Saving buddy icon to %@", [self buddyIconPath]);
[responseData writeToFile:[self buddyIconPath] atomically:YES];
}

}
}

Expand All @@ -85,7 +87,7 @@ - (NSURL *)buddyIconURL
return nil;
}

NSString *iconURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/buddyicons/%@.jpg",
NSString *iconURLString = [NSString stringWithFormat:@"https://farm%@.static.flickr.com/%@/buddyicons/%@.jpg",
[self.photoAttributes objectForKey:@"iconfarm"],
[self.photoAttributes objectForKey:@"iconserver"],
[self.photoAttributes objectForKey:@"owner"]
Expand All @@ -95,7 +97,7 @@ - (NSURL *)buddyIconURL

- (NSString *)buddyIconPath
{
return [NSString stringWithFormat:@"%@/%@.jpg",
return [NSString stringWithFormat:@"%@/%@.jpg",
NSTemporaryDirectory(),
[self.photoAttributes objectForKey:@"owner"]];
}
Expand Down
6 changes: 0 additions & 6 deletions FlickrSavr/FlickrSavrView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ @implementation FlickrSavrView
AF_SYNTHESIZE(flickrManager);
AF_SYNTHESIZE(isPreview);

- (void)dealloc
{
AF_RELEASE(flickrManager);

[super dealloc];
}
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
Expand Down

0 comments on commit 081a206

Please sign in to comment.