// // 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 "MessageInputView.h" @interface ChatViewController () @property (nonatomic, readwrite, retain) MessageInputView *accessoryView; @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.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic; [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]; [[AppDelegate shared].api 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; } - (BOOL)canResignFirstResponder { return YES; } - (UIView *) inputAccessoryView { if (!_accessoryView) { _accessoryView = [[[NSBundle mainBundle] loadNibNamed:@"MessageInputView" owner:self options:nil] firstObject]; _accessoryView.delegate = self; } return _accessoryView; } -(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) textSent:(NSString *)text { [[AppDelegate shared].api postPMToUser:self.uname text:text result:^(NSError *err) { if (!err) { NSLog(@"Success!"); [self.accessoryView becomeFirstResponder]; [self reloadChat]; } }]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { [self reloadChat]; } @end