NewsBlur/media/ios/Classes/ShareViewController.m

386 lines
15 KiB
Mathematica
Raw Normal View History

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"
#import "StoryDetailViewController.h"
2012-11-09 14:13:44 -08:00
#import "StoryPageControl.h"
2012-06-22 11:52:10 -07:00
#import <QuartzCore/QuartzCore.h>
#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-06-24 20:08:39 -07:00
@synthesize facebookButton;
@synthesize twitterButton;
2012-06-25 18:05:25 -07:00
@synthesize submitButton;
2012-06-22 11:52:10 -07:00
@synthesize commentField;
2012-06-21 11:53:48 -07:00
@synthesize appDelegate;
@synthesize activeReplyId;
2012-08-13 23:54:10 -07:00
@synthesize currentType;
2012-06-21 11:53:48 -07:00
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;
}
2012-08-13 18:45:06 -07:00
- (void)viewDidLoad {
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
2012-08-03 00:13:56 -07:00
// For textField1
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onTextChange:)
name:UITextViewTextDidChangeNotification
object:self.commentField];
2012-08-03 00:13:56 -07:00
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(doCancelButton:)];
self.navigationItem.leftBarButtonItem = cancel;
UIBarButtonItem *submit = [[UIBarButtonItem alloc] initWithTitle:@"Post" style:UIBarButtonSystemItemDone target:self action:@selector(doShareThisStory:)];
self.submitButton = submit;
self.navigationItem.rightBarButtonItem = submit;
2012-06-21 11:53:48 -07:00
// Do any additional setup after loading the view from its nib.
2012-06-22 11:52:10 -07:00
commentField.layer.borderWidth = 1.0f;
2012-08-13 20:09:43 -07:00
commentField.layer.cornerRadius = 4;
2012-06-22 11:52:10 -07:00
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
2012-08-13 18:45:06 -07:00
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];
2012-08-13 23:41:16 -07:00
} else {
self.submitButton.tintColor = UIColorFromRGB(0x709d3c);
2012-08-13 18:45:06 -07:00
}
2012-07-20 18:04:54 -07:00
[super viewDidLoad];
2012-06-21 11:53:48 -07:00
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
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];
2012-06-21 11:53:48 -07:00
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
2012-06-21 11:53:48 -07:00
return YES;
}
2012-08-07 11:27:00 -07:00
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self adjustCommentField];
}
- (void)viewWillAppear:(BOOL)animated {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.commentField becomeFirstResponder];
2012-08-07 11:27:00 -07:00
[self adjustCommentField];
}
}
- (void)adjustCommentField {
UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientation)){
2012-08-13 20:09:43 -07:00
self.commentField.frame = CGRectMake(20, 20, 280, 124);
self.twitterButton.frame = CGRectMake(228, 152, 32, 32);
self.facebookButton.frame = CGRectMake(268, 152, 32, 32);
2012-08-07 11:27:00 -07:00
} else {
self.commentField.frame = CGRectMake(60, 20, 400, 74);
self.twitterButton.frame = CGRectMake(15, 20, 32, 32);
2012-08-13 20:09:43 -07:00
self.facebookButton.frame = CGRectMake(15, 63, 32, 32);
}
2012-06-21 11:53:48 -07:00
}
- (IBAction)doCancelButton:(id)sender {
[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];
button.selected = !button.selected;
int selected = button.selected ? 1 : 0;
if (button.tag == 1) {
[userPreferences setInteger:selected forKey:@"shareToTwitter"];
} else if (button.tag == 2) {
[userPreferences setInteger:selected forKey:@"shareToFacebook"];
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
}
- (void)setSiteInfo:(NSString *)type setUserId:(NSString *)userId setUsername:(NSString *)username setReplyId:(NSString *)replyId {
2012-08-13 18:45:06 -07:00
[self.submitButton setStyle:UIBarButtonItemStyleDone];
2012-07-20 15:54:10 -07:00
if ([type isEqualToString: @"edit-reply"]) {
2012-08-13 23:54:10 -07:00
self.currentType = nil;
2012-08-13 19:46:47 -07:00
[submitButton setTitle:@"Save your reply"];
2012-07-20 15:54:10 -07:00
facebookButton.hidden = YES;
twitterButton.hidden = YES;
2012-08-13 19:46:47 -07:00
// self.navigationItem.title = @"Edit Your Reply";
2012-07-20 15:54:10 -07:00
[submitButton setAction:(@selector(doReplyToComment:))];
self.activeReplyId = replyId;
2012-07-20 18:04:54 -07:00
// get existing reply
2012-07-20 15:54:10 -07:00
NSArray *replies = [appDelegate.activeComment objectForKey:@"replies"];
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-08-13 23:54:10 -07:00
self.activeReplyId = nil;
2012-08-13 19:46:47 -07:00
[submitButton setTitle:[NSString stringWithFormat:@"Reply to %@", username]];
facebookButton.hidden = YES;
twitterButton.hidden = YES;
2012-08-13 19:46:47 -07:00
// self.navigationItem.title = [NSString stringWithFormat:@"Reply to %@", username];
2012-06-25 18:05:25 -07:00
[submitButton setAction:(@selector(doReplyToComment:))];
2012-08-13 23:54:10 -07:00
if (![self.currentType isEqualToString:@"share"] &&
![self.currentType isEqualToString:@"reply"]) {
self.commentField.text = @"";
self.currentType = type;
}
2012-07-20 15:54:10 -07:00
} else if ([type isEqualToString: @"edit-share"]) {
2012-08-13 23:54:10 -07:00
self.currentType = nil;
2012-07-20 15:54:10 -07:00
facebookButton.hidden = NO;
twitterButton.hidden = NO;
// get old comment
self.commentField.text = [self stringByStrippingHTML:[appDelegate.activeComment objectForKey:@"comments"]];
2012-07-20 15:54:10 -07:00
2012-08-13 19:46:47 -07:00
// if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// self.navigationItem.title = @"Edit Your Comment";
// [submitButton setTitle:@"Save your comments"];
// } else {
// self.navigationItem.title = @"Edit Comment";
// [submitButton setTitle:@"Save"];
// }
[submitButton setTitle:@"Save your comments"];
2012-07-20 15:54:10 -07:00
[submitButton setAction:(@selector(doShareThisStory:))];
} else if ([type isEqualToString: @"share"]) {
facebookButton.hidden = NO;
twitterButton.hidden = NO;
2012-08-13 19:46:47 -07:00
[submitButton setTitle:@"Share this story"];
2012-06-25 18:05:25 -07:00
[submitButton setAction:(@selector(doShareThisStory:))];
2012-08-13 23:54:10 -07:00
if (![self.currentType isEqualToString:@"share"] &&
![self.currentType isEqualToString:@"reply"]) {
self.commentField.text = @"";
self.currentType = type;
}
2012-06-25 18:05:25 -07:00
}
2012-06-27 15:18:51 -07:00
}
2012-06-25 18:05:25 -07:00
- (void)clearComments {
self.commentField.text = nil;
2012-08-13 23:54:10 -07:00
self.currentType = nil;
}
# pragma mark
# pragma mark Share Story
- (IBAction)doShareThisStory:(id)sender {
2012-11-09 14:13:44 -08:00
[appDelegate.storyPageControl showShareHUD:@"Sharing"];
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-06-22 11:52:10 -07:00
[request setPostValue:feedIdStr forKey:@"feed_id"];
[request setPostValue:storyIdStr forKey:@"story_id"];
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
if ([userPreferences integerForKey:@"shareToFacebook"]){
[request addPostValue:@"facebook" forKey:@"post_to_services"];
}
if ([userPreferences integerForKey:@"shareToTwitter"]){
[request addPostValue:@"twitter" forKey:@"post_to_services"];
}
2012-08-16 20:10:50 -07:00
if (appDelegate.isSocialRiverView) {
if ([appDelegate.activeStory objectForKey:@"friend_user_ids"] != nil) {
NSString *sourceUserIdStr = [NSString stringWithFormat:@"%@", [[appDelegate.activeStory objectForKey:@"friend_user_ids"] objectAtIndex:0]];
[request setPostValue:sourceUserIdStr forKey:@"source_user_id"];
}
} else {
if ([appDelegate.activeStory objectForKey:@"social_user_id"] != nil) {
NSString *sourceUserIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"social_user_id"]];
[request setPostValue:sourceUserIdStr forKey:@"source_user_id"];
}
}
2012-08-16 20:10:50 -07:00
NSString *comments = commentField.text;
2012-06-25 18:05:25 -07:00
if ([comments length]) {
[request setPostValue:comments forKey:@"comments"];
}
2012-06-22 11:52:10 -07:00
[request setDelegate:self];
[request setDidFinishSelector:@selector(finishShareThisStory:)];
2012-06-22 11:52:10 -07:00
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
[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];
NSArray *userProfiles = [results objectForKey:@"user_profiles"];
appDelegate.activeFeedUserProfiles = [DataUtilities
updateUserProfiles:appDelegate.activeFeedUserProfiles
withNewUserProfiles:userProfiles];
[self replaceStory:[results objectForKey:@"story"] withReplyId:nil];
2012-06-22 11:52:10 -07:00
}
# pragma mark
# pragma mark Reply to Story
2012-06-25 18:05:25 -07:00
- (IBAction)doReplyToComment:(id)sender {
2012-11-09 14:13:44 -08:00
[appDelegate.storyPageControl showShareHUD:@"Replying"];
2012-06-25 18:05:25 -07:00
NSString *comments = commentField.text;
if ([comments length] == 0) {
return;
}
// 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"];
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];
[request setDidFinishSelector:@selector(finishAddReply:)];
2012-06-25 18:05:25 -07:00
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
[appDelegate hideShareView:YES];
2012-06-25 18:05:25 -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];
// add the comment into the activeStory dictionary
2012-07-26 13:54:45 -07:00
NSDictionary *newStory = [DataUtilities updateComment:results for:appDelegate];
[self replaceStory:newStory withReplyId:[results objectForKey:@"reply_id"]];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Error: %@", error);
}
- (void)replaceStory:(NSDictionary *)newStory withReplyId:(NSString *)replyId {
2012-08-07 09:57:21 -07:00
NSMutableDictionary *newStoryParsed = [newStory mutableCopy];
[newStoryParsed setValue:[NSNumber numberWithInt:1] forKey:@"read_status"];
// update the current story and the activeFeedStories
2012-08-07 09:57:21 -07:00
appDelegate.activeStory = newStoryParsed;
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-08-07 09:57:21 -07:00
[newActiveFeedStories addObject:newStoryParsed];
} else {
[newActiveFeedStories addObject:[appDelegate.activeFeedStories objectAtIndex:i]];
2012-07-15 18:23:08 -07:00
}
}
appDelegate.activeFeedStories = [NSArray arrayWithArray:newActiveFeedStories];
self.commentField.text = nil;
[appDelegate.storyPageControl refreshPages];
[appDelegate.storyPageControl.currentPage refreshComments:replyId];
2012-06-22 11:52:10 -07:00
}
2012-07-25 20:38:44 -07:00
- (NSString *)stringByStrippingHTML:(NSString *)s {
NSRange r;
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];
return s;
}
-(void)onTextChange:(NSNotification*)notification {
NSString *text = self.commentField.text;
2012-08-13 18:45:06 -07:00
if ([self.submitButton.title isEqualToString:@"Share this story"] ||
2012-08-13 19:46:47 -07:00
[self.submitButton.title isEqualToString:@"Share with comments"]) {
if (text.length) {
2012-08-13 19:46:47 -07:00
self.submitButton.title = @"Share with comments";
} else {
2012-08-13 18:45:06 -07:00
self.submitButton.title = @"Share this story";
}
}
}
2012-06-21 11:53:48 -07:00
@end