From 9ac2c9e13eb63c2a3a5e9b5a14b079d4fc24a220 Mon Sep 17 00:00:00 2001 From: matt handler Date: Tue, 26 Apr 2011 14:48:07 -0400 Subject: update for new json layout --- Classes/Source.h | 4 ++-- Classes/Source.m | 12 ++++++------ Classes/SourcesEditViewController.m | 17 +++++++++-------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Classes/Source.h b/Classes/Source.h index 84703ee..ea8fe0d 100644 --- a/Classes/Source.h +++ b/Classes/Source.h @@ -14,13 +14,13 @@ NSString *title; NSString *category; BOOL subscribed; - NSInteger foreignId; + NSNumber *foreignId; } @property (retain) NSString *category; @property (retain) NSString *title; @property BOOL subscribed; -@property NSInteger foreignId; +@property (retain) NSNumber *foreignId; + (Source *) initFromDatabaseRow:(FMResultSet *)result; + (Source *) initWithJsonObject:(NSDictionary *)obj; diff --git a/Classes/Source.m b/Classes/Source.m index bd3037c..f36e41e 100644 --- a/Classes/Source.m +++ b/Classes/Source.m @@ -38,7 +38,7 @@ Source *source = [[[Source alloc] init] autorelease]; source.title = [result stringForColumn:@"title"]; - source.foreignId = [result intForColumn:@"foreignId"]; + source.foreignId = [NSNumber numberWithInt:[result intForColumn:@"foreignId"]]; source.subscribed = [result intForColumn:@"subscribed"] == 0 ? false : true; source.category = [result stringForColumn:@"category"]; @@ -49,12 +49,12 @@ Source *source = [[[Source alloc] init] autorelease]; - NSDictionary *info = [obj objectForKey:@"fields"]; + //NSDictionary *info = [obj objectForKey:@"fields"]; - source.title = [info objectForKey:@"title"]; - source.foreignId = [obj objectForKey:@"pk"]; + source.title = [obj objectForKey:@"title"]; + source.foreignId = [obj objectForKey:@"id"]; source.subscribed = false; - source.category = @"Potpourri"; + source.category = [obj objectForKey:@"category"]; return source; } @@ -66,7 +66,7 @@ (foreignId, title, subscribed, category) \ VALUES \ (?, ?, ?, ?);", - foreignId, title,[NSNumber numberWithInt:(subscribed ? 1 : 0)], category]; + foreignId, title, [NSNumber numberWithInt:(subscribed ? 1 : 0)], category]; [db release]; } diff --git a/Classes/SourcesEditViewController.m b/Classes/SourcesEditViewController.m index 070b8a8..34a299f 100644 --- a/Classes/SourcesEditViewController.m +++ b/Classes/SourcesEditViewController.m @@ -53,8 +53,8 @@ db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain]; [db setShouldCacheStatements:YES]; -// [db setTraceExecution:true]; -// [db setLogsErrors:true]; + [db setTraceExecution:true]; + [db setLogsErrors:true]; [self loadSourcesFromDb]; @@ -78,7 +78,7 @@ FMResultSet *result = [db executeQuery:@"SELECT * FROM subscribedSources"]; while ([result next]) { Source *source = [Source initFromDatabaseRow:result]; - [sectionDictionary appendObject:source forKey:@"Potpourri"]; + [sectionDictionary appendObject:source forKey:source.category]; } [db close]; } @@ -126,12 +126,13 @@ - (void) addSourcesFromJson:(NSData *)data { NSError *theError = nil; - id array = [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:&theError]; + id dictionary = [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:&theError]; + NSArray *array = [dictionary objectForKey:@"list"]; //NSLog(@"class: %@", [array class]); [db open]; for (NSDictionary *obj in array) { - NSLog(@"foreign key: %d", [obj objectForKey:@"pk"]); - FMResultSet *result = [db executeQuery:@"SELECT * FROM subscribedSources WHERE foreignId = ?", [obj objectForKey:@"pk"]]; + NSLog(@"foreign key: %d", [obj objectForKey:@"id"]); + FMResultSet *result = [db executeQuery:@"SELECT * FROM subscribedSources WHERE foreignId = ?", [obj objectForKey:@"id"]]; if (![result next]) { NSLog(@"found source"); Source *source = [Source initWithJsonObject:obj]; @@ -259,12 +260,12 @@ // method is sent after switch actually gets switched, so the state is what we want it to be MyButton *switcher = (MyButton *)sender; NSIndexPath *indexPath = switcher.indexPath; - NSInteger foreignId = [[[sectionDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row] foreignId]; + NSNumber *foreignId = [[[sectionDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row] foreignId]; NSLog(@"number %d", foreignId); [db open]; [db beginTransaction]; - [db executeUpdate:@"UPDATE subscribedSources SET subscribed = ? WHERE foreignId = ?", [NSNumber numberWithInt:(switcher.on ? 1 : 0)], [NSNumber numberWithInt:foreignId]]; + [db executeUpdate:@"UPDATE subscribedSources SET subscribed = ? WHERE foreignId = ?", [NSNumber numberWithInt:(switcher.on ? 1 : 0)], foreignId]; [db commit]; [db close]; } -- cgit v1.2.3