Skip to content

Commit

Permalink
Cleaned up Post class a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexJWayne committed Oct 3, 2008
1 parent 8c2fb4e commit ca8119f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Classes/Post.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
- (void) abortLoadIfLoading;
- (int)compare:(Post *)otherPost;

@property (readwrite, retain) Post *parent;
@property (retain) Post *parent;
@property (readwrite, copy) NSString *author;
@property (readwrite, copy) NSString *preview;
@property (readwrite, copy) NSString *body;
@property (readwrite, copy) NSString *category;
@property (readwrite, copy) NSDate *date;
@property (readwrite) int postId;
@property (readwrite, retain) NSMutableArray *children;
@property (retain) NSMutableArray *children;
@property (readwrite, assign) int depth;
@property (readwrite, assign) int cachedReplyCount;
@property (readwrite, assign) int newPostCount;
Expand Down
57 changes: 23 additions & 34 deletions Classes/Post.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ - (id)init {
- (id)initWithXmlElement:(CXMLElement *)xml parent:(Post *)aParent lastRefreshDict:(NSMutableDictionary *)lastRefresh {
[self init];

self.parent = [aParent retain];
//NSLog(@"parent retainCount: %i", [aParent retainCount] );
self.parent = aParent;
if ([self parseXml:xml lastRefreshDict:lastRefresh]) {
return self;
} else {
Expand Down Expand Up @@ -63,7 +62,7 @@ - (BOOL)parseXml:(CXMLElement *)xml lastRefreshDict:(NSMutableDictionary *)lastR
}

// traverse children
self.children = [[NSMutableArray alloc] init];
self.children = [[[NSMutableArray alloc] init] autorelease];
NSArray *postElements = [xml nodesForXPath:@"comments/comment" error:nil];
for (CXMLElement *postXml in [postElements objectEnumerator]) {
Post *postObject = [[Post alloc] initWithXmlElement:postXml parent:self lastRefreshDict:nil];
Expand All @@ -73,10 +72,10 @@ - (BOOL)parseXml:(CXMLElement *)xml lastRefreshDict:(NSMutableDictionary *)lastR
}
}

if((parent == nil) && (lastRefresh != nil)) {
NSString* postID = [NSString stringWithFormat:@"%d", self.postId];
NSNumber* previousPostCount = [lastRefresh valueForKey:postID];
// If we haven't seen the post before, or if the count of children is more than the last time we saw it, mark it as having new posts.
// If we haven't seen the post before, or if the count of children is more than the last time we saw it, mark it as having new posts.
if ((parent == nil) && (lastRefresh != nil)) {
NSString *postID = [NSString stringWithFormat:@"%d", self.postId];
NSNumber *previousPostCount = [lastRefresh valueForKey:postID];
self.newPostCount = (previousPostCount == nil) ? self.cachedReplyCount : (self.cachedReplyCount - [previousPostCount intValue]);
[lastRefresh setValue:[NSNumber numberWithInt:self.cachedReplyCount] forKey:postID];
}
Expand All @@ -85,16 +84,9 @@ - (BOOL)parseXml:(CXMLElement *)xml lastRefreshDict:(NSMutableDictionary *)lastR
if (parent == nil) {
int i;
NSMutableArray *sortedByRecent = [[NSMutableArray alloc] initWithObjects:self, nil];

for (i = 0; i <= cachedReplyCount; i++) {
[sortedByRecent addObject:[self postAtIndex:i]];
}
for (i = 0; i <= cachedReplyCount; i++) [sortedByRecent addObject:[self postAtIndex:i]];
[sortedByRecent sortUsingSelector:@selector(compare:)];

for (i = 0; i <= cachedReplyCount; i++) {
[[sortedByRecent objectAtIndex:i] setRecentIndex:i];
}

for (i = 0; i <= cachedReplyCount; i++) [[sortedByRecent objectAtIndex:i] setRecentIndex:i];
[sortedByRecent release];
}
// Filter post
Expand All @@ -116,17 +108,16 @@ - (id)initWithThreadId:(int)threadId delegate:(id)aDelegate {
[self init];
delegate = aDelegate;
NSString *urlString = [Feed urlStringWithPath:[NSString stringWithFormat:@"thread/%d.xml", threadId]];
theConnection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self startImmediately:YES];
//[NSURLConnection connectionWithRequest:] delegate:self];
if (theConnection) [theConnection release];
theConnection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self startImmediately:YES];
return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[partialData appendData:data];
}

- (void) abortLoadIfLoading
{
- (void)abortLoadIfLoading {
[theConnection cancel];
}

Expand All @@ -142,14 +133,14 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {


- (void)dealloc {
if( parent ) [parent release];
if( author )[author release];
if( preview )[preview release];
if( body )[body release];
if( date )[date release];
if( children )[children dealloc];
if( partialData )[partialData release];
if( category )[category release];
if (parent) [parent release];
if (author) [author release];
if (preview) [preview release];
if (body) [body release];
if (date) [date release];
if (children) [children dealloc];
if (partialData) [partialData release];
if (category) [category release];
[theConnection release];
[super dealloc];
}
Expand All @@ -163,16 +154,14 @@ - (NSString *)html {
template = [template stringByReplacingOccurrencesOfString:@"<%= date %>" withString:[self formattedDate]];
template = [template stringByReplacingOccurrencesOfString:@"<%= author %>" withString:author];
template = [template stringByReplacingOccurrencesOfString:@"<%= body %>" withString:body];
template = [template stringByReplacingOccurrencesOfString:@"<%= postId %>" withString:[[[NSString alloc] initWithFormat:@"%i", postId]autorelease]];
template = [template stringByReplacingOccurrencesOfString:@"<%= postId %>" withString:[NSString stringWithFormat:@"%i", postId]];

return template;
}

- (NSInteger)replyCount {
NSInteger count = [children count];
for (Post *post in [children objectEnumerator]) {
count = count + [post replyCount];
}
for (Post *post in children) count = count + [post replyCount];
return count;
}

Expand All @@ -182,8 +171,8 @@ - (Post *)postAtIndex:(int)index {
if (index == 0) {
result = self;
} else {
for (Post *post in [children objectEnumerator]) {
if (index <= [post replyCount]+1) {
for (Post *post in children) {
if (index <= [post replyCount] + 1) {
return [post postAtIndex:index - 1];
} else {
index = index - [post replyCount] - 1;
Expand Down

0 comments on commit ca8119f

Please sign in to comment.