// // NewPostViewController.m // Juick // // Created by Vitaly Takmazov on 07.11.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "NewPostViewController.h" #import "MessagesViewController.h" #import "ColorScheme.h" #import "MessageInputView.h" #import "APIClient.h" @interface NewPostViewController () @property (nonatomic, assign) BOOL didSetupConstraints; @property (nonatomic, assign) int paddingValue; @property (nonatomic, readwrite, retain) UIView *inputAccessoryView; @end @implementation NewPostViewController - (void)viewDidLoad { [super viewDidLoad]; if (_replyTo != nil) { MessageInputView *inputView = (MessageInputView *) self.inputAccessoryView; inputView.quoteText.text = _replyTo.text; self.navigationController.visibleViewController.title = [NSString stringWithFormat:@"Reply to %@", _replyTo.user.uname]; } else { self.navigationController.visibleViewController.title = @"Post"; } self.navigationController.visibleViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCompose)]; self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(doneCompose)]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [self.textView becomeFirstResponder]; } - (void) cancelCompose { [self.textView resignFirstResponder]; [self.navigationController.visibleViewController.navigationController popViewControllerAnimated:YES]; } - (void) doneCompose { if (_replyTo == nil) { [[APIClient sharedClient] postMessage:self.textView.text result:^(Message *msg, NSError *err) { [self.navigationController.visibleViewController.navigationController popViewControllerAnimated:YES]; MessagesViewController *target = (MessagesViewController *)self.navigationController.visibleViewController; [target refreshData:YES]; }]; } else { [[APIClient sharedClient] postReplyToThread:_replyTo.mid inReplyTo:_replyTo.rid text:self.textView.text result:^(Message *msg, NSError *err) { [self.navigationController.visibleViewController.navigationController popViewControllerAnimated:YES]; MessagesViewController *target = (MessagesViewController *)self.navigationController.visibleViewController; [target refreshData:YES]; }]; } } -(void) keyboardDidShow:(NSNotification *)sender { CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window]; [self.view layoutIfNeeded]; [UIView animateWithDuration:[sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{ self.bottomConstraint.constant = newFrame.origin.y - CGRectGetHeight(self.view.frame) + self.paddingValue; [self.view layoutIfNeeded]; }]; } -(void) keyboardWillHide:(NSNotification *)sender { self.bottomConstraint.constant = self.paddingValue; [self.view layoutIfNeeded]; } -(BOOL) canBecomeFirstResponder { return YES; } - (UIView *) inputAccessoryView { if (!_inputAccessoryView) { MessageInputView *inputView = (MessageInputView *)[[[NSBundle mainBundle] loadNibNamed:@"MessageInputView" owner:self options:nil] firstObject]; inputView.autoresizingMask = UIViewAutoresizingFlexibleHeight; inputView.quoteText.text = self.replyTo.text; _inputAccessoryView = inputView; } return _inputAccessoryView; } @end