summaryrefslogtreecommitdiffstats
path: root/Classes/TopicsViewController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/TopicsViewController.m')
-rw-r--r--Classes/TopicsViewController.m69
1 files changed, 18 insertions, 51 deletions
diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m
index c1bab5f..8635e1d 100644
--- a/Classes/TopicsViewController.m
+++ b/Classes/TopicsViewController.m
@@ -52,28 +52,20 @@
[fileCache trimCacheUsingBackgroundThread];
tableDictionary = [[[SectionDictionary alloc] init] retain];
-
- //[self getTopicsFromDb];
-
- //[self startParsing];
-}
-- (void) openDb {
db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain];
[db setShouldCacheStatements:YES];
[db setTraceExecution:true];
- [db setLogsErrors:true];
- [db open];
-}
-
-- (void) closeDb {
- [db close];
- db = nil;
+ [db setLogsErrors:true];
+
+ [self getTopicsFromDb];
+
+ //[self startParsing];
}
- (void) getTopicsFromDb {
// grab topics
- [self openDb];
+ [db open];
FMResultSet *fResult = [db executeQuery:@"SELECT * FROM picCasts"]; //order by date, limite _max
// put into sections
@@ -83,64 +75,39 @@
}
[fResult close];
- [self closeDb];
+ [db close];
// put into data structure
}
- (void) addTopicToSectionDictionary:(Topic *)topic {
- 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];
-// NSLog(@"today: %@", today);
-// NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
-// [dateFormatter setDateFormat:@"yyyy-MM-dd"];
-// NSDate *published = [dateFormatter dateFromString:string];
-
- return [date timeIntervalSinceNow];
+ //NSLog(@"time since: %f", [topic.releaseDate timeIntervalSinceNow]);
+ NSTimeInterval seconds = fabs([topic.releaseDate timeIntervalSinceNow]);
+ if (seconds <= 86400.0f)
+ [tableDictionary appendObject:topic forKey:@"Today"];
+ else if (seconds <= 172800.0f)
+ [tableDictionary appendObject:topic forKey:@"Yesterday"];
+ else
+ [tableDictionary appendObject:topic forKey:[Topic dateToString:topic.releaseDate]];
}
// called after parsing is finished
- (void) updateTopicsFromWeb:(NSArray *)parsedTopics {
// add topics to sql
- [self openDb];
+ [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];
- NSLog(@"tamp");
[topic serializeToDatabase:db];
[db commit];
- NSLog(@"%@", [db lastErrorMessage]);
- NSLog(@"%d", [db lastInsertRowId]);
+
// add topics to structure
[self addTopicToSectionDictionary:topic];
}
}
- [self closeDb];
+ [db close];
// tell table to animate things being added
[self.tableView reloadData];