From e6eb634beac831e64becc03928a467701f9625cf Mon Sep 17 00:00:00 2001 From: matt handler Date: Wed, 27 Apr 2011 01:25:19 -0400 Subject: made fetcher take a payload, added favicon, turned off statement caching, may turn it back on who knows, added sourceid to topics, added category to topictableviewcell, did a little cleaning --- Classes/TopicsViewController.m | 175 +++++++++++++++++++++++------------------ 1 file changed, 100 insertions(+), 75 deletions(-) (limited to 'Classes/TopicsViewController.m') diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 3431e62..31191ee 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -7,7 +7,6 @@ // #import "TopicsViewController.h" -#import "XMLParser.h" #import "Topic.h" #import "TopicTableViewCell.h" #import "AcidCowFeedburnerParser.h" @@ -22,10 +21,11 @@ #import "PicCastAppDelegate.h" #import "CJSONDeserializer.h" #import "Fetcher.h" +#import "Source.h" @implementation TopicsViewController -@synthesize topics, parser, tableView; +@synthesize tableView, sourcesDictionary; - (void)viewDidLoad { [super viewDidLoad]; @@ -44,9 +44,11 @@ [fileCache trimCacheUsingBackgroundThread]; tableDictionary = [[[SectionDictionary alloc] init] retain]; - + sourcesDictionary = [[[NSMutableDictionary alloc] init] retain]; + + db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain]; - [db setShouldCacheStatements:YES]; + [db setShouldCacheStatements:NO]; // [db setTraceExecution:true]; // [db setLogsErrors:true]; @@ -109,19 +111,21 @@ #pragma mark - #pragma mark custom sheeyit -- (void) addTopicsFromJson:(NSData *)data { - +- (void) addTopicsFromJson:(NSData *)data forSource:(Source *)source { + NSError *theError = nil; id dictionary = [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:&theError]; NSArray *array = [dictionary objectForKey:@"list"]; - + [db open]; for (NSDictionary *obj in array) { - NSLog(@"foreign key: %d", [obj objectForKey:@"id"]); FMResultSet *result = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [obj objectForKey:@"id"]]; if (![result next]) { NSLog(@"found new topic"); Topic *topic = [Topic initWithJsonObject:obj]; + topic.sourceId = source.foreignId; + + if ([topic.picCount intValue] == 0) break; [db beginTransaction]; [topic serializeToDatabase:db]; @@ -133,43 +137,60 @@ } [db close]; - [self.tableView reloadData]; + [self.tableView reloadData]; + } -// called after parsing is finished -- (void) updateTopicsFromWeb:(NSArray *)parsedTopics { - // add topics to sql - [db open]; - for (Topic *topic in parsedTopics) { - // if topic is already in database - //FMResultSet *rs = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [NSNumber numberWithInt:10]]; - if (true) { - //[rs close]; +- (void) addTopicsFromJson:(NSData *)data { + + NSError *theError = nil; + id dictionary = [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:&theError]; + NSArray *array = [dictionary objectForKey:@"list"]; + + [db open]; + for (NSDictionary *obj in array) { + FMResultSet *result = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [obj objectForKey:@"id"]]; + if (![result next]) { + Topic *topic = [Topic initWithJsonObject:obj]; + + if (topic.picCount == 0) break; + [db beginTransaction]; [topic serializeToDatabase:db]; [db commit]; - // add topics to structure [self addTopicToSectionDictionary:topic]; } - } + [result close]; + } [db close]; - // tell table to animate things being added - [self.tableView reloadData]; + [self.tableView reloadData]; } - (void) loadSubscribedTopics { + NSLog(@"load subscribed"); [db open]; - FMResultSet *result = [db executeQuery:@"SELECT foreignId FROM subscribedSources WHERE subscribed = ?", [NSNumber numberWithInt:1]]; + FMResultSet *result = [db executeQuery:@"SELECT * FROM subscribedSources WHERE subscribed = ?", [NSNumber numberWithInt:1]]; while ([result next]) { - + Source *source = [[[Source alloc] init] autorelease]; + if ([sourcesDictionary objectForKey:[NSNumber numberWithInt:[result intForColumn:@"foreignId"]]] != nil) { + source = [sourcesDictionary objectForKey:[NSNumber numberWithInt:[result intForColumn:@"foreignId"]]]; + } + else { + source = [Source initFromDatabaseRow:result]; + [sourcesDictionary setObject:source forKey:source.foreignId]; + } + Fetcher *fetcher = [Fetcher initWithString:[NSString - stringWithFormat:@"http://piccast.memeschemes.com/json/sets_by_feed/%d/", - [result intForColumn:@"foreignId"]] + stringWithFormat:@"http://piccast.memeschemes.com/json/sets_by_feed/%@/", + source.foreignId] + payload:source andDelegate:self]; } + [result close]; + [db close]; } #pragma mark - @@ -184,6 +205,10 @@ [self addTopicsFromJson:data]; } +- (void) connection:(NSURLConnection *)connection finishedWithData:(NSData *)data andPayload:(id)payload { + [self addTopicsFromJson:data forSource:(Source *)payload]; +} + #pragma mark - #pragma mark Table view data source @@ -276,9 +301,11 @@ Topic *topic = [[tableDictionary objectForIndex:[indexPath section]] objectAtIndex:[indexPath row]]; //cell.useDarkBackground = (indexPath.row % 2 == 0); cell.title = topic.title; + [cell.categoryLabel setText:topic.category]; + cell.picCount = [NSString stringWithFormat:@"%@", topic.picCount]; - micon.url = [NSURL URLWithString:@"http://www.acidcow.com/favicon.ico"]; + micon.url = [NSURL URLWithString:[[sourcesDictionary objectForKey:topic.sourceId] favicon]]; mi.url = [NSURL URLWithString:topic.iconUrl]; mi.squareCropped = true; @@ -343,51 +370,51 @@ return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - -#pragma mark Implementation - -- (void)startParsing { - NSLog(@"startParsing"); - //self.navigationItem.rightBarButtonItem.enabled = NO; - // Reset the title - //self.title = NSLocalizedString(@"Getting Recent Picdumps...", @"Loading"); - // Allocate the array for song storage, or empty the results of previous parses - if (topics == nil) { - self.topics = [NSMutableArray array]; - } else { - [topics removeAllObjects]; - [self.tableView reloadData]; - } - // Create the parser, set its delegate, and start it. - self.parser = [[[AcidCowFeedburnerParser alloc] init] autorelease]; - parser.delegate = self; - [parser start]; -} - -- (void)parserDidEndParsingData:(XMLParser *)parser { - //self.title = [NSString stringWithFormat:NSLocalizedString(@"Recent Picdumps (%d)", @"Recent"), [topics count]]; - [self.tableView reloadData]; - //self.navigationItem.rightBarButtonItem.enabled = YES; - self.parser = nil; -} - -- (void)parser:(XMLParser *)parser didParseTopics:(NSArray *)parsedTopics { - [self updateTopicsFromWeb:parsedTopics]; - - // Three scroll view properties are checked to keep the user interface smooth during parse. When new objects are delivered - // by the parser, the table view is reloaded to display them. If the table is reloaded while - // the user is scrolling, this can result in eratic behavior. dragging, tracking, and decelerating can be checked - // for this purpose. When the parser finishes, reloadData will be called in parserDidEndParsingData:, guaranteeing - // that all data will ultimately be displayed even if reloadData is not called in this method because of user interaction. - if (!self.tableView.dragging && !self.tableView.tracking && !self.tableView.decelerating) { - //self.title = [NSString stringWithFormat:NSLocalizedString(@"Recent Picdumps (%d)", @"Recent"), [topics count]]; - [self.tableView reloadData]; - } -} - -- (void)parser:(XMLParser *)parser didFailWithError:(NSError *)error { - // handle errors as appropriate to your application... -} +// +//#pragma mark Implementation +// +//- (void)startParsing { +// NSLog(@"startParsing"); +// //self.navigationItem.rightBarButtonItem.enabled = NO; +// // Reset the title +// //self.title = NSLocalizedString(@"Getting Recent Picdumps...", @"Loading"); +// // Allocate the array for song storage, or empty the results of previous parses +// if (topics == nil) { +// self.topics = [NSMutableArray array]; +// } else { +// [topics removeAllObjects]; +// [self.tableView reloadData]; +// } +// // Create the parser, set its delegate, and start it. +// self.parser = [[[AcidCowFeedburnerParser alloc] init] autorelease]; +// parser.delegate = self; +// [parser start]; +//} +// +//- (void)parserDidEndParsingData:(XMLParser *)parser { +// //self.title = [NSString stringWithFormat:NSLocalizedString(@"Recent Picdumps (%d)", @"Recent"), [topics count]]; +// [self.tableView reloadData]; +// //self.navigationItem.rightBarButtonItem.enabled = YES; +// self.parser = nil; +//} +// +//- (void)parser:(XMLParser *)parser didParseTopics:(NSArray *)parsedTopics { +// [self updateTopicsFromWeb:parsedTopics]; +// +// // Three scroll view properties are checked to keep the user interface smooth during parse. When new objects are delivered +// // by the parser, the table view is reloaded to display them. If the table is reloaded while +// // the user is scrolling, this can result in eratic behavior. dragging, tracking, and decelerating can be checked +// // for this purpose. When the parser finishes, reloadData will be called in parserDidEndParsingData:, guaranteeing +// // that all data will ultimately be displayed even if reloadData is not called in this method because of user interaction. +// if (!self.tableView.dragging && !self.tableView.tracking && !self.tableView.decelerating) { +// //self.title = [NSString stringWithFormat:NSLocalizedString(@"Recent Picdumps (%d)", @"Recent"), [topics count]]; +// [self.tableView reloadData]; +// } +//} +// +//- (void)parser:(XMLParser *)parser didFailWithError:(NSError *)error { +// // handle errors as appropriate to your application... +//} #pragma mark - #pragma mark Memory management @@ -406,11 +433,9 @@ - (void)dealloc { [db release]; + [sourcesDictionary release]; [tableDictionary release]; [objMan release]; - [topics release]; - //[detailController release]; - [parser release]; [super dealloc]; } -- cgit v1.2.3