2010-06-20 11:04:23 -04:00
|
|
|
//
|
|
|
|
// NewsBlurViewController.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 6/16/10.
|
|
|
|
// Copyright NewsBlur 2010. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "NewsBlurViewController.h"
|
2010-06-21 17:17:26 -04:00
|
|
|
#import "NewsBlurAppDelegate.h"
|
2010-06-20 11:04:23 -04:00
|
|
|
#import "JSON.h"
|
|
|
|
|
|
|
|
@implementation NewsBlurViewController
|
2010-06-21 17:17:26 -04:00
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
|
2010-06-20 11:04:23 -04:00
|
|
|
@synthesize viewTableFeedTitles;
|
|
|
|
@synthesize feedViewToolbar;
|
2010-06-21 17:17:26 -04:00
|
|
|
@synthesize feedScoreSlider;
|
2010-06-20 11:04:23 -04:00
|
|
|
|
|
|
|
@synthesize feedTitleList;
|
|
|
|
@synthesize dictFolders;
|
|
|
|
@synthesize dictFoldersArray;
|
|
|
|
|
2010-06-21 17:17:26 -04:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Globals
|
|
|
|
|
2010-06-20 11:04:23 -04:00
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
|
2010-06-21 17:17:26 -04:00
|
|
|
|
|
|
|
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
|
|
|
|
[appDelegate hideNavigationBar:NO];
|
2010-06-20 11:04:23 -04:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
self.feedTitleList = [[NSMutableArray alloc] init];
|
|
|
|
self.dictFolders = [[NSDictionary alloc] init];
|
|
|
|
self.dictFoldersArray = [[NSMutableArray alloc] init];
|
2010-06-21 17:17:26 -04:00
|
|
|
[appDelegate hideNavigationBar:NO];
|
2010-06-20 11:04:23 -04:00
|
|
|
[super viewDidLoad];
|
|
|
|
}
|
|
|
|
|
2010-06-25 18:36:01 -04:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
[viewTableFeedTitles deselectRowAtIndexPath:[viewTableFeedTitles indexPathForSelectedRow] animated:animated];
|
|
|
|
|
|
|
|
[appDelegate hideNavigationBar:animated];
|
|
|
|
}
|
|
|
|
|
2010-06-21 17:17:26 -04:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
2010-06-25 18:36:01 -04:00
|
|
|
appDelegate.activeFeed = nil;
|
2010-06-21 17:17:26 -04:00
|
|
|
[super viewDidAppear:animated];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
[appDelegate showNavigationBar:YES];
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
}
|
2010-06-20 11:04:23 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
// Override to allow orientations other than the default portrait orientation.
|
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
|
|
// Return YES for supported orientations
|
|
|
|
return (interfaceOrientation == UIInterfaceOrientationPortrait);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning {
|
|
|
|
// Releases the view if it doesn't have a superview.
|
|
|
|
[super didReceiveMemoryWarning];
|
|
|
|
|
|
|
|
// Release any cached data, images, etc that aren't in use.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidUnload {
|
|
|
|
// Release any retained subviews of the main view.
|
|
|
|
// e.g. self.myOutlet = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[feedTitleList release];
|
|
|
|
[dictFolders release];
|
|
|
|
[dictFoldersArray release];
|
2010-06-21 17:17:26 -04:00
|
|
|
[appDelegate release];
|
2010-06-20 11:04:23 -04:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Initialization
|
|
|
|
|
|
|
|
- (void)fetchFeedList {
|
|
|
|
NSURL *urlFeedList = [NSURL URLWithString:[NSString
|
2010-11-11 20:05:53 -05:00
|
|
|
stringWithFormat:@"http://nb.local.host:8000/reader/load_feeds_iphone/"]];
|
2010-06-20 11:04:23 -04:00
|
|
|
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: urlFeedList];
|
|
|
|
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
|
|
|
|
[connection release];
|
|
|
|
[request release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
|
|
|
|
{
|
|
|
|
|
|
|
|
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
2010-06-24 00:22:26 -04:00
|
|
|
NSDictionary *results = [[NSDictionary alloc] initWithDictionary:[jsonString JSONValue]];
|
2010-06-20 11:04:23 -04:00
|
|
|
self.dictFolders = [results objectForKey:@"flat_folders"];
|
|
|
|
|
|
|
|
NSSortDescriptor *sortDescriptor;
|
|
|
|
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"feed_title"
|
|
|
|
ascending:YES] autorelease];
|
|
|
|
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
|
|
|
|
NSMutableDictionary *sortedFolders = [[NSMutableDictionary alloc] init];
|
|
|
|
NSArray *sortedArray;
|
|
|
|
|
|
|
|
for (id f in self.dictFolders) {
|
|
|
|
[self.dictFoldersArray addObject:f];
|
|
|
|
|
|
|
|
sortedArray = [[self.dictFolders objectForKey:f] sortedArrayUsingDescriptors:sortDescriptors];
|
|
|
|
[sortedFolders setValue:sortedArray forKey:f];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.dictFolders = sortedFolders;
|
|
|
|
[self.dictFoldersArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
|
|
|
|
|
|
|
|
[[self viewTableFeedTitles] reloadData];
|
|
|
|
|
|
|
|
[sortedFolders release];
|
2010-06-24 00:22:26 -04:00
|
|
|
[results release];
|
2010-06-20 11:04:23 -04:00
|
|
|
[jsonString release];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Table View - Feed List
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
|
// NSInteger count = [self.dictFolders count];
|
|
|
|
// NSLog(@"Folders: %d: %@", count, self.dictFolders);
|
|
|
|
return [self.dictFoldersArray count];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
|
|
|
int index = 0;
|
|
|
|
for (id f in self.dictFoldersArray) {
|
|
|
|
if (index == section) {
|
|
|
|
// NSLog(@"Computing Table view header: %i: %@", index, f);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
int index = 0;
|
|
|
|
for (id f in self.dictFoldersArray) {
|
|
|
|
if (index == section) {
|
|
|
|
// NSLog(@"Computing Table view rows: %i: %@", index, f);
|
|
|
|
NSArray *feeds = [self.dictFolders objectForKey:f];
|
|
|
|
//NSLog(@"Table view items: %i: %@", [feeds count], f);
|
|
|
|
return [feeds count];
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
|
|
|
|
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
|
|
|
|
if (cell == nil) {
|
|
|
|
cell = [[[UITableViewCell alloc]
|
|
|
|
initWithStyle:UITableViewCellStyleDefault
|
|
|
|
reuseIdentifier:SimpleTableIdentifier] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
int section_index = 0;
|
|
|
|
for (id f in self.dictFoldersArray) {
|
|
|
|
// NSLog(@"Cell: %i: %@", section_index, f);
|
|
|
|
if (section_index == indexPath.section) {
|
|
|
|
NSArray *feeds = [self.dictFolders objectForKey:f];
|
|
|
|
// NSLog(@"Cell: %i: %@: %@", section_index, f, [feeds objectAtIndex:indexPath.row]);
|
2010-06-21 17:17:26 -04:00
|
|
|
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row]
|
|
|
|
objectForKey:@"feed_title"];
|
2010-06-20 11:04:23 -04:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
section_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
2010-06-21 17:17:26 -04:00
|
|
|
int section_index = 0;
|
|
|
|
for (id f in self.dictFoldersArray) {
|
|
|
|
// NSLog(@"Cell: %i: %@", section_index, f);
|
|
|
|
if (section_index == indexPath.section) {
|
2010-06-24 00:22:26 -04:00
|
|
|
NSArray *feeds = [[NSArray alloc] initWithArray:[self.dictFolders objectForKey:f]];
|
2010-06-25 18:36:01 -04:00
|
|
|
[appDelegate setActiveFeed:[feeds objectAtIndex:indexPath.row]];
|
2010-06-24 00:22:26 -04:00
|
|
|
[feeds release];
|
2010-06-25 18:36:01 -04:00
|
|
|
NSLog(@"Active feed: %@", [appDelegate activeFeed]);
|
2010-06-21 17:17:26 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
section_index++;
|
|
|
|
}
|
2010-06-24 12:53:50 -04:00
|
|
|
//NSLog(@"App Delegate: %@", self.appDelegate);
|
2010-06-27 19:35:17 -04:00
|
|
|
|
|
|
|
UILabel *label = [[UILabel alloc] init];
|
|
|
|
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
|
|
|
|
[label setBackgroundColor:[UIColor clearColor]];
|
|
|
|
[label setTextColor:[UIColor whiteColor]];
|
|
|
|
[label setText:[appDelegate.activeFeed objectForKey:@"feed_title"]];
|
|
|
|
[label sizeToFit];
|
|
|
|
[appDelegate.navigationController.navigationBar.topItem setTitleView:label];
|
|
|
|
appDelegate.navigationController.navigationBar.backItem.title = @"All";
|
|
|
|
[label release];
|
|
|
|
appDelegate.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.8];
|
|
|
|
|
2010-06-25 18:36:01 -04:00
|
|
|
[appDelegate loadFeedDetailView];
|
2010-06-20 11:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|