summaryrefslogtreecommitdiffstats
path: root/Classes
diff options
context:
space:
mode:
Diffstat (limited to 'Classes')
-rw-r--r--Classes/SourcesEditViewController.m2
-rw-r--r--Classes/Topic.h2
-rw-r--r--Classes/Topic.m26
-rw-r--r--Classes/TopicsViewController.m69
4 files changed, 42 insertions, 57 deletions
diff --git a/Classes/SourcesEditViewController.m b/Classes/SourcesEditViewController.m
index 7211707..070b8a8 100644
--- a/Classes/SourcesEditViewController.m
+++ b/Classes/SourcesEditViewController.m
@@ -241,7 +241,7 @@
Source *source = [[sectionDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row];
//[cell setText:source.title];
- cell.text = source.title;
+ [cell.textLabel setText:source.title];
MyButton *mySwitch = [[[MyButton alloc] initWithFrame:CGRectZero] autorelease];
if (source.subscribed) [mySwitch setOn:true];
diff --git a/Classes/Topic.h b/Classes/Topic.h
index 26f8c29..a808ee1 100644
--- a/Classes/Topic.h
+++ b/Classes/Topic.h
@@ -18,9 +18,11 @@
NSString *iconUrl;
NSNumber *picCount;
NSString *guid;
+ NSNumber *foreignId;
}
+@property (nonatomic, retain) NSNumber *foreignId;
@property (nonatomic, copy) NSNumber *picCount;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *guid;
diff --git a/Classes/Topic.m b/Classes/Topic.m
index 123df3f..1ed4420 100644
--- a/Classes/Topic.m
+++ b/Classes/Topic.m
@@ -12,7 +12,7 @@
@implementation Topic
-@synthesize guid, title, iconUrl, description, releaseDate, category, picCount;
+@synthesize guid, title, iconUrl, description, releaseDate, category, picCount, foreignId;
+ (Topic *) initFromDatabaseRow:(FMResultSet *)result {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
@@ -26,9 +26,25 @@
topic.title = [result stringForColumn:@"title"];
topic.iconUrl = [result stringForColumn:@"iconUrl"];
topic.description = [result stringForColumn:@"description"];
- topic.releaseDate = [Topic stringToDate:[result stringForColumn:@"releaseDate"]];
+ topic.releaseDate = [result dateForColumn:@"releaseDate"];
topic.category = [result stringForColumn:@"category"];
- topic.picCount = [result intForColumn:@"picCount"];
+ topic.picCount = [NSNumber numberWithInt:[result intForColumn:@"picCount"]];
+
+ return topic;
+}
+
++ (Topic *) initFromJsonObject:(NSDictionary *)obj {
+ Topic *topic = [[[Topic alloc] init] autorelease];
+
+ NSDictionary *info = [obj objectForKey:@"fields"];
+
+ topic.title = [info objectForKey:@"title"];
+ topic.foreignId = [obj objectForKey:@"pk"];
+ topic.iconUrl = [info objectForKey:@"iconUrl"];
+ topic.description = [info objectForKey:@"description"];
+ topic.releaseDate = [self stringToDate:[info objectForKey:@"releaseDate"]];
+ topic.category = [info objectForKey:@"category"];
+ topic.picCount = [info objectForKey:@"picCount"];
return topic;
}
@@ -53,12 +69,12 @@
- (void)serializeToDatabase:(FMDatabase *)db {
[db retain];
- NSLog(@"title: %@", title);
+ NSLog(@"title: %d", [releaseDate timeIntervalSince1970]);
[db executeUpdate:@"INSERT INTO picCasts \
(foreignId, link, title, description, releaseDate, category, iconUrl, picCount) \
VALUES \
(?, ?, ?, ?, ?, ?, ?, ?)",
- [NSNumber numberWithInt:12], guid, title, description, [Topic dateToString:releaseDate], category, iconUrl, [NSNumber numberWithInt:12]];
+ [NSNumber numberWithInt:12], guid, title, description, releaseDate, category, iconUrl, picCount];
//NSLog(@"insert id:%d error: %@", [db lastInsertRowId], [db lastErrorMessage]);
// [NSString stringWithFormat:@"number %d", i],
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];