From 08dd9e08a6f712f4c599edb26dddd3ea7c73070a Mon Sep 17 00:00:00 2001 From: matt handler Date: Mon, 25 Apr 2011 16:16:04 -0400 Subject: source view edit is at a stable state, cleaned up some stuff, need to connect feeds to it but waiting on fucking bryan that's right, i'm going to be fucking bryan --- Classes/PicCastAppDelegate.m | 2 +- Classes/Source.h | 29 ++++++++++ Classes/Source.m | 74 ++++++++++++++++++++++++++ Classes/SourcesEditViewController.h | 5 ++ Classes/SourcesEditViewController.m | 103 +++++++++++++++++++++++++++++++----- Classes/TopicsViewController.m | 5 +- 6 files changed, 202 insertions(+), 16 deletions(-) create mode 100644 Classes/Source.h create mode 100644 Classes/Source.m (limited to 'Classes') diff --git a/Classes/PicCastAppDelegate.m b/Classes/PicCastAppDelegate.m index 9e3b744..1b72a0c 100644 --- a/Classes/PicCastAppDelegate.m +++ b/Classes/PicCastAppDelegate.m @@ -147,7 +147,7 @@ NSFileManager *fileManager = [NSFileManager defaultManager]; // change this when you are ready to test persistence - if(true || ![fileManager fileExistsAtPath:databasePath]) { + if(![fileManager fileExistsAtPath:databasePath]) { NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; } diff --git a/Classes/Source.h b/Classes/Source.h new file mode 100644 index 0000000..84703ee --- /dev/null +++ b/Classes/Source.h @@ -0,0 +1,29 @@ +// +// Source.h +// PicCast +// +// Created by Matthew Handler on 4/25/11. +// Copyright 2011 Earl Industries. All rights reserved. +// + +#import +#import "FMDatabase.h" +#import "FMResultSet.h" + +@interface Source : NSObject { + NSString *title; + NSString *category; + BOOL subscribed; + NSInteger foreignId; +} + +@property (retain) NSString *category; +@property (retain) NSString *title; +@property BOOL subscribed; +@property NSInteger foreignId; + ++ (Source *) initFromDatabaseRow:(FMResultSet *)result; ++ (Source *) initWithJsonObject:(NSDictionary *)obj; +- (void) serializeToDb:(FMDatabase *)db; + +@end diff --git a/Classes/Source.m b/Classes/Source.m new file mode 100644 index 0000000..bd3037c --- /dev/null +++ b/Classes/Source.m @@ -0,0 +1,74 @@ +// +// Source.m +// PicCast +// +// Created by Matthew Handler on 4/25/11. +// Copyright 2011 Earl Industries. All rights reserved. +// + +#import "Source.h" +#import "FMDatabase.h" +#import "FMResultSet.h" + +@implementation Source + +@synthesize title, foreignId, subscribed, category; + +//+ (Topic *) initFromDatabaseRow:(FMResultSet *)result { +// NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; +// [dateFormatter setDateStyle:NSDateFormatterLongStyle]; +// [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; +// [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]]; +// +// Topic *topic = [[[Topic alloc] init] autorelease]; +// +// topic.guid = [result stringForColumn:@"link"]; +// topic.title = [result stringForColumn:@"title"]; +// topic.iconUrl = [result stringForColumn:@"iconUrl"]; +// topic.description = [result stringForColumn:@"description"]; +// topic.releaseDate = [Topic stringToDate:[result stringForColumn:@"releaseDate"]]; +// topic.category = [result stringForColumn:@"category"]; +// topic.picCount = [result intForColumn:@"picCount"]; +// +// return topic; +//} + ++ (Source *) initFromDatabaseRow:(FMResultSet *)result { + + Source *source = [[[Source alloc] init] autorelease]; + + source.title = [result stringForColumn:@"title"]; + source.foreignId = [result intForColumn:@"foreignId"]; + source.subscribed = [result intForColumn:@"subscribed"] == 0 ? false : true; + source.category = [result stringForColumn:@"category"]; + + return source; +} + ++ (Source *) initWithJsonObject:(NSDictionary *)obj { + + Source *source = [[[Source alloc] init] autorelease]; + + NSDictionary *info = [obj objectForKey:@"fields"]; + + source.title = [info objectForKey:@"title"]; + source.foreignId = [obj objectForKey:@"pk"]; + source.subscribed = false; + source.category = @"Potpourri"; + + return source; +} + +- (void) serializeToDb:(FMDatabase *)db { + [db retain]; + + [db executeUpdate:@"INSERT INTO subscribedSources \ + (foreignId, title, subscribed, category) \ + VALUES \ + (?, ?, ?, ?);", + foreignId, title,[NSNumber numberWithInt:(subscribed ? 1 : 0)], category]; + + [db release]; +} + +@end diff --git a/Classes/SourcesEditViewController.h b/Classes/SourcesEditViewController.h index 1506242..e7f2de9 100644 --- a/Classes/SourcesEditViewController.h +++ b/Classes/SourcesEditViewController.h @@ -8,6 +8,7 @@ #import #import "SectionDictionary.h" +#import "FMDatabase.h" @protocol SourcesEditViewControllerDelegate; @@ -15,12 +16,16 @@ id delegate; NSMutableData *_receivedData; SectionDictionary *sectionDictionary; + FMDatabase *db; } @property (nonatomic, assign) id delegate; @property (nonatomic, retain) IBOutlet UITableView *tableView; - (IBAction)done:(id)sender; +- (void) loadSourcesFromDb; +- (void) addSourcesFromJson:(NSData *)data; +- (void) switchedOnOff:(id)sender; @end 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"]; @@ -84,6 +120,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 @@ -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]; diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 67c8490..c1bab5f 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -30,7 +30,6 @@ [super viewDidLoad]; NSLog(@"viewDidLoad"); self.tableView.rowHeight = 100; - self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; //UIBarButtonItem *doneItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(returnToParserChoices)] autorelease]; //self.navigationItem.rightBarButtonItem = doneItem; @@ -54,9 +53,9 @@ tableDictionary = [[[SectionDictionary alloc] init] retain]; - [self getTopicsFromDb]; + //[self getTopicsFromDb]; - [self startParsing]; + //[self startParsing]; } - (void) openDb { -- cgit v1.2.3