2012-07-22 14:23:50 -07:00
|
|
|
//
|
|
|
|
// FTUXAddFriendsViewController.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Roy Yang on 7/22/12.
|
|
|
|
// Copyright (c) 2012 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FirstTimeUserAddFriendsViewController.h"
|
|
|
|
#import "FirstTimeUserAddNewsBlurViewController.h"
|
2012-08-10 21:10:32 -07:00
|
|
|
#import "AuthorizeServicesViewController.h"
|
2012-08-12 16:50:34 -07:00
|
|
|
#import "ASIHTTPRequest.h"
|
2012-07-22 14:23:50 -07:00
|
|
|
|
|
|
|
@interface FirstTimeUserAddFriendsViewController ()
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FirstTimeUserAddFriendsViewController
|
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
@synthesize nextButton;
|
2012-08-10 21:10:32 -07:00
|
|
|
@synthesize facebookButton;
|
|
|
|
@synthesize twitterButton;
|
2012-08-11 16:43:53 -07:00
|
|
|
@synthesize facebookActivityIndicator;
|
|
|
|
@synthesize twitterActivityIndicator;
|
2012-08-12 16:50:34 -07:00
|
|
|
@synthesize friendsLabel;
|
2012-07-22 14:23:50 -07:00
|
|
|
|
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
|
|
|
|
{
|
|
|
|
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
|
|
|
if (self) {
|
|
|
|
// Custom initialization
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (void)viewDidLoad {
|
2012-07-22 14:23:50 -07:00
|
|
|
[super viewDidLoad];
|
|
|
|
// Do any additional setup after loading the view from its nib.
|
|
|
|
|
|
|
|
|
2012-08-14 17:20:45 -07:00
|
|
|
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Skip this step" style:UIBarButtonSystemItemDone target:self action:@selector(tapNextButton)];
|
2012-07-22 14:23:50 -07:00
|
|
|
self.nextButton = next;
|
|
|
|
self.navigationItem.rightBarButtonItem = next;
|
|
|
|
|
2013-03-04 18:21:10 -08:00
|
|
|
self.navigationItem.title = @"Friends";
|
2012-07-22 14:23:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (void)viewDidUnload {
|
2012-07-22 14:23:50 -07:00
|
|
|
[self setNextButton:nil];
|
2012-08-10 21:10:32 -07:00
|
|
|
[self setFacebookButton:nil];
|
|
|
|
[self setTwitterButton:nil];
|
2012-08-11 16:43:53 -07:00
|
|
|
[self setFacebookActivityIndicator:nil];
|
|
|
|
[self setTwitterActivityIndicator:nil];
|
2012-07-22 14:23:50 -07:00
|
|
|
[super viewDidUnload];
|
|
|
|
// Release any retained subviews of the main view.
|
|
|
|
// e.g. self.myOutlet = nil;
|
|
|
|
}
|
|
|
|
|
2012-08-12 18:07:54 -07:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
2014-09-18 11:25:51 -07:00
|
|
|
[super viewWillAppear:animated];
|
2012-08-12 18:07:54 -07:00
|
|
|
// [self selectTwitterButton];
|
2012-08-14 17:20:45 -07:00
|
|
|
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
|
2012-08-12 18:07:54 -07:00
|
|
|
}
|
2012-07-22 14:23:50 -07:00
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
2012-08-11 18:03:28 -07:00
|
|
|
// Return YES for supported orientations
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
return YES;
|
|
|
|
} else if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
2012-07-22 14:23:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)tapNextButton {
|
|
|
|
[appDelegate.ftuxNavigationController pushViewController:appDelegate.firstTimeUserAddNewsBlurViewController animated:YES];
|
|
|
|
}
|
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (IBAction)tapTwitterButton {
|
|
|
|
AuthorizeServicesViewController *service = [[AuthorizeServicesViewController alloc] init];
|
|
|
|
service.url = @"/oauth/twitter_connect";
|
|
|
|
service.type = @"twitter";
|
|
|
|
[appDelegate.ftuxNavigationController pushViewController:service animated:YES];
|
2012-07-22 14:23:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (IBAction)tapFacebookButton {
|
|
|
|
AuthorizeServicesViewController *service = [[AuthorizeServicesViewController alloc] init];
|
|
|
|
service.url = @"/oauth/facebook_connect";
|
|
|
|
service.type = @"facebook";
|
|
|
|
[appDelegate.ftuxNavigationController pushViewController:service animated:YES];
|
2012-07-22 14:23:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (void)selectTwitterButton {
|
2012-08-12 18:07:54 -07:00
|
|
|
self.nextButton.title = @"Next";
|
2012-08-10 21:10:32 -07:00
|
|
|
self.twitterButton.userInteractionEnabled = NO;
|
2012-08-12 18:07:54 -07:00
|
|
|
[self.twitterButton setTitle:@"Connecting" forState:UIControlStateNormal];
|
|
|
|
[self.twitterActivityIndicator startAnimating];
|
|
|
|
[self connectToSocial];
|
2012-07-22 14:23:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-10 21:10:32 -07:00
|
|
|
- (void)selectFacebookButton {
|
2012-08-12 18:07:54 -07:00
|
|
|
self.nextButton.title = @"Next";
|
2012-08-10 21:10:32 -07:00
|
|
|
self.facebookButton.userInteractionEnabled = NO;
|
2012-08-12 18:07:54 -07:00
|
|
|
[self.facebookButton setTitle:@"Connecting" forState:UIControlStateNormal];
|
|
|
|
[self.facebookActivityIndicator startAnimating];
|
|
|
|
[self connectToSocial];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Check Social
|
|
|
|
|
|
|
|
- (void)connectToSocial {
|
2013-06-18 21:23:20 -04:00
|
|
|
NSString *urlString = [NSString stringWithFormat:@"%@/social/load_user_friends",
|
2016-01-21 22:11:37 -08:00
|
|
|
self.appDelegate.url];
|
2012-08-12 18:07:54 -07:00
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
|
|
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
|
2015-10-06 18:52:58 -07:00
|
|
|
[request setValidatesSecureCertificate:NO];
|
2012-08-12 18:07:54 -07:00
|
|
|
[request setDelegate:self];
|
|
|
|
[request setDidFinishSelector:@selector(finishConnectFromSocial:)];
|
|
|
|
[request setDidFailSelector:@selector(requestFailed:)];
|
|
|
|
[request startAsynchronous];
|
2012-07-22 14:23:50 -07:00
|
|
|
}
|
|
|
|
|
2013-03-06 14:29:40 -08:00
|
|
|
- (void)requestFailed:(ASIHTTPRequest *)request {
|
|
|
|
NSError *error = [request error];
|
|
|
|
NSLog(@"Error: %@", error);
|
|
|
|
[appDelegate informError:error];
|
|
|
|
}
|
|
|
|
|
2012-08-12 18:07:54 -07:00
|
|
|
- (void)finishConnectFromSocial:(ASIHTTPRequest *)request {
|
|
|
|
NSString *responseString = [request responseString];
|
|
|
|
NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary *results = [NSJSONSerialization
|
|
|
|
JSONObjectWithData:responseData
|
|
|
|
options:kNilOptions
|
|
|
|
error:&error];
|
|
|
|
NSLog(@"results are %@", results);
|
|
|
|
|
|
|
|
BOOL facebookSync = [[[[results objectForKey:@"services"] objectForKey:@"facebook"] objectForKey:@"syncing"] boolValue];
|
|
|
|
BOOL twitterSync = [[[[results objectForKey:@"services"] objectForKey:@"twitter"] objectForKey:@"syncing"] boolValue];
|
|
|
|
|
|
|
|
if (![[[[results objectForKey:@"services"] objectForKey:@"facebook"] objectForKey:@"facebook_uid"] isKindOfClass:[NSNull class]]) {
|
|
|
|
[self finishFacebookConnect];
|
2012-08-12 18:50:32 -07:00
|
|
|
} else {
|
|
|
|
if (facebookSync) {
|
|
|
|
[self performSelector:@selector(connectToSocial) withObject:self afterDelay:3];
|
|
|
|
}
|
2012-08-12 18:07:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (![[[[results objectForKey:@"services"] objectForKey:@"twitter"] objectForKey:@"twitter_uid"] isKindOfClass:[NSNull class]]) {
|
|
|
|
[self finishTwitterConnect];
|
2012-08-12 18:50:32 -07:00
|
|
|
} else {
|
|
|
|
if (twitterSync) {
|
|
|
|
[self performSelector:@selector(connectToSocial) withObject:self afterDelay:3];
|
|
|
|
}
|
2012-08-12 18:07:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishFacebookConnect {
|
|
|
|
[self.facebookActivityIndicator stopAnimating];
|
2012-08-15 19:31:34 -07:00
|
|
|
self.friendsLabel.textColor = UIColorFromRGB(0x333333);
|
2012-08-12 18:07:54 -07:00
|
|
|
self.facebookButton.selected = YES;
|
2012-08-15 19:31:34 -07:00
|
|
|
self.friendsLabel.text = @"You have successfully connected to Facebook.";
|
2012-08-12 18:07:54 -07:00
|
|
|
UIImage *checkmark = [UIImage imageNamed:@"258-checkmark"];
|
|
|
|
UIImageView *checkmarkView = [[UIImageView alloc] initWithImage:checkmark];
|
|
|
|
checkmarkView.frame = CGRectMake(self.facebookButton.frame.origin.x + self.facebookButton.frame.size.width - 24,
|
|
|
|
self.facebookButton.frame.origin.y + 8,
|
|
|
|
16,
|
|
|
|
16);
|
|
|
|
[self.view addSubview:checkmarkView];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishTwitterConnect {
|
|
|
|
[self.twitterActivityIndicator stopAnimating];
|
2012-08-15 19:31:34 -07:00
|
|
|
self.friendsLabel.textColor = UIColorFromRGB(0x333333);
|
|
|
|
self.friendsLabel.text = @"You have successfully connected to Twitter.";
|
2012-08-12 18:07:54 -07:00
|
|
|
|
|
|
|
self.twitterButton.selected = YES;
|
|
|
|
UIImage *checkmark = [UIImage imageNamed:@"258-checkmark"];
|
|
|
|
UIImageView *checkmarkView = [[UIImageView alloc] initWithImage:checkmark];
|
|
|
|
checkmarkView.frame = CGRectMake(self.twitterButton.frame.origin.x + self.twitterButton.frame.size.width - 24,
|
|
|
|
self.twitterButton.frame.origin.y + 8,
|
|
|
|
16,
|
|
|
|
16);
|
|
|
|
[self.view addSubview:checkmarkView];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Toggle Auto Follow
|
|
|
|
|
2012-08-12 16:50:34 -07:00
|
|
|
- (IBAction)toggleAutoFollowFriends:(id)sender {
|
|
|
|
UISwitch *button = (UISwitch *)sender;
|
|
|
|
|
|
|
|
NSURL *preferenceURL = [NSURL URLWithString:
|
2013-06-18 21:23:20 -04:00
|
|
|
[NSString stringWithFormat:@"%@/profile/set_preference",
|
2016-01-21 22:11:37 -08:00
|
|
|
self.appDelegate.url]];
|
2012-08-12 16:50:34 -07:00
|
|
|
|
|
|
|
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:preferenceURL];
|
|
|
|
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
|
|
|
|
setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
|
|
|
|
|
|
|
|
if (button.on) {
|
|
|
|
[request setPostValue:@"false" forKey:@"autofollow_friends"];
|
|
|
|
} else {
|
|
|
|
[request setPostValue:@"true" forKey:@"autofollow_friends"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[request setDelegate:self];
|
|
|
|
[request setResponseEncoding:NSUTF8StringEncoding];
|
|
|
|
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
|
|
|
|
[request setDidFinishSelector:@selector(finishToggleAutoFollowFriends:)];
|
|
|
|
[request setDidFailSelector:@selector(finishedWithError:)];
|
|
|
|
[request setTimeOutSeconds:30];
|
|
|
|
[request startAsynchronous];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishedWithError:(ASIHTTPRequest *)request {
|
|
|
|
NSString *responseString = [request responseString];
|
|
|
|
NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary *results = [NSJSONSerialization
|
|
|
|
JSONObjectWithData:responseData
|
|
|
|
options:kNilOptions
|
|
|
|
error:&error];
|
|
|
|
NSLog(@"results are %@", results);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishToggleAutoFollowFriends:(ASIHTTPRequest *)request {
|
|
|
|
NSString *responseString = [request responseString];
|
|
|
|
NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary *results = [NSJSONSerialization
|
|
|
|
JSONObjectWithData:responseData
|
|
|
|
options:kNilOptions
|
|
|
|
error:&error];
|
|
|
|
NSLog(@"results are %@", results);
|
|
|
|
}
|
2012-07-22 14:23:50 -07:00
|
|
|
|
2012-08-14 17:20:45 -07:00
|
|
|
- (void)changeMessaging:(NSString *)msg {
|
|
|
|
self.friendsLabel.text = msg;
|
2012-08-15 19:31:34 -07:00
|
|
|
self.friendsLabel.textColor = [UIColor redColor];
|
2012-08-14 17:20:45 -07:00
|
|
|
}
|
|
|
|
|
2012-07-22 14:23:50 -07:00
|
|
|
@end
|