// // StoryDetailViewController.m // NewsBlur // // Created by Samuel Clay on 6/24/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "StoryDetailViewController.h" #import "NewsBlurAppDelegate.h" #import "FeedDetailViewController.h" #import "FontSettingsViewController.h" #import "SplitStoryDetailViewController.h" #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" #import "Base64.h" #import "Utilities.h" @implementation StoryDetailViewController @synthesize appDelegate; @synthesize activeStoryId; @synthesize progressView; @synthesize webView; @synthesize toolbar; @synthesize buttonPrevious; @synthesize buttonNext; @synthesize buttonAction; @synthesize activity; @synthesize loadingIndicator; @synthesize feedTitleGradient; @synthesize popoverController; #pragma mark - #pragma mark View boilerplate - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { } return self; } - (void)dealloc { [activeStoryId release]; [appDelegate release]; [progressView release]; [webView release]; [toolbar release]; [buttonNext release]; [buttonPrevious release]; [buttonAction release]; [activity release]; [loadingIndicator release]; [feedTitleGradient release]; [popoverController release]; [super dealloc]; } - (void)viewDidLoad { UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(0, 0, 51, 31); [backBtn setImage:[UIImage imageNamed:@"nav_btn_back.png"] forState:UIControlStateNormal]; [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *back = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease]; self.navigationItem.backBarButtonItem = back; self.loadingIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease]; [super viewDidLoad]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } - (void)viewWillAppear:(BOOL)animated { id storyId = [appDelegate.activeStory objectForKey:@"id"]; if (self.activeStoryId != storyId) { [appDelegate pushReadStory:storyId]; [self setActiveStory]; [self showStory]; [self markStoryAsRead]; [self setNextPreviousButtons]; self.webView.scalesPageToFit = YES; } [self.loadingIndicator stopAnimating]; [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { UIBarButtonItem *originalButton = [[UIBarButtonItem alloc] initWithTitle:@"Original" style:UIBarButtonItemStyleBordered target:self action:@selector(showOriginalSubview:) ]; if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) { appDelegate.splitStoryDetailViewController.navigationItem.rightBarButtonItem = originalButton; } else { self.navigationItem.rightBarButtonItem = originalButton; } [originalButton release]; [super viewDidAppear:animated]; } - (void)viewDidDisappear:(BOOL)animated { Class viewClass = [appDelegate.navigationController.visibleViewController class]; if (viewClass == [appDelegate.feedDetailViewController class] || viewClass == [appDelegate.feedsViewController class]) { self.activeStoryId = nil; [webView loadHTMLString:@"" baseURL:[NSURL URLWithString:@""]]; } } #pragma mark - #pragma mark Story layout - (NSString *)getAvatars:(BOOL)areFriends { NSString *avatarString = @""; NSArray *share_user_ids; if (areFriends) { NSMutableArray *friends = [appDelegate.activeStory objectForKey:@"share_user_ids"]; NSArray *all_share_user_ids = [appDelegate.activeStory objectForKey:@"share_user_ids"]; NSArray *all_shared_by_public = [appDelegate.activeStory objectForKey:@"shared_by_public"]; for (int i = 0; i < all_share_user_ids.count; i++) { for (int j = 0; j < all_shared_by_public.count; j++) { if ([[all_share_user_ids objectAtIndex:i] intValue] == [[all_shared_by_public objectAtIndex:j] intValue]) { [friends removeObject:[all_share_user_ids objectAtIndex:i]]; break; } } } share_user_ids = [NSArray arrayWithArray:friends]; // only if your friends are sharing to do you see the shared label if ([share_user_ids count]) { avatarString = [avatarString stringByAppendingString:@ "
Shared by:
" "
"]; } } else { share_user_ids = [appDelegate.activeStory objectForKey:@"shared_by_public"]; } for (int i = 0; i < share_user_ids.count; i++) { NSDictionary *user = [self getUser:[[share_user_ids objectAtIndex:i] intValue]]; NSString *avatar = [NSString stringWithFormat:@ "
" "" "
", [user objectForKey:@"photo_url"]]; avatarString = [avatarString stringByAppendingString:avatar]; } if (areFriends && [share_user_ids count]) { avatarString = [avatarString stringByAppendingString:@"" "
" "
" "
Shared by %@
" "
" "%@" "
" "%@" "
", [[appDelegate.activeStory objectForKey:@"share_count"] intValue] == 1 ? [NSString stringWithFormat:@"1 person"] : [NSString stringWithFormat:@"%@ people", [appDelegate.activeStory objectForKey:@"share_count"]], [self getAvatars:NO], [self getAvatars:YES] ]]; for (int i = 0; i < comments_array.count; i++) { NSDictionary *comment_dict = [comments_array objectAtIndex:i]; NSDictionary *user = [self getUser:[[comment_dict objectForKey:@"user_id"] intValue]]; NSString *comment = [NSString stringWithFormat:@ "
" "
" "
" "
%@
" "
%@
" "
" "reply" "
" "
" "
%@
" "%@" "
", [user objectForKey:@"photo_url"], [user objectForKey:@"username"], [comment_dict objectForKey:@"shared_date"], [comment_dict objectForKey:@"comments"], [self getReplies:[comment_dict objectForKey:@"replies"]]]; comments = [comments stringByAppendingString:comment]; } comments = [comments stringByAppendingString:[NSString stringWithFormat:@"
"]]; } return comments; } - (NSString *)getReplies:(NSArray *)replies { NSString *repliesString = @""; if (replies.count > 0) { repliesString = [repliesString stringByAppendingString:@"
"]; for (int i = 0; i < replies.count; i++) { NSDictionary *reply_dict = [replies objectAtIndex:i]; NSDictionary *user = [self getUser:[[reply_dict objectForKey:@"user_id"] intValue]]; NSString *reply = [NSString stringWithFormat:@ "
" "" "
%@
" "
%@
" "
%@
" "
", [user objectForKey:@"photo_url"], [user objectForKey:@"username"], [reply_dict objectForKey:@"publish_date"], [reply_dict objectForKey:@"comments"]]; repliesString = [repliesString stringByAppendingString:reply]; } repliesString = [repliesString stringByAppendingString:@"
"]; } return repliesString; } - (NSDictionary *)getUser:(int)user_id { for (int i = 0; i < appDelegate.activeFeedUserProfiles.count; i++) { if ([[[appDelegate.activeFeedUserProfiles objectAtIndex:i] objectForKey:@"user_id"] intValue] == user_id) { return [appDelegate.activeFeedUserProfiles objectAtIndex:i]; } } return nil; } - (void)showStory { for (id key in appDelegate.activeStory) { NSLog(@"key is: %@ value: %@", key, [appDelegate.activeStory objectForKey:key]); } NSString *commentsString = [self getComments]; NSString *customImgCssString, *universalImgCssString, *sharingHtmlString; // set up layout values based on iPad/iPhone universalImgCssString = [NSString stringWithFormat:@ "" "" "" "" ""]; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { customImgCssString = [NSString stringWithFormat:@""]; } else { customImgCssString = [NSString stringWithFormat:@""]; } sharingHtmlString = [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:@"