diff --git a/media/iphone/Classes/NewsBlurAppDelegate.h b/media/iphone/Classes/NewsBlurAppDelegate.h index ec7e238da..1f8841fa5 100644 --- a/media/iphone/Classes/NewsBlurAppDelegate.h +++ b/media/iphone/Classes/NewsBlurAppDelegate.h @@ -37,6 +37,7 @@ NSInteger selectedIntelligence; NSMutableArray * recentlyReadStories; NSIndexPath * activeFeedIndexPath; + NSMutableArray * readStories; } @property (nonatomic, retain) IBOutlet UIWindow *window; @@ -61,6 +62,7 @@ @property (readwrite) NSInteger selectedIntelligence; @property (readwrite, retain) NSMutableArray * recentlyReadStories; @property (readwrite, retain) NSIndexPath * activeFeedIndexPath; +@property (readwrite, retain) NSMutableArray * readStories; - (void)showLogin; - (void)loadFeedDetailView; @@ -71,10 +73,15 @@ - (void)setTitle:(NSString *)title; - (void)showOriginalStory:(NSURL *)url; - (void)closeOriginalStory; + - (int)indexOfNextStory; - (int)indexOfPreviousStory; - (int)indexOfActiveStory; - (int)locationOfActiveStory; +- (void)pushReadStory:(id)storyId; +- (id)popReadStory; +- (int)locationOfStoryId:(id)storyId; + - (void)setStories:(NSArray *)activeFeedStoriesValue; - (void)addStories:(NSArray *)stories; - (int)unreadCount; diff --git a/media/iphone/Classes/NewsBlurAppDelegate.m b/media/iphone/Classes/NewsBlurAppDelegate.m index cb87dcbac..313bcdc4d 100644 --- a/media/iphone/Classes/NewsBlurAppDelegate.m +++ b/media/iphone/Classes/NewsBlurAppDelegate.m @@ -36,6 +36,7 @@ @synthesize activeOriginalStoryURL; @synthesize recentlyReadStories; @synthesize activeFeedIndexPath; +@synthesize readStories; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { navigationController.viewControllers = [NSArray arrayWithObject:feedsViewController]; @@ -73,6 +74,7 @@ [activeOriginalStoryURL release]; [recentlyReadStories release]; [activeFeedIndexPath release]; + [readStories release]; [super dealloc]; } @@ -194,6 +196,31 @@ return -1; } +- (void)pushReadStory:(id)storyId { + if ([self.readStories lastObject] != storyId) { + [self.readStories addObject:storyId]; + } +} + +- (id)popReadStory { + if (storyCount == 0) { + return nil; + } else { + [self.readStories removeLastObject]; + id lastStory = [self.readStories lastObject]; + return lastStory; + } +} + +- (int)locationOfStoryId:(id)storyId { + for (int i=0; i < [activeFeedStoryLocations count]; i++) { + if ([activeFeedStoryLocationIds objectAtIndex:i] == storyId) { + return [[activeFeedStoryLocations objectAtIndex:i] intValue]; + } + } + return -1; +} + - (int)unreadCount { int total = 0; total += [[self.activeFeed objectForKey:@"ps"] intValue]; @@ -225,10 +252,10 @@ - (void)markActiveStoryRead { int activeLocation = [self locationOfActiveStory]; - int activeIndex = [[activeFeedStoryLocations objectAtIndex:activeLocation] intValue]; if (activeLocation == -1) { return; } + int activeIndex = [[activeFeedStoryLocations objectAtIndex:activeLocation] intValue]; NSDictionary *story = [activeFeedStories objectAtIndex:activeIndex]; [story setValue:[NSNumber numberWithInt:1] forKey:@"read_status"]; diff --git a/media/iphone/Classes/NewsBlurViewController.m b/media/iphone/Classes/NewsBlurViewController.m index b0125ac11..68493b0b1 100644 --- a/media/iphone/Classes/NewsBlurViewController.m +++ b/media/iphone/Classes/NewsBlurViewController.m @@ -409,6 +409,7 @@ blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] [appDelegate setActiveFeed:feed]; [appDelegate setActiveFeedIndexPath:indexPath]; + appDelegate.readStories = [NSMutableArray array]; [appDelegate loadFeedDetailView]; } diff --git a/media/iphone/Classes/StoryDetailViewController.m b/media/iphone/Classes/StoryDetailViewController.m index 3e417eb10..36c3624e0 100644 --- a/media/iphone/Classes/StoryDetailViewController.m +++ b/media/iphone/Classes/StoryDetailViewController.m @@ -43,7 +43,9 @@ - (void)viewWillAppear:(BOOL)animated { // NSLog(@"Stories; %@ -- %@ (%d)", self.activeStoryId, [appDelegate.activeStory objectForKey:@"id"], self.activeStoryId == [appDelegate.activeStory objectForKey:@"id"]); - if (self.activeStoryId != [appDelegate.activeStory objectForKey:@"id"]) { + id storyId = [appDelegate.activeStory objectForKey:@"id"]; + if (self.activeStoryId != storyId) { + [appDelegate pushReadStory:storyId]; [self setActiveStory]; [self showStory]; [self markStoryAsRead]; @@ -83,8 +85,10 @@ [buttonNext setTitle:@"Next Unread"]; } - int previousIndex = [appDelegate indexOfPreviousStory]; - if (previousIndex == -1) { + int readStoryCount = [appDelegate.readStories count]; + if (readStoryCount == 0 || + (readStoryCount == 1 && + [appDelegate.readStories lastObject] == [appDelegate.activeStory objectForKey:@"id"])) { [buttonPrevious setTitle:@"Done"]; } else { [buttonPrevious setTitle:@"Previous"]; @@ -255,6 +259,7 @@ } else { [appDelegate setActiveStory:[[appDelegate activeFeedStories] objectAtIndex:nextIndex]]; + [appDelegate pushReadStory:[appDelegate.activeStory objectForKey:@"id"]]; [self setActiveStory]; [self showStory]; [self markStoryAsRead]; @@ -271,13 +276,17 @@ } - (IBAction)doPreviousStory { - int previousIndex = [appDelegate indexOfPreviousStory]; - if (previousIndex == -1) { + id previousStoryId = [appDelegate popReadStory]; + if (!previousStoryId || previousStoryId == [appDelegate.activeStory objectForKey:@"id"]) { [appDelegate.navigationController popToViewController:[appDelegate.navigationController.viewControllers objectAtIndex:0] animated:YES]; } else { + int previousIndex = [appDelegate locationOfStoryId:previousStoryId]; + if (previousIndex == -1) { + return [self doPreviousStory]; + } [appDelegate setActiveStory:[[appDelegate activeFeedStories] objectAtIndex:previousIndex]]; [self setActiveStory];