NewsBlur/media/iphone/Classes/NewsBlurViewController.m

800 lines
32 KiB
Mathematica
Raw Normal View History

//
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"
#import "NewsBlurAppDelegate.h"
#import "FeedTableCell.h"
#import "ASIHTTPRequest.h"
#import "PullToRefreshView.h"
#import "MBProgressHUD.h"
#import "Base64.h"
2010-06-20 11:04:23 -04:00
#import "JSON.h"
#import "Utilities.h"
2010-06-20 11:04:23 -04:00
#define kTableViewRowHeight 40;
2010-06-20 11:04:23 -04:00
@implementation NewsBlurViewController
@synthesize appDelegate;
@synthesize feedTitlesTable;
2010-06-20 11:04:23 -04:00
@synthesize feedViewToolbar;
@synthesize feedScoreSlider;
@synthesize logoutButton;
@synthesize intelligenceControl;
@synthesize activeFeedLocations;
@synthesize visibleFeeds;
@synthesize stillVisibleFeeds;
@synthesize sitesButton;
@synthesize addButton;
@synthesize viewShowingAllFeeds;
@synthesize pull;
@synthesize lastUpdate;
2011-10-14 09:51:27 -07:00
@synthesize imageCache;
2010-06-20 11:04:23 -04:00
#pragma mark -
2011-08-24 21:44:16 -07:00
#pragma mark Globals
2010-06-20 11:04:23 -04:00
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
2011-08-24 21:44:16 -07:00
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
2011-08-24 21:44:16 -07:00
[appDelegate hideNavigationBar:NO];
2010-06-20 11:04:23 -04:00
}
return self;
}
- (void)viewDidLoad {
2011-08-24 21:44:16 -07:00
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(doLogoutButton)] autorelease];
[appDelegate showNavigationBar:NO];
self.viewShowingAllFeeds = NO;
pull = [[PullToRefreshView alloc] initWithScrollView:self.feedTitlesTable];
[pull setDelegate:self];
[self.feedTitlesTable addSubview:pull];
2011-08-24 21:44:16 -07:00
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(returnToApp)
name:UIApplicationWillEnterForegroundNotification
object:nil];
2011-08-24 21:44:16 -07:00
2011-10-14 09:51:27 -07:00
imageCache = [[NSCache alloc] init];
[imageCache setDelegate:self];
[addButton setWidth:40];
2010-06-20 11:04:23 -04:00
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
// If there is an active feed or a set of feeds readin the river,
// we need to update its table row to match the updated unread counts.
if (appDelegate.activeFeed || appDelegate.isRiverView) {
[self.feedTitlesTable beginUpdates];
[self.feedTitlesTable
reloadRowsAtIndexPaths:[self.feedTitlesTable indexPathsForVisibleRows]
2011-08-24 21:44:16 -07:00
withRowAnimation:UITableViewRowAnimationNone];
[self.feedTitlesTable endUpdates];
2011-08-24 21:44:16 -07:00
NSInteger previousLevel = [self.intelligenceControl selectedSegmentIndex] - 1;
NSInteger newLevel = [appDelegate selectedIntelligence];
if (newLevel != previousLevel) {
[appDelegate setSelectedIntelligence:newLevel];
if (!self.viewShowingAllFeeds) {
[self updateFeedsWithIntelligence:previousLevel newLevel:newLevel];
}
[self redrawUnreadCounts];
2011-08-24 21:44:16 -07:00
}
}
[self.intelligenceControl setImage:[UIImage imageNamed:@"bullet_red.png"]
forSegmentAtIndex:0];
[self.intelligenceControl setImage:[UIImage imageNamed:@"bullet_yellow.png"]
forSegmentAtIndex:1];
[self.intelligenceControl setImage:[UIImage imageNamed:@"bullet_green.png"]
forSegmentAtIndex:2];
[self.intelligenceControl addTarget:self
2011-08-24 21:44:16 -07:00
action:@selector(selectIntelligence)
forControlEvents:UIControlEventValueChanged];
[self.intelligenceControl
2011-08-24 21:44:16 -07:00
setSelectedSegmentIndex:[appDelegate selectedIntelligence]+1];
[appDelegate showNavigationBar:animated];
[self.feedTitlesTable selectRowAtIndexPath:[feedTitlesTable indexPathForSelectedRow]
animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
- (void)viewDidAppear:(BOOL)animated {
// appDelegate.activeFeed = nil;
2011-08-24 21:44:16 -07:00
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
//[appDelegate showNavigationBar:YES];
[super viewWillDisappear:animated];
}
2010-06-20 11:04:23 -04:00
/*
2011-08-24 21:44:16 -07:00
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
2010-06-20 11:04:23 -04:00
- (void)didReceiveMemoryWarning {
2011-08-24 21:44:16 -07:00
// Releases the view if it doesn't have a superview.
2010-06-20 11:04:23 -04:00
[super didReceiveMemoryWarning];
2011-08-24 21:44:16 -07:00
// Release any cached data, images, etc that aren't in use.
2010-06-20 11:04:23 -04:00
}
- (void)viewDidUnload {
2011-08-24 21:44:16 -07:00
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
2010-06-20 11:04:23 -04:00
}
2011-08-24 21:44:16 -07:00
- (void)dealloc {
[appDelegate release];
[feedTitlesTable release];
[feedViewToolbar release];
[feedScoreSlider release];
[logoutButton release];
[intelligenceControl release];
2011-08-24 21:44:16 -07:00
[activeFeedLocations release];
[visibleFeeds release];
[stillVisibleFeeds release];
2011-08-24 21:44:16 -07:00
[sitesButton release];
[addButton release];
2011-08-24 21:44:16 -07:00
[pull release];
[lastUpdate release];
2011-10-14 09:51:27 -07:00
[imageCache release];
2011-08-24 21:44:16 -07:00
2010-06-20 11:04:23 -04:00
[super dealloc];
}
#pragma mark -
#pragma mark Initialization
- (void)returnToApp {
NSDate *decayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:(BACKGROUND_REFRESH_SECONDS)];
2011-08-24 21:44:16 -07:00
NSLog(@"Last Update: %@ - %f", self.lastUpdate, [self.lastUpdate timeIntervalSinceDate:decayDate]);
if ([self.lastUpdate timeIntervalSinceDate:decayDate] < 0) {
[self fetchFeedList:YES];
2011-08-24 21:44:16 -07:00
}
[decayDate release];
}
- (void)fetchFeedList:(BOOL)showLoader {
// NSLog(@"fetchFeedList: %d %d %@", showLoader, appDelegate.navigationController.topViewController == appDelegate.feedsViewController, [appDelegate activeFeed]);
if (showLoader && appDelegate.navigationController.topViewController == appDelegate.feedsViewController) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
2011-08-24 21:44:16 -07:00
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = @"On its way...";
}
2011-10-14 17:15:48 -07:00
2011-08-24 21:44:16 -07:00
NSURL *urlFeedList = [NSURL URLWithString:
[NSString stringWithFormat:@"http://%@/reader/feeds?flat=true",
NEWSBLUR_URL]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlFeedList];
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
[request setDelegate:self];
[request setResponseEncoding:NSUTF8StringEncoding];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setDidFinishSelector:@selector(finishLoadingFeedList:)];
[request setDidFailSelector:@selector(finishedWithError:)];
[request setTimeOutSeconds:30];
[request startAsynchronous];
2011-08-24 21:44:16 -07:00
self.lastUpdate = [NSDate date];
2010-06-20 11:04:23 -04:00
}
2011-10-14 17:15:48 -07:00
- (void)finishedWithError:(ASIHTTPRequest *)request {
2011-08-24 21:44:16 -07:00
[MBProgressHUD hideHUDForView:self.view animated:YES];
[pull finishedLoading];
// User clicking on another link before the page loads is OK.
[self informError:[request error]];
}
- (void)finishLoadingFeedList:(ASIHTTPRequest *)request {
if ([request responseStatusCode] == 403) {
return [appDelegate showLogin];
} else if ([request responseStatusCode] >= 500) {
[pull finishedLoading];
return [self informError:@"The server barfed!"];
}
NSString *responseString = [request responseString];
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
2011-08-24 21:44:16 -07:00
[MBProgressHUD hideHUDForView:self.view animated:YES];
self.stillVisibleFeeds = [NSMutableDictionary dictionary];
self.visibleFeeds = [NSMutableDictionary dictionary];
[pull finishedLoading];
[self loadFavicons];
appDelegate.activeUsername = [results objectForKey:@"user"];
if (appDelegate.feedsViewController.view.window) {
[appDelegate setTitle:[results objectForKey:@"user"]];
2011-08-24 21:44:16 -07:00
}
appDelegate.dictFolders = [results objectForKey:@"flat_folders"];
appDelegate.dictFeeds = [results objectForKey:@"feeds"];
NSMutableDictionary *sortedFolders = [[NSMutableDictionary alloc] init];
2011-10-14 17:15:48 -07:00
NSArray *sortedArray;
appDelegate.dictFoldersArray = [NSMutableArray array];
for (id f in appDelegate.dictFolders) {
[appDelegate.dictFoldersArray addObject:f];
2011-10-14 17:15:48 -07:00
NSArray *folder = [appDelegate.dictFolders objectForKey:f];
sortedArray = [folder sortedArrayUsingComparator:^NSComparisonResult(id id1, id id2) {
NSString *feedTitleA = [[appDelegate.dictFeeds
objectForKey:[NSString stringWithFormat:@"%@", id1]]
objectForKey:@"feed_title"];
NSString *feedTitleB = [[appDelegate.dictFeeds
objectForKey:[NSString stringWithFormat:@"%@", id2]]
objectForKey:@"feed_title"];
return [feedTitleA caseInsensitiveCompare:feedTitleB];
2011-10-14 17:15:48 -07:00
}];
[sortedFolders setValue:sortedArray forKey:f];
}
2011-10-14 17:15:48 -07:00
appDelegate.dictFolders = sortedFolders;
[appDelegate.dictFoldersArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"Folders: %@, Array: %@", appDelegate.dictFolders, appDelegate.dictFoldersArray);
[self calculateFeedLocations:YES];
[self.feedTitlesTable reloadData];
NSString *serveriPhoneVersion = [results objectForKey:@"iphone_version"];
NSString *currentiPhoneVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
if (![currentiPhoneVersion isEqualToString:serveriPhoneVersion]) {
NSLog(@"Version: %@ - %@", serveriPhoneVersion, currentiPhoneVersion);
NSString *title = [NSString stringWithFormat:@"You should download the new version of NewsBlur.\n\nNew version: v%@.\nYou have: v%@.", serveriPhoneVersion, currentiPhoneVersion];
UIAlertView *upgradeConfirm = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Upgrade!", nil];
[upgradeConfirm show];
[upgradeConfirm setTag:2];
[upgradeConfirm release];
}
[sortedFolders release];
[results release];
}
- (IBAction)doLogoutButton {
2011-08-24 21:44:16 -07:00
UIAlertView *logoutConfirm = [[UIAlertView alloc] initWithTitle:@"Positive?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Logout", nil];
[logoutConfirm show];
[logoutConfirm setTag:1];
2011-08-24 21:44:16 -07:00
[logoutConfirm release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1) {
if (buttonIndex == 0) {
return;
} else {
NSLog(@"Logging out...");
NSString *urlS = [NSString stringWithFormat:@"http://%@/reader/logout?api=1",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlS];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setResponseEncoding:NSUTF8StringEncoding];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setFailedBlock:^(void) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self finishedWithError:request];
}];
[request setCompletionBlock:^(void) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[appDelegate showLogin];
}];
[request setTimeOutSeconds:30];
[request startAsynchronous];
[ASIHTTPRequest setSessionCookies:nil];
2011-09-15 18:15:47 -07:00
[MBProgressHUD hideHUDForView:self.view animated:YES];
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = @"Logging out...";
}
} else if (alertView.tag == 2) {
if (buttonIndex == 0) {
return;
} else {
NSURL *url = [NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=463981119&mt=8"];
[[UIApplication sharedApplication] openURL:url];
}
2011-08-24 21:44:16 -07:00
}
}
- (IBAction)doSwitchSitesUnread {
2011-08-24 21:44:16 -07:00
self.viewShowingAllFeeds = !self.viewShowingAllFeeds;
if (self.viewShowingAllFeeds) {
[self.sitesButton setImage:[UIImage imageNamed:@"ellipses_half.png"]];
// [self.sitesButton setTitle:@"Unreads"];
} else {
[self.sitesButton setImage:[UIImage imageNamed:@"ellipses.png"]];
// [self.sitesButton setTitle:@"All Sites"];
}
2011-08-24 21:44:16 -07:00
NSInteger intelligenceLevel = [appDelegate selectedIntelligence];
NSMutableArray *indexPaths = [NSMutableArray array];
if (self.viewShowingAllFeeds) {
[self calculateFeedLocations:NO];
}
// NSLog(@"View showing all: %d and %@", self.viewShowingAllFeeds, self.stillVisibleFeeds);
for (int s=0; s < [appDelegate.dictFoldersArray count]; s++) {
NSString *folderName = [appDelegate.dictFoldersArray objectAtIndex:s];
2011-08-24 21:44:16 -07:00
NSArray *activeFolderFeeds = [self.activeFeedLocations objectForKey:folderName];
NSArray *originalFolder = [appDelegate.dictFolders objectForKey:folderName];
2011-08-24 21:44:16 -07:00
for (int f=0; f < [activeFolderFeeds count]; f++) {
int location = [[activeFolderFeeds objectAtIndex:f] intValue];
id feedId = [originalFolder objectAtIndex:location];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:f inSection:s];
NSString *feedIdStr = [NSString stringWithFormat:@"%@",feedId];
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
2011-08-24 21:44:16 -07:00
int maxScore = [NewsBlurViewController computeMaxScoreForFeed:feed];
if (!self.viewShowingAllFeeds ||
(self.viewShowingAllFeeds && ![self.stillVisibleFeeds objectForKey:feedIdStr])) {
if (maxScore < intelligenceLevel) {
[indexPaths addObject:indexPath];
}
2011-08-24 21:44:16 -07:00
}
}
}
if (!self.viewShowingAllFeeds) {
[self calculateFeedLocations:YES];
}
[self.feedTitlesTable beginUpdates];
if ([indexPaths count] > 0) {
2011-08-24 21:44:16 -07:00
if (self.viewShowingAllFeeds) {
[self.feedTitlesTable insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationNone];
} else {
[self.feedTitlesTable deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationNone];
}
}
[self.feedTitlesTable endUpdates];
2011-08-24 21:44:16 -07:00
CGPoint offset = CGPointMake(0, 0);
[self.feedTitlesTable setContentOffset:offset animated:YES];
2011-08-24 21:44:16 -07:00
// Forget still visible feeds, since they won't be populated when
// all feeds are showing, and shouldn't be populated after this
// hide/show runs.
self.stillVisibleFeeds = [NSMutableDictionary dictionary];
[self redrawUnreadCounts];
}
- (IBAction)doAddButton {
[appDelegate showAdd];
}
2010-06-20 11:04:23 -04:00
#pragma mark -
#pragma mark Table View - Feed List
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [appDelegate.dictFoldersArray count];
2010-06-20 11:04:23 -04:00
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [appDelegate.dictFoldersArray objectAtIndex:section];
2010-06-20 11:04:23 -04:00
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *folderName = [appDelegate.dictFoldersArray objectAtIndex:section];
2011-08-24 21:44:16 -07:00
return [[self.activeFeedLocations objectForKey:folderName] count];
2010-06-20 11:04:23 -04:00
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
2011-08-24 21:44:16 -07:00
static NSString *FeedCellIdentifier = @"FeedCellIdentifier";
FeedTableCell *cell = (FeedTableCell *)[tableView dequeueReusableCellWithIdentifier:FeedCellIdentifier];
if (cell == nil) {
2011-10-15 15:26:18 -07:00
cell = [[[FeedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FeedCellIdentifier"] autorelease];
2011-08-24 21:44:16 -07:00
cell.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
}
NSString *folderName = [appDelegate.dictFoldersArray objectAtIndex:indexPath.section];
NSArray *feeds = [appDelegate.dictFolders objectForKey:folderName];
2011-08-24 21:44:16 -07:00
NSArray *activeFolderFeeds = [self.activeFeedLocations objectForKey:folderName];
int location = [[activeFolderFeeds objectAtIndex:indexPath.row] intValue];
id feedId = [feeds objectAtIndex:location];
NSString *feedIdStr = [NSString stringWithFormat:@"%@",feedId];
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
2011-08-24 21:44:16 -07:00
cell.feedTitle = [feed objectForKey:@"feed_title"];
cell.feedFavicon = [Utilities getImage:feedIdStr];
2011-08-24 21:44:16 -07:00
cell.positiveCount = [[feed objectForKey:@"ps"] intValue];
cell.neutralCount = [[feed objectForKey:@"nt"] intValue];
cell.negativeCount = [[feed objectForKey:@"ng"] intValue];
return cell;
2010-06-20 11:04:23 -04:00
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *folderName = [appDelegate.dictFoldersArray objectAtIndex:indexPath.section];
NSArray *feeds = [appDelegate.dictFolders objectForKey:folderName];
2011-08-24 21:44:16 -07:00
NSArray *activeFolderFeeds = [self.activeFeedLocations objectForKey:folderName];
int location = [[activeFolderFeeds objectAtIndex:indexPath.row] intValue];
id feedId = [feeds objectAtIndex:location];
NSString *feedIdStr = [NSString stringWithFormat:@"%@",feedId];
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
2011-08-24 21:44:16 -07:00
// If all feeds are already showing, no need to remember this one.
if (!self.viewShowingAllFeeds) {
[self.stillVisibleFeeds setObject:indexPath forKey:feedIdStr];
}
[appDelegate setActiveFeed:feed];
[appDelegate setActiveFolder:folderName];
2011-08-24 21:44:16 -07:00
appDelegate.readStories = [NSMutableArray array];
appDelegate.isRiverView = NO;
2011-08-24 21:44:16 -07:00
[appDelegate loadFeedDetailView];
2010-06-20 11:04:23 -04:00
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kTableViewRowHeight;
}
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
2011-08-24 21:44:16 -07:00
// create the parent view that will hold header Label
UIControl* customView = [[[UIControl alloc]
initWithFrame:CGRectMake(0.0, 0.0,
tableView.bounds.size.width, 21.0)]
autorelease];
2011-08-24 21:44:16 -07:00
UIView *borderBottom = [[[UIView alloc]
initWithFrame:CGRectMake(0.0, 20.0,
tableView.bounds.size.width, 1.0)]
autorelease];
borderBottom.backgroundColor = [UIColorFromRGB(0xB7BDC6) colorWithAlphaComponent:0.5];
borderBottom.opaque = NO;
[customView addSubview:borderBottom];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
customView.opaque = NO;
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:11];
headerLabel.frame = CGRectMake(36.0, 1.0, 286.0, 20.0);
headerLabel.shadowColor = [UIColor colorWithRed:.94 green:0.94 blue:0.97 alpha:1.0];
headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
if (section == 0) {
headerLabel.text = @"EVERYTHING";
customView.backgroundColor = [UIColorFromRGB(0xE6DDD7)
colorWithAlphaComponent:0.8];
} else {
headerLabel.text = [[appDelegate.dictFoldersArray objectAtIndex:section] uppercaseString];
customView.backgroundColor = [UIColorFromRGB(0xD7DDE6)
colorWithAlphaComponent:0.8];
}
2011-08-24 21:44:16 -07:00
[customView addSubview:headerLabel];
[headerLabel release];
UIImage *folderImage = [UIImage imageNamed:@"folder.png"];
UIImageView *folderImageView = [[UIImageView alloc] initWithImage:folderImage];
folderImageView.frame = CGRectMake(14.0, 2.0, 16.0, 16.0);
[customView addSubview:folderImageView];
[folderImageView release];
UIImage *disclosureImage = [UIImage imageNamed:@"disclosure.png"];
UIImageView *disclosureImageView = [[UIImageView alloc] initWithImage:disclosureImage];
disclosureImageView.frame = CGRectMake(customView.frame.size.width - 20, 3.0, 9.0, 14.0);
[customView addSubview:disclosureImageView];
[disclosureImageView release];
UIButton *invisibleHeaderButton = [UIButton buttonWithType:UIButtonTypeCustom];
invisibleHeaderButton.frame = CGRectMake(0, 0, customView.frame.size.width, customView.frame.size.height);
invisibleHeaderButton.alpha = .1;
invisibleHeaderButton.tag = section;
[invisibleHeaderButton addTarget:self action:@selector(didSelectSectionHeader:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:invisibleHeaderButton];
[invisibleHeaderButton addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
[invisibleHeaderButton addTarget:self action:@selector(sectionUntapped:) forControlEvents:UIControlEventTouchUpInside];
[invisibleHeaderButton addTarget:self action:@selector(sectionUntapped:) forControlEvents:UIControlEventTouchUpOutside];
[customView setAutoresizingMask:UIViewAutoresizingNone];
2011-08-24 21:44:16 -07:00
return customView;
}
- (IBAction)sectionTapped:(UIButton *)button {
button.backgroundColor =[UIColor colorWithRed:0.15 green:0.55 blue:0.95 alpha:1.0];
}
- (IBAction)sectionUntapped:(UIButton *)button {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.15 * NSEC_PER_SEC),
dispatch_get_current_queue(), ^{
button.backgroundColor = [UIColor clearColor];
});
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// NSString *folder = [appDelegate.dictFoldersArray objectAtIndex:section];
// if ([[folder stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
// return 0;
// }
2011-08-24 21:44:16 -07:00
return 21;
}
- (void)didSelectSectionHeader:(UIButton *)button {
appDelegate.readStories = [NSMutableArray array];
appDelegate.isRiverView = YES;
NSMutableArray *feeds = [NSMutableArray array];
if (button.tag == 0) {
[appDelegate setActiveFolder:@"Everything"];
for (NSString *folderName in self.activeFeedLocations) {
NSArray *originalFolder = [appDelegate.dictFolders objectForKey:folderName];
NSArray *folderFeeds = [self.activeFeedLocations objectForKey:folderName];
for (int l=0; l < [folderFeeds count]; l++) {
[feeds addObject:[originalFolder objectAtIndex:[[folderFeeds objectAtIndex:l] intValue]]];
}
}
} else {
NSString *folderName = [appDelegate.dictFoldersArray objectAtIndex:button.tag];
[appDelegate setActiveFolder:folderName];
NSArray *originalFolder = [appDelegate.dictFolders objectForKey:folderName];
NSArray *activeFolderFeeds = [self.activeFeedLocations objectForKey:folderName];
for (int l=0; l < [activeFolderFeeds count]; l++) {
[feeds addObject:[originalFolder objectAtIndex:[[activeFolderFeeds objectAtIndex:l] intValue]]];
}
}
appDelegate.activeFolderFeeds = feeds;
[appDelegate loadRiverFeedDetailView];
}
- (IBAction)selectIntelligence {
NSInteger newLevel = [self.intelligenceControl selectedSegmentIndex] - 1;
NSInteger previousLevel = [appDelegate selectedIntelligence];
[appDelegate setSelectedIntelligence:newLevel];
2011-08-24 21:44:16 -07:00
if (!self.viewShowingAllFeeds) {
// NSLog(@"Select Intelligence from %d to %d.", previousLevel, newLevel);
[self updateFeedsWithIntelligence:previousLevel newLevel:newLevel];
}
[self redrawUnreadCounts];
}
- (void)updateFeedsWithIntelligence:(int)previousLevel newLevel:(int)newLevel {
NSMutableArray *insertIndexPaths = [NSMutableArray array];
NSMutableArray *deleteIndexPaths = [NSMutableArray array];
NSMutableDictionary *addToVisibleFeeds = [NSMutableDictionary dictionary];
2011-08-24 21:44:16 -07:00
if (newLevel <= previousLevel) {
2011-08-24 21:44:16 -07:00
[self calculateFeedLocations:NO];
}
for (int s=0; s < [appDelegate.dictFoldersArray count]; s++) {
NSString *folderName = [appDelegate.dictFoldersArray objectAtIndex:s];
2011-08-24 21:44:16 -07:00
NSArray *activeFolderFeeds = [self.activeFeedLocations objectForKey:folderName];
NSArray *originalFolder = [appDelegate.dictFolders objectForKey:folderName];
// if (s == 9) {
// NSLog(@"Section %d: %@. %d to %d", s, folderName, previousLevel, newLevel);
// }
2011-08-24 21:44:16 -07:00
for (int f=0; f < [originalFolder count]; f++) {
NSNumber *feedId = [originalFolder objectAtIndex:f];
NSString *feedIdStr = [NSString stringWithFormat:@"%@",feedId];
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
2011-08-24 21:44:16 -07:00
int maxScore = [NewsBlurViewController computeMaxScoreForFeed:feed];
// if (s == 9) {
// NSLog(@"MaxScore: %d for %@ (%@/%@/%@). Visible: %@", maxScore,
// [feed objectForKey:@"feed_title"],
// [feed objectForKey:@"ng"], [feed objectForKey:@"nt"], [feed objectForKey:@"ng"],
// [self.visibleFeeds objectForKey:feedIdStr]);
// }
2011-08-24 21:44:16 -07:00
if ([self.visibleFeeds objectForKey:feedIdStr]) {
if (maxScore < newLevel) {
for (int l=0; l < [activeFolderFeeds count]; l++) {
if ([originalFolder objectAtIndex:[[activeFolderFeeds objectAtIndex:l] intValue]] == feedId) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:l inSection:s];
[deleteIndexPaths addObject:indexPath];
if ([self.stillVisibleFeeds objectForKey:feedIdStr]) {
[self.stillVisibleFeeds removeObjectForKey:feedIdStr];
}
break;
}
}
}
} else {
if (maxScore >= newLevel) {
for (int l=0; l < [activeFolderFeeds count]; l++) {
if ([originalFolder objectAtIndex:[[activeFolderFeeds objectAtIndex:l] intValue]] == feedId) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:l inSection:s];
[addToVisibleFeeds setObject:[NSNumber numberWithBool:YES] forKey:feedIdStr];
2011-08-24 21:44:16 -07:00
[insertIndexPaths addObject:indexPath];
break;
}
}
}
}
}
}
for (id feedIdStr in addToVisibleFeeds) {
[self.visibleFeeds setObject:[addToVisibleFeeds objectForKey:feedIdStr] forKey:feedIdStr];
}
2011-08-24 21:44:16 -07:00
for (id feedIdStr in [self.stillVisibleFeeds allKeys]) {
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
2011-08-24 21:44:16 -07:00
int maxScore = [NewsBlurViewController computeMaxScoreForFeed:feed];
if (previousLevel != newLevel && maxScore < newLevel) {
[deleteIndexPaths addObject:[self.stillVisibleFeeds objectForKey:feedIdStr]];
[self.stillVisibleFeeds removeObjectForKey:feedIdStr];
[self.visibleFeeds removeObjectForKey:feedIdStr];
}
}
if (newLevel > previousLevel) {
[self calculateFeedLocations:NO];
}
[self.feedTitlesTable beginUpdates];
if ([deleteIndexPaths count] > 0) {
[self.feedTitlesTable deleteRowsAtIndexPaths:deleteIndexPaths
2011-08-24 21:44:16 -07:00
withRowAnimation:UITableViewRowAnimationNone];
}
if ([insertIndexPaths count] > 0) {
[self.feedTitlesTable insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation:UITableViewRowAnimationNone];
}
[self.feedTitlesTable endUpdates];
[self calculateFeedLocations:YES];
}
- (void)redrawUnreadCounts {
for (UITableViewCell *cell in self.feedTitlesTable.visibleCells) {
[cell setNeedsDisplay];
}
}
- (void)calculateFeedLocations:(BOOL)markVisible {
self.activeFeedLocations = [NSMutableDictionary dictionary];
2011-08-24 21:44:16 -07:00
if (markVisible) {
self.visibleFeeds = [NSMutableDictionary dictionary];
}
for (NSString *folderName in appDelegate.dictFoldersArray) {
NSArray *folder = [appDelegate.dictFolders objectForKey:folderName];
2011-08-24 21:44:16 -07:00
NSMutableArray *feedLocations = [NSMutableArray array];
for (int f=0; f < [folder count]; f++) {
id feedId = [folder objectAtIndex:f];
NSString *feedIdStr = [NSString stringWithFormat:@"%@",feedId];
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
2011-08-24 21:44:16 -07:00
if (self.viewShowingAllFeeds) {
NSNumber *location = [NSNumber numberWithInt:f];
[feedLocations addObject:location];
} else {
int maxScore = [NewsBlurViewController computeMaxScoreForFeed:feed];
// NSLog(@"Computing score for %@: %d in %d (markVisible: %d)",
// [feed objectForKey:@"feed_title"], maxScore, appDelegate.selectedIntelligence, markVisible);
2011-08-24 21:44:16 -07:00
if (maxScore >= appDelegate.selectedIntelligence) {
NSNumber *location = [NSNumber numberWithInt:f];
[feedLocations addObject:location];
if (markVisible) {
[self.visibleFeeds setObject:[NSNumber numberWithBool:YES] forKey:feedIdStr];
}
}
}
}
[self.activeFeedLocations setObject:feedLocations forKey:folderName];
}
}
+ (int)computeMaxScoreForFeed:(NSDictionary *)feed {
2011-08-24 21:44:16 -07:00
int maxScore = -2;
if ([[feed objectForKey:@"ng"] intValue] > 0) maxScore = -1;
if ([[feed objectForKey:@"nt"] intValue] > 0) maxScore = 0;
if ([[feed objectForKey:@"ps"] intValue] > 0) maxScore = 1;
return maxScore;
}
#pragma mark -
#pragma mark Favicons
- (void)loadFavicons {
2011-08-24 21:44:16 -07:00
NSString *urlString = [NSString stringWithFormat:@"http://%@/reader/favicons",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDidFinishSelector:@selector(saveAndDrawFavicons:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)saveAndDrawFavicons:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
2011-08-24 21:44:16 -07:00
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
for (id feed_id in results) {
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feed_id];
[feed setValue:[results objectForKey:feed_id] forKey:@"favicon"];
[appDelegate.dictFeeds setValue:feed forKey:feed_id];
NSString *favicon = [feed objectForKey:@"favicon"];
if ((NSNull *)favicon != [NSNull null] && [favicon length] > 0) {
NSData *imageData = [NSData dataWithBase64EncodedString:favicon];
UIImage *faviconImage = [UIImage imageWithData:imageData];
[Utilities saveImage:faviconImage feedId:feed_id];
}
}
[Utilities saveimagesToDisk];
dispatch_sync(dispatch_get_main_queue(), ^{
[results release];
[self.feedTitlesTable reloadData];
});
});
2011-08-24 21:44:16 -07:00
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(@"Error: %@", error);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
#pragma mark -
#pragma mark PullToRefresh
// called when the user pulls-to-refresh
- (void)pullToRefreshViewShouldRefresh:(PullToRefreshView *)view {
2011-08-24 21:44:16 -07:00
[self fetchFeedList:NO];
}
// called when the date shown needs to be updated, optional
- (NSDate *)pullToRefreshViewLastUpdated:(PullToRefreshView *)view {
2011-08-24 21:44:16 -07:00
return self.lastUpdate;
}
2010-06-20 11:04:23 -04:00
@end