diff options
Diffstat (limited to 'Classes/TopicsViewController.m')
-rw-r--r-- | Classes/TopicsViewController.m | 111 |
1 files changed, 84 insertions, 27 deletions
diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 8635e1d..1d93566 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -20,7 +20,8 @@ #import "FMDatabase.h" #import "FMResultSet.h" #import "PicCastAppDelegate.h" - +#import "CJSONDeserializer.h" +#import "Fetcher.h" @implementation TopicsViewController @@ -55,12 +56,12 @@ db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain]; [db setShouldCacheStatements:YES]; - [db setTraceExecution:true]; - [db setLogsErrors:true]; +// [db setTraceExecution:true]; +// [db setLogsErrors:true]; [self getTopicsFromDb]; - //[self startParsing]; + [self loadSubscribedTopics]; } - (void) getTopicsFromDb { @@ -90,32 +91,10 @@ [tableDictionary appendObject:topic forKey:[Topic dateToString:topic.releaseDate]]; } -// 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]; - [db beginTransaction]; - [topic serializeToDatabase:db]; - [db commit]; - - // add topics to structure - [self addTopicToSectionDictionary:topic]; - } - } - [db close]; - - // tell table to animate things being added - [self.tableView reloadData]; -} - - (void) sourcesEditViewControllerDidFinish:(SourcesEditViewController *)controller { [self dismissModalViewControllerAnimated:YES]; + [self loadSubscribedTopics]; //[self _userIs:[controller getUserGenderString] heets:[controller getHeetGenderString]]; } @@ -176,6 +155,84 @@ } */ +#pragma mark - +#pragma mark custom sheeyit + +- (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) { + 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]; + + [db beginTransaction]; + [topic serializeToDatabase:db]; + [db commit]; + + [self addTopicToSectionDictionary:topic]; + } + [result close]; + } + [db close]; + + [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]; + [db beginTransaction]; + [topic serializeToDatabase:db]; + [db commit]; + + // add topics to structure + [self addTopicToSectionDictionary:topic]; + } + } + [db close]; + + // tell table to animate things being added + [self.tableView reloadData]; +} + +- (void) loadSubscribedTopics { + [db open]; + FMResultSet *result = [db executeQuery:@"SELECT foreignId FROM subscribedSources WHERE subscribed = ?", [NSNumber numberWithInt:1]]; + while ([result next]) { + + Fetcher *fetcher = [Fetcher initWithString:[NSString + stringWithFormat:@"http://piccast.memeschemes.com/json/sets_by_feed/%d/", + [result intForColumn:@"foreignId"]] + andDelegate:self]; + + } +} + +#pragma mark - +#pragma mark fetcher delegate protocol + +- (void) connection:(NSURLConnection *)connection failedWithError:(NSError *)error { + [PicCastAppDelegate prompt:@"Error" withMessage:[error localizedDescription] andButtonTitle:@"Aww man..." withDelegate:self]; +} + +- (void) connection:(NSURLConnection *)connection finishedWithData:(NSData *)data { + //NSLog(@"%@", [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:nil]); + [self addTopicsFromJson:data]; +} + #pragma mark - #pragma mark Table view data source |