// // MainViewController.m // QuickTweet // // Created by Floyd Price on 10/09/2009. // Copyright Component Workshop Ltd 2009. All rights reserved. // #import "MainViewController.h" #import "MainView.h" @interface MainViewController (private) -(void)presentModalView:(UIView *)modalView OverView:(UIView *)superview; -(void)dismissModalView:(UIView *)modalView; -(void)setupEngine; -(void)postTweetWithString:(NSString *)tweetData; -(void)visualizeSendingTweet; -(void)resetTextView; @end @implementation MainViewController @synthesize textView, tweetToolbar, undercoat, friendsView, friendsViewController, friendPicker; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; } - (void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [textView becomeFirstResponder]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([defaults boolForKey:@"setupcomplete"] ==YES) { [self setupEngine]; } else { [self showInfo]; return; } } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // The resizing that Interface builder exposes isn't good enough, so i'm manually moving things around to support rotation, fancy! [UIView beginAnimations :nil context:nil]; [UIView setAnimationDuration:duration]; CGFloat horizontalOffset; CGFloat verticalOffset; CGPoint drawPoint; horizontalOffset = tweetToolbar.center.x; if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { if ([self interfaceOrientation] != UIInterfaceOrientationLandscapeLeft && [self interfaceOrientation] != UIInterfaceOrientationLandscapeRight) { verticalOffset = 180; drawPoint = CGPointMake(horizontalOffset,verticalOffset); [tweetToolbar setCenter:drawPoint]; CGRect rect = [undercoat frame]; rect.size.height = 200; rect.origin.y = 300; [undercoat setFrame:rect]; rect = [textView frame]; rect.size.height = 120; [textView setFrame:rect]; } } else { if ([self interfaceOrientation] != UIInterfaceOrientationPortrait && [self interfaceOrientation] != UIInterfaceOrientationPortraitUpsideDown) { verticalOffset = 145 ; drawPoint = CGPointMake(horizontalOffset,verticalOffset); [tweetToolbar setCenter:drawPoint]; CGRect rect = [undercoat frame]; rect.size.height = 200; rect.origin.y = 84; [undercoat setFrame:rect]; rect = [textView frame]; rect.size.height = 114; [textView setFrame:rect]; } } [UIView commitAnimations]; } - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller { [self dismissModalViewControllerAnimated:YES]; [self setupEngine]; } - (IBAction)showInfo { FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; controller.delegate = self; controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:controller animated:YES]; [controller release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { [super viewDidUnload]; [engine release]; engine = nil; } - (void)dealloc { [super dealloc]; } #pragma mark - #pragma mark Actions - (IBAction)insertFriend{ [[self textView] resignFirstResponder]; [friendPicker reloadAllComponents]; // [self presentModalView:friendsView OverView:[self view]]; [self presentModalViewController:friendsViewController animated:true]; } - (IBAction)cancelFriendView{ [friendsViewController dismissModalViewControllerAnimated:true]; //[self dismissModalView:friendsView]; [textView becomeFirstResponder]; } - (IBAction)doneWithFriendView{ [self dismissModalView:friendsView]; //[textView becomeFirstResponder]; [friendsViewController dismissModalViewControllerAnimated:true]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSArray *followers = [defaults objectForKey:@"followers"]; if ([friendPicker numberOfRowsInComponent:0] > 0) selectedFollower = [followers objectAtIndex:[friendPicker selectedRowInComponent:0]]; if (selectedFollower == nil) return; NSString *follower = [NSString stringWithFormat:@"@%@ ", selectedFollower]; NSMutableString *theString = [NSMutableString stringWithString:[textView text]]; [theString insertString:follower atIndex:0]; [textView setText:theString]; } - (IBAction)postTweet{ [self postTweetWithString:[textView text]]; } #pragma mark - #pragma mark Private Methods - (void)presentModalView:(UIView *)modalView OverView:(UIView *)superview { // Set initial location at bottom of superview CGRect frame = [modalView frame]; frame.origin = CGPointMake(0.0, 550.0); [modalView setFrame:frame]; [superview addSubview:modalView]; // Animate to new location [UIView beginAnimations:@"presentWithSuperview" context:nil]; [UIView setAnimationDuration:0.5]; frame.origin = CGPointMake(0.0, 0.0); [modalView setFrame:frame]; [UIView commitAnimations]; } -(void)dismissModalView:(UIView *)modalView{ // Set initial location at bottom of superview CGRect frame = [modalView frame]; // Animate to new location [UIView beginAnimations:@"dismissWithSuperview" context:nil]; [UIView setAnimationDuration:0.5]; frame.origin = CGPointMake(0.0, 555.0); [modalView setFrame:frame]; [UIView commitAnimations]; } -(void)setupEngine{ engine = [[MGTwitterEngine twitterEngineWithDelegate:self] retain]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [engine setClientName:@"web" version:nil URL:nil token:nil]; [engine setUsername:[defaults stringForKey:@"username"] password:[defaults stringForKey:@"password"]]; } -(void)postTweetWithString:(NSString *)tweetData{ [self visualizeSendingTweet]; [engine sendUpdate:tweetData]; } -(void)visualizeSendingTweet{ CGRect frame = [textView frame]; [UIView beginAnimations:@"visualizeSendingTweet" context:nil]; [UIView setAnimationDuration:0.5]; frame.origin = CGPointMake(-frame.size.width, 10); [textView setFrame:frame]; [UIView commitAnimations]; } -(void)resetTextView{ [textView setAlpha:0]; CGRect frame = [textView frame]; frame.origin = CGPointMake(10, 10); [textView setFrame:frame]; [UIView beginAnimations:@"resetTextView" context:nil]; [UIView setAnimationDuration:0.5]; [textView setAlpha:1]; [UIView commitAnimations]; } #pragma mark - #pragma mark UIPickerView delegate and DataSource protocols -(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSArray *followers = [defaults objectForKey:@"followers"]; return [followers count]; } -(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } -(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSArray *followers = [defaults objectForKey:@"followers"]; return [followers objectAtIndex:row]; } -(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSArray *followers = [defaults objectForKey:@"followers"]; selectedFollower = [followers objectAtIndex:row]; } #pragma mark - #pragma mark Twitter Engine Delegates - (void)requestSucceeded:(NSString *)connectionIdentifier{ [textView setText:@""]; [self resetTextView]; } - (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error{ [self resetTextView]; UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error sending this tweet, please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alert show]; } - (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier{ } - (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier{ } - (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier{ } - (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier{ } @end