NewsBlur/media/ios/Classes/StoryDetailViewController.m

1382 lines
64 KiB
Mathematica
Raw Normal View History

//
// StoryDetailViewController.m
// NewsBlur
//
// Created by Samuel Clay on 6/24/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "StoryDetailViewController.h"
#import "NewsBlurAppDelegate.h"
2012-07-15 15:06:06 -07:00
#import "NewsBlurViewController.h"
#import "FeedDetailViewController.h"
#import "FontSettingsViewController.h"
#import "UserProfileViewController.h"
#import "ShareViewController.h"
2012-11-07 17:54:16 -08:00
#import "StoryPageControl.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "Base64.h"
#import "Utilities.h"
#import "NSString+HTML.h"
#import "NBContainerViewController.h"
2012-07-26 13:54:45 -07:00
#import "DataUtilities.h"
2012-07-27 16:48:30 -07:00
#import "JSON.h"
#import "SHK.h"
#import "StringHelper.h"
@implementation StoryDetailViewController
@synthesize appDelegate;
2012-06-20 07:39:49 -07:00
@synthesize activeStoryId;
@synthesize activeStory;
@synthesize innerView;
@synthesize webView;
@synthesize feedTitleGradient;
@synthesize noStorySelectedLabel;
@synthesize pullingScrollview;
@synthesize pageIndex;
@synthesize storyHUD;
2012-07-26 18:07:29 -07:00
#pragma mark -
#pragma mark View boilerplate
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
2012-07-29 00:07:17 -07:00
// settings button to right
2012-08-02 18:00:48 -07:00
// UIImage *settingsImage = [UIImage imageNamed:@"settings.png"];
// UIButton *settings = [UIButton buttonWithType:UIButtonTypeCustom];
// settings.bounds = CGRectMake(0, 0, 32, 32);
// [settings addTarget:self action:@selector(toggleFontSize:) forControlEvents:UIControlEventTouchUpInside];
// [settings setImage:settingsImage forState:UIControlStateNormal];
//
// UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
// initWithCustomView:settings];
2012-07-23 15:27:20 -07:00
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.webView.scalesPageToFit = YES;
self.webView.multipleTouchEnabled = NO;
self.pageIndex = -2;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)viewDidUnload {
[self setInnerView:nil];
[self setNoStorySelectedLabel:nil];
[super viewDidUnload];
}
- (void)viewDidDisappear:(BOOL)animated {
2012-07-23 15:27:20 -07:00
// Class viewClass = [appDelegate.navigationController.visibleViewController class];
// if (viewClass == [appDelegate.feedDetailViewController class] ||
// viewClass == [appDelegate.feedsViewController class]) {
//// self.activeStoryId = nil;
// [webView loadHTMLString:@"" baseURL:[NSURL URLWithString:@""]];
// }
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
2012-07-25 20:38:44 -07:00
// [appDelegate.shareViewController.commentField resignFirstResponder];
}
#pragma mark -
2012-11-08 17:39:32 -08:00
#pragma mark Story setup
- (void)initStory {
appDelegate.inStoryDetail = YES;
self.noStorySelectedLabel.hidden = YES;
appDelegate.shareViewController.commentField.text = nil;
2012-11-08 17:39:32 -08:00
self.webView.hidden = NO;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[appDelegate.masterContainerViewController transitionFromShareView];
}
[appDelegate hideShareView:YES];
[appDelegate resetShareComments];
2012-11-08 17:39:32 -08:00
}
- (void)drawStory {
if (self.activeStoryId == [self.activeStory objectForKey:@"id"]) {
NSLog(@"Already drawn story. Ignoring.");
// return;
}
NSString *shareBarString = [self getShareBar];
NSString *commentString = [self getComments];
NSString *headerString;
NSString *sharingHtmlString;
NSString *footerString;
NSString *fontStyleClass = @"";
NSString *fontSizeClass = @"";
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
if ([userPreferences stringForKey:@"fontStyle"]){
fontStyleClass = [fontStyleClass stringByAppendingString:[userPreferences stringForKey:@"fontStyle"]];
} else {
fontStyleClass = [fontStyleClass stringByAppendingString:@"NB-san-serif"];
}
if ([userPreferences stringForKey:@"fontSizing"]){
fontSizeClass = [fontSizeClass stringByAppendingString:[userPreferences stringForKey:@"fontSizing"]];
} else {
fontSizeClass = [fontSizeClass stringByAppendingString:@"NB-medium"];
}
2012-11-07 17:54:16 -08:00
int contentWidth = self.appDelegate.storyPageControl.view.frame.size.width;
NSString *contentWidthClass;
if (contentWidth > 700) {
contentWidthClass = @"NB-ipad-wide";
} else if (contentWidth > 480) {
contentWidthClass = @"NB-ipad-narrow";
} else {
contentWidthClass = @"NB-iphone";
}
// set up layout values based on iPad/iPhone
headerString = [NSString stringWithFormat:@
"<link rel=\"stylesheet\" type=\"text/css\" href=\"reader.css\" >"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"storyDetailView.css\" >"
"<meta name=\"viewport\" id=\"viewport\" content=\"width=%i, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/>",
contentWidth];
footerString = [NSString stringWithFormat:@
"<script src=\"zepto.js\"></script>"
"<script src=\"storyDetailView.js\"></script>"];
sharingHtmlString = [NSString stringWithFormat:@
"<div class='NB-share-header'></div>"
"<div class='NB-share-wrapper'><div class='NB-share-inner-wrapper'>"
"<div id=\"NB-share-button-id\" class='NB-share-button NB-button'>"
"<a href=\"http://ios.newsblur.com/share\"><div>"
"Share this story <span class=\"NB-share-icon\"></span>"
"</div></a>"
"</div>"
"</div></div>"];
NSString *storyHeader = [self getHeader];
NSString *htmlString = [NSString stringWithFormat:@
"<html>"
"<head>%@</head>" // header string
"<body id=\"story_pane\" class=\"%@\">"
" <div class=\"%@\" id=\"NB-font-style\">"
" <div class=\"%@\" id=\"NB-font-size\">"
2012-12-11 17:53:17 -08:00
" <div id=\"NB-header-container\">%@</div>" // storyHeader
" %@" // shareBar
" <div class=\"NB-story\">%@</div>"
2012-12-11 17:53:17 -08:00
" %@" // share
" <div id=\"NB-comments-wrapper\">"
" %@" // friends comments
" </div>"
" %@"
" </div>" // font-size
" </div>" // font-style
"</body>"
"</html>",
headerString,
contentWidthClass,
fontStyleClass,
fontSizeClass,
2012-12-11 17:53:17 -08:00
storyHeader,
shareBarString,
[self.activeStory objectForKey:@"story_content"],
sharingHtmlString,
commentString,
footerString
];
2012-12-11 12:54:50 -08:00
NSLog(@"\n\n\n\nhtmlString:\n\n\n%@\n\n\n", htmlString);
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
NSDictionary *feed;
NSString *feedIdStr = [NSString stringWithFormat:@"%@",
[self.activeStory
objectForKey:@"story_feed_id"]];
if (appDelegate.isSocialView || appDelegate.isSocialRiverView) {
feed = [appDelegate.dictActiveFeeds objectForKey:feedIdStr];
// this is to catch when a user is already subscribed
if (!feed) {
feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
}
} else {
feed = [appDelegate.dictFeeds objectForKey:feedIdStr];
}
self.feedTitleGradient = [appDelegate
makeFeedTitleGradient:feed
withRect:CGRectMake(0, -1, self.view.frame.size.width, 21)]; // 1024 hack for self.webView.frame.size.width
self.feedTitleGradient.tag = FEED_TITLE_GRADIENT_TAG; // Not attached yet. Remove old gradients, first.
[self.feedTitleGradient.layer setShadowColor:[[UIColor blackColor] CGColor]];
[self.feedTitleGradient.layer setShadowOffset:CGSizeMake(0, 0)];
[self.feedTitleGradient.layer setShadowOpacity:0];
[self.feedTitleGradient.layer setShadowRadius:12.0];
for (UIView *subview in self.webView.subviews) {
if (subview.tag == FEED_TITLE_GRADIENT_TAG) {
[subview removeFromSuperview];
}
}
if (appDelegate.isRiverView || appDelegate.isSocialView) {
self.webView.scrollView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
self.webView.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(20, 0, 0, 0);
} else {
self.webView.scrollView.contentInset = UIEdgeInsetsMake(9, 0, 0, 0);
self.webView.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(9, 0, 0, 0);
}
[self.webView insertSubview:feedTitleGradient aboveSubview:self.webView.scrollView];
// [self.webView.scrollView setContentOffset:CGPointMake(0, (appDelegate.isRiverView ||
// appDelegate.isSocialView) ? -20 : -9)
// animated:NO];
[self.webView.scrollView addObserver:self forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
self.activeStoryId = [self.activeStory objectForKey:@"id"];
}
2012-11-08 17:39:32 -08:00
- (void)showStory {
id storyId = [self.activeStory objectForKey:@"id"];
[appDelegate pushReadStory:storyId];
}
- (void)clearStory {
self.activeStoryId = nil;
2012-11-09 14:13:44 -08:00
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
2012-11-08 17:39:32 -08:00
}
- (void)hideStory {
self.activeStoryId = nil;
2012-11-08 17:39:32 -08:00
self.webView.hidden = YES;
self.noStorySelectedLabel.hidden = NO;
}
#pragma mark -
#pragma mark Story layout
- (NSString *)getHeader {
NSString *feedId = [NSString stringWithFormat:@"%@", [appDelegate.activeStory
objectForKey:@"story_feed_id"]];
NSString *storyAuthor = @"";
if ([[self.activeStory objectForKey:@"story_authors"] class] != [NSNull class] &&
[[self.activeStory objectForKey:@"story_authors"] length]) {
NSString *author = [NSString stringWithFormat:@"%@",
[self.activeStory objectForKey:@"story_authors"]];
if (author && [author class] != [NSNull class]) {
int authorScore;
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
authorScore = [[[[appDelegate.activeClassifiers objectForKey:feedId]
objectForKey:@"authors"]
objectForKey:author] intValue];
} else {
authorScore = [[[appDelegate.activeClassifiers objectForKey:@"authors"]
objectForKey:author] intValue];
}
storyAuthor = [NSString stringWithFormat:@"<a href=\"http://ios.newsblur.com/classify-author/%@\" "
"class=\"NB-story-author %@\" id=\"NB-story-author\"><div class=\"NB-highlight\"></div>%@</a>",
author,
authorScore > 0 ? @"NB-story-author-positive" : authorScore < 0 ? @"NB-story-author-negative" : @"",
author];
}
}
NSString *storyTags = @"";
if ([self.activeStory objectForKey:@"story_tags"]) {
NSArray *tagArray = [self.activeStory objectForKey:@"story_tags"];
if ([tagArray count] > 0) {
NSMutableArray *tagStrings = [NSMutableArray array];
for (NSString *tag in tagArray) {
int tagScore;
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
tagScore = [[[[appDelegate.activeClassifiers objectForKey:feedId]
objectForKey:@"tags"]
objectForKey:tag] intValue];
} else {
tagScore = [[[appDelegate.activeClassifiers objectForKey:@"tags"]
objectForKey:tag] intValue];
}
NSString *tagHtml = [NSString stringWithFormat:@"<a href=\"http://ios.newsblur.com/classify-tag/%@\" "
"class=\"NB-story-tag %@\"><div class=\"NB-highlight\"></div>%@</a>",
tag,
tagScore > 0 ? @"NB-story-tag-positive" : tagScore < 0 ? @"NB-story-tag-negative" : @"",
tag];
[tagStrings addObject:tagHtml];
}
storyTags = [NSString
stringWithFormat:@"<div id=\"NB-story-tags\" class=\"NB-story-tags\">"
"%@"
"</div>",
[tagStrings componentsJoinedByString:@""]];
}
}
NSString *storyTitle = [self.activeStory objectForKey:@"story_title"];
NSMutableDictionary *titleClassifiers;
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
titleClassifiers = [[appDelegate.activeClassifiers objectForKey:feedId]
objectForKey:@"titles"];
} else {
titleClassifiers = [appDelegate.activeClassifiers objectForKey:@"titles"];
}
for (NSString *titleClassifier in titleClassifiers) {
if ([storyTitle containsString:titleClassifier]) {
int titleScore = [[titleClassifiers objectForKey:titleClassifier] intValue];
storyTitle = [storyTitle
stringByReplacingOccurrencesOfString:titleClassifier
withString:[NSString stringWithFormat:@"<span class=\"NB-story-title-%@\">%@</span>",
titleScore > 0 ? @"positive" : titleScore < 0 ? @"negative" : @"",
titleClassifier]];
}
}
NSString *storyHeader = [NSString stringWithFormat:@
"<div class=\"NB-header\"><div class=\"NB-header-inner\">"
"<div class=\"NB-story-date\">%@</div>"
"<div class=\"NB-story-title\">%@</div>"
"%@"
"%@"
"</div></div>",
[storyTags length] ?
[self.activeStory objectForKey:@"long_parsed_date"] :
[self.activeStory objectForKey:@"short_parsed_date"],
storyTitle,
storyAuthor,
storyTags];
return storyHeader;
}
2012-08-10 09:45:44 -07:00
- (NSString *)getAvatars:(NSString *)key {
NSString *avatarString = @"";
NSArray *shareUserIds = [self.activeStory objectForKey:key];
for (int i = 0; i < shareUserIds.count; i++) {
NSDictionary *user = [self getUser:[[shareUserIds objectAtIndex:i] intValue]];
2012-08-13 15:34:53 -07:00
NSString *avatarClass = @"NB-user-avatar";
if ([key isEqualToString:@"commented_by_public"] ||
[key isEqualToString:@"shared_by_public"]) {
avatarClass = @"NB-public-user NB-user-avatar";
}
NSString *avatar = [NSString stringWithFormat:@
2012-08-13 15:34:53 -07:00
"<div class=\"NB-story-share-profile\"><div class=\"%@\">"
"<a id=\"NB-user-share-bar-%@\" class=\"NB-show-profile\" "
" href=\"http://ios.newsblur.com/show-profile/%@\">"
"<div class=\"NB-highlight\"></div>"
"<img src=\"%@\" />"
"</a>"
"</div></div>",
2012-08-13 15:34:53 -07:00
avatarClass,
2012-07-01 21:58:55 -07:00
[user objectForKey:@"user_id"],
2012-08-01 12:41:02 -07:00
[user objectForKey:@"user_id"],
[user objectForKey:@"photo_url"]];
avatarString = [avatarString stringByAppendingString:avatar];
}
2012-08-10 09:45:44 -07:00
return avatarString;
}
2012-08-07 11:27:00 -07:00
- (NSString *)getComments {
NSString *comments = @"<div class=\"NB-feed-story-comments\">";
if ([self.activeStory objectForKey:@"share_count"] != [NSNull null] &&
[[self.activeStory objectForKey:@"share_count"] intValue] > 0) {
NSDictionary *story = self.activeStory;
NSArray *friendsCommentsArray = [story objectForKey:@"friend_comments"];
NSArray *publicCommentsArray = [story objectForKey:@"public_comments"];
2012-08-07 11:27:00 -07:00
// add friends comments
for (int i = 0; i < friendsCommentsArray.count; i++) {
NSString *comment = [self getComment:[friendsCommentsArray objectAtIndex:i]];
comments = [comments stringByAppendingString:comment];
}
if ([[story objectForKey:@"comment_count_public"] intValue] > 0 ) {
NSString *publicCommentHeader = [NSString stringWithFormat:@
"<div class=\"NB-story-comments-public-header-wrapper\">"
"<div class=\"NB-story-comments-public-header\">%i public comment%@</div>"
"</div>",
[[story objectForKey:@"comment_count_public"] intValue],
[[story objectForKey:@"comment_count_public"] intValue] == 1 ? @"" : @"s"];
comments = [comments stringByAppendingString:publicCommentHeader];
// add friends comments
for (int i = 0; i < publicCommentsArray.count; i++) {
NSString *comment = [self getComment:[publicCommentsArray objectAtIndex:i]];
comments = [comments stringByAppendingString:comment];
}
}
comments = [comments stringByAppendingString:[NSString stringWithFormat:@"</div>"]];
}
return comments;
}
- (NSString *)getShareBar {
2012-08-13 15:34:53 -07:00
NSString *comments = @"<div id=\"NB-share-bar-wrapper\">";
2012-08-10 09:45:44 -07:00
NSString *commentLabel = @"";
NSString *shareLabel = @"";
2012-08-13 15:34:53 -07:00
// NSString *replyStr = @"";
2012-08-10 09:45:44 -07:00
// if ([[self.activeStory objectForKey:@"reply_count"] intValue] == 1) {
2012-08-13 15:34:53 -07:00
// replyStr = [NSString stringWithFormat:@" and <b>1 reply</b>"];
// } else if ([[self.activeStory objectForKey:@"reply_count"] intValue] == 1) {
// replyStr = [NSString stringWithFormat:@" and <b>%@ replies</b>", [self.activeStory objectForKey:@"reply_count"]];
2012-08-13 15:34:53 -07:00
// }
if (![[self.activeStory objectForKey:@"comment_count"] isKindOfClass:[NSNull class]] &&
[[self.activeStory objectForKey:@"comment_count"] intValue]) {
2012-08-10 09:45:44 -07:00
commentLabel = [commentLabel stringByAppendingString:[NSString stringWithFormat:@
"<div class=\"NB-story-comments-label\">"
2012-12-11 17:53:17 -08:00
"%@" // comment count
//"%@" // reply count
2012-08-10 09:45:44 -07:00
"</div>"
"<div class=\"NB-story-share-profiles NB-story-share-profiles-comments\">"
2012-12-11 17:53:17 -08:00
"%@" // friend avatars
"%@" // public avatars
2012-08-10 09:45:44 -07:00
"</div>",
[[self.activeStory objectForKey:@"comment_count"] intValue] == 1
2012-08-10 09:45:44 -07:00
? [NSString stringWithFormat:@"<b>1 comment</b>"] :
[NSString stringWithFormat:@"<b>%@ comments</b>", [self.activeStory objectForKey:@"comment_count"]],
2012-08-10 09:45:44 -07:00
2012-08-13 15:34:53 -07:00
//replyStr,
2012-08-10 09:45:44 -07:00
[self getAvatars:@"commented_by_friends"],
[self getAvatars:@"commented_by_public"]]];
}
if (![[self.activeStory objectForKey:@"share_count"] isKindOfClass:[NSNull class]] &&
[[self.activeStory objectForKey:@"share_count"] intValue]) {
2012-08-10 09:45:44 -07:00
shareLabel = [shareLabel stringByAppendingString:[NSString stringWithFormat:@
2012-08-13 15:34:53 -07:00
"<div class=\"NB-right\">"
"<div class=\"NB-story-share-profiles NB-story-share-profiles-shares\">"
2012-08-10 09:45:44 -07:00
"%@" // friend avatars
"%@" // public avatars
2012-08-13 15:34:53 -07:00
"</div>"
2012-12-11 17:53:17 -08:00
"<div class=\"NB-story-share-label\">"
"%@" // comment count
"</div>"
2012-08-13 15:34:53 -07:00
"</div>",
[self getAvatars:@"shared_by_public"],
[self getAvatars:@"shared_by_friends"],
[[self.activeStory objectForKey:@"share_count"] intValue] == 1
2012-08-10 09:45:44 -07:00
? [NSString stringWithFormat:@"<b>1 share</b>"] :
[NSString stringWithFormat:@"<b>%@ shares</b>", [self.activeStory objectForKey:@"share_count"]]]];
2012-08-10 09:45:44 -07:00
}
2012-08-07 11:27:00 -07:00
if ([self.activeStory objectForKey:@"share_count"] != [NSNull null] &&
[[self.activeStory objectForKey:@"share_count"] intValue] > 0) {
2012-08-02 18:33:55 -07:00
2012-08-09 17:01:37 -07:00
comments = [comments stringByAppendingString:[NSString stringWithFormat:@
"<div class=\"NB-story-shares\">"
2012-12-11 17:53:17 -08:00
"<div class=\"NB-story-comments-shares-teaser-wrapper\">"
"<div class=\"NB-story-comments-shares-teaser\">"
"%@"
"%@"
"</div>"
"</div>"
"</div>",
2012-08-10 09:45:44 -07:00
commentLabel,
shareLabel
2012-08-09 17:01:37 -07:00
]];
2012-06-22 15:53:51 -07:00
}
2012-08-13 15:34:53 -07:00
comments = [comments stringByAppendingString:[NSString stringWithFormat:@"</div>"]];
2012-06-22 15:53:51 -07:00
return comments;
}
2012-06-25 20:28:07 -07:00
- (NSString *)getComment:(NSDictionary *)commentDict {
2012-06-25 20:28:07 -07:00
NSDictionary *user = [self getUser:[[commentDict objectForKey:@"user_id"] intValue]];
NSString *userAvatarClass = @"NB-user-avatar";
NSString *userReshareString = @"";
NSString *userEditButton = @"";
NSString *userLikeButton = @"";
NSString *commentUserId = [NSString stringWithFormat:@"%@", [commentDict objectForKey:@"user_id"]];
NSString *currentUserId = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"user_id"]];
2012-07-26 13:54:45 -07:00
NSArray *likingUsers = [commentDict objectForKey:@"liking_users"];
if ([commentUserId isEqualToString:currentUserId]) {
userEditButton = [NSString stringWithFormat:@
"<div class=\"NB-story-comment-edit-button NB-story-comment-share-edit-button NB-button\">"
"<a href=\"http://ios.newsblur.com/edit-share/%@\"><div class=\"NB-story-comment-edit-button-wrapper\">"
"Edit"
"</div></a>"
2012-07-20 18:04:54 -07:00
"</div>",
commentUserId];
} else {
2012-07-26 13:54:45 -07:00
BOOL isInLikingUsers = NO;
for (int i = 0; i < likingUsers.count; i++) {
if ([[[likingUsers objectAtIndex:i] stringValue] isEqualToString:currentUserId]) {
isInLikingUsers = YES;
break;
}
}
if (isInLikingUsers) {
userLikeButton = [NSString stringWithFormat:@
"<div class=\"NB-story-comment-like-button NB-button selected\">"
"<a href=\"http://ios.newsblur.com/unlike-comment/%@\"><div class=\"NB-story-comment-like-button-wrapper\">"
2012-12-11 17:53:17 -08:00
"<span class=\"NB-favorite-icon\"></span>"
"</div></a>"
2012-07-26 13:54:45 -07:00
"</div>",
commentUserId];
} else {
userLikeButton = [NSString stringWithFormat:@
"<div class=\"NB-story-comment-like-button NB-button\">"
"<a href=\"http://ios.newsblur.com/like-comment/%@\"><div class=\"NB-story-comment-like-button-wrapper\">"
2012-12-11 17:53:17 -08:00
"<span class=\"NB-favorite-icon\"></span>"
"</div></a>"
2012-07-26 13:54:45 -07:00
"</div>",
commentUserId];
}
}
if ([commentDict objectForKey:@"source_user_id"] != [NSNull null]) {
userAvatarClass = @"NB-user-avatar NB-story-comment-reshare";
NSDictionary *sourceUser = [self getUser:[[commentDict objectForKey:@"source_user_id"] intValue]];
userReshareString = [NSString stringWithFormat:@
"<div class=\"NB-story-comment-reshares\">"
" <div class=\"NB-story-share-profile\">"
" <div class=\"NB-user-avatar\"><img src=\"%@\"></div>"
" </div>"
"</div>",
[sourceUser objectForKey:@"photo_url"]];
}
NSString *commentContent = [self textToHtml:[commentDict objectForKey:@"comments"]];
2012-07-29 13:24:03 -07:00
NSString *comment;
2012-08-09 16:34:59 -07:00
NSString *locationHtml = @"";
NSString *location = [NSString stringWithFormat:@"%@", [user objectForKey:@"location"]];
if (location.length) {
locationHtml = [NSString stringWithFormat:@"<div class=\"NB-story-comment-location\">%@</div>", location];
}
2012-07-29 13:24:03 -07:00
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
comment = [NSString stringWithFormat:@
"<div class=\"NB-story-comment\" id=\"NB-user-comment-%@\">"
"<div class=\"%@\">"
"<a class=\"NB-show-profile\" href=\"http://ios.newsblur.com/show-profile/%@\">"
"<div class=\"NB-highlight\"></div>"
"<img src=\"%@\" />"
"</a>"
"</div>"
2012-07-29 13:24:03 -07:00
"<div class=\"NB-story-comment-author-container\">"
" %@"
" <div class=\"NB-story-comment-username\">%@</div>"
" <div class=\"NB-story-comment-date\">%@ ago</div>"
2012-12-11 17:53:17 -08:00
"</div>"
"<div class=\"NB-story-comment-content\">%@</div>"
"%@" // location
"<div class=\"NB-button-wrapper\">"
2012-07-29 13:24:03 -07:00
" <div class=\"NB-story-comment-reply-button NB-button\">"
" <a href=\"http://ios.newsblur.com/reply/%@/%@\"><div class=\"NB-story-comment-reply-button-wrapper\">"
" Reply"
" </div></a>"
2012-07-29 13:24:03 -07:00
" </div>"
2012-12-11 17:53:17 -08:00
" %@" //User Like Button
" %@" //User Edit Button
2012-07-29 13:24:03 -07:00
"</div>"
"%@"
"</div>",
[commentDict objectForKey:@"user_id"],
userAvatarClass,
[commentDict objectForKey:@"user_id"],
[user objectForKey:@"photo_url"],
userReshareString,
[user objectForKey:@"username"],
[commentDict objectForKey:@"shared_date"],
2012-12-11 17:53:17 -08:00
commentContent,
locationHtml,
2012-08-13 15:34:53 -07:00
[commentDict objectForKey:@"user_id"],
[user objectForKey:@"username"],
2012-12-11 17:53:17 -08:00
userEditButton,
userLikeButton,
[self getReplies:[commentDict objectForKey:@"replies"] forUserId:[commentDict objectForKey:@"user_id"]]];
2012-07-29 13:24:03 -07:00
} else {
comment = [NSString stringWithFormat:@
"<div class=\"NB-story-comment\" id=\"NB-user-comment-%@\">"
"<div class=\"%@\">"
"<a class=\"NB-show-profile\" href=\"http://ios.newsblur.com/show-profile/%@\">"
"<div class=\"NB-highlight\"></div>"
"<img src=\"%@\" />"
"</a>"
"</div>"
2012-07-29 13:24:03 -07:00
"<div class=\"NB-story-comment-author-container\">"
2012-12-11 17:53:17 -08:00
" %@"
2012-07-29 13:24:03 -07:00
" <div class=\"NB-story-comment-username\">%@</div>"
" <div class=\"NB-story-comment-date\">%@ ago</div>"
"</div>"
2012-08-13 16:20:23 -07:00
"<div class=\"NB-story-comment-content\">%@</div>"
2012-12-11 17:53:17 -08:00
"%@" // location
"<div class=\"NB-button-wrapper\">"
2012-07-29 13:24:03 -07:00
" <div class=\"NB-story-comment-reply-button NB-button\">"
" <a href=\"http://ios.newsblur.com/reply/%@/%@\"><div class=\"NB-story-comment-reply-button-wrapper\">"
" Reply"
" </div></a>"
2012-07-29 13:24:03 -07:00
" </div>"
2012-12-11 17:53:17 -08:00
" %@" // User Like Button
" %@" // User Edit Button
2012-07-29 13:24:03 -07:00
"</div>"
"%@"
"</div>",
[commentDict objectForKey:@"user_id"],
userAvatarClass,
[commentDict objectForKey:@"user_id"],
[user objectForKey:@"photo_url"],
userReshareString,
[user objectForKey:@"username"],
[commentDict objectForKey:@"shared_date"],
commentContent,
2012-12-11 17:53:17 -08:00
locationHtml,
2012-08-13 16:20:23 -07:00
[commentDict objectForKey:@"user_id"],
[user objectForKey:@"username"],
2012-12-11 17:53:17 -08:00
userEditButton,
userLikeButton,
2012-07-29 13:24:03 -07:00
[self getReplies:[commentDict objectForKey:@"replies"] forUserId:[commentDict objectForKey:@"user_id"]]];
2012-07-29 13:24:03 -07:00
}
2012-06-25 20:28:07 -07:00
return comment;
}
2012-07-20 15:54:10 -07:00
- (NSString *)getReplies:(NSArray *)replies forUserId:(NSString *)commentUserId {
NSString *repliesString = @"";
if (replies.count > 0) {
repliesString = [repliesString stringByAppendingString:@"<div class=\"NB-story-comment-replies\">"];
for (int i = 0; i < replies.count; i++) {
2012-07-20 15:54:10 -07:00
NSDictionary *replyDict = [replies objectAtIndex:i];
NSDictionary *user = [self getUser:[[replyDict objectForKey:@"user_id"] intValue]];
NSString *userEditButton = @"";
NSString *replyUserId = [NSString stringWithFormat:@"%@", [replyDict objectForKey:@"user_id"]];
NSString *replyId = [replyDict objectForKey:@"reply_id"];
2012-07-20 15:54:10 -07:00
NSString *currentUserId = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"user_id"]];
if ([replyUserId isEqualToString:currentUserId]) {
userEditButton = [NSString stringWithFormat:@
"<div class=\"NB-story-comment-edit-button NB-story-comment-share-edit-button NB-button\">"
"<a href=\"http://ios.newsblur.com/edit-reply/%@/%@/%@\">"
2012-07-20 15:54:10 -07:00
"<div class=\"NB-story-comment-edit-button-wrapper\">"
2012-08-13 16:20:23 -07:00
"Edit"
2012-07-20 15:54:10 -07:00
"</div>"
"</a>"
2012-07-20 15:54:10 -07:00
"</div>",
commentUserId,
replyUserId,
replyId
2012-07-20 15:54:10 -07:00
];
}
NSString *replyContent = [self textToHtml:[replyDict objectForKey:@"comments"]];
2012-08-09 16:45:52 -07:00
NSString *locationHtml = @"";
NSString *location = [NSString stringWithFormat:@"%@", [user objectForKey:@"location"]];
if (location.length) {
locationHtml = [NSString stringWithFormat:@"<div class=\"NB-story-comment-location\">%@</div>", location];
}
2012-07-21 18:25:56 -07:00
2012-08-13 15:34:53 -07:00
NSString *reply;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
reply = [NSString stringWithFormat:@
2012-12-11 17:53:17 -08:00
"<div class=\"NB-story-comment-reply\" id=\"NB-user-comment-%@\">"
" <a class=\"NB-show-profile\" href=\"http://ios.newsblur.com/show-profile/%@\">"
" <div class=\"NB-highlight\"></div>"
" <img class=\"NB-story-comment-reply-photo\" src=\"%@\" />"
" </a>"
" <div class=\"NB-story-comment-username NB-story-comment-reply-username\">%@</div>"
" <div class=\"NB-story-comment-date NB-story-comment-reply-date\">%@ ago</div>"
" <div class=\"NB-story-comment-reply-content\">%@</div>"
" %@" // location
" <div class=\"NB-button-wrapper\">"
" %@" // edit
" </div>"
"</div>",
[replyDict objectForKey:@"reply_id"],
[user objectForKey:@"user_id"],
[user objectForKey:@"photo_url"],
[user objectForKey:@"username"],
[replyDict objectForKey:@"publish_date"],
replyContent,
locationHtml,
userEditButton];
2012-08-13 15:34:53 -07:00
} else {
2012-08-13 16:20:23 -07:00
reply = [NSString stringWithFormat:@
"<div class=\"NB-story-comment-reply\" id=\"NB-user-comment-%@\">"
" <a class=\"NB-show-profile\" href=\"http://ios.newsblur.com/show-profile/%@\">"
" <div class=\"NB-highlight\"></div>"
2012-08-13 16:20:23 -07:00
" <img class=\"NB-story-comment-reply-photo\" src=\"%@\" />"
" </a>"
" <div class=\"NB-story-comment-username NB-story-comment-reply-username\">%@</div>"
" <div class=\"NB-story-comment-date NB-story-comment-reply-date\">%@ ago</div>"
" <div class=\"NB-story-comment-reply-content\">%@</div>"
2012-12-11 17:53:17 -08:00
" %@"
" <div class=\"NB-button-wrapper\">"
" %@" // edit
" </div>"
2012-08-13 16:20:23 -07:00
"</div>",
[replyDict objectForKey:@"reply_id"],
[user objectForKey:@"user_id"],
[user objectForKey:@"photo_url"],
[user objectForKey:@"username"],
[replyDict objectForKey:@"publish_date"],
replyContent,
2012-12-11 17:53:17 -08:00
locationHtml,
2012-08-13 16:20:23 -07:00
userEditButton];
2012-08-13 15:34:53 -07:00
}
repliesString = [repliesString stringByAppendingString:reply];
}
repliesString = [repliesString stringByAppendingString:@"</div>"];
}
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)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (keyPath == @"contentOffset") {
if (self.webView.scrollView.contentOffset.y < (-1 * self.feedTitleGradient.frame.size.height + 1)) {
// Pulling
if (!pullingScrollview) {
pullingScrollview = YES;
[self.feedTitleGradient.layer setShadowOpacity:.5];
[self.webView insertSubview:self.feedTitleGradient belowSubview:self.webView.scrollView];
for (id subview in self.webView.scrollView.subviews) {
UIImageView *imgView = [subview isKindOfClass:[UIImageView class]] ?
(UIImageView*)subview : nil;
// image views whose image is 1px wide are shadow images, hide them
if (imgView && imgView.image.size.width == 1) {
imgView.hidden = YES;
}
}
}
float y = -1 * self.webView.scrollView.contentOffset.y - self.feedTitleGradient.frame.size.height;
self.feedTitleGradient.frame = CGRectMake(0, y,
self.feedTitleGradient.frame.size.width,
self.feedTitleGradient.frame.size.height);
} else {
// Normal reading
if (pullingScrollview) {
pullingScrollview = NO;
[self.feedTitleGradient.layer setShadowOpacity:0];
[self.webView insertSubview:self.feedTitleGradient aboveSubview:self.webView.scrollView];
self.feedTitleGradient.frame = CGRectMake(0, -1,
self.feedTitleGradient.frame.size.width,
self.feedTitleGradient.frame.size.height);
for (id subview in self.webView.scrollView.subviews) {
UIImageView *imgView = [subview isKindOfClass:[UIImageView class]] ?
(UIImageView*)subview : nil;
// image views whose image is 1px wide are shadow images, hide them
if (imgView && imgView.image.size.width == 1) {
imgView.hidden = NO;
}
}
}
}
}
}
- (void)setActiveStoryAtIndex:(NSInteger)activeStoryIndex {
if (activeStoryIndex >= 0) {
self.activeStory = [appDelegate.activeFeedStories objectAtIndex:activeStoryIndex];
} else {
self.activeStory = appDelegate.activeStory;
}
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSArray *urlComponents = [url pathComponents];
NSString *action = @"";
if ([urlComponents count] > 1) {
action = [NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:1]];
}
// HACK: Using ios.newsblur.com to intercept the javascript share, reply, and edit events.
// the pathComponents do not work correctly unless it is a correctly formed url
// Is there a better way? Someone show me the light
if ([[url host] isEqualToString: @"ios.newsblur.com"]){
// reset the active comment
appDelegate.activeComment = nil;
appDelegate.activeShareType = action;
2012-07-20 15:54:10 -07:00
if ([action isEqualToString:@"reply"] ||
[action isEqualToString:@"edit-reply"] ||
2012-07-26 13:54:45 -07:00
[action isEqualToString:@"edit-share"] ||
[action isEqualToString:@"like-comment"] ||
[action isEqualToString:@"unlike-comment"]) {
// search for the comment from friends comments
NSArray *friendComments = [self.activeStory objectForKey:@"friend_comments"];
for (int i = 0; i < friendComments.count; i++) {
NSString *userId = [NSString stringWithFormat:@"%@",
[[friendComments objectAtIndex:i] objectForKey:@"user_id"]];
if([userId isEqualToString:[NSString stringWithFormat:@"%@",
[urlComponents objectAtIndex:2]]]){
appDelegate.activeComment = [friendComments objectAtIndex:i];
}
}
if (appDelegate.activeComment == nil) {
NSArray *publicComments = [self.activeStory objectForKey:@"public_comments"];
for (int i = 0; i < publicComments.count; i++) {
NSString *userId = [NSString stringWithFormat:@"%@",
[[publicComments objectAtIndex:i] objectForKey:@"user_id"]];
if([userId isEqualToString:[NSString stringWithFormat:@"%@",
[urlComponents objectAtIndex:2]]]){
appDelegate.activeComment = [publicComments objectAtIndex:i];
}
2012-06-25 18:05:25 -07:00
}
}
2012-07-20 15:54:10 -07:00
if (appDelegate.activeComment == nil) {
NSLog(@"PROBLEM! the active comment was not found in friend or public comments");
return NO;
}
2012-07-20 15:54:10 -07:00
if ([action isEqualToString:@"reply"]) {
[appDelegate showShareView:@"reply"
setUserId:[NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:2]]
setUsername:[NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:3]]
setReplyId:nil];
2012-07-20 15:54:10 -07:00
} else if ([action isEqualToString:@"edit-reply"]) {
[appDelegate showShareView:@"edit-reply"
setUserId:[NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:2]]
setUsername:nil
setReplyId:[NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:4]]];
2012-07-20 15:54:10 -07:00
} else if ([action isEqualToString:@"edit-share"]) {
[appDelegate showShareView:@"edit-share"
setUserId:nil
setUsername:nil
setReplyId:nil];
2012-07-26 13:54:45 -07:00
} else if ([action isEqualToString:@"like-comment"]) {
[self toggleLikeComment:YES];
} else if ([action isEqualToString:@"unlike-comment"]) {
[self toggleLikeComment:NO];
2012-07-20 15:54:10 -07:00
}
return NO;
} else if ([action isEqualToString:@"share"]) {
[self openShareDialog];
return NO;
} else if ([action isEqualToString:@"classify-author"]) {
NSString *author = [NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:2]];
[self toggleAuthorClassifier:author];
return NO;
} else if ([action isEqualToString:@"classify-tag"]) {
NSString *tag = [NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:2]];
[self toggleTagClassifier:tag];
return NO;
2012-07-01 21:58:55 -07:00
} else if ([action isEqualToString:@"show-profile"]) {
appDelegate.activeUserProfileId = [NSString stringWithFormat:@"%@", [urlComponents objectAtIndex:2]];
for (int i = 0; i < appDelegate.activeFeedUserProfiles.count; i++) {
NSString *userId = [NSString stringWithFormat:@"%@", [[appDelegate.activeFeedUserProfiles objectAtIndex:i] objectForKey:@"user_id"]];
if ([userId isEqualToString:appDelegate.activeUserProfileId]){
appDelegate.activeUserProfileName = [NSString stringWithFormat:@"%@", [[appDelegate.activeFeedUserProfiles objectAtIndex:i] objectForKey:@"username"]];
break;
}
}
[self showUserProfile:[urlComponents objectAtIndex:2]
xCoordinate:[[urlComponents objectAtIndex:3] intValue]
yCoordinate:[[urlComponents objectAtIndex:4] intValue]
width:[[urlComponents objectAtIndex:5] intValue]
height:[[urlComponents objectAtIndex:6] intValue]];
2012-07-01 21:58:55 -07:00
return NO;
2012-06-22 13:15:31 -07:00
}
}
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[appDelegate showOriginalStory:url];
return NO;
}
return YES;
}
- (void)showUserProfile:(NSString *)userId xCoordinate:(int)x yCoordinate:(int)y width:(int)width height:(int)height {
CGRect frame = CGRectZero;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
2012-07-25 17:34:45 -07:00
// only adjust for the bar if user is scrolling
if (appDelegate.isRiverView || appDelegate.isSocialView) {
if (self.webView.scrollView.contentOffset.y == -20) {
y = y + 20;
2012-07-25 17:34:45 -07:00
}
} else {
if (self.webView.scrollView.contentOffset.y == -9) {
y = y + 9;
}
}
frame = CGRectMake(x, y, width, height);
}
[appDelegate showUserProfileModal:[NSValue valueWithCGRect:frame]];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
2012-06-24 23:02:37 -07:00
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
if ([userPreferences integerForKey:@"fontSizing"]){
[self changeFontSize:[userPreferences stringForKey:@"fontSizing"]];
2012-06-24 23:02:37 -07:00
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[MBProgressHUD hideHUDForView:self.view animated:YES];
2012-11-09 14:13:44 -08:00
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
if ([userPreferences integerForKey:@"fontSizing"]){
[self changeFontSize:[userPreferences stringForKey:@"fontSizing"]];
2012-06-24 23:02:37 -07:00
}
if ([appDelegate.activeFeedStories count] &&
self.activeStoryId &&
![self.webView.request.URL.absoluteString isEqualToString:@"about:blank"]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, .15 * NSEC_PER_SEC),
dispatch_get_current_queue(), ^{
[self checkTryFeedStory];
});
}
}
- (void)checkTryFeedStory {
// see if it's a tryfeed for animation
2012-11-09 14:13:44 -08:00
if (!self.webView.hidden &&
appDelegate.tryFeedCategory &&
[[appDelegate.activeStory objectForKey:@"id"] isEqualToString:appDelegate.tryFeedStoryId]) {
[MBProgressHUD hideHUDForView:appDelegate.storyPageControl.view animated:YES];
if ([appDelegate.tryFeedCategory isEqualToString:@"comment_like"] ||
[appDelegate.tryFeedCategory isEqualToString:@"comment_reply"]) {
NSString *currentUserId = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"user_id"]];
2012-08-01 12:41:02 -07:00
NSString *jsFlashString = [[NSString alloc] initWithFormat:@"slideToComment('%@', true, true);", currentUserId];
[self.webView stringByEvaluatingJavaScriptFromString:jsFlashString];
} else if ([appDelegate.tryFeedCategory isEqualToString:@"story_reshare"] ||
[appDelegate.tryFeedCategory isEqualToString:@"reply_reply"]) {
NSString *blurblogUserId = [NSString stringWithFormat:@"%@", [self.activeStory objectForKey:@"social_user_id"]];
NSString *jsFlashString = [[NSString alloc] initWithFormat:@"slideToComment('%@', true, true);", blurblogUserId];
2012-08-01 12:41:02 -07:00
[self.webView stringByEvaluatingJavaScriptFromString:jsFlashString];
}
appDelegate.tryFeedCategory = nil;
}
}
- (void)setFontStyle:(NSString *)fontStyle {
NSString *jsString;
NSString *fontStyleStr;
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
if ([fontStyle isEqualToString:@"Helvetica"]) {
[userPreferences setObject:@"NB-san-serif" forKey:@"fontStyle"];
fontStyleStr = @"NB-san-serif";
} else {
[userPreferences setObject:@"NB-serif" forKey:@"fontStyle"];
fontStyleStr = @"NB-serif";
}
[userPreferences synchronize];
jsString = [NSString stringWithFormat:@
"document.getElementById('NB-font-style').setAttribute('class', '%@')",
fontStyleStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
- (void)changeFontSize:(NSString *)fontSize {
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementById('NB-font-size').setAttribute('class', '%@')",
fontSize];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
#pragma mark -
#pragma mark Actions
2012-07-26 13:54:45 -07:00
- (void)toggleLikeComment:(BOOL)likeComment {
2012-11-09 14:13:44 -08:00
[appDelegate.storyPageControl showShareHUD:@"Favoriting"];
2012-07-26 13:54:45 -07:00
NSString *urlString;
if (likeComment) {
urlString = [NSString stringWithFormat:@"http://%@/social/like_comment",
NEWSBLUR_URL];
} else {
urlString = [NSString stringWithFormat:@"http://%@/social/remove_like_comment",
NEWSBLUR_URL];
}
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[self.activeStory
2012-07-26 13:54:45 -07:00
objectForKey:@"id"]
forKey:@"story_id"];
[request setPostValue:[self.activeStory
2012-07-26 13:54:45 -07:00
objectForKey:@"story_feed_id"]
forKey:@"story_feed_id"];
[request setPostValue:[appDelegate.activeComment objectForKey:@"user_id"] forKey:@"comment_user_id"];
[request setDidFinishSelector:@selector(finishLikeComment:)];
[request setDidFailSelector:@selector(requestFailed:)];
2012-07-26 13:54:45 -07:00
[request setDelegate:self];
[request startAsynchronous];
}
- (void)finishLikeComment:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];
2012-07-27 16:21:44 -07:00
NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *results = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
2012-07-26 13:54:45 -07:00
// add the comment into the activeStory dictionary
NSDictionary *newStory = [DataUtilities updateComment:results for:appDelegate];
// update the current story and the activeFeedStories
appDelegate.activeStory = newStory;
[self setActiveStoryAtIndex:-1];
2012-07-26 13:54:45 -07:00
NSMutableArray *newActiveFeedStories = [[NSMutableArray alloc] init];
for (int i = 0; i < appDelegate.activeFeedStories.count; i++) {
NSDictionary *feedStory = [appDelegate.activeFeedStories objectAtIndex:i];
NSString *storyId = [NSString stringWithFormat:@"%@", [feedStory objectForKey:@"id"]];
NSString *currentStoryId = [NSString stringWithFormat:@"%@", [self.activeStory objectForKey:@"id"]];
2012-07-26 13:54:45 -07:00
if ([storyId isEqualToString: currentStoryId]){
[newActiveFeedStories addObject:newStory];
} else {
[newActiveFeedStories addObject:[appDelegate.activeFeedStories objectAtIndex:i]];
}
}
appDelegate.activeFeedStories = [NSArray arrayWithArray:newActiveFeedStories];
[MBProgressHUD hideHUDForView:appDelegate.storyPageControl.view animated:NO];
[self refreshComments:@"like"];
2012-07-26 13:54:45 -07:00
}
2012-06-29 12:54:52 -07:00
- (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@"Error in story detail: %@", [request error]);
NSString *error;
if ([request error]) {
error = [NSString stringWithFormat:@"%@", [request error]];
} else {
error = @"The server barfed!";
}
[self informError:error];
2012-06-29 12:54:52 -07:00
}
- (void)openShareDialog {
// test to see if the user has commented
// search for the comment from friends comments
NSArray *friendComments = [self.activeStory objectForKey:@"friend_comments"];
NSString *currentUserId = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"user_id"]];
for (int i = 0; i < friendComments.count; i++) {
NSString *userId = [NSString stringWithFormat:@"%@",
[[friendComments objectAtIndex:i] objectForKey:@"user_id"]];
if([userId isEqualToString:currentUserId]){
appDelegate.activeComment = [friendComments objectAtIndex:i];
}
}
if (appDelegate.activeComment == nil) {
[appDelegate showShareView:@"share"
setUserId:nil
setUsername:nil
setReplyId:nil];
} else {
[appDelegate showShareView:@"edit-share"
setUserId:nil
setUsername:nil
setReplyId:nil];
}
}
# pragma mark
# pragma mark Subscribing to blurblog
- (void)subscribeToBlurblog {
2012-11-09 14:13:44 -08:00
[appDelegate.storyPageControl showShareHUD:@"Following"];
NSString *urlString = [NSString stringWithFormat:@"http://%@/social/follow",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[appDelegate.activeFeed
objectForKey:@"user_id"]
forKey:@"user_id"];
[request setDidFinishSelector:@selector(finishSubscribeToBlurblog:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)finishSubscribeToBlurblog:(ASIHTTPRequest *)request {
[MBProgressHUD hideHUDForView:appDelegate.storyPageControl.view animated:NO];
self.storyHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.storyHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
self.storyHUD.mode = MBProgressHUDModeCustomView;
self.storyHUD.removeFromSuperViewOnHide = YES;
self.storyHUD.labelText = @"Followed";
[self.storyHUD hide:YES afterDelay:1];
appDelegate.storyPageControl.navigationItem.leftBarButtonItem = nil;
[appDelegate reloadFeedsView:NO];
2012-08-07 09:57:21 -07:00
// [appDelegate.feedDetailViewController resetFeedDetail];
// [appDelegate.feedDetailViewController fetchFeedDetail:1 withCallback:nil];
}
- (void)refreshComments:(NSString *)replyId {
2012-08-13 15:34:53 -07:00
NSString *shareBarString = [self getShareBar];
2012-08-07 11:27:00 -07:00
NSString *commentString = [self getComments];
2012-06-25 20:28:07 -07:00
NSString *jsString = [[NSString alloc] initWithFormat:@
2012-08-13 15:34:53 -07:00
"document.getElementById('NB-comments-wrapper').innerHTML = '%@';"
"document.getElementById('NB-share-bar-wrapper').innerHTML = '%@';",
2012-08-13 15:34:53 -07:00
commentString,
shareBarString];
NSString *shareType = appDelegate.activeShareType;
2012-06-25 20:28:07 -07:00
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
[self.webView stringByEvaluatingJavaScriptFromString:@"attachFastClick();"];
// HACK to make the scroll event happen after the replace innerHTML event above happens.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, .15 * NSEC_PER_SEC),
dispatch_get_current_queue(), ^{
if (!replyId) {
NSString *currentUserId = [NSString stringWithFormat:@"%@",
[appDelegate.dictUserProfile objectForKey:@"user_id"]];
NSString *jsFlashString = [[NSString alloc]
initWithFormat:@"slideToComment('%@', true);", currentUserId];
[self.webView stringByEvaluatingJavaScriptFromString:jsFlashString];
} else if ([replyId isEqualToString:@"like"]) {
} else {
NSString *jsFlashString = [[NSString alloc]
initWithFormat:@"slideToComment('%@', true);", replyId];
[self.webView stringByEvaluatingJavaScriptFromString:jsFlashString];
}
});
// // adding in a simulated delay
// sleep(1);
self.storyHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.storyHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
self.storyHUD.mode = MBProgressHUDModeCustomView;
self.storyHUD.removeFromSuperViewOnHide = YES;
if ([shareType isEqualToString:@"reply"]) {
self.storyHUD.labelText = @"Replied";
} else if ([shareType isEqualToString:@"edit-reply"]) {
self.storyHUD.labelText = @"Edited Reply";
} else if ([shareType isEqualToString:@"edit-share"]) {
self.storyHUD.labelText = @"Edited Comment";
} else if ([shareType isEqualToString:@"share"]) {
self.storyHUD.labelText = @"Shared";
} else if ([shareType isEqualToString:@"like-comment"]) {
self.storyHUD.labelText = @"Favorited";
} else if ([shareType isEqualToString:@"unlike-comment"]) {
self.storyHUD.labelText = @"Unfavorited";
}
[self.storyHUD hide:YES afterDelay:1];
}
- (void)scrolltoComment {
NSString *currentUserId = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"user_id"]];
2012-07-30 15:50:52 -07:00
NSString *jsFlashString = [[NSString alloc] initWithFormat:@"slideToComment('%@', true);", currentUserId];
[self.webView stringByEvaluatingJavaScriptFromString:jsFlashString];
2012-06-25 20:28:07 -07:00
}
- (NSString *)textToHtml:(NSString*)htmlString {
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'" withString:@"&#039;"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
return htmlString;
}
2012-11-07 17:54:16 -08:00
- (void)changeWebViewWidth {
int contentWidth = self.appDelegate.storyPageControl.view.frame.size.width;
2012-06-28 11:12:38 -07:00
NSString *contentWidthClass;
if (contentWidth > 740) {
2012-06-28 11:12:38 -07:00
contentWidthClass = @"NB-ipad-wide";
} else if (contentWidth > 480) {
2012-06-28 11:12:38 -07:00
contentWidthClass = @"NB-ipad-narrow";
} else {
contentWidthClass = @"NB-iphone";
}
NSString *jsString = [[NSString alloc] initWithFormat:
@"$('body').attr('class', '%@');"
2012-07-19 15:48:35 -07:00
"document.getElementById(\"viewport\").setAttribute(\"content\", \"width=%i;initial-scale=1; maximum-scale=1.0; user-scalable=0;\");",
contentWidthClass,
contentWidth];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
2012-11-08 17:39:32 -08:00
2012-11-09 14:57:33 -08:00
// self.webView.hidden = NO;
}
#pragma mark -
#pragma mark Classifiers
- (void)toggleAuthorClassifier:(NSString *)author {
NSString *feedId = [NSString stringWithFormat:@"%@", [appDelegate.activeStory
objectForKey:@"story_feed_id"]];
int authorScore;
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
authorScore = [[[[appDelegate.activeClassifiers objectForKey:feedId]
objectForKey:@"authors"]
objectForKey:author] intValue];
} else {
authorScore = [[[appDelegate.activeClassifiers objectForKey:@"authors"]
objectForKey:author] intValue];
}
if (authorScore > 0) {
authorScore = -1;
} else if (authorScore < 0) {
authorScore = 0;
} else {
authorScore = 1;
}
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
NSMutableDictionary *feedClassifiers = [[appDelegate.activeClassifiers objectForKey:feedId]
mutableCopy];
NSMutableDictionary *authors = [[feedClassifiers objectForKey:@"authors"] mutableCopy];
[authors setObject:[NSNumber numberWithInt:authorScore] forKey:author];
[feedClassifiers setObject:authors forKey:@"authors"];
[appDelegate.activeClassifiers setObject:feedClassifiers forKey:feedId];
} else {
NSMutableDictionary *authors = [[appDelegate.activeClassifiers objectForKey:@"authors"] mutableCopy];
[authors setObject:[NSNumber numberWithInt:authorScore] forKey:author];
[appDelegate.activeClassifiers setObject:authors forKey:@"authors"];
}
[appDelegate.storyPageControl refreshHeaders];
NSString *urlString = [NSString stringWithFormat:@"http://%@/classifier/save",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:author
forKey:authorScore >= 1 ? @"like_author" :
authorScore <= -1 ? @"dislike_author" :
@"remove_like_author"];
[request setPostValue:feedId forKey:@"feed_id"];
[request setDidFinishSelector:@selector(finishTrain:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)toggleTagClassifier:(NSString *)tag {
NSString *feedId = [NSString stringWithFormat:@"%@", [appDelegate.activeStory
objectForKey:@"story_feed_id"]];
int tagScore;
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
tagScore = [[[[appDelegate.activeClassifiers objectForKey:feedId]
objectForKey:@"tags"]
objectForKey:tag] intValue];
} else {
tagScore = [[[appDelegate.activeClassifiers objectForKey:@"tags"]
objectForKey:tag] intValue];
}
if (tagScore > 0) {
tagScore = -1;
} else if (tagScore < 0) {
tagScore = 0;
} else {
tagScore = 1;
}
if (appDelegate.isRiverView || appDelegate.isSocialRiverView || appDelegate.isSocialView) {
NSMutableDictionary *feedClassifiers = [[appDelegate.activeClassifiers objectForKey:feedId]
mutableCopy];
NSMutableDictionary *tags = [[feedClassifiers objectForKey:@"tags"] mutableCopy];
[tags setObject:[NSNumber numberWithInt:tagScore] forKey:tag];
[feedClassifiers setObject:tags forKey:@"tags"];
[appDelegate.activeClassifiers setObject:feedClassifiers forKey:feedId];
} else {
NSMutableDictionary *tags = [[appDelegate.activeClassifiers objectForKey:@"tags"] mutableCopy];
[tags setObject:[NSNumber numberWithInt:tagScore] forKey:tag];
[appDelegate.activeClassifiers setObject:tags forKey:@"tags"];
}
[appDelegate.storyPageControl refreshHeaders];
NSString *urlString = [NSString stringWithFormat:@"http://%@/classifier/save",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:tag
forKey:tagScore >= 1 ? @"like_tag" :
tagScore <= -1 ? @"dislike_tag" :
@"remove_like_tag"];
[request setPostValue:feedId forKey:@"feed_id"];
[request setDidFinishSelector:@selector(finishTrain:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)finishTrain:(ASIHTTPRequest *)request {
[appDelegate.feedsViewController refreshFeedList:[self.activeStory
objectForKey:@"story_feed_id"]];
}
- (void)refreshHeader {
NSString *headerString = [[self getHeader] stringByReplacingOccurrencesOfString:@"\'" withString:@"\\'"];
NSString *jsString = [NSString stringWithFormat:@"document.getElementById('NB-header-container').innerHTML = '%@';",
headerString];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
[self.webView stringByEvaluatingJavaScriptFromString:@"attachFastClick();"];
}
@end