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"
|
2011-07-18 09:56:48 -07:00
|
|
|
#import "FeedTableCell.h"
|
2010-06-20 11:04:23 -04:00
|
|
|
#import "JSON.h"
|
|
|
|
|
2011-07-19 09:38:49 -07:00
|
|
|
#define kTableViewRowHeight 40;
|
|
|
|
|
2010-06-20 11:04:23 -04:00
|
|
|
@implementation NewsBlurViewController
|
2010-06-21 17:17:26 -04:00
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
|
2010-11-15 19:40:17 -05:00
|
|
|
@synthesize responseData;
|
2010-06-20 11:04:23 -04:00
|
|
|
@synthesize viewTableFeedTitles;
|
|
|
|
@synthesize feedViewToolbar;
|
2010-06-21 17:17:26 -04:00
|
|
|
@synthesize feedScoreSlider;
|
2010-11-11 23:48:27 -05:00
|
|
|
@synthesize logoutButton;
|
2010-06-20 11:04:23 -04:00
|
|
|
|
|
|
|
@synthesize feedTitleList;
|
|
|
|
@synthesize dictFolders;
|
2011-06-10 10:40:44 -04:00
|
|
|
@synthesize dictFeeds;
|
2010-06-20 11:04:23 -04:00
|
|
|
@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];
|
2011-06-10 10:40:44 -04:00
|
|
|
self.dictFeeds = [[NSDictionary alloc] init];
|
2010-06-20 11:04:23 -04:00
|
|
|
self.dictFoldersArray = [[NSMutableArray alloc] init];
|
2010-11-15 19:40:17 -05:00
|
|
|
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(doLogoutButton)];
|
2010-11-13 18:32:43 -05:00
|
|
|
[appDelegate showNavigationBar: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];
|
|
|
|
|
2010-11-13 18:32:43 -05:00
|
|
|
[appDelegate showNavigationBar:animated];
|
2010-06-25 18:36:01 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
2010-11-12 10:55:44 -05:00
|
|
|
//[appDelegate showNavigationBar:YES];
|
2010-06-21 17:17:26 -04:00
|
|
|
[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];
|
2011-06-10 10:40:44 -04:00
|
|
|
[dictFeeds release];
|
2010-06-20 11:04:23 -04:00
|
|
|
[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
|
2011-07-18 09:56:48 -07:00
|
|
|
stringWithFormat:@"http://nb.local.host:8000/reader/feeds?flat=true&favicons=true"]];
|
2010-11-13 13:42:20 -05:00
|
|
|
responseData = [[NSMutableData data] retain];
|
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];
|
|
|
|
}
|
|
|
|
|
2010-11-11 23:48:27 -05:00
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
2010-11-13 13:42:20 -05:00
|
|
|
[responseData setLength:0];
|
2010-11-11 23:48:27 -05:00
|
|
|
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
|
|
|
|
int responseStatusCode = [httpResponse statusCode];
|
|
|
|
if (responseStatusCode == 403) {
|
|
|
|
[appDelegate showLogin];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:42:20 -05:00
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
|
2010-11-13 18:32:43 -05:00
|
|
|
//NSLog(@"didReceiveData: %@", data);
|
2010-11-13 13:42:20 -05:00
|
|
|
[responseData appendData:data];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
|
|
|
NSLog(@"%@", [NSString stringWithFormat:@"Connection failed: %@", [error description]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
|
|
|
|
//[connection release];
|
|
|
|
NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
|
|
|
|
[responseData release];
|
|
|
|
if ([jsonString length] > 0) {
|
|
|
|
NSDictionary *results = [[NSDictionary alloc] initWithDictionary:[jsonString JSONValue]];
|
2010-11-13 18:32:43 -05:00
|
|
|
appDelegate.activeUsername = [results objectForKey:@"user"];
|
|
|
|
[appDelegate setTitle:[results objectForKey:@"user"]];
|
2010-11-13 13:42:20 -05:00
|
|
|
self.dictFolders = [results objectForKey:@"flat_folders"];
|
2011-06-10 10:40:44 -04:00
|
|
|
self.dictFeeds = [results objectForKey:@"feeds"];
|
2010-11-13 18:32:43 -05:00
|
|
|
//NSLog(@"Received Feeds: %@", dictFolders);
|
2010-11-13 13:42:20 -05:00
|
|
|
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];
|
2011-06-10 10:40:44 -04:00
|
|
|
// NSArray *folder = [self.dictFolders objectForKey:f];
|
|
|
|
// NSLog(@"F: %@", f);
|
|
|
|
// NSLog(@"F: %@", folder);
|
|
|
|
// NSLog(@"F: %@", sortDescriptors);
|
|
|
|
// sortedArray = [folder sortedArrayUsingDescriptors:sortDescriptors];
|
|
|
|
// [sortedFolders setValue:sortedArray forKey:f];
|
2010-11-13 13:42:20 -05:00
|
|
|
}
|
|
|
|
|
2011-06-10 10:40:44 -04:00
|
|
|
// self.dictFolders = sortedFolders;
|
2010-11-13 13:42:20 -05:00
|
|
|
[self.dictFoldersArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
|
|
|
|
|
|
|
|
[[self viewTableFeedTitles] reloadData];
|
|
|
|
|
|
|
|
[sortedFolders release];
|
|
|
|
[results release];
|
|
|
|
}
|
2011-03-09 18:23:55 -05:00
|
|
|
[jsonString release];
|
2010-11-11 23:48:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (IBAction)doLogoutButton {
|
2010-11-15 19:40:17 -05:00
|
|
|
NSLog(@"Logging out...");
|
2010-11-13 13:42:20 -05:00
|
|
|
NSString *urlS = @"http://nb.local.host:8000/reader/logout?api=1";
|
|
|
|
NSURL *url = [NSURL URLWithString:urlS];
|
|
|
|
NSURLRequest *urlR=[[[NSURLRequest alloc] initWithURL:url] autorelease];
|
2010-11-11 23:48:27 -05:00
|
|
|
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
|
|
|
|
setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
|
2010-11-13 13:42:20 -05:00
|
|
|
LogoutDelegate *ld = [LogoutDelegate alloc];
|
|
|
|
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlR delegate:ld];
|
|
|
|
[urlConnection release];
|
2011-03-09 18:23:55 -05:00
|
|
|
[ld release];
|
2010-11-13 13:42:20 -05:00
|
|
|
}
|
2010-11-11 23:48:27 -05:00
|
|
|
|
2010-06-20 11:04:23 -04:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Table View - Feed List
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
|
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 {
|
2011-07-18 09:56:48 -07:00
|
|
|
static NSString *FeedCellIdentifier = @"FeedCellIdentifier";
|
2010-06-20 11:04:23 -04:00
|
|
|
|
2011-07-19 09:38:49 -07:00
|
|
|
FeedTableCell *cell = (FeedTableCell *)[tableView dequeueReusableCellWithIdentifier:FeedCellIdentifier];
|
2010-06-20 11:04:23 -04:00
|
|
|
if (cell == nil) {
|
2011-07-18 09:56:48 -07:00
|
|
|
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FeedTableCell"
|
2011-07-19 09:38:49 -07:00
|
|
|
owner:nil
|
2011-07-18 09:56:48 -07:00
|
|
|
options:nil];
|
|
|
|
for (id oneObject in nib) {
|
|
|
|
if ([oneObject isKindOfClass:[FeedTableCell class]]) {
|
|
|
|
cell = (FeedTableCell *)oneObject;
|
2011-07-19 09:38:49 -07:00
|
|
|
break;
|
2011-07-18 09:56:48 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-20 11:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int section_index = 0;
|
|
|
|
for (id f in self.dictFoldersArray) {
|
|
|
|
if (section_index == indexPath.section) {
|
|
|
|
NSArray *feeds = [self.dictFolders objectForKey:f];
|
2011-06-10 10:40:44 -04:00
|
|
|
id feed_id = [feeds objectAtIndex:indexPath.row];
|
|
|
|
NSString *feed_id_str = [NSString stringWithFormat:@"%@",feed_id];
|
2011-07-18 09:56:48 -07:00
|
|
|
NSDictionary *feed = [self.dictFeeds objectForKey:feed_id_str];
|
|
|
|
cell.feedTitle.text = [feed objectForKey:@"feed_title"];
|
2011-07-19 09:38:49 -07:00
|
|
|
NSURL *url = [NSURL URLWithString:[feed objectForKey:@"favicon"]];
|
|
|
|
if (url) {
|
|
|
|
NSData *imageData = [NSData dataWithContentsOfURL:url];
|
|
|
|
cell.feedFavicon.image = [UIImage imageWithData:imageData];
|
|
|
|
}
|
|
|
|
[cell.feedUnreadView loadHTMLString:[self showUnreadCount:feed] baseURL:nil];
|
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) {
|
2011-06-10 10:40:44 -04:00
|
|
|
//NSLog(@"Cell: %i: %@", section_index, f);
|
2010-06-21 17:17:26 -04:00
|
|
|
if (section_index == indexPath.section) {
|
2010-06-24 00:22:26 -04:00
|
|
|
NSArray *feeds = [[NSArray alloc] initWithArray:[self.dictFolders objectForKey:f]];
|
2011-06-10 10:40:44 -04:00
|
|
|
id feed_id = [feeds objectAtIndex:indexPath.row];
|
|
|
|
NSString *feed_id_str = [NSString stringWithFormat:@"%@",feed_id];
|
|
|
|
[appDelegate setActiveFeed:[self.dictFeeds
|
|
|
|
objectForKey:feed_id_str]];
|
2010-06-24 00:22:26 -04:00
|
|
|
[feeds release];
|
2010-11-13 18:32:43 -05: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
|
|
|
|
2010-06-25 18:36:01 -04:00
|
|
|
[appDelegate loadFeedDetailView];
|
2010-06-20 11:04:23 -04:00
|
|
|
}
|
|
|
|
|
2011-07-19 09:38:49 -07:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
return kTableViewRowHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)showUnreadCount:(NSDictionary *)feed {
|
|
|
|
NSString *imgCssString = [NSString stringWithFormat:@"<style>"
|
|
|
|
"body {"
|
|
|
|
" line-height: 18px;"
|
|
|
|
" font-size: 13px;"
|
|
|
|
" font-family: 'Lucida Grande',Helvetica, Arial;"
|
|
|
|
" text-rendering: optimizeLegibility;"
|
|
|
|
" margin: 0;"
|
|
|
|
" background-color: white"
|
|
|
|
"}"
|
|
|
|
".NB-count {"
|
|
|
|
" float: right;"
|
|
|
|
" margin: 1px 2px 0 0;"
|
|
|
|
" padding: 3px 4px 2px;"
|
|
|
|
" border: none;"
|
|
|
|
" border-radius: 5px;"
|
|
|
|
"}"
|
|
|
|
".NB-positive {"
|
|
|
|
" color: white;"
|
|
|
|
" background-color: #559F4D;"
|
|
|
|
" background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#559F4D), to(#3B7613));"
|
|
|
|
"}"
|
|
|
|
".NB-neutral {"
|
|
|
|
" background-color: #F9C72A;"
|
|
|
|
" background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#F9C72A), to(#E4AB00));"
|
|
|
|
"}"
|
|
|
|
".NB-negative {"
|
|
|
|
" color: white;"
|
|
|
|
" background-color: #CC2A2E;"
|
|
|
|
" background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#CC2A2E), to(#9B181B));"
|
|
|
|
"}"
|
|
|
|
"</style>"];
|
|
|
|
|
|
|
|
int negativeCount = (int)[feed objectForKey:@"ng"];
|
|
|
|
int neutralCount = (int)[feed objectForKey:@"nt"];
|
|
|
|
int positiveCount = (int)[feed objectForKey:@"ps"];
|
|
|
|
|
|
|
|
NSString *negativeCountString = [NSString stringWithFormat:@"<div class=\"NB-count NB-negative\">%@</div>",
|
|
|
|
[feed objectForKey:@"ng"]];
|
|
|
|
NSString *neutralCountString = [NSString stringWithFormat:@"<div class=\"NB-count NB-neutral\">%@</div>",
|
|
|
|
[feed objectForKey:@"nt"]];
|
|
|
|
NSString *positiveCountString = [NSString stringWithFormat:@"<div class=\"NB-count NB-positive\">%@</div>",
|
|
|
|
[feed objectForKey:@"ps"]];
|
|
|
|
NSLog(@"%@", [[feed objectForKey:@"ps"] class]);
|
|
|
|
NSString *htmlString = [NSString stringWithFormat:@"%@ %@ %@ %@",
|
|
|
|
imgCssString,
|
|
|
|
positiveCount ? positiveCountString : @"",
|
|
|
|
neutralCount ? neutralCountString : @"",
|
|
|
|
negativeCount ? negativeCountString : @""];
|
|
|
|
|
|
|
|
return htmlString;
|
|
|
|
}
|
|
|
|
|
2010-06-20 11:04:23 -04:00
|
|
|
@end
|
2010-11-13 13:42:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
@implementation LogoutDelegate
|
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
|
|
|
|
|
|
|
}
|
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
|
2010-11-15 19:40:17 -05:00
|
|
|
appDelegate = [[UIApplication sharedApplication] delegate];
|
2010-11-13 13:42:20 -05:00
|
|
|
NSLog(@"Logout: %@", appDelegate);
|
|
|
|
[appDelegate reloadFeedsView];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|