summaryrefslogtreecommitdiffstats
path: root/Classes/TopicsViewController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/TopicsViewController.m')
-rw-r--r--Classes/TopicsViewController.m276
1 files changed, 175 insertions, 101 deletions
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 -