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;
|
2011-07-29 21:27:37 -07:00
|
|
|
@synthesize activeFeedStoryLocations;
|
2011-08-08 09:58:15 -07:00
|
|
|
@synthesize activeFeedStoryLocationIds;
|
2010-06-25 18:36:01 -04:00
|
|
|
@synthesize activeStory;
|
2011-07-24 16:52:24 -07:00
|
|
|
@synthesize storyCount;
|
2011-08-09 17:58:43 -07:00
|
|
|
@synthesize originalStoryCount;
|
2011-07-29 09:06:17 -07:00
|
|
|
@synthesize selectedIntelligence;
|
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
|
|
|
|
2011-08-13 23:00:51 -07:00
|
|
|
[feedsViewController fetchFeedList:YES];
|
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
|
|
|
|
2011-07-29 09:06:17 -07:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
self.selectedIntelligence = 1;
|
2011-08-01 09:43:13 -07:00
|
|
|
[self setRecentlyReadStories:[NSMutableArray array]];
|
2011-07-29 09:06:17 -07:00
|
|
|
}
|
|
|
|
|
2010-06-17 13:10:45 -04:00
|
|
|
- (void)dealloc {
|
2011-08-01 09:43:13 -07:00
|
|
|
NSLog(@"Dealloc on AppDelegate");
|
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];
|
2011-07-29 21:27:37 -07:00
|
|
|
[activeFeedStoryLocations release];
|
2011-08-08 09:58:15 -07:00
|
|
|
[activeFeedStoryLocationIds release];
|
2010-06-25 18:36:01 -04:00
|
|
|
[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];
|
2011-08-13 17:08:26 -07:00
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];
|
2010-06-21 17:17:26 -04:00
|
|
|
}
|
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 {
|
2010-11-11 23:48:27 -05:00
|
|
|
[self setTitle:@"NewsBlur"];
|
2011-08-13 23:00:51 -07:00
|
|
|
[feedsViewController fetchFeedList:YES];
|
2010-11-13 13:42:20 -05:00
|
|
|
[loginViewController dismissModalViewControllerAnimated:YES];
|
2011-08-13 17:08:26 -07:00
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];
|
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];
|
2011-08-13 17:08:26 -07:00
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];
|
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 {
|
2011-08-08 09:58:15 -07:00
|
|
|
int activeLocation = [self locationOfActiveStory];
|
2011-08-09 17:58:43 -07:00
|
|
|
// int activeIndex = [[activeFeedStoryLocations objectAtIndex:activeLocation] intValue];
|
2011-07-24 21:47:58 -07:00
|
|
|
int readStatus = -1;
|
2011-08-08 17:17:32 -07:00
|
|
|
// NSLog(@"ActiveStory: %d (%d)/%d", activeLocation, activeIndex, self.storyCount);
|
2011-08-08 09:58:15 -07:00
|
|
|
for (int i=activeLocation+1; i < [self.activeFeedStoryLocations count]; i++) {
|
|
|
|
int location = [[self.activeFeedStoryLocations objectAtIndex:i] intValue];
|
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:location];
|
2011-07-24 21:47:58 -07:00
|
|
|
readStatus = [[story objectForKey:@"read_status"] intValue];
|
2011-08-08 17:17:32 -07:00
|
|
|
// NSLog(@"+1 readStatus at %d (%d): %d", location, i, readStatus);
|
2011-07-24 21:47:58 -07:00
|
|
|
if (readStatus == 0) {
|
2011-08-08 17:17:32 -07:00
|
|
|
// NSLog(@"NextStory after: %d", i);
|
|
|
|
return location;
|
2011-07-24 21:47:58 -07:00
|
|
|
}
|
|
|
|
}
|
2011-08-08 17:17:32 -07:00
|
|
|
if (activeLocation > 0) {
|
|
|
|
for (int i=activeLocation-1; i >= 0; i--) {
|
|
|
|
int location = [[self.activeFeedStoryLocations objectAtIndex:i] intValue];
|
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:location];
|
|
|
|
readStatus = [[story objectForKey:@"read_status"] intValue];
|
|
|
|
// NSLog(@"-1 readStatus at %d (%d): %d", location, i, readStatus);
|
|
|
|
if (readStatus == 0) {
|
|
|
|
// NSLog(@"NextStory before: %d", i);
|
|
|
|
return location;
|
|
|
|
}
|
2011-06-15 11:21:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-08-08 09:58:15 -07:00
|
|
|
- (int)locationOfActiveStory {
|
|
|
|
for (int i=0; i < [activeFeedStoryLocations count]; i++) {
|
|
|
|
if ([activeFeedStoryLocationIds objectAtIndex:i] ==
|
|
|
|
[self.activeStory objectForKey:@"id"]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-26 08:37:10 -07:00
|
|
|
- (int)unreadCount {
|
2011-08-09 17:58:43 -07:00
|
|
|
int total = 0;
|
|
|
|
total += [[self.activeFeed objectForKey:@"ps"] intValue];
|
|
|
|
if ([self selectedIntelligence] <= 0) {
|
|
|
|
total += [[self.activeFeed objectForKey:@"nt"] intValue];
|
|
|
|
}
|
|
|
|
if ([self selectedIntelligence] <= -1) {
|
|
|
|
total += [[self.activeFeed objectForKey:@"ng"] intValue];
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)visibleUnreadCount {
|
2011-08-12 10:12:30 -07:00
|
|
|
return 0;
|
2011-07-26 08:37:10 -07:00
|
|
|
}
|
|
|
|
|
2011-07-24 16:52:24 -07:00
|
|
|
- (void)addStories:(NSArray *)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];
|
2011-07-29 21:27:37 -07:00
|
|
|
[self calculateStoryLocations];
|
2011-07-24 16:52:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setStories:(NSArray *)activeFeedStoriesValue {
|
|
|
|
self.activeFeedStories = activeFeedStoriesValue;
|
|
|
|
self.storyCount = [self.activeFeedStories count];
|
2011-08-01 09:43:13 -07:00
|
|
|
[self setRecentlyReadStories:[NSMutableArray array]];
|
2011-07-29 21:27:37 -07:00
|
|
|
[self calculateStoryLocations];
|
2011-07-20 22:21:11 -07:00
|
|
|
}
|
|
|
|
|
2011-07-24 20:34:54 -07:00
|
|
|
- (void)markActiveStoryRead {
|
2011-08-08 09:58:15 -07:00
|
|
|
int activeLocation = [self locationOfActiveStory];
|
|
|
|
int activeIndex = [[activeFeedStoryLocations objectAtIndex:activeLocation] intValue];
|
|
|
|
if (activeLocation == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-24 20:34:54 -07:00
|
|
|
NSDictionary *story = [activeFeedStories objectAtIndex:activeIndex];
|
2011-07-24 22:14:50 -07:00
|
|
|
[story setValue:[NSNumber numberWithInt:1] forKey:@"read_status"];
|
2011-08-08 09:58:15 -07:00
|
|
|
[self.recentlyReadStories addObject:[NSNumber numberWithInt:activeLocation]];
|
2011-07-24 22:14:50 -07:00
|
|
|
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
|
|
|
|
if (score > 0) {
|
2011-08-08 09:58:15 -07:00
|
|
|
int unreads = MAX(0, [[activeFeed objectForKey:@"ps"] intValue] - 1);
|
2011-07-24 22:14:50 -07:00
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:unreads] forKey:@"ps"];
|
|
|
|
} else if (score == 0) {
|
2011-08-08 09:58:15 -07:00
|
|
|
int unreads = MAX(0, [[activeFeed objectForKey:@"nt"] intValue] - 1);
|
2011-07-24 22:14:50 -07:00
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:unreads] forKey:@"nt"];
|
|
|
|
} else if (score < 0) {
|
2011-08-08 09:58:15 -07:00
|
|
|
int unreads = MAX(0, [[activeFeed objectForKey:@"ng"] intValue] - 1);
|
2011-07-24 22:14:50 -07:00
|
|
|
[self.activeFeed setValue:[NSNumber numberWithInt:unreads] forKey:@"ng"];
|
|
|
|
}
|
2011-08-08 09:58:15 -07:00
|
|
|
// NSLog(@"Marked read %d-%d: %@: %d", activeIndex, activeLocation, 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"];
|
|
|
|
}
|
|
|
|
|
2011-07-29 21:27:37 -07:00
|
|
|
- (void)calculateStoryLocations {
|
2011-08-01 09:43:13 -07:00
|
|
|
self.activeFeedStoryLocations = [NSMutableArray array];
|
2011-08-08 09:58:15 -07:00
|
|
|
self.activeFeedStoryLocationIds = [NSMutableArray array];
|
2011-07-29 21:27:37 -07:00
|
|
|
for (int i=0; i < self.storyCount; i++) {
|
|
|
|
NSDictionary *story = [self.activeFeedStories objectAtIndex:i];
|
|
|
|
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
|
2011-08-01 09:43:13 -07:00
|
|
|
if (score >= self.selectedIntelligence) {
|
2011-07-29 21:27:37 -07:00
|
|
|
NSNumber *location = [NSNumber numberWithInt:i];
|
|
|
|
[self.activeFeedStoryLocations addObject:location];
|
2011-08-08 09:58:15 -07:00
|
|
|
[self.activeFeedStoryLocationIds addObject:[story objectForKey:@"id"]];
|
2011-07-29 21:27:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-29 09:06:17 -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
|