// // 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 "QuoteView.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]; self.paddingValue = self.bottomConstraint.constant; [self.view setBackgroundColor:[UIColor colorNamed:@"Background"]]; if (_replyTo != nil) { QuoteView *inputView = (QuoteView *) self.inputAccessoryView; inputView.quoteText.text = _replyTo.text; self.title = [NSString stringWithFormat:@"Reply to %@", _replyTo.user.uname]; } else { self.title = @"Post"; } [[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]; } - (IBAction)sendAction:(id)sender { self.navigationController.navigationItem.rightBarButtonItem.enabled = NO; if (_replyTo == nil) { [[APIClient sharedClient] postMessage:self.textView.text result:^(Message *msg, NSError *err) { self.navigationController.navigationItem.rightBarButtonItem.enabled = YES; [self.navigationController popViewControllerAnimated:YES]; [(MessagesViewController *)self.navigationController.visibleViewController setShouldScrollToUnreadOnRefresh:YES]; }]; } else { [[APIClient sharedClient] postReplyToThread:_replyTo.mid inReplyTo:_replyTo.rid text:self.textView.text result:^(Message *msg, NSError *err) { self.navigationController.navigationItem.rightBarButtonItem.enabled = YES; [self.navigationController popViewControllerAnimated:YES]; [(MessagesViewController *)self.navigationController.visibleViewController setShouldScrollToUnreadOnRefresh:YES]; [(MessagesViewController *)self.navigationController.visibleViewController refreshData]; }]; } } -(void) keyboardDidShow:(NSNotification *)sender { CGRect keyboardRect = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; self.bottomConstraint.constant = keyboardRect.size.height - self.view.safeAreaInsets.bottom + 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) { QuoteView *inputView = (QuoteView *)[[[NSBundle mainBundle] loadNibNamed:@"QuoteView" owner:self options:nil] firstObject]; inputView.autoresizingMask = UIViewAutoresizingFlexibleHeight; inputView.quoteText.text = self.replyTo.text; _inputAccessoryView = inputView; } return _inputAccessoryView; } @end