// // ChatViewController.m // Juick // // Created by Vitaly Takmazov on 04/03/2018. // Copyright © 2018 com.juick. All rights reserved. // #import "ChatViewController.h" #import "BubbleMessageCell.h" #import "APIClient.h" @interface ChatViewController () @property (nonatomic, readwrite, retain) UIView *inputAccessoryView; @property (nonatomic, strong) NSString *me; @end @implementation ChatViewController - (void)viewDidLoad { [super viewDidLoad]; [self.navigationController.visibleViewController setTitle:self.uname]; [self.tableView registerNib:[UINib nibWithNibName:@"BubbleMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"bubbleMessageCell"]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; self.tableView.allowsSelection = NO; self.me = [APIClient sharedClient].credential.user; [self reloadChat]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; self.refreshControl = [UIRefreshControl new]; [self.refreshControl addTarget:self action:@selector(reloadChat) forControlEvents:UIControlEventValueChanged]; [self addObserver:self forKeyPath:@"uname" options:0 context: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]]; NSMutableArray *indexPaths = [NSMutableArray new]; for (NSUInteger index = 0; index < [messages count]; index++) { [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]]; } [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; [self.tableView endUpdates]; NSInteger messagesCount = [self.messages count]; if (messagesCount > 0) { [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; } } [self.refreshControl endRefreshing]; }]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.messages count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BubbleMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bubbleMessageCell" forIndexPath:indexPath]; Message *message = [self.messages objectAtIndex:indexPath.row]; [cell configureWithMessage:message isMe:[self.me isEqualToString:message.user.uname]]; 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]; } }]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { [self reloadChat]; } @end