-
-
Save tjweir/142761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Events.h | |
// TWI | |
// | |
// Created by Shaun Harrison on 3/18/09. | |
// Copyright enormego 2009. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
enum EventType { | |
// Other Events | |
EventImageLoaded, | |
EventSmallImageLoaded, | |
EventThumbnailImageLoaded, | |
}; typedef int EventType; | |
@interface Events : NSObject { | |
} | |
+ (void)addObserver:(id)observer selector:(SEL)selector event:(EventType)type; | |
+ (void)addObserver:(id)observer selector:(SEL)selector object:(id)object event:(EventType)type; | |
+ (void)removeObserver:(id)observer event:(EventType)type; | |
+ (void)postEvent:(EventType)type; | |
+ (void)postEvent:(EventType)type object:(id)object; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Events.m | |
// TWI | |
// | |
// Created by Shaun Harrison on 3/18/09. | |
// Copyright enormego 2009. All rights reserved. | |
// | |
#import "Events.h" | |
#define eventNameFromType(type) [NSString stringWithFormat:@"kTWINotification-%d",(type)] | |
@implementation Events | |
+ (void)addObserver:(id)observer selector:(SEL)selector event:(EventType)type { | |
[Events addObserver:observer selector:selector object:nil event:type]; | |
} | |
+ (void)addObserver:(id)observer selector:(SEL)selector object:(id)object event:(EventType)type { | |
[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:eventNameFromType(type) object:nil]; | |
} | |
+ (void)removeObserver:(id)observer event:(EventType)type { | |
[[NSNotificationCenter defaultCenter] removeObserver:observer name:eventNameFromType(type) object:nil]; | |
} | |
+ (void)postEvent:(EventType)type { | |
[[self class] postEvent:type object:nil]; | |
} | |
+ (void)postEvent:(EventType)type object:(id)object { | |
[object retain]; | |
if(![NSThread isMainThread]) { | |
[self performSelectorOnMainThread:@selector(postEventOnMainThread:) withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:type],object,nil] waitUntilDone:YES]; | |
} else { | |
[[NSNotificationCenter defaultCenter] postNotificationName:eventNameFromType(type) object:object]; | |
} | |
[object release]; | |
} | |
+ (void)postEventOnMainThread:(NSArray*)info { | |
[[NSNotificationCenter defaultCenter] postNotificationName:eventNameFromType([[info objectAtIndex:0] intValue]) object:info.count > 1 ? [info objectAtIndex:1] : nil]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage*)thumbnailImage { | |
if(!thumbnailImageURL) return nil; | |
NSData* imageData = [[EGOCache currentCache] dataForKey:[[thumbnailImageURL description] md5]]; | |
if(!imageData) { | |
@synchronized(self) { | |
if(!loadingThumbnailImage) { | |
[self performSelectorInBackground:@selector(loadThumbnailImage) withObject:nil]; | |
loadingThumbnailImage = YES; | |
} | |
} | |
return nil; | |
} else { | |
return [UIImage imageWithData:imageData]; | |
} | |
} | |
- (void)loadThumbnailImage { | |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | |
ASIHTTPRequest* request = [[ASIHTTPRequest alloc] initWithURL:thumbnailImageURL]; | |
[request start]; | |
NSData* imageData = [request responseData]; | |
if(imageData) { | |
[[EGOCache currentCache] setData:imageData forKey:[[thumbnailImageURL description] md5] withTimeoutInterval:604800]; | |
[Events postEvent:EventThumbnailImageLoaded object:self]; | |
} | |
[request release]; | |
[pool release]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)thumbnailLoaded:(NSNotification*)aNotification { | |
if(aNotification.object != post) return; | |
if(post.postType == PostImage) { | |
self.imageView.image = post.thumbnailImage; | |
} else { | |
self.imageView.image = nil; | |
} | |
[self setNeedsLayout]; | |
//[self setNeedsDisplay]; | |
} | |
- (void)setPost:(Post*)aPost { | |
[post release]; | |
post = [aPost retain]; | |
[Events removeObserver:self event:EventThumbnailImageLoaded]; | |
[Events addObserver:self selector:@selector(thumbnailLoaded:) object:post event:EventThumbnailImageLoaded]; | |
self.textLabel.text = [post.text stringByReplacingOccurrencesOfString:@"<br />" withString:@""]; | |
if(post.postType == PostImage) { | |
self.imageView.image = post.thumbnailImage; | |
} else { | |
self.imageView.image = nil; | |
} | |
} | |
- (void)dealloc { | |
[Events removeObserver:self event:EventThumbnailImageLoaded]; | |
[post release]; | |
[super dealloc]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Customize the appearance of table view cells. | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"PostCell"; | |
PostCell *cell = (PostCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[[PostCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
// Configure the cell. | |
Post* post; | |
if(indexPath.row < postsArray.count) { | |
post = [postsArray objectAtIndex:indexPath.row]; | |
} else { | |
post = nil; | |
} | |
cell.post = post; | |
cell.alternate = indexPath.row % 2 == 0 ? NO : YES; | |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
cell.selectionStyle = UITableViewCellSelectionStyleGray; | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment