diff options
Diffstat (limited to 'Classes/TopicsViewController.m')
-rw-r--r-- | Classes/TopicsViewController.m | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 8bd4531..2548bf3 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -16,6 +16,9 @@ #import "HJObjManager.h" #import "HJManagedImageV.h" #import "SourcesEditViewController.h" +#import "SectionDictionary.h" +#import "FMDatabase.h" +#import "FMResultSet.h" @implementation TopicsViewController @@ -46,9 +49,87 @@ fileCache.fileAgeLimit = 60*60*24*7; //1 week [fileCache trimCacheUsingBackgroundThread]; + tableDictionary = [[[SectionDictionary alloc] init] retain]; + + NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleDB" ofType:@"sqlite"]; + FMDatabase *db = [[FMDatabase alloc] initWithPath:path]; + [db open]; + + [self getTopicsFromDb]; + [self startParsing]; } +- (void) getTopicsFromDb { + // grab topics + FMResultSet *fResult= [db executeQuery:@"SELECT * FROM picCasts"]; //order by date, limite _max + + // put into sections + while([fResult next]) + { + [self addTopicToSectionDictionary:[Topic initFromDatabaseRow:fResult]]; + + } + [fResult close]; + + // put into data structure +} + +- (void) addTopicToSectionDictionary:(Topic *)topic { + NSLog(@"days apart: %d", [self daysApart:topic.releaseDate]); + switch ((int)[self daysApart:topic.releaseDate]) { + case 0: + [tableDictionary appendObject:topic forKey:@"Today"]; + break; + case 1: + [tableDictionary appendObject:topic forKey:@"Yesterday"]; + break; + default: + [tableDictionary appendObject:topic forKey:[Topic dateToString:topic.releaseDate]]; + break; + } +} + +//- (void) addTopic:(FMResultSet *)result forKey:(NSString *)key { +// NSMutableArray *store = [tableDictionary objectForKey:key]; +// if (store == nil) { +// NSMutableArray *store = [[[NSMutableArray alloc] init] autorelease]; +// [tableDictionary setObject:store forKey:key]; +// } +// [store addObject:result]; +//} + +- (NSTimeInterval) daysApart:(NSDate *)date { + //NSLog(@"datestring: %@", [Topic dateToString:date]); + NSDate *today = [NSDate date]; + +// NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; +// [dateFormatter setDateFormat:@"yyyy-MM-dd"]; +// NSDate *published = [dateFormatter dateFromString:string]; + + return [today timeIntervalSinceDate:date]; +} + +// called after parsing is finished +- (void) updateTopicsFromWeb:(NSArray *)parsedTopics { + // add topics to sql + [db beginTransaction]; + for (Topic *topic in parsedTopics) { + // if topic is already in database + FMResultSet *rs = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", 10]; + if (![rs hasAnotherRow]) { + [topic serializeToDatabase:db]; + // add topics to structure + [self addTopicToSectionDictionary:topic]; + } + } + [db commit]; +// NSLog(@"%@", [db lastErrorMessage]); +// NSLog(@"%d", [db lastInsertRowId]); + // tell table to animate things being added + [self.tableView reloadData]; +} + - (void) sourcesEditViewControllerDidFinish:(SourcesEditViewController *)controller { [self dismissModalViewControllerAnimated:YES]; @@ -118,7 +199,8 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. - return 2; + NSLog(@"number of sections: %d", [tableDictionary count]); + return [tableDictionary count]; } #pragma mark - @@ -138,14 +220,13 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. - return [topics count]; + NSLog(@"number of rows in section %d: %d", section, [[tableDictionary objectForIndex:section] count]); + return [[tableDictionary objectForIndex:section] count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { - if (section == 0) - return @"Today"; - else - return @"Yesterday"; + NSLog(@"row title: %@", [tableDictionary keyForIndex:section]); + return [tableDictionary keyForIndex:section]; //how do you convert a key to index from mutabledictionary } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { @@ -258,7 +339,7 @@ } - (void)parser:(XMLParser *)parser didParseTopics:(NSArray *)parsedTopics { - [topics addObjectsFromArray: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 @@ -291,6 +372,7 @@ } - (void)dealloc { + [tableDictionary release]; [objMan release]; [topics release]; //[detailController release]; |