diff options
author | matt handler <matt.handler@gmail.com> | 2011-04-27 18:45:32 -0400 |
---|---|---|
committer | matt handler <matt.handler@gmail.com> | 2011-04-27 18:45:32 -0400 |
commit | e788dbb246a2c9673d595420beca061123526d50 (patch) | |
tree | 70fbbee15ffdaef04c6b7afee40c2964e41fbcac /Classes | |
parent | e6eb634beac831e64becc03928a467701f9625cf (diff) | |
download | piccast-app-e788dbb246a2c9673d595420beca061123526d50.tar.gz piccast-app-e788dbb246a2c9673d595420beca061123526d50.zip |
sorting automatically, load more button, remove some logging, customizing things?
Diffstat (limited to 'Classes')
-rw-r--r-- | Classes/PhotoViewController.m | 4 | ||||
-rw-r--r-- | Classes/SectionDictionary.h | 3 | ||||
-rw-r--r-- | Classes/SectionDictionary.m | 33 | ||||
-rw-r--r-- | Classes/TopicsViewController.m | 276 |
4 files changed, 207 insertions, 109 deletions
diff --git a/Classes/PhotoViewController.m b/Classes/PhotoViewController.m index 850d425..35e58ca 100644 --- a/Classes/PhotoViewController.m +++ b/Classes/PhotoViewController.m @@ -58,7 +58,7 @@ @implementation TTThumbsViewController (test) - (TTPhotoViewController*)createPhotoViewController { - NSLog(@"test"); + //NSLog(@"test"); return (TTPhotoViewController *)[[[MyPhotoViewController alloc] init] autorelease]; } @end @@ -92,7 +92,7 @@ NSError *theError = nil; id dictionary = [[CJSONDeserializer deserializer] deserialize:(NSData *)_receivedData error:&theError]; - NSLog(@"%@", dictionary); + //NSLog(@"%@", dictionary); NSArray *array = [dictionary objectForKey:@"list"]; for (NSDictionary *obj in array) { diff --git a/Classes/SectionDictionary.h b/Classes/SectionDictionary.h index f227e44..b4dd6b0 100644 --- a/Classes/SectionDictionary.h +++ b/Classes/SectionDictionary.h @@ -12,12 +12,15 @@ @interface SectionDictionary : NSObject { NSInteger index; NSMutableDictionary *indexMap; + NSMutableArray *sortedKeys; NSMutableDictionary *theRealDictionary; } - (void) appendObject:(id)anObject forKey:(id)aKey; - (void) setObject:(id)anObject forKey:(id)aKey; - (id) objectForIndex:(NSInteger)i; +- (void) sortKeys; - (NSString *) keyForIndex:(NSInteger)i; +- (NSArray *) allIndices; @end diff --git a/Classes/SectionDictionary.m b/Classes/SectionDictionary.m index b183725..9124714 100644 --- a/Classes/SectionDictionary.m +++ b/Classes/SectionDictionary.m @@ -15,6 +15,7 @@ - (SectionDictionary *) init { index = 0; + sortedKeys = [[[NSMutableArray alloc] init] retain]; indexMap = [[[NSMutableDictionary alloc] init] retain]; theRealDictionary = [[[NSMutableDictionary alloc] init] retain]; [super init]; @@ -27,22 +28,39 @@ return theRealDictionary; } +- (void) sortKeys { + [sortedKeys sortUsingComparator:(NSComparator)^(id obj1, id obj2){ + return [obj2 compare:obj1]; + }]; +} + - (void) setObject:(id)anObject forKey:(id)aKey { - [indexMap setObject:aKey forKey:[NSString stringWithFormat:@"%d", index]]; - index++; +// [indexMap setObject:aKey forKey:[NSString stringWithFormat:@"%d", index]]; +// index++; [theRealDictionary setObject:anObject forKey:aKey]; + + [sortedKeys addObject:aKey]; + [self sortKeys]; +} + +- (NSArray *) allIndices { + NSLog(@"%@ %@", [indexMap allKeys], [indexMap allValues]); + return [indexMap allKeys]; } // lets you push objects into sections without worry about storage - (void) appendObject:(id)anObject forKey:(id)aKey { NSMutableArray *store = [theRealDictionary objectForKey:aKey]; if (store == nil) { - [indexMap setObject:aKey forKey:[NSString stringWithFormat:@"%d", index]]; - index++; +// [indexMap setObject:aKey forKey:[NSString stringWithFormat:@"%d", index]]; +// index++; NSMutableArray *store = [[[NSMutableArray alloc] init] autorelease]; [store addObject:anObject]; [theRealDictionary setObject:store forKey:aKey]; + + [sortedKeys addObject:aKey]; + [self sortKeys]; } else [store addObject:anObject]; @@ -50,14 +68,17 @@ } - (id) objectForIndex:(NSInteger)i { - return [theRealDictionary objectForKey:[indexMap objectForKey:[NSString stringWithFormat:@"%d", i]]]; + return [theRealDictionary objectForKey:[sortedKeys objectAtIndex:i]]; + //return [theRealDictionary objectForKey:[indexMap objectForKey:[NSString stringWithFormat:@"%d", i]]]; } - (NSString *) keyForIndex:(NSInteger)i { - return [indexMap objectForKey:[NSString stringWithFormat:@"%d", i]]; + return [sortedKeys objectAtIndex:i]; + //return [indexMap objectForKey:[NSString stringWithFormat:@"%d", i]]; } - (void) dealloc { + [sortedKeys release]; [theRealDictionary release]; [indexMap release]; [super dealloc]; diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 31191ee..3ad8d95 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -31,6 +31,8 @@ [super viewDidLoad]; NSLog(@"viewDidLoad"); self.tableView.rowHeight = 100; + [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; + [self.tableView setSeparatorColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]]; objMan = [[HJObjManager alloc] initWithLoadingBufferSize:6 memCacheSize:20]; @@ -74,14 +76,12 @@ } - (void) addTopicToSectionDictionary:(Topic *)topic { - //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]]; + NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; + [dateFormatter setDateStyle:NSDateFormatterLongStyle]; + [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; + [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]]; + + [tableDictionary appendObject:topic forKey:[dateFormatter dateFromString:[dateFormatter stringFromDate:topic.releaseDate]]]; } - (void) sourcesEditViewControllerDidFinish:(SourcesEditViewController *)controller { @@ -124,52 +124,58 @@ NSLog(@"found new topic"); Topic *topic = [Topic initWithJsonObject:obj]; topic.sourceId = source.foreignId; - - if ([topic.picCount intValue] == 0) break; - - [db beginTransaction]; - [topic serializeToDatabase:db]; - [db commit]; - - [self addTopicToSectionDictionary:topic]; + NSLog(@"new topic on: %@", [Topic dateToString:topic.releaseDate]); + + if ([topic.picCount intValue] != 0) { + [db beginTransaction]; + [topic serializeToDatabase:db]; + [db commit]; + + [self addTopicToSectionDictionary:topic]; + } } [result close]; } [db close]; - [self.tableView reloadData]; + [self restructureSections]; } -- (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) { - FMResultSet *result = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [obj objectForKey:@"id"]]; - if (![result next]) { - Topic *topic = [Topic initWithJsonObject:obj]; - - if (topic.picCount == 0) break; - - [db beginTransaction]; - [topic serializeToDatabase:db]; - [db commit]; - - [self addTopicToSectionDictionary:topic]; - } - [result close]; - } - [db close]; +- (void) restructureSections { [self.tableView reloadData]; } +// +//- (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) { +// FMResultSet *result = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [obj objectForKey:@"id"]]; +// if (![result next]) { +// Topic *topic = [Topic initWithJsonObject:obj]; +// +// if (topic.picCount == 0) break; +// NSLog(@"new topic on: %@", [Topic dateToString:topic.releaseDate]); +// [db beginTransaction]; +// [topic serializeToDatabase:db]; +// [db commit]; +// +// [self addTopicToSectionDictionary:topic]; +// } +// [result close]; +// } +// [db close]; +// +// [self.tableView reloadData]; +//} - (void) loadSubscribedTopics { - NSLog(@"load subscribed"); + //NSLog(@"load subscribed"); [db open]; FMResultSet *result = [db executeQuery:@"SELECT * FROM subscribedSources WHERE subscribed = ?", [NSNumber numberWithInt:1]]; while ([result next]) { @@ -237,12 +243,39 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. //NSLog(@"number of rows in section %d: %d", section, [[tableDictionary objectForIndex:section] count]); - return [[tableDictionary objectForIndex:section] count]; + //NSLog(@"section: %d count: %d", section, [tableDictionary count]); + if (section + 1 == ([tableDictionary count])) + return [[tableDictionary objectForIndex:section] count] + 1; + else + return [[tableDictionary objectForIndex:section] count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { //NSLog(@"row title: %@", [tableDictionary keyForIndex:section]); - return [tableDictionary keyForIndex:section]; //how do you convert a key to index from mutabledictionary + //return [tableDictionary keyForIndex:section]; //how do you convert a key to index from mutabledictionary + NSDate *date = [NSDate new]; + NSString *key = [Topic dateToString:[tableDictionary keyForIndex:section]]; + NSString *today = [Topic dateToString:date]; + NSString *yesterday = [Topic dateToString:[NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)-86400]]; + //NSLog(@"%@ %@ %@ %@", date, key, today, yesterday); + //NSLog(@"%@", [tableDictionary allIndices], [tableDictionary ); + //NSLog(@"%@", [tableDictionary allKeys]); + //[tableDictionary allIndices]; + if ([key compare:today] == 0) + return @"Today"; + else if ([key compare:yesterday] == 0) + return @"Yesterday"; + else + return key; + + // +// 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]]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { @@ -259,76 +292,117 @@ } } -//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath -//{ -// return 100; -//} +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (indexPath.section + 1 == [tableDictionary count] && + indexPath.row == [[tableDictionary objectForIndex:indexPath.section] count]) + return 44; + else + return 100; +} - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { - - static NSString *kCellIdentifier = @"TopicTableViewCell"; - TopicTableViewCell *cell = (TopicTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; - - HJManagedImageV* mi; - HJManagedImageV* micon; - - if (cell == nil) { - NSArray *tmpCell = [[NSBundle mainBundle] loadNibNamed:@"TopicTableViewCell" owner:self options:nil]; + //NSLog(@"count: %d section: %d", [tableDictionary count], indexPath.section + 1); + if ((indexPath.section + 1) == [tableDictionary count] && + indexPath.row == [[tableDictionary objectForIndex:indexPath.section] count]) { - for (id currentObj in tmpCell) { - if ([currentObj isKindOfClass:[TopicTableViewCell class]]) { - cell = (TopicTableViewCell *)currentObj; - break; - } + static NSString *cellIdentifier = @"moreCell"; + UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; + + if (cell == nil) { + cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault + reuseIdentifier:cellIdentifier] autorelease]; } - micon = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(105,72,16,16)] autorelease]; - mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(3,3,94,94)] autorelease]; - mi.tag = 998; - mi.tag = 999; - [cell addSubview:micon]; - [cell addSubview:mi]; + [cell.textLabel setText:@"load older PicCasts..."]; + [cell.textLabel setTextAlignment:UITextAlignmentCenter]; + [cell.textLabel setBackgroundColor:[UIColor clearColor]]; + //[cell.textLabel setTextColor:[UIColor colorWithRed:0.05 green:0.05 blue:0.05 alpha:0.05]]; - } + UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; + myBackView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0]; + cell.backgroundView = myBackView; + + cell.layer.shadowOffset = CGSizeMake(1, 0); + cell.layer.shadowColor = [[UIColor blackColor] CGColor]; + cell.layer.shadowRadius = 5; + cell.layer.shadowOpacity = .75; + + CGRect shadowFrame = cell.layer.bounds; + CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath; + cell.layer.shadowPath = shadowPath; + + return cell; + } else { - //Get a reference to the managed image view that was already in the recycled cell, and clear it - micon = (HJManagedImageV*)[cell viewWithTag:998]; - mi = (HJManagedImageV*)[cell viewWithTag:999]; - [micon clear]; - [mi clear]; - } - - Topic *topic = [[tableDictionary objectForIndex:[indexPath section]] objectAtIndex:[indexPath row]]; - //cell.useDarkBackground = (indexPath.row % 2 == 0); - cell.title = topic.title; - [cell.categoryLabel setText:topic.category]; - - cell.picCount = [NSString stringWithFormat:@"%@", topic.picCount]; - - micon.url = [NSURL URLWithString:[[sourcesDictionary objectForKey:topic.sourceId] favicon]]; - mi.url = [NSURL URLWithString:topic.iconUrl]; - mi.squareCropped = true; - - [objMan manage:mi]; - [objMan manage:micon]; - + static NSString *kCellIdentifier = @"TopicTableViewCell"; + TopicTableViewCell *cell = (TopicTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; + + HJManagedImageV* mi; + HJManagedImageV* micon; + + if (cell == nil) { + NSArray *tmpCell = [[NSBundle mainBundle] loadNibNamed:@"TopicTableViewCell" owner:self options:nil]; + + for (id currentObj in tmpCell) { + if ([currentObj isKindOfClass:[TopicTableViewCell class]]) { + cell = (TopicTableViewCell *)currentObj; + break; + } + } + + micon = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(105,72,16,16)] autorelease]; + mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(3,3,94,94)] autorelease]; + mi.tag = 998; + mi.tag = 999; + [cell.contentView addSubview:micon]; + [cell.contentView addSubview:mi]; + + } + else { + //Get a reference to the managed image view that was already in the recycled cell, and clear it + micon = (HJManagedImageV*)[cell viewWithTag:998]; + mi = (HJManagedImageV*)[cell viewWithTag:999]; + [micon clear]; + [mi clear]; + } + + Topic *topic = [[tableDictionary objectForIndex:[indexPath section]] objectAtIndex:[indexPath row]]; + //cell.useDarkBackground = (indexPath.row % 2 == 0); + cell.title = topic.title; + [cell.categoryLabel setText:topic.category]; + + cell.picCount = [NSString stringWithFormat:@"%@", topic.picCount]; + + micon.url = [NSURL URLWithString:[[sourcesDictionary objectForKey:topic.sourceId] favicon]]; + mi.url = [NSURL URLWithString:topic.iconUrl]; + mi.squareCropped = true; + + [objMan manage:mi]; + [objMan manage:micon]; + + return cell; + } - return cell; } - (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // self.picDumpViewController.topic = [topics objectAtIndex:indexPath.row]; - self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" - style:UIBarButtonItemStyleBordered - target:nil - action:nil]; - // [self.navigationController pushViewController:picDumpViewController animated:YES]; - // - photoViewController = [[[PhotoViewController alloc] init] autorelease]; - photoViewController.topic = [[tableDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row]; - [self.navigationController pushViewController:photoViewController animated:YES]; - //[application setStatusBarStyle:UIStatusBarStyleBlackOpaque]; - + if (indexPath.section + 1 == [tableDictionary count] && + indexPath.row == [[tableDictionary objectForIndex:indexPath.section] count]) { + NSLog(@"load more from database"); + } + else { + self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" + style:UIBarButtonItemStyleBordered + target:nil + action:nil]; + + photoViewController = [[[PhotoViewController alloc] init] autorelease]; + photoViewController.topic = [[tableDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row]; + [self.navigationController pushViewController:photoViewController animated:YES]; + //[application setStatusBarStyle:UIStatusBarStyleBlackOpaque]; + } } #pragma mark - |