Permalink
Browse files

Memory leaks fixed, using background of the twitter user being displa…

…yed, new icon
  • Loading branch information...
1 parent a354675 commit 93080f4af8e7c0c43896dfe863eb67232c40b095 xdamman committed Aug 30, 2010
@@ -8,16 +8,13 @@
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
+#import "Loader.h"
-@interface ImageLoader : NSObject {
+@interface ImageLoader : Loader {
}
+ (UIImage *)loadImageFromURL: (NSURL *)url;
-+ (BOOL)fileExists: (NSString *) filePath;
-
-+ (NSString *) md5:(NSString *)str;
-
+ (void)saveImage:(UIImage *)image withName:(NSString *)name;
@end
View
@@ -17,34 +17,18 @@ + (UIImage *)loadImageFromURL: (NSURL *)url {
NSString* filePath = [documentsPath stringByAppendingPathComponent:fileName];
if ([self fileExists:filePath]) {
- //NSLog(@"File %@ exists",filePath);
+ NSLog(@"Returning %@ from cache",url);
return [UIImage imageWithContentsOfFile:filePath];
}
- NSLog(@"File %@ does not exist, fetching %@",filePath,url);
+ //NSLog(@"File %@ does not exist, fetching %@",filePath,url);
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[ImageLoader saveImage:image withName:fileName];
[image autorelease];
return image;
}
-+ (BOOL)fileExists: (NSString *)filePath {
- return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
-}
-
-+ (NSString *) md5:(NSString *)str {
- const char *cStr = [str UTF8String];
- unsigned char result[16];
- CC_MD5( cStr, strlen(cStr), result );
- return [NSString stringWithFormat:
- @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
- result[0], result[1], result[2], result[3],
- result[4], result[5], result[6], result[7],
- result[8], result[9], result[10], result[11],
- result[12], result[13], result[14], result[15]
- ];
-}
+ (void)saveImage:(UIImage *)image withName:(NSString *)name {
NSData *data = UIImageJPEGRepresentation(image, 1.0);
View
@@ -0,0 +1,23 @@
+//
+// JSONLoader.h
+// TweetWall
+//
+// Created by Xavier Damman on 8/23/10.
+// Copyright 2010 Kwenti inc. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "Loader.h"
+#import "CJSONDeserializer.h"
+#import "CJSONSerializer.h"
+
+@interface JSONLoader : Loader {
+
+}
+
++ (NSDictionary *)loadJsonFromURL: (NSURL *)url;
++ (NSDictionary *)loadJsonFromURL: (NSURL *)url withMaxCacheInSeconds:(int)seconds;
++ (void)saveJson:(NSDictionary *)json withName:(NSString *)name;
+
+
+@end
View
@@ -0,0 +1,98 @@
+//
+// JSONLoader.m
+// TweetWall
+//
+// Created by Xavier Damman on 8/23/10.
+// Copyright 2010 Kwenti inc. All rights reserved.
+//
+
+#import "JSONLoader.h"
+
+
+@implementation JSONLoader
+
+
++ (NSDictionary *)loadJsonFromURL: (NSURL *)url {
+ return [JSONLoader loadJsonFromURL:url withMaxCacheInSeconds:0];
+}
+
++ (NSDictionary *)loadJsonFromURL: (NSURL *)url withMaxCacheInSeconds:(int)seconds {
+ [url retain];
+
+ NSString* documentsPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] retain];
+ NSString *fileName = [[NSString stringWithFormat:@"%@.json",[self md5:[NSString stringWithFormat:@"%@",url]]] retain];
+ NSString* filePath = [[documentsPath stringByAppendingPathComponent:fileName] retain];
+
+ if ([self fileExists:filePath]) {
+ //NSLog(@"File %@ exists",filePath);
+ BOOL returnFromCache = FALSE;
+ if (seconds == 0) {
+ returnFromCache = TRUE;
+ }
+ else if (seconds > 0) {
+ NSFileManager *fm = [NSFileManager defaultManager];
+ NSDate *fileMTimeDate = [[fm fileAttributesAtPath:filePath traverseLink:YES] fileModificationDate];
+ if ([[NSDate date] timeIntervalSinceDate:fileMTimeDate] <= seconds) {
+ returnFromCache = TRUE;
+ }
+ }
+
+ if (returnFromCache) {
+ NSLog(@"Return JSON from cache %@",url);
+
+ NSString *jsonString = [[NSString alloc] initWithContentsOfFile:filePath] ;
+ NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
+ NSError *error = nil;
+
+ NSDictionary *json = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
+
+
+ //NSDictionary *json = [NSDictionary dictionaryWithContentsOfFile:filePath];
+ NSLog(@"json from cache %@\n%@",filePath,json);
+
+ [url release];
+ [jsonString release];
+
+ return json;
+ }
+ }
+
+ NSError *error = nil;
+
+ [[NSURLCache sharedURLCache] setMemoryCapacity:0];
+ [[NSURLCache sharedURLCache] setDiskCapacity:0];
+ NSString *rawDataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
+
+ NSString *jsonString = [[NSString stringWithFormat:@"{\"root\":%@}",rawDataString] retain];
+
+ NSLog(@"\nJSONLoader\n\tQuery: %@\n\tError: %@\n\tRESULT:%@",url,error,jsonString);
+
+ NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
+ error = nil;
+
+ NSDictionary *json = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
+
+ [rawDataString release];
+ [documentsPath release];
+ [filePath release];
+ [fileName release];
+ [url release];
+ [JSONLoader saveJson:json withName:fileName];
+
+ [json autorelease];
+ return json;
+}
+
++ (void)saveJson:(NSDictionary *)json withName:(NSString *)name {
+ [json retain];
+ NSData* data=[[[CJSONSerializer serializer] serializeDictionary:json] dataUsingEncoding:NSUTF8StringEncoding];
+
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+ NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
+ NSString *fullPath = [documentsPath stringByAppendingPathComponent:name];
+ [fileManager createFileAtPath:fullPath contents:data attributes:nil];
+
+ [json release];
+}
+
+@end
View
@@ -0,0 +1,20 @@
+//
+// Loader.h
+// TweetWall
+//
+// Created by Xavier Damman on 8/23/10.
+// Copyright 2010 Kwenti inc. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface Loader : NSObject {
+
+}
+
++ (void) clean;
++ (BOOL)fileExists: (NSString *) filePath;
++ (NSString *) md5:(NSString *)str;
+
+@end
View
@@ -0,0 +1,48 @@
+//
+// Loader.m
+// TweetWall
+//
+// Created by Xavier Damman on 8/23/10.
+// Copyright 2010 Kwenti inc. All rights reserved.
+//
+
+#import "Loader.h"
+
+
+@implementation Loader
+
+
++ (void) clean {
+ NSFileManager *fm = [NSFileManager defaultManager];
+ NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
+ NSError *error = nil;
+ int total = 0;
+ for (NSString *file in [fm contentsOfDirectoryAtPath:documentsPath error:&error]) {
+ BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsPath, file] error:&error];
+ total++;
+ if (!success || error) {
+ // it failed.
+ NSLog(@"Couldn't remove file %@ because %@",file,error);
+ }
+ }
+ NSLog(@"[ImageLoader] %i files removed",total);
+}
+
++ (BOOL)fileExists: (NSString *)filePath {
+ return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
+}
+
++ (NSString *) md5:(NSString *)str {
+ const char *cStr = [str UTF8String];
+ unsigned char result[16];
+ CC_MD5( cStr, strlen(cStr), result );
+ return [NSString stringWithFormat:
+ @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
+ result[0], result[1], result[2], result[3],
+ result[4], result[5], result[6], result[7],
+ result[8], result[9], result[10], result[11],
+ result[12], result[13], result[14], result[15]
+ ];
+}
+
+@end
View
@@ -0,0 +1,25 @@
+//
+// StorifyAPI.h
+// TweetWall
+//
+// Created by Xavier Damman on 8/29/10.
+// Copyright 2010 Kwenti inc. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "CJSONDeserializer.h"
+#import "JSONLoader.h"
+#import "Tweet.h"
+
+
+@interface StorifyAPI : NSObject {
+
+
+}
+
+- (NSArray *)getStoryElements:(NSString *)permalink;
+- (NSDictionary *)getUserInfo:(NSString *)screenName;
+- (NSArray *) processElements:(NSDictionary *)elementsData;
+
+
+@end
View
@@ -0,0 +1,83 @@
+//
+// StorifyAPI.m
+// Timelines
+//
+// Created by Xavier Damman on 4/17/10.
+// Copyright 2010 xhead software. All rights reserved.
+//
+
+#import "StorifyAPI.h"
+
+#pragma mark Private Interface
+
+@interface StorifyAPI (Private)
+
+- (NSArray *)processTweets:(NSDictionary *)tweetsData;
+
+@end
+
+#pragma mark -
+#pragma mark Public Interface
+
+@implementation StorifyAPI
+
+- (id)init
+{
+ [super init];
+
+ return self;
+}
+
+
+- (NSArray *)getStoryElements:(NSString *)permalink
+{
+ NSString *queryString = [[NSString stringWithFormat:@"%@.json",permalink] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+
+ NSLog(@"Fetching %@",queryString);
+
+ NSDictionary *storyData = [JSONLoader loadJsonFromURL:[NSURL URLWithString:queryString] withMaxCacheInSeconds:5];
+
+ NSArray *story = [[self processTweets:storyData] retain];
+ return [story autorelease];
+}
+
+- (NSDictionary *)getUserInfo:(NSString *)screenName {
+
+ NSString *queryString = [[NSString stringWithFormat:@"http://api.twitter.com/1/users/show/%@.json",screenName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+
+ NSLog(@"Requesting %@",queryString);
+
+ NSDictionary *userData = [JSONLoader loadJsonFromURL:[NSURL URLWithString:queryString]];
+
+ return [userData objectForKey:@"root"];
+
+}
+
+
+- (NSArray *) processElements:(NSDictionary *)elementsData {
+ [elementsData retain];
+ NSEnumerator *enumerator = [[[elementsData objectForKey:@"root"] objectForKey:@"elements"] objectEnumerator];
+ NSDictionary *t;
+
+ NSLog(@"processing %i elements",[[[elementsData objectForKey:@"root"] objectForKey:@"elements"] count]);
+
+ NSMutableArray *elements = [[NSMutableArray alloc] init];
+
+ while ((t = [enumerator nextObject])) {
+ Tweet *tweet = [[Tweet alloc] initWithDictionary:t];
+ NSLog(@"Story Element: %@",tweet);
+ [elements addObject:tweet];
+ [tweet release];
+ }
+
+ [elementsData release];
+ [elements autorelease];
+
+ return elements;
+}
+
+- (void) dealloc {
+ [super dealloc];
+}
+
+@end
View
@@ -43,7 +43,8 @@ - (id) initWithDictionary:(NSDictionary *)tweetData {
NSArray *matchedURLsArray = [content componentsMatchedByRegex:urlRegex];
//NSLog(@"URLs found in da tweet: %@",matchedURLsArray);
- for (NSString *u in matchedURLsArray) {
+ NSString *u;
+ for (u in matchedURLsArray) {
if ([[u substringToIndex:14] isEqualToString:@"http://twitpic"]) {
NSString *imageUrl = [NSString stringWithFormat:@"http://twitpic.com/show/large/%@",[u substringFromIndex:19]];
image = [[NSURL URLWithString:imageUrl] retain];
@@ -65,18 +66,20 @@ - (id) initWithDictionary:(NSDictionary *)tweetData {
- (void) getUserInfoThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSLog(@"New thread");
+
Twitter *twitter = [[Twitter alloc] init];
- user = [twitter getUserInfo:self.screenName];
- NSLog(@"Fetching user %@: %@",self.screenName,user);
+ user = [[twitter getUserInfo:self.screenName] retain];
+ NSLog(@"Fetching userinfo for %@",self.screenName);
// [self performSelectorOnMainThread:@selector(searchTwitterDidFinish) withObject:nil waitUntilDone:NO];
[twitter release];
[pool release];
}
- (void)dealloc {
[screenName release];
+ [user release];
[avatar release];
+ [timestamp release];
[content release];
[image release];
[super dealloc];
Oops, something went wrong.

0 comments on commit 93080f4

Please sign in to comment.