summaryrefslogtreecommitdiffstats
path: root/Classes/SourcesEditViewController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/SourcesEditViewController.m')
-rw-r--r--Classes/SourcesEditViewController.m103
1 files changed, 91 insertions, 12 deletions
diff --git a/Classes/SourcesEditViewController.m b/Classes/SourcesEditViewController.m
index 93007a9..7211707 100644
--- a/Classes/SourcesEditViewController.m
+++ b/Classes/SourcesEditViewController.m
@@ -10,6 +10,25 @@
#import "SourcesEditViewCell.h"
#import "CJSONDeserializer.h"
#import "PicCastAppDelegate.h"
+#import "FMDatabase.h"
+#import "FMResultSet.h"
+#import "Source.h"
+
+@interface MyButton : UISwitch {
+ NSIndexPath *indexPath;
+}
+
+@property (nonatomic, retain) NSIndexPath *indexPath;
+
+@end
+
+@implementation MyButton
+
+@synthesize indexPath;
+
+@end
+
+
@implementation SourcesEditViewController
@@ -31,6 +50,13 @@
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
sectionDictionary = [[[SectionDictionary alloc] init] retain];
+
+ db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain];
+ [db setShouldCacheStatements:YES];
+// [db setTraceExecution:true];
+// [db setLogsErrors:true];
+
+ [self loadSourcesFromDb];
_receivedData = [[NSMutableData data] retain];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://piccast.memeschemes.com/json/feeds/"]
@@ -47,6 +73,16 @@
//[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"haveSearched"];
}
+- (void) loadSourcesFromDb {
+ [db open];
+ FMResultSet *result = [db executeQuery:@"SELECT * FROM subscribedSources"];
+ while ([result next]) {
+ Source *source = [Source initFromDatabaseRow:result];
+ [sectionDictionary appendObject:source forKey:@"Potpourri"];
+ }
+ [db close];
+}
+
- (IBAction)done:(id)sender {
// NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// [prefs setObject:[self getUserGenderString] forKey:@"userGender"];
@@ -85,6 +121,33 @@
*/
#pragma mark -
+#pragma mark custom sheeyit
+
+- (void) addSourcesFromJson:(NSData *)data {
+
+ NSError *theError = nil;
+ id array = [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:&theError];
+ //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"]];
+ if (![result next]) {
+ NSLog(@"found source");
+ Source *source = [Source initWithJsonObject:obj];
+ [sectionDictionary appendObject:source forKey:source.category];
+
+ [db beginTransaction];
+ [source serializeToDb:db];
+ [db commit];
+ }
+ [result close];
+ }
+ [db close];
+
+}
+
+#pragma mark -
#pragma mark url response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
@@ -119,15 +182,10 @@
// }
//[MBProgressHUD hideHUDForView:self.view animated:YES];
+
+ [self addSourcesFromJson:_receivedData];
- //[_receivedData release];
-
- NSError *theError = nil;
- id array = [[CJSONDeserializer deserializer] deserialize:(NSData *)_receivedData error:&theError];
- //NSLog(@"class: %@", [array class]);
- for (NSDictionary *obj in array) {
- [sectionDictionary appendObject:obj forKey:@"Potpourri"];
- }
+ //[_receivedData release];
[tableView reloadData];
}
@@ -181,16 +239,36 @@
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
- NSDictionary *obj = [[sectionDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row];
- [cell setText:[[obj objectForKey:@"fields"] objectForKey:@"title"]];
-
- UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
+ Source *source = [[sectionDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row];
+ //[cell setText:source.title];
+ cell.text = source.title;
+
+ MyButton *mySwitch = [[[MyButton alloc] initWithFrame:CGRectZero] autorelease];
+ if (source.subscribed) [mySwitch setOn:true];
+
+ mySwitch.indexPath = indexPath;
+
+ [mySwitch addTarget:self action:@selector(switchedOnOff:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:mySwitch];
cell.accessoryView = mySwitch;
return cell;
}
+- (void) switchedOnOff:(id)sender {
+ // 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];
+ 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 commit];
+ [db close];
+}
+
/*
// Override to support conditional editing of the table view.
@@ -264,6 +342,7 @@
- (void)dealloc {
+ [db release];
[sectionDictionary release];
[_receivedData release];
[super dealloc];