NewsBlur/media/iphone/Classes/ShareViewController.m

260 lines
9.6 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-06-22 11:52:10 -07:00
#import <QuartzCore/QuartzCore.h>
#import "Utilities.h"
2012-06-25 20:28:07 -07:00
#import "JSON.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;
@synthesize toolbarTitle;
2012-06-22 11:52:10 -07:00
@synthesize commentField;
2012-06-21 11:53:48 -07:00
@synthesize appDelegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super 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-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;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
2012-06-21 11:53:48 -07:00
return YES;
}
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
2012-06-21 11:53:48 -07:00
- (void)dealloc {
[appDelegate release];
2012-06-22 11:52:10 -07:00
[commentField release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
2012-06-24 20:08:39 -07:00
[facebookButton release];
[twitterButton release];
2012-06-25 18:05:25 -07:00
[submitButton release];
[toolbarTitle release];
2012-06-21 11:53:48 -07:00
[super dealloc];
}
- (IBAction)doCancelButton:(id)sender {
[commentField resignFirstResponder];
[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-06-25 18:05:25 -07:00
- (void)setSiteInfo:(NSString *)userId setUsername:(NSString *)username {
if (userId) {
2012-06-27 14:37:37 -07:00
[submitButton setTitle:@"Reply"];
[toolbarTitle setTitle:[NSString stringWithFormat:@"Reply to %@", username]];
2012-06-25 18:05:25 -07:00
[submitButton setAction:(@selector(doReplyToComment:))];
} else {
2012-06-27 14:37:37 -07:00
[toolbarTitle setTitle:@"Post to Blurblog"];
[submitButton setTitle:@"Post"];
2012-06-25 18:05:25 -07:00
[submitButton setAction:(@selector(doShareThisStory:))];
}
2012-06-27 15:18:51 -07:00
}
2012-06-25 18:05:25 -07:00
- (void)clearComments {
self.commentField.text = nil;
}
2012-06-24 23:02:37 -07:00
- (IBAction)doShareThisStory:(id)sender {
2012-06-22 11:52:10 -07:00
NSString *urlString = [NSString stringWithFormat:@"http://%@/social/share_story",
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:@"feed_id"];
[request setPostValue:storyIdStr forKey:@"story_id"];
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(finishAddComment:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
2012-06-25 18:05:25 -07:00
- (IBAction)doReplyToComment:(id)sender {
NSString *comments = commentField.text;
if ([comments length] == 0) {
NSLog(@"NO COMMENTS");
return;
}
NSLog(@"REPLY TO COMMENT, %@", appDelegate.activeComment);
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"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(finishAddComment:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
2012-06-22 11:52:10 -07:00
- (void)finishAddComment:(ASIHTTPRequest *)request {
NSLog(@"%@", [request responseString]);
NSLog(@"Successfully added.");
[commentField resignFirstResponder];
[appDelegate hideShareView:YES];
2012-06-25 20:28:07 -07:00
NSString *responseString = [request responseString];
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
appDelegate.activeStory = [results objectForKey:@"story"];
[results release];
self.commentField.text = nil;
2012-06-25 20:28:07 -07:00
[appDelegate refreshComments];
2012-06-22 11:52:10 -07:00
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(@"Error: %@", error);
}
-(void)keyboardWillHide:(NSNotification*)notification
{
NSDictionary *userInfo = notification.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect shareViewFrame = self.view.frame;
CGRect storyDetailViewFrame = appDelegate.storyDetailViewController.webView.frame;
2012-06-24 23:02:37 -07:00
//NSLog(@"Keyboard y is %f", keyboardFrame.size.height);
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
2012-06-27 15:18:51 -07:00
shareViewFrame.origin.y = shareViewFrame.origin.y + keyboardFrame.size.height;
storyDetailViewFrame.size.height = storyDetailViewFrame.size.height + keyboardFrame.size.height;
} else {
shareViewFrame.origin.y = shareViewFrame.origin.y + keyboardFrame.size.width;
storyDetailViewFrame.size.height = storyDetailViewFrame.size.height + keyboardFrame.size.width;
}
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | curve
animations:^{
self.view.frame = shareViewFrame;
appDelegate.storyDetailViewController.webView.frame = storyDetailViewFrame;
} completion:nil];
}
-(void)keyboardWillShow:(NSNotification*)notification
{
NSDictionary *userInfo = notification.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect shareViewFrame = self.view.frame;
CGRect storyDetailViewFrame = appDelegate.storyDetailViewController.webView.frame;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
2012-06-27 15:18:51 -07:00
shareViewFrame.origin.y = shareViewFrame.origin.y - keyboardFrame.size.height;
storyDetailViewFrame.size.height = storyDetailViewFrame.size.height - keyboardFrame.size.height;
} else {
shareViewFrame.origin.y = shareViewFrame.origin.y - keyboardFrame.size.width;
storyDetailViewFrame.size.height = storyDetailViewFrame.size.height - keyboardFrame.size.width;
}
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | curve
animations:^{
self.view.frame = shareViewFrame;
appDelegate.storyDetailViewController.webView.frame = storyDetailViewFrame;
} completion:nil];
}
2012-06-21 11:53:48 -07:00
@end