2010-06-17 13:10:45 -04:00
|
|
|
//
|
|
|
|
// NewsBlurAppDelegate.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 6/16/10.
|
2010-06-21 17:17:26 -04:00
|
|
|
// Copyright NewsBlur 2010. All rights reserved.
|
2010-06-17 13:10:45 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "NewsBlurAppDelegate.h"
|
|
|
|
#import "NewsBlurViewController.h"
|
2010-06-21 17:17:26 -04:00
|
|
|
#import "FeedDetailViewController.h"
|
2010-06-25 18:36:01 -04:00
|
|
|
#import "StoryDetailViewController.h"
|
2010-10-31 23:02:13 -04:00
|
|
|
#import "LoginViewController.h"
|
2010-11-13 18:32:43 -05:00
|
|
|
#import "OriginalStoryViewController.h"
|
2010-06-17 13:10:45 -04:00
|
|
|
|
|
|
|
@implementation NewsBlurAppDelegate
|
|
|
|
|
|
|
|
@synthesize window;
|
2010-06-21 17:17:26 -04:00
|
|
|
@synthesize navigationController;
|
|
|
|
@synthesize feedsViewController;
|
2010-06-24 12:53:50 -04:00
|
|
|
@synthesize feedDetailViewController;
|
2010-06-25 18:36:01 -04:00
|
|
|
@synthesize storyDetailViewController;
|
2010-10-31 23:02:13 -04:00
|
|
|
@synthesize loginViewController;
|
2010-11-13 18:32:43 -05:00
|
|
|
@synthesize originalStoryViewController;
|
2010-06-17 13:10:45 -04:00
|
|
|
|
2010-11-13 18:32:43 -05:00
|
|
|
@synthesize logoutDelegate;
|
|
|
|
@synthesize activeUsername;
|
2010-06-25 18:36:01 -04:00
|
|
|
@synthesize activeFeed;
|
|
|
|
@synthesize activeFeedStories;
|
|
|
|
@synthesize activeStory;
|
2011-07-24 16:52:24 -07:00
|
|
|
@synthesize storyCount;
|
2010-11-13 18:32:43 -05:00
|
|
|
@synthesize activeOriginalStoryURL;
|
2011-07-24 21:47:58 -07:00
|
|
|
@synthesize recentlyReadStories;
|
2011-07-24 22:14:50 -07:00
|
|
|
@synthesize activeFeedIndexPath;
|
2010-06-17 13:10:45 -04:00
|
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
2010-06-21 17:17:26 -04:00
|
|
|
navigationController.viewControllers = [NSArray arrayWithObject:feedsViewController];
|
2010-06-17 13:10:45 -04:00
|
|
|
|
2010-06-21 17:17:26 -04:00
|
|
|
[window addSubview:navigationController.view];
|
2010-06-17 13:10:45 -04:00
|
|
|
[window makeKeyAndVisible];
|
2010-11-11 23:48:27 -05:00
|
|
|
|
|
|
|
[feedsViewController fetchFeedList];
|
2010-06-25 18:36:01 -04:00
|
|
|
|
2010-06-17 13:10:45 -04:00
|
|
|
return YES;
|
|
|
|
}
|
2010-11-11 20:05:53 -05:00
|
|
|
|
2010-06-17 13:10:45 -04:00
|
|
|
- (void)dealloc {
|
2010-06-21 17:17:26 -04:00
|
|
|
[feedsViewController release];
|
2010-06-24 12:53:50 -04:00
|
|
|
[feedDetailViewController release];
|
2010-06-25 18:36:01 -04:00
|
|
|
[storyDetailViewController release];
|
2010-10-31 23:02:13 -04:00
|
|
|
[loginViewController release];
|
2010-11-13 18:32:43 -05:00
|
|
|
[originalStoryViewController release];
|
2010-06-21 17:17:26 -04:00
|
|
|
[navigationController release];
|
2010-06-17 13:10:45 -04:00
|
|
|
[window release];
|
2010-11-13 18:32:43 -05:00
|
|
|
[activeUsername release];
|
2010-06-25 18:36:01 -04:00
|
|
|
[activeFeed release];
|
|
|
|
[activeFeedStories release];
|
|
|
|
[activeStory release];
|
2010-11-13 18:32:43 -05:00
|
|
|
[activeOriginalStoryURL release];
|
2011-07-24 21:47:58 -07:00
|
|
|
[recentlyReadStories release];
|
2011-07-24 22:14:50 -07:00
|
|
|
[activeFeedIndexPath release];
|
2010-06-17 13:10:45 -04:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2010-06-21 17:17:26 -04:00
|
|
|
- (void)hideNavigationBar:(BOOL)animated {
|
|
|
|
[[self navigationController] setNavigationBarHidden:YES animated:animated];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showNavigationBar:(BOOL)animated {
|
|
|
|
[[self navigationController] setNavigationBarHidden:NO animated:animated];
|
|
|
|
}
|
2010-06-17 13:10:45 -04:00
|
|
|
|
2010-06-24 00:22:26 -04:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Views
|
|
|
|
|
2010-11-11 23:48:27 -05:00
|
|
|
- (void)showLogin {
|
|
|
|
UINavigationController *navController = self.navigationController;
|
|
|
|
[navController presentModalViewController:loginViewController animated:YES];
|
|
|
|
}
|
|
|
|
|
2010-11-11 20:05:53 -05:00
|
|
|
- (void)reloadFeedsView {
|
|
|
|
NSLog(@"Reloading feeds list");
|
2010-11-11 23:48:27 -05:00
|
|
|
[self setTitle:@"NewsBlur"];
|
2010-11-11 20:05:53 -05:00
|
|
|
[feedsViewController fetchFeedList];
|
2010-11-13 13:42:20 -05:00
|
|
|
[loginViewController dismissModalViewControllerAnimated:YES];
|
2010-11-13 18:32:43 -05:00
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.6];
|
2010-11-11 20:05:53 -05:00
|
|
|
}
|
|
|
|
|
2010-06-25 18:36:01 -04:00
|
|
|
- (void)loadFeedDetailView {
|
2010-06-24 00:22:26 -04:00
|
|
|
UINavigationController *navController = self.navigationController;
|
2010-11-13 18:32:43 -05:00
|
|
|
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"All" style:UIBarButtonItemStylePlain target:nil action:nil];
|
2011-07-24 16:52:24 -07:00
|
|
|
[self setStories:nil];
|
2010-11-13 18:32:43 -05:00
|
|
|
navController.navigationItem.backBarButtonItem = backButton;
|
|
|
|
[backButton release];
|
2010-06-24 00:22:26 -04:00
|
|
|
[navController pushViewController:feedDetailViewController animated:YES];
|
2011-07-20 22:21:11 -07:00
|
|
|
[feedDetailViewController fetchFeedDetail:1];
|
2010-11-13 13:42:20 -05:00
|
|
|
[self showNavigationBar:YES];
|
2010-11-13 18:32:43 -05:00
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.8];
|
2010-06-25 18:36:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadStoryDetailView {
|
|
|
|
UINavigationController *navController = self.navigationController;
|
|
|
|
[navController pushViewController:storyDetailViewController animated:YES];
|
2010-11-13 18:32:43 -05:00
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];
|
2010-06-24 00:22:26 -04:00
|
|
|
}
|
|
|
|
|
2010-11-11 23:48:27 -05:00
|
|
|
- (void)setTitle:(NSString *)title {
|
|
|
|
UILabel *label = [[UILabel alloc] init];
|
|
|
|
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
|
|
|
|
[label setBackgroundColor:[UIColor clearColor]];
|
|
|
|
[label setTextColor:[UIColor whiteColor]];
|
|
|
|
[label setText:title];
|
|
|
|
[label sizeToFit];
|
|
|
|
[navigationController.navigationBar.topItem setTitleView:label];
|
|
|
|
[label release];
|
|
|
|
}
|
2010-06-24 00:22:26 -04:00
|
|
|
|
2010-11-13 18:32:43 -05:00
|
|
|
- (void)showOriginalStory:(NSURL *)url {
|
|
|
|
self.activeOriginalStoryURL = url;
|
|
|
|
UINavigationController *navController = self.navigationController;
|
|
|
|
[navController presentModalViewController:originalStoryViewController animated:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)closeOriginalStory {
|
|
|
|
[originalStoryViewController dismissModalViewControllerAnimated:YES];
|
|
|
|
}
|
2010-06-24 00:22:26 -04:00
|
|
|
|
2011-06-15 11:21:55 -04:00
|
|
|
- (int)indexOfNextStory {
|
|
|
|
int activeIndex = [self indexOfActiveStory];
|
2011-07-24 21:47:58 -07:00
|
|
|
int readStatus = -1;
|
|
|
|
NSLog(@"ActiveStory: %d/%d", activeIndex, self.storyCount);
|
2011-07-24 16:52:24 -07:00
|
|
|
for (int i=activeIndex+1; i < self.storyCount; i++) {
|
2011-06-15 11:21:55 -04:00
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:i];
|
2011-07-24 21:47:58 -07:00
|
|
|
readStatus = [[story objectForKey:@"read_status"] intValue];
|
|
|
|
NSLog(@"readStatus at %d: %d", i, readStatus);
|
|
|
|
if (readStatus == 0) {
|
|
|
|
NSLog(@"NextStory: %d", i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=activeIndex; i >= 0; i--) {
|
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:i];
|
|
|
|
readStatus = [[story objectForKey:@"read_status"] intValue];
|
|
|
|
NSLog(@"readStatus at %d: %d", i, readStatus);
|
2011-06-16 09:02:40 -04:00
|
|
|
if (readStatus == 0) {
|
|
|
|
NSLog(@"NextStory: %d", i);
|
2011-06-15 11:21:55 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)indexOfPreviousStory {
|
|
|
|
NSInteger activeIndex = [self indexOfActiveStory];
|
2011-07-25 09:08:20 -07:00
|
|
|
return MAX(-1, activeIndex-1);
|
2011-06-15 11:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)indexOfActiveStory {
|
2011-07-24 16:52:24 -07:00
|
|
|
for (int i=0; i < self.storyCount; i++) {
|
2011-06-15 11:21:55 -04:00
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:i];
|
|
|
|
if ([activeStory objectForKey:@"id"] == [story objectForKey:@"id"]) {
|
|
|
|
return i;
|
2011-06-14 10:35:33 -04:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:21:55 -04:00
|
|
|
return -1;
|
2011-06-14 10:35:33 -04:00
|
|
|
}
|
|
|
|
|
2011-07-26 08:37:10 -07:00
|
|
|
- (int)unreadCount {
|
|
|
|
int ps = [[self.activeFeed objectForKey:@"ps"] intValue];
|
|
|
|
int nt = [[self.activeFeed objectForKey:@"nt"] intValue];
|
|
|
|
int ng = [[self.activeFeed objectForKey:@"ng"] intValue];
|
|
|
|
return ps + nt + ng;
|
|
|
|
}
|
|
|
|
|
2011-07-24 16:52:24 -07:00
|
|
|
- (void)addStories:(NSArray *)stories {
|
2011-07-22 09:10:13 -07:00
|
|
|
// NSLog(@"Adding: %d to %@", [stories count], stories);
|
2011-07-20 22:21:11 -07:00
|
|
|
self.activeFeedStories = [self.activeFeedStories arrayByAddingObjectsFromArray:stories];
|
2011-07-24 16:52:24 -07:00
|
|
|
self.storyCount = [self.activeFeedStories count];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setStories:(NSArray *)activeFeedStoriesValue {
|
|
|
|
self.activeFeedStories = activeFeedStoriesValue;
|
|
|
|
self.storyCount = [self.activeFeedStories count];
|
2011-07-24 21:47:58 -07:00
|
|
|
self.recentlyReadStories = [[NSMutableArray alloc] init];
|
2011-07-20 22:21:11 -07:00
|
|
|
}
|
|
|
|
|
2011-07-24 20:34:54 -07:00
|
|
|
- (void)markActiveStoryRead {
|
|
|
|
int activeIndex = [self indexOfActiveStory];
|
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:activeIndex];
|
2011-07-24 22:14:50 -07:00
|
|
|
[story setValue:[NSNumber numberWithInt:1] forKey:@"read_status"];
|
2011-07-24 21:47:58 -07:00
|
|
|
[self.recentlyReadStories addObject:[NSNumber numberWithInt:activeIndex]];
|
2011-07-24 22:14:50 -07:00
|
|
|
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
|
|
|
|
if (score > 0) {
|
|
|
|
int unreads = [[activeFeed objectForKey:@"ps"] intValue] - 1;
|
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:unreads] forKey:@"ps"];
|
|
|
|
} else if (score == 0) {
|
|
|
|
int unreads = [[activeFeed objectForKey:@"nt"] intValue] - 1;
|
|
|
|
NSLog(@"Neutral story: %@ -- %d", [activeFeed objectForKey:@"nt"], unreads);
|
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:unreads] forKey:@"nt"];
|
|
|
|
} else if (score < 0) {
|
|
|
|
int unreads = [[activeFeed objectForKey:@"ng"] intValue] - 1;
|
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:unreads] forKey:@"ng"];
|
|
|
|
}
|
|
|
|
NSLog(@"Marked read %d: %@: %d", activeIndex, self.recentlyReadStories, score);
|
2011-07-24 20:34:54 -07:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:23:38 -07:00
|
|
|
- (void)markActiveFeedAllRead {
|
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:0] forKey:@"ps"];
|
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:0] forKey:@"nt"];
|
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:0] forKey:@"ng"];
|
|
|
|
}
|
|
|
|
|
2010-11-22 10:44:52 -05:00
|
|
|
+ (int)computeStoryScore:(NSDictionary *)intelligence {
|
|
|
|
int score = 0;
|
2011-07-26 08:37:10 -07:00
|
|
|
int title = [[intelligence objectForKey:@"title"] intValue];
|
|
|
|
int author = [[intelligence objectForKey:@"author"] intValue];
|
|
|
|
int tags = [[intelligence objectForKey:@"tags"] intValue];
|
|
|
|
|
|
|
|
int score_max = MAX(title, MAX(author, tags));
|
|
|
|
int score_min = MIN(title, MIN(author, tags));
|
2011-07-24 20:07:38 -07:00
|
|
|
|
|
|
|
if (score_max > 0) score = score_max;
|
|
|
|
else if (score_min < 0) score = score_min;
|
2010-11-22 10:44:52 -05:00
|
|
|
|
2011-07-24 20:07:38 -07:00
|
|
|
if (score == 0) score = [[intelligence objectForKey:@"feed"] integerValue];
|
|
|
|
|
2011-07-26 08:37:10 -07:00
|
|
|
NSLog(@"%d/%d -- %d: %@", score_max, score_min, score, intelligence);
|
2010-11-22 10:44:52 -05:00
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:10:45 -04:00
|
|
|
@end
|