2012-06-21 11:53:48 -07:00
|
|
|
//
|
|
|
|
// ShareViewController.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Roy Yang on 6/21/12.
|
|
|
|
// Copyright (c) 2012 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "ShareViewController.h"
|
|
|
|
#import "NewsBlurAppDelegate.h"
|
2012-06-22 22:57:17 -07:00
|
|
|
#import "StoryDetailViewController.h"
|
2012-06-22 11:52:10 -07:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
2012-06-24 18:55:57 -07:00
|
|
|
#import "Utilities.h"
|
2012-07-20 15:54:10 -07:00
|
|
|
#import "DataUtilities.h"
|
2012-06-22 11:52:10 -07:00
|
|
|
#import "ASIHTTPRequest.h"
|
2012-06-21 11:53:48 -07:00
|
|
|
|
|
|
|
@implementation ShareViewController
|
2012-07-12 13:34:41 -07:00
|
|
|
|
2012-06-24 20:08:39 -07:00
|
|
|
@synthesize facebookButton;
|
|
|
|
@synthesize twitterButton;
|
2012-06-25 18:05:25 -07:00
|
|
|
@synthesize submitButton;
|
|
|
|
@synthesize toolbarTitle;
|
2012-06-22 11:52:10 -07:00
|
|
|
@synthesize commentField;
|
2012-06-21 11:53:48 -07:00
|
|
|
@synthesize appDelegate;
|
2012-07-30 14:58:57 -07:00
|
|
|
@synthesize activeReplyId;
|
2012-06-21 11:53:48 -07:00
|
|
|
|
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
|
|
|
|
{
|
|
|
|
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
|
|
|
if (self) {
|
|
|
|
// Custom initialization
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
// Do any additional setup after loading the view from its nib.
|
2012-06-22 11:52:10 -07:00
|
|
|
commentField.layer.borderWidth = 1.0f;
|
|
|
|
commentField.layer.cornerRadius = 8;
|
|
|
|
commentField.layer.borderColor = [[UIColor grayColor] CGColor];
|
2012-06-24 20:08:39 -07:00
|
|
|
|
2012-06-24 23:02:37 -07:00
|
|
|
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
|
2012-06-24 20:08:39 -07:00
|
|
|
if ([userPreferences integerForKey:@"shareToFacebook"]){
|
|
|
|
facebookButton.selected = YES;
|
|
|
|
}
|
|
|
|
if ([userPreferences integerForKey:@"shareToTwitter"]){
|
|
|
|
twitterButton.selected = YES;
|
|
|
|
}
|
2012-07-12 23:44:14 -07:00
|
|
|
|
|
|
|
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
|
2012-07-20 18:04:54 -07:00
|
|
|
[super viewDidLoad];
|
2012-06-21 11:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidUnload
|
|
|
|
{
|
2012-06-22 11:52:10 -07:00
|
|
|
[self setCommentField:nil];
|
2012-06-24 20:08:39 -07:00
|
|
|
[self setFacebookButton:nil];
|
|
|
|
[self setTwitterButton:nil];
|
2012-06-25 18:05:25 -07:00
|
|
|
[self setSubmitButton:nil];
|
|
|
|
[self setToolbarTitle:nil];
|
2012-06-21 11:53:48 -07:00
|
|
|
[super viewDidUnload];
|
|
|
|
// Release any retained subviews of the main view.
|
|
|
|
// e.g. self.myOutlet = nil;
|
|
|
|
}
|
|
|
|
|
2012-07-12 13:34:41 -07:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
2012-06-21 11:53:48 -07:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2012-07-26 10:17:08 -07:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
|
|
|
|
[self.commentField becomeFirstResponder];
|
|
|
|
}
|
2012-06-21 11:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)doCancelButton:(id)sender {
|
2012-07-12 13:34:41 -07:00
|
|
|
[appDelegate hideShareView:NO];
|
2012-06-21 11:53:48 -07:00
|
|
|
}
|
2012-06-22 11:52:10 -07:00
|
|
|
|
|
|
|
- (IBAction)doToggleButton:(id)sender {
|
|
|
|
UIButton *button = (UIButton *)sender;
|
2012-06-24 20:08:39 -07:00
|
|
|
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
|
2012-06-22 11:52:10 -07:00
|
|
|
if (button.selected) {
|
|
|
|
button.selected = NO;
|
2012-06-24 20:08:39 -07:00
|
|
|
if ([[button currentTitle] isEqualToString: @"Facebook"]) {
|
|
|
|
[userPreferences setInteger:0 forKey:@"shareToFacebook"];
|
|
|
|
} else if ([[button currentTitle] isEqualToString: @"Twitter"]) {
|
|
|
|
[userPreferences setInteger:0 forKey:@"shareToTwitter"];
|
|
|
|
}
|
2012-06-22 11:52:10 -07:00
|
|
|
} else {
|
|
|
|
button.selected = YES;
|
2012-06-24 20:08:39 -07:00
|
|
|
if ([[button currentTitle] isEqualToString: @"Facebook"]) {
|
|
|
|
[userPreferences setInteger:1 forKey:@"shareToFacebook"];
|
|
|
|
} else if ([[button currentTitle] isEqualToString: @"Twitter"]) {
|
|
|
|
[userPreferences setInteger:1 forKey:@"shareToTwitter"];
|
|
|
|
}
|
2012-06-22 11:52:10 -07:00
|
|
|
}
|
2012-06-24 20:08:39 -07:00
|
|
|
[userPreferences synchronize];
|
2012-06-22 11:52:10 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:58:57 -07:00
|
|
|
- (void)setSiteInfo:(NSString *)type setUserId:(NSString *)userId setUsername:(NSString *)username setReplyId:(NSString *)replyId {
|
2012-07-20 15:54:10 -07:00
|
|
|
if ([type isEqualToString: @"edit-reply"]) {
|
|
|
|
[submitButton setTitle:@"Save"];
|
|
|
|
facebookButton.hidden = YES;
|
|
|
|
twitterButton.hidden = YES;
|
|
|
|
[toolbarTitle setTitle:[NSString stringWithFormat:@"Edit Your Reply"]];
|
|
|
|
[submitButton setAction:(@selector(doReplyToComment:))];
|
2012-07-30 14:58:57 -07:00
|
|
|
self.activeReplyId = replyId;
|
2012-07-20 18:04:54 -07:00
|
|
|
|
2012-07-30 14:58:57 -07:00
|
|
|
// get existing reply
|
2012-07-20 15:54:10 -07:00
|
|
|
NSArray *replies = [appDelegate.activeComment objectForKey:@"replies"];
|
2012-07-30 14:58:57 -07:00
|
|
|
NSDictionary *reply = nil;
|
|
|
|
for (int i = 0; i < replies.count; i++) {
|
|
|
|
NSString *replyId = [NSString stringWithFormat:@"%@", [[replies objectAtIndex:i] valueForKey:@"reply_id"]];
|
|
|
|
NSLog(@"[replies objectAtIndex:i] valueForKey:@reply_id] %@", [[replies objectAtIndex:i] valueForKey:@"reply_id"]);
|
|
|
|
NSLog(@":self.activeReplyId %@", self.activeReplyId);
|
|
|
|
|
|
|
|
if ([replyId isEqualToString:self.activeReplyId]) {
|
|
|
|
reply = [replies objectAtIndex:i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reply) {
|
|
|
|
self.commentField.text = [self stringByStrippingHTML:[reply objectForKey:@"comments"]];
|
|
|
|
}
|
2012-07-20 15:54:10 -07:00
|
|
|
} else if ([type isEqualToString: @"reply"]) {
|
2012-07-30 14:58:57 -07:00
|
|
|
self.activeReplyId = nil;
|
2012-06-27 14:37:37 -07:00
|
|
|
[submitButton setTitle:@"Reply"];
|
2012-07-12 22:05:23 -07:00
|
|
|
facebookButton.hidden = YES;
|
|
|
|
twitterButton.hidden = YES;
|
2012-06-27 14:37:37 -07:00
|
|
|
[toolbarTitle setTitle:[NSString stringWithFormat:@"Reply to %@", username]];
|
2012-06-25 18:05:25 -07:00
|
|
|
[submitButton setAction:(@selector(doReplyToComment:))];
|
2012-07-20 18:04:54 -07:00
|
|
|
self.commentField.text = @"";
|
2012-07-20 15:54:10 -07:00
|
|
|
} else if ([type isEqualToString: @"edit-share"]) {
|
|
|
|
facebookButton.hidden = NO;
|
|
|
|
twitterButton.hidden = NO;
|
|
|
|
|
|
|
|
// get old comment
|
2012-07-23 16:17:49 -07:00
|
|
|
self.commentField.text = [self stringByStrippingHTML:[appDelegate.activeComment objectForKey:@"comments"]];
|
2012-07-20 15:54:10 -07:00
|
|
|
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
[toolbarTitle setTitle:@"Edit Your Comment"];
|
|
|
|
[submitButton setTitle:@"Save"];
|
|
|
|
} else {
|
2012-07-20 18:04:54 -07:00
|
|
|
[toolbarTitle setTitle:@"Edit Comment"];
|
2012-07-20 15:54:10 -07:00
|
|
|
[submitButton setTitle:@"Save"];
|
|
|
|
}
|
|
|
|
[submitButton setAction:(@selector(doShareThisStory:))];
|
2012-07-27 12:27:13 -07:00
|
|
|
} else if ([type isEqualToString: @"share"]) {
|
2012-07-12 22:05:23 -07:00
|
|
|
facebookButton.hidden = NO;
|
|
|
|
twitterButton.hidden = NO;
|
2012-07-12 23:44:14 -07:00
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
[toolbarTitle setTitle:@"Post to Blurblog"];
|
|
|
|
[submitButton setTitle:@"Share this Story"];
|
|
|
|
} else {
|
2012-07-20 18:04:54 -07:00
|
|
|
[toolbarTitle setTitle:@"Post to Blurblog"];
|
2012-07-12 23:44:14 -07:00
|
|
|
[submitButton setTitle:@"Share"];
|
|
|
|
}
|
2012-06-25 18:05:25 -07:00
|
|
|
[submitButton setAction:(@selector(doShareThisStory:))];
|
2012-07-20 18:04:54 -07:00
|
|
|
self.commentField.text = @"";
|
2012-06-25 18:05:25 -07:00
|
|
|
}
|
2012-06-27 15:18:51 -07:00
|
|
|
}
|
2012-06-25 18:05:25 -07:00
|
|
|
|
2012-06-27 15:24:18 -07:00
|
|
|
- (void)clearComments {
|
|
|
|
self.commentField.text = nil;
|
|
|
|
}
|
|
|
|
|
2012-07-27 12:27:13 -07:00
|
|
|
# pragma mark
|
|
|
|
# pragma mark Share Story
|
|
|
|
|
|
|
|
- (IBAction)doShareThisStory:(id)sender {
|
|
|
|
[appDelegate.storyDetailViewController showShareHUD];
|
2012-06-22 11:52:10 -07:00
|
|
|
NSString *urlString = [NSString stringWithFormat:@"http://%@/social/share_story",
|
|
|
|
NEWSBLUR_URL];
|
|
|
|
|
2012-07-27 16:48:30 -07:00
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
|
|
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
|
|
|
|
|
2012-06-22 11:52:10 -07:00
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]];
|
|
|
|
NSString *storyIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"id"]];
|
2012-07-26 10:17:08 -07:00
|
|
|
|
2012-06-22 11:52:10 -07:00
|
|
|
[request setPostValue:feedIdStr forKey:@"feed_id"];
|
2012-06-22 22:57:17 -07:00
|
|
|
[request setPostValue:storyIdStr forKey:@"story_id"];
|
2012-07-27 18:42:42 -07:00
|
|
|
|
|
|
|
if ([appDelegate.activeStory objectForKey:@"social_user_id"] != nil) {
|
|
|
|
NSString *sourceUserIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"social_user_id"]];
|
2012-07-26 10:17:08 -07:00
|
|
|
[request setPostValue:sourceUserIdStr forKey:@"source_user_id"];
|
|
|
|
}
|
|
|
|
|
2012-06-22 22:57:17 -07:00
|
|
|
NSString *comments = commentField.text;
|
2012-06-25 18:05:25 -07:00
|
|
|
if ([comments length]) {
|
2012-06-22 22:57:17 -07:00
|
|
|
[request setPostValue:comments forKey:@"comments"];
|
|
|
|
}
|
2012-06-22 11:52:10 -07:00
|
|
|
[request setDelegate:self];
|
2012-07-27 12:27:13 -07:00
|
|
|
[request setDidFinishSelector:@selector(finishShareThisStory:)];
|
2012-06-22 11:52:10 -07:00
|
|
|
[request setDidFailSelector:@selector(requestFailed:)];
|
|
|
|
[request startAsynchronous];
|
2012-07-27 12:27:13 -07:00
|
|
|
[appDelegate hideShareView:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishShareThisStory:(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-27 12:27:13 -07:00
|
|
|
|
2012-07-30 14:58:57 -07:00
|
|
|
[self replaceStory:[results objectForKey:@"story"] withReplyId:nil];
|
2012-06-22 11:52:10 -07:00
|
|
|
}
|
|
|
|
|
2012-07-27 12:27:13 -07:00
|
|
|
# pragma mark
|
|
|
|
# pragma mark Reply to Story
|
|
|
|
|
2012-06-25 18:05:25 -07:00
|
|
|
- (IBAction)doReplyToComment:(id)sender {
|
2012-07-27 12:27:13 -07:00
|
|
|
[appDelegate.storyDetailViewController showShareHUD];
|
2012-06-25 18:05:25 -07:00
|
|
|
NSString *comments = commentField.text;
|
|
|
|
if ([comments length] == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:58:57 -07:00
|
|
|
// NSLog(@"REPLY TO COMMENT, %@", appDelegate.activeComment);
|
2012-06-25 18:05:25 -07:00
|
|
|
NSString *urlString = [NSString stringWithFormat:@"http://%@/social/save_comment_reply",
|
|
|
|
NEWSBLUR_URL];
|
|
|
|
|
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]];
|
|
|
|
NSString *storyIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"id"]];
|
|
|
|
|
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
|
|
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
|
|
|
|
[request setPostValue:feedIdStr forKey:@"story_feed_id"];
|
|
|
|
[request setPostValue:storyIdStr forKey:@"story_id"];
|
|
|
|
[request setPostValue:[appDelegate.activeComment objectForKey:@"user_id"] forKey:@"comment_user_id"];
|
|
|
|
[request setPostValue:commentField.text forKey:@"reply_comments"];
|
|
|
|
|
2012-07-30 14:58:57 -07:00
|
|
|
if (self.activeReplyId) {
|
|
|
|
[request setPostValue:activeReplyId forKey:@"reply_id"];
|
2012-07-20 18:04:54 -07:00
|
|
|
}
|
|
|
|
|
2012-06-25 18:05:25 -07:00
|
|
|
[request setDelegate:self];
|
2012-07-20 00:21:24 -07:00
|
|
|
[request setDidFinishSelector:@selector(finishAddReply:)];
|
2012-06-25 18:05:25 -07:00
|
|
|
[request setDidFailSelector:@selector(requestFailed:)];
|
|
|
|
[request startAsynchronous];
|
2012-07-27 12:27:13 -07:00
|
|
|
[appDelegate hideShareView:YES];
|
|
|
|
[appDelegate.storyDetailViewController showShareHUD];
|
2012-06-25 18:05:25 -07:00
|
|
|
}
|
|
|
|
|
2012-07-20 00:21:24 -07:00
|
|
|
- (void)finishAddReply:(ASIHTTPRequest *)request {
|
|
|
|
NSLog(@"Successfully added.");
|
|
|
|
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-20 00:21:24 -07:00
|
|
|
// add the comment into the activeStory dictionary
|
2012-07-26 13:54:45 -07:00
|
|
|
NSDictionary *newStory = [DataUtilities updateComment:results for:appDelegate];
|
2012-07-30 14:58:57 -07:00
|
|
|
[self replaceStory:newStory withReplyId:[results objectForKey:@"reply_id"]];
|
2012-07-20 00:21:24 -07:00
|
|
|
}
|
|
|
|
|
2012-07-23 16:17:49 -07:00
|
|
|
- (void)requestFailed:(ASIHTTPRequest *)request
|
|
|
|
{
|
|
|
|
NSError *error = [request error];
|
|
|
|
NSLog(@"Error: %@", error);
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:58:57 -07:00
|
|
|
- (void)replaceStory:(NSDictionary *)newStory withReplyId:(NSString *)replyId {
|
2012-07-20 00:21:24 -07:00
|
|
|
// update the current story and the activeFeedStories
|
|
|
|
appDelegate.activeStory = newStory;
|
|
|
|
|
|
|
|
NSMutableArray *newActiveFeedStories = [[NSMutableArray alloc] init];
|
|
|
|
|
2012-07-15 18:23:08 -07:00
|
|
|
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:@"%@", [appDelegate.activeStory objectForKey:@"id"]];
|
|
|
|
if ([storyId isEqualToString: currentStoryId]){
|
2012-07-20 00:21:24 -07:00
|
|
|
[newActiveFeedStories addObject:newStory];
|
|
|
|
} else {
|
|
|
|
[newActiveFeedStories addObject:[appDelegate.activeFeedStories objectAtIndex:i]];
|
2012-07-15 18:23:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 00:21:24 -07:00
|
|
|
appDelegate.activeFeedStories = [NSArray arrayWithArray:newActiveFeedStories];
|
|
|
|
|
2012-06-27 15:24:18 -07:00
|
|
|
self.commentField.text = nil;
|
2012-07-30 14:58:57 -07:00
|
|
|
[appDelegate.storyDetailViewController refreshComments:replyId];
|
2012-06-22 11:52:10 -07:00
|
|
|
}
|
|
|
|
|
2012-06-22 22:57:17 -07:00
|
|
|
|
2012-07-25 20:38:44 -07:00
|
|
|
- (NSString *)stringByStrippingHTML:(NSString *)s {
|
2012-07-23 16:17:49 -07:00
|
|
|
NSRange r;
|
|
|
|
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
|
|
|
|
s = [s stringByReplacingCharactersInRange:r withString:@""];
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-06-21 11:53:48 -07:00
|
|
|
@end
|