Skip to content

Instantly share code, notes, and snippets.

@tjweir
Forked from enormego/Events.h
Created July 8, 2009 11:57
Show Gist options
  • Save tjweir/142761 to your computer and use it in GitHub Desktop.
Save tjweir/142761 to your computer and use it in GitHub Desktop.
//
// 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
//
// 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
- (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];
}
- (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];
}
// 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