// // StoryDetailViewController.m // NewsBlur // // Created by Samuel Clay on 6/24/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "StoryDetailViewController.h" #import "NewsBlurAppDelegate.h" #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" @implementation StoryDetailViewController @synthesize appDelegate; @synthesize webView; @synthesize toolbar; @synthesize buttonNext; @synthesize buttonPrevious; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { } return self; } - (void)viewWillAppear:(BOOL)animated { [self showStory]; if ([[appDelegate.activeStory objectForKey:@"read_status"] intValue] != 1) { [self markStoryAsRead]; } [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Original" style:UIBarButtonItemStyleBordered target:self action:@selector(showOriginalSubview:) ] autorelease]; [super viewDidAppear:animated]; } - (void)markStoryAsRead { [appDelegate.activeStory setValue:[NSDecimalNumber numberWithInt:1] forKey:@"read_status"]; NSString *urlString = @"http://nb.local.host:8000/reader/mark_story_as_read"; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:[appDelegate.activeStory objectForKey:@"id"] forKey:@"story_id"]; [request setPostValue:[appDelegate.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; [request setDelegate:self]; [request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { NSString *responseString = [request responseString]; NSDictionary *results = [[NSDictionary alloc] initWithDictionary:[responseString JSONValue]]; int code = [[results valueForKey:@"code"] intValue]; NSLog(@"Read Story: %@", code); [results release]; } - (void)requestFailed:(ASIHTTPRequest *)request { // NSError *error = [request error]; // [error release]; } - (void)showStory { // NSLog(@"Loaded Story view: %@", appDelegate.activeStory); NSString *imgCssString = [NSString stringWithFormat:@""]; NSString *story_author = @""; if ([appDelegate.activeStory objectForKey:@"story_authors"]) { NSString *author = [NSString stringWithFormat:@"%@",[appDelegate.activeStory objectForKey:@"story_authors"]]; if (author && ![author isEqualToString:@""]) { story_author = [NSString stringWithFormat:@"
%@
",author]; } } NSString *story_tags = @""; if ([appDelegate.activeStory objectForKey:@"story_tags"]) { NSArray *tag_array = [appDelegate.activeStory objectForKey:@"story_tags"]; if ([tag_array count] > 0) { story_tags = [NSString stringWithFormat:@"
", [tag_array componentsJoinedByString:@"
"]]; } } NSString *storyHeader = [NSString stringWithFormat:@"
" "" "
%@
" "%@" "%@" "
", [story_tags length] ? [appDelegate.activeStory objectForKey:@"long_parsed_date"] : [appDelegate.activeStory objectForKey:@"short_parsed_date"], [appDelegate.activeStory objectForKey:@"story_title"], story_author, story_tags]; NSString *htmlString = [NSString stringWithFormat:@"%@ %@
%@
", imgCssString, storyHeader, [appDelegate.activeStory objectForKey:@"story_content"]]; [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:[appDelegate.activeFeed objectForKey:@"feed_link"]]]; } - (IBAction)doNextUnreadStory { int nextIndex = [appDelegate indexOfNextStory]; if (nextIndex == -1) { } else { [appDelegate setActiveStory:[[appDelegate activeFeedStories] objectAtIndex:nextIndex]]; [self showStory]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:.5]; [UIView setAnimationBeginsFromCurrentState:NO]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO]; [UIView commitAnimations]; } } - (IBAction)doPreviousStory { NSInteger nextIndex = [appDelegate indexOfPreviousStory]; if (nextIndex == -1) { } else { [appDelegate setActiveStory:[[appDelegate activeFeedStories] objectAtIndex:nextIndex]]; [self showStory]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:.5]; [UIView setAnimationBeginsFromCurrentState:NO]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:NO]; [UIView commitAnimations]; } } - (void)showOriginalSubview:(id)sender { NSURL *url = [NSURL URLWithString:[appDelegate.activeStory objectForKey:@"story_permalink"]]; [appDelegate showOriginalStory:url]; } - (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; self.webView = nil; self.appDelegate = nil; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (navigationType == UIWebViewNavigationTypeLinkClicked) { NSURL *url = [request URL]; [appDelegate showOriginalStory:url]; //[url release]; return NO; } return YES; } - (void)webViewDidStartLoad:(UIWebView *)webView { } - (void)webViewDidFinishLoad:(UIWebView *)webView { } - (void)dealloc { [appDelegate release]; [webView release]; [super dealloc]; } @end