diff options
author | matt handler <matt.handler@gmail.com> | 2011-04-26 17:18:49 -0400 |
---|---|---|
committer | matt handler <matt.handler@gmail.com> | 2011-04-26 17:18:49 -0400 |
commit | 46642320901ebaaedae5618e423ebfe70f316351 (patch) | |
tree | fa4f7d55589c1057ee8e33194a71bbf7b587960a /Classes | |
parent | 9ac2c9e13eb63c2a3a5e9b5a14b079d4fc24a220 (diff) | |
download | piccast-app-46642320901ebaaedae5618e423ebfe70f316351.tar.gz piccast-app-46642320901ebaaedae5618e423ebfe70f316351.zip |
updated topics dynamic loading, added fetcher class, storing in database and not overwriting is good, need to add info to photo view controller and also sort results
Diffstat (limited to 'Classes')
-rw-r--r-- | Classes/Fetcher.h | 29 | ||||
-rw-r--r-- | Classes/Fetcher.m | 61 | ||||
-rw-r--r-- | Classes/PicCastAppDelegate.h | 2 | ||||
-rw-r--r-- | Classes/PicCastAppDelegate.m | 22 | ||||
-rw-r--r-- | Classes/SourcesEditViewController.m | 1 | ||||
-rw-r--r-- | Classes/Topic.h | 4 | ||||
-rw-r--r-- | Classes/Topic.m | 27 | ||||
-rw-r--r-- | Classes/TopicsViewController.h | 6 | ||||
-rw-r--r-- | Classes/TopicsViewController.m | 111 |
9 files changed, 212 insertions, 51 deletions
diff --git a/Classes/Fetcher.h b/Classes/Fetcher.h new file mode 100644 index 0000000..d51a39d --- /dev/null +++ b/Classes/Fetcher.h @@ -0,0 +1,29 @@ +// +// Fetcher.h +// PicCast +// +// Created by Matthew Handler on 4/26/11. +// Copyright 2011 Earl Industries. All rights reserved. +// + +#import <Foundation/Foundation.h> + +@protocol FetcherDelegate + +- (void) connection:(NSURLConnection *)connection failedWithError:(NSError *)error; +- (void) connection:(NSURLConnection *)connection finishedWithData:(NSData *)data; + +@end + + +@interface Fetcher : NSObject <NSURLProtocolClient> { + NSMutableData *receivedData; + id<FetcherDelegate> delegate; +} + +@property (nonatomic, assign) id<FetcherDelegate> delegate; + ++ (Fetcher *) initWithString:(NSString *)string andDelegate:(id <FetcherDelegate>)delegate; +- (void) setUrlAndConnect:(NSString *)string; + +@end diff --git a/Classes/Fetcher.m b/Classes/Fetcher.m new file mode 100644 index 0000000..3fe162f --- /dev/null +++ b/Classes/Fetcher.m @@ -0,0 +1,61 @@ +// +// Fetcher.m +// PicCast +// +// Created by Matthew Handler on 4/26/11. +// Copyright 2011 Earl Industries. All rights reserved. +// + +#import "Fetcher.h" +#import "PicCastAppDelegate.h" + +@implementation Fetcher + +@synthesize delegate; + ++ (Fetcher *) initWithString:(NSString *)string andDelegate:(id<FetcherDelegate>)d { + Fetcher *fetcher = [[[Fetcher alloc] init] autorelease]; + fetcher.delegate = d; + [fetcher setUrlAndConnect:string]; + return fetcher; +} + +- (Fetcher *) init { + receivedData = [[NSMutableData data] retain]; + return self; +} + +- (void) setUrlAndConnect:(NSString *)string { + + NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:string] + cachePolicy:NSURLRequestUseProtocolCachePolicy + timeoutInterval:30.0]; + if ([NSURLConnection connectionWithRequest:theRequest delegate:self]) { + [receivedData setLength:0]; + } else { + [PicCastAppDelegate prompt:@"Error" withMessage:@"No internet connection" andButtonTitle:@"shucks" withDelegate:self]; + } +} + +- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { + [receivedData setLength:0]; +} + +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { + [receivedData appendData:data]; +} + +- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + [delegate connection:connection failedWithError:error]; +} + +- (void)connectionDidFinishLoading:(NSURLConnection *)connection { + [delegate connection:connection finishedWithData:receivedData]; +} + +- (void) dealloc { + [receivedData release]; + [super dealloc]; +} + +@end diff --git a/Classes/PicCastAppDelegate.h b/Classes/PicCastAppDelegate.h index 1085903..de0100a 100644 --- a/Classes/PicCastAppDelegate.h +++ b/Classes/PicCastAppDelegate.h @@ -19,5 +19,7 @@ @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; + (NSString *) getDatabasePath; ++ (void) prompt:(NSString *)title withMessage:(NSString *)message andButtonTitle:(NSString *)buttonTitle; ++ (void) prompt:(NSString *)title withMessage:(NSString *)message andButtonTitle:(NSString *)buttonTitle withDelegate:(id)delegate; @end diff --git a/Classes/PicCastAppDelegate.m b/Classes/PicCastAppDelegate.m index 1b72a0c..a3bc294 100644 --- a/Classes/PicCastAppDelegate.m +++ b/Classes/PicCastAppDelegate.m @@ -123,17 +123,17 @@ #pragma mark - #pragma mark CustomSheeyit -//+ (void) prompt:(NSString *)title withMessage:(NSString *)message andButtonTitle:(NSString *)buttonTitle withDelegate:(id)delegate { -// UIAlertView *prompt = [UIAlertView alloc]; -// prompt = [prompt initWithTitle:title message:message delegate:delegate cancelButtonTitle:buttonTitle otherButtonTitles:nil]; -// [prompt show]; -// [prompt release]; -//} -// -//+ (void) prompt:(NSString *)title withMessage:(NSString *)message andButtonTitle:(NSString *)buttonTitle { -// [self prompt:title withMessage:message andButtonTitle:buttonTitle withDelegate:self]; -//} -// ++ (void) prompt:(NSString *)title withMessage:(NSString *)message andButtonTitle:(NSString *)buttonTitle withDelegate:(id)delegate { + UIAlertView *prompt = [UIAlertView alloc]; + prompt = [prompt initWithTitle:title message:message delegate:delegate cancelButtonTitle:buttonTitle otherButtonTitles:nil]; + [prompt show]; + [prompt release]; +} + ++ (void) prompt:(NSString *)title withMessage:(NSString *)message andButtonTitle:(NSString *)buttonTitle { + [self prompt:title withMessage:message andButtonTitle:buttonTitle withDelegate:self]; +} + //- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { // return [facebook handleOpenURL:url]; //} diff --git a/Classes/SourcesEditViewController.m b/Classes/SourcesEditViewController.m index 34a299f..0d5bd85 100644 --- a/Classes/SourcesEditViewController.m +++ b/Classes/SourcesEditViewController.m @@ -88,7 +88,6 @@ // [prefs setObject:[self getUserGenderString] forKey:@"userGender"]; // [prefs setObject:[self getHeetGenderString] forKey:@"heetGender"]; // [prefs synchronize]; - [self.delegate sourcesEditViewControllerDidFinish:self]; } diff --git a/Classes/Topic.h b/Classes/Topic.h index a808ee1..93c1925 100644 --- a/Classes/Topic.h +++ b/Classes/Topic.h @@ -32,7 +32,9 @@ @property (nonatomic, copy) NSString *iconUrl; + (Topic *) initFromDatabaseRow:(FMResultSet *)result; ++ (Topic *) initWithJsonObject:(NSDictionary *)obj; + (NSString *) dateToString:(NSDate *)date; + (NSDate *) stringToDate:(NSString *)string; - +- (void) serializeToDatabase:(FMDatabase *)db; ++ (NSString *) getImageUrl:(NSDictionary *)obj; @end diff --git a/Classes/Topic.m b/Classes/Topic.m index 1ed4420..8b7ec20 100644 --- a/Classes/Topic.m +++ b/Classes/Topic.m @@ -33,22 +33,29 @@ return topic; } -+ (Topic *) initFromJsonObject:(NSDictionary *)obj { ++ (Topic *) initWithJsonObject:(NSDictionary *)obj { Topic *topic = [[[Topic alloc] init] autorelease]; - NSDictionary *info = [obj objectForKey:@"fields"]; + //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"]; + topic.title = [obj objectForKey:@"title"]; + topic.foreignId = [obj objectForKey:@"id"]; + topic.iconUrl = [self getImageUrl:[obj objectForKey:@"image"]]; + topic.description = [obj objectForKey:@"description"]; + topic.releaseDate = [NSDate dateWithTimeIntervalSince1970:[[obj objectForKey:@"created_unixtime"] doubleValue]]; + topic.category = [obj objectForKey:@"category"]; + topic.picCount = [NSNumber numberWithInt:10]; //[obj objectForKey:@"picCount"]; return topic; } ++ (NSString *) getImageUrl:(NSDictionary *)obj { + if (obj == nil || [obj class] == [NSNull class]) return nil; + else { + return [obj objectForKey:@"original_url"]; + } +} + + (NSString *) dateToString:(NSDate *)date { NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; @@ -74,7 +81,7 @@ (foreignId, link, title, description, releaseDate, category, iconUrl, picCount) \ VALUES \ (?, ?, ?, ?, ?, ?, ?, ?)", - [NSNumber numberWithInt:12], guid, title, description, releaseDate, category, iconUrl, picCount]; + foreignId, 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.h b/Classes/TopicsViewController.h index 78b154f..490046d 100644 --- a/Classes/TopicsViewController.h +++ b/Classes/TopicsViewController.h @@ -14,10 +14,12 @@ #import "PhotoViewController.h" #import "FMDatabase.h" #import "SectionDictionary.h" +#import "Fetcher.h" -@interface TopicsViewController : UIViewController <XMLParserDelegate, SourcesEditViewControllerDelegate, UITableViewDataSource>{ +@interface TopicsViewController : UIViewController <FetcherDelegate, XMLParserDelegate, SourcesEditViewControllerDelegate, UITableViewDataSource>{ UINavigationController *topicsNavigationController; NSMutableArray *topics; // topics loaded from the web + SectionDictionary *tableDictionary; // structure to sync with table PhotoViewController *photoViewController; XMLParser *parser; @@ -38,5 +40,7 @@ - (void)startParsing; - (void)getTopicsFromDb; - (void)addTopicToSectionDictionary:(Topic *)topic; +- (void)loadSubscribedTopics; +- (void)addTopicsFromJson:(NSData *)topics; @end diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 8635e1d..1d93566 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -20,7 +20,8 @@ #import "FMDatabase.h" #import "FMResultSet.h" #import "PicCastAppDelegate.h" - +#import "CJSONDeserializer.h" +#import "Fetcher.h" @implementation TopicsViewController @@ -55,12 +56,12 @@ db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain]; [db setShouldCacheStatements:YES]; - [db setTraceExecution:true]; - [db setLogsErrors:true]; +// [db setTraceExecution:true]; +// [db setLogsErrors:true]; [self getTopicsFromDb]; - //[self startParsing]; + [self loadSubscribedTopics]; } - (void) getTopicsFromDb { @@ -90,32 +91,10 @@ [tableDictionary appendObject:topic forKey:[Topic dateToString:topic.releaseDate]]; } -// called after parsing is finished -- (void) updateTopicsFromWeb:(NSArray *)parsedTopics { - // add topics to sql - [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]; - [topic serializeToDatabase:db]; - [db commit]; - - // add topics to structure - [self addTopicToSectionDictionary:topic]; - } - } - [db close]; - - // tell table to animate things being added - [self.tableView reloadData]; -} - - (void) sourcesEditViewControllerDidFinish:(SourcesEditViewController *)controller { [self dismissModalViewControllerAnimated:YES]; + [self loadSubscribedTopics]; //[self _userIs:[controller getUserGenderString] heets:[controller getHeetGenderString]]; } @@ -176,6 +155,84 @@ } */ +#pragma mark - +#pragma mark custom sheeyit + +- (void) addTopicsFromJson:(NSData *)data { + + NSError *theError = nil; + id dictionary = [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:&theError]; + NSArray *array = [dictionary objectForKey:@"list"]; + + [db open]; + for (NSDictionary *obj in array) { + NSLog(@"foreign key: %d", [obj objectForKey:@"id"]); + FMResultSet *result = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [obj objectForKey:@"id"]]; + if (![result next]) { + NSLog(@"found new topic"); + Topic *topic = [Topic initWithJsonObject:obj]; + + [db beginTransaction]; + [topic serializeToDatabase:db]; + [db commit]; + + [self addTopicToSectionDictionary:topic]; + } + [result close]; + } + [db close]; + + [self.tableView reloadData]; +} + +// called after parsing is finished +- (void) updateTopicsFromWeb:(NSArray *)parsedTopics { + // add topics to sql + [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]; + [topic serializeToDatabase:db]; + [db commit]; + + // add topics to structure + [self addTopicToSectionDictionary:topic]; + } + } + [db close]; + + // tell table to animate things being added + [self.tableView reloadData]; +} + +- (void) loadSubscribedTopics { + [db open]; + FMResultSet *result = [db executeQuery:@"SELECT foreignId FROM subscribedSources WHERE subscribed = ?", [NSNumber numberWithInt:1]]; + while ([result next]) { + + Fetcher *fetcher = [Fetcher initWithString:[NSString + stringWithFormat:@"http://piccast.memeschemes.com/json/sets_by_feed/%d/", + [result intForColumn:@"foreignId"]] + andDelegate:self]; + + } +} + +#pragma mark - +#pragma mark fetcher delegate protocol + +- (void) connection:(NSURLConnection *)connection failedWithError:(NSError *)error { + [PicCastAppDelegate prompt:@"Error" withMessage:[error localizedDescription] andButtonTitle:@"Aww man..." withDelegate:self]; +} + +- (void) connection:(NSURLConnection *)connection finishedWithData:(NSData *)data { + //NSLog(@"%@", [[CJSONDeserializer deserializer] deserialize:(NSData *)data error:nil]); + [self addTopicsFromJson:data]; +} + #pragma mark - #pragma mark Table view data source |