diff options
Diffstat (limited to 'Juick/ViewControllers/ChatViewController.m')
-rw-r--r-- | Juick/ViewControllers/ChatViewController.m | 53 |
1 files changed, 48 insertions, 5 deletions
diff --git a/Juick/ViewControllers/ChatViewController.m b/Juick/ViewControllers/ChatViewController.m index e94bb40..489de66 100644 --- a/Juick/ViewControllers/ChatViewController.m +++ b/Juick/ViewControllers/ChatViewController.m @@ -12,6 +12,8 @@ @interface ChatViewController () +@property (nonatomic, readwrite, retain) UIView *inputAccessoryView; + @end @implementation ChatViewController @@ -23,7 +25,13 @@ self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; self.tableView.allowsSelection = NO; + [self reloadChat]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; +} + +-(void) reloadChat { self.messages = [NSMutableArray array]; + [self.tableView reloadData]; [[APIClient sharedClient] fetchChatWithUser:self.uname callback:^(NSArray *messages, NSError *err) { if (err == nil) { [self.messages addObjectsFromArray:[[messages reverseObjectEnumerator] allObjects]]; @@ -39,11 +47,6 @@ }]; } -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { @@ -62,4 +65,44 @@ return cell; } +-(BOOL) canBecomeFirstResponder { + return YES; +} + +- (UIView *) inputAccessoryView { + if (!_inputAccessoryView) { + PHFComposeBarView *composeView = [[PHFComposeBarView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - PHFComposeBarViewInitialHeight, self.view.bounds.size.width, PHFComposeBarViewInitialHeight)]; + composeView.delegate = self; + [composeView.bottomAnchor constraintEqualToSystemSpacingBelowAnchor:self.view.safeAreaLayoutGuide.bottomAnchor multiplier:1.0f]; + composeView.maxLinesCount = 4; + _inputAccessoryView = composeView; + } + return _inputAccessoryView; +} + +-(void) keyboardWillChangeFrame:(NSNotification *)notification { + NSDictionary *userInfo = [notification userInfo]; + if (userInfo) { + CGRect beginFrame = [userInfo [UIKeyboardFrameBeginUserInfoKey] CGRectValue]; + CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; + CGFloat delta = endFrame.origin.y - beginFrame.origin.y; + CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; + UIViewAnimationCurve animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; + [UIView animateWithDuration:duration delay:0 options:(animationCurve << 16) animations:^{ + self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y - delta); + } completion:nil]; + } +} + +-(void) composeBarViewDidPressButton:(PHFComposeBarView *)composeBarView { + [[APIClient sharedClient] postPMToUser:self.uname text:composeBarView.text result:^(NSError *err) { + if (!err) { + NSLog(@"Success!"); + [composeBarView becomeFirstResponder]; + [composeBarView setText:nil animated:NO]; + [self reloadChat]; + } + }]; +} + @end |