NewsBlur/media/ios/Classes/FirstTimeUserAddSitesViewController.m

207 lines
6.9 KiB
Mathematica
Raw Normal View History

2012-07-22 14:23:50 -07:00
//
// FTUXaddSitesViewController.m
// NewsBlur
//
// Created by Roy Yang on 7/22/12.
// Copyright (c) 2012 NewsBlur. All rights reserved.
//
#import "NewsBlurAppDelegate.h"
2012-07-22 14:23:50 -07:00
#import "FirstTimeUserAddSitesViewController.h"
#import "FirstTimeUserAddFriendsViewController.h"
2012-08-10 21:10:32 -07:00
#import "AuthorizeServicesViewController.h"
#import "NewsBlurViewController.h"
@interface FirstTimeUserAddSitesViewController()
@property (readwrite) int importedFeedCount_;
@end;
2012-07-22 14:23:50 -07:00
@implementation FirstTimeUserAddSitesViewController
@synthesize appDelegate;
@synthesize googleReaderButton;
@synthesize nextButton;
@synthesize activityIndicator;
@synthesize instructionLabel;
2012-07-22 14:23:50 -07:00
@synthesize categories;
2012-08-12 20:09:45 -07:00
@synthesize googleReaderButtonWrapper;
@synthesize importedFeedCount_;
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;
}
- (void)viewDidLoad
{
categories = [[NSMutableArray alloc] init];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonSystemItemDone target:self action:@selector(tapNextButton)];
self.nextButton = next;
// self.nextButton.enabled = NO;
2012-07-22 14:23:50 -07:00
self.navigationItem.rightBarButtonItem = next;
2012-08-12 16:50:34 -07:00
self.navigationItem.title = @"Add Sites";
self.activityIndicator.hidesWhenStopped = YES;
2012-08-10 21:10:32 -07:00
2012-08-11 18:03:28 -07:00
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.instructionLabel.font = [UIFont systemFontOfSize:13];
2012-08-11 18:03:28 -07:00
}
2012-07-22 14:23:50 -07:00
}
- (void)viewDidUnload
{
2012-08-10 21:10:32 -07:00
[self setActivityIndicator:nil];
[self setInstructionLabel: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-10 21:10:32 -07:00
[self setGoogleReaderButton:nil];
[self setNextButton:nil];
2012-07-22 14:23:50 -07:00
}
2012-08-11 18:03:28 -07:00
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// 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.firstTimeUserAddFriendsViewController animated:YES];
}
- (IBAction)tapCategoryButton:(id)sender {
}
#pragma mark -
#pragma mark Import Google Reader
- (IBAction)tapGoogleReaderButton {
2012-08-10 21:10:32 -07:00
AuthorizeServicesViewController *service = [[AuthorizeServicesViewController alloc] init];
service.url = @"/import/authorize";
service.type = @"google";
[appDelegate.ftuxNavigationController pushViewController:service animated:YES];
2012-07-22 14:23:50 -07:00
}
- (void)importFromGoogleReader {
self.nextButton.enabled = YES;
[self.googleReaderButton setTitle:@"Importing..." forState:UIControlStateNormal];
2012-07-22 14:23:50 -07:00
self.googleReaderButton.userInteractionEnabled = NO;
self.instructionLabel.text = @"This might take a minute. Feel free to continue and we'll let you know when we finish importing";
[self.activityIndicator startAnimating];
NSString *urlString = [NSString stringWithFormat:@"http://%@/import/import_from_google_reader/",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"true" forKey:@"auto_active"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(finishImportFromGoogleReader:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
- (void)importFromGoogleReaderFailed:(NSString *)error {
2012-08-11 18:03:28 -07:00
[self.googleReaderButton setTitle:@"Retry Google Reader" forState:UIControlStateNormal];
self.instructionLabel.text = error;
2012-08-11 18:03:28 -07:00
}
- (void)finishImportFromGoogleReader:(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);
self.importedFeedCount_ = [[results objectForKey:@"feed_count"] intValue];
[self performSelector:@selector(updateSites) withObject:nil afterDelay:1];
}
- (void)updateSites {
2012-08-12 16:50:34 -07:00
self.instructionLabel.text = @"And just like that, we're done! Time to see what your friends are sharing or add some categories.";
[appDelegate.feedsViewController fetchFeedList:NO];
NSString *msg = [NSString stringWithFormat:@"Imported %i site%@",
self.importedFeedCount_,
self.importedFeedCount_ == 1 ? @"" : @"s"];
[self.googleReaderButton setTitle:msg forState:UIControlStateSelected];
self.googleReaderButton.selected = YES;
[self.activityIndicator stopAnimating];
2012-08-12 16:50:34 -07:00
UIImage *checkmark = [UIImage imageNamed:@"258-checkmark"];
UIImageView *checkmarkView = [[UIImageView alloc] initWithImage:checkmark];
2012-08-12 20:09:45 -07:00
checkmarkView.frame = CGRectMake(self.googleReaderButton.frame.size.width - 24,
8,
2012-08-12 16:50:34 -07:00
16,
16);
2012-08-12 20:09:45 -07:00
[self.googleReaderButtonWrapper addSubview:checkmarkView];
2012-07-22 14:23:50 -07:00
}
#pragma mark -
#pragma mark Add Categories
- (void)addCategories {
// TO DO: curate the list of sites
for (id key in categories) {
// add folder
NSString *urlString = [NSString stringWithFormat:@"http://%@/reader/add_folder",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:key forKey:@"folder"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(finishAddFolder:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
}
- (void)finishAddFolder:(ASIHTTPRequest *)request {
NSLog(@"Successfully added.");
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(@"Error: %@", error);
}
#pragma mark -
#pragma mark Add Site
- (void)addSite:(NSString *)siteUrl {
NSString *urlString = [NSString stringWithFormat:@"http://%@/reader/add_url",
NEWSBLUR_URL];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:siteUrl forKey:@"url"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(finishAddFolder:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
@end