// // CustomerViewController.m // FirstLink // // Created by ascii on 15/6/8. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "CustomerListController.h" #import "FKMessageManager.h" #import "UIImageView+FLAddition.h" @interface CustomerListController () @end @implementation CustomerListController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"客服消息"; [self.navigationController.tabBarController.tabBar setHidden:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; [self configTableViewFrame]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[FKMessageManager sharedInstance] addEaseMobDelegate:self observerType:FKEaseMobObserverTypeService]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[FKMessageManager sharedInstance] removeEaseMobDelegate:self observerType:FKEaseMobObserverTypeService]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark - - (NSMutableArray *)loadDataSource { WeakSelf(weakSelf); __block NSMutableArray *customerConversations = [NSMutableArray array]; NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB]; [conversations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { EMConversation *con = (EMConversation*)obj; if (con.conversationId.integerValue >= MinCustomerConversationID && con.conversationId.integerValue <= MaxCustomerConversationID) { [customerConversations addObject:obj]; } }]; NSArray *sorteConversations = [customerConversations sortedArrayUsingComparator: ^(EMConversation *obj1, EMConversation* obj2){ EMMessage *message1 = [weakSelf realLastMessage:obj1]; EMMessage *message2 = [weakSelf realLastMessage:obj2]; if (message1.timestamp > message2.timestamp) { return (NSComparisonResult)NSOrderedAscending; } else { return (NSComparisonResult)NSOrderedDescending; } }]; [self.statusView removeFromSuperview]; if (sorteConversations.count == 0) { [weakSelf showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoMessageIcon"] title:NoMessageTip]; } return [NSMutableArray arrayWithArray:sorteConversations]; } #pragma mark - ChatManager Delegate - (void)messagesDidReceive:(NSArray *)aMessages { [self refreshDataSource]; } #pragma mark - TableView Delegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 64.0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomerListCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomerListCellIndetifier]; if (indexPath.row < self.dataSource.count) { EMConversation *conversation = self.dataSource[indexPath.row]; [self setHeadView:cell userID:conversation.conversationId]; cell.nickLabel.text = self.nicknameAndHearurlDict[conversation.conversationId][@"nickname"]; cell.messageLabel.text = [self subTitleMessageByConversation:conversation]; cell.timeLabel.text = [self lastMessageTimeByConversation:conversation]; NSInteger unreadCount = conversation.unreadMessagesCount; if (unreadCount <= 0) { cell.unreadLabel.hidden = YES; } else { cell.unreadLabel.hidden = NO; cell.unreadLabel.text = [NSString stringWithFormat:@"%lu", unreadCount]; } } return cell; } - (void)setHeadView:(CustomerListCell *)cell userID:(NSString*)userID { [cell.headView setImageWithURL:self.nicknameAndHearurlDict[userID][@"headURL"] placeholderImage:nil width:HEAD_PHOTO_LENGTH_35 height:HEAD_PHOTO_LENGTH_35]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row < self.dataSource.count) { EMConversation *conversation = self.dataSource[indexPath.row]; BOOL isGroup = (conversation.type == EMConversationTypeGroupChat); ChatViewController *chatController = [[ChatViewController alloc] initWithChatter:conversation.conversationId isGroup:isGroup]; chatController.naviTitle = conversation.conversationId; if ([[self.nicknameAndHearurlDict allKeys] containsObject:conversation.conversationId]) { chatController.naviTitle = self.nicknameAndHearurlDict[conversation.conversationId][@"nickname"]; chatController.receiverHeadURL = self.nicknameAndHearurlDict[conversation.conversationId][@"headURL"]; chatController.myNickname = [FKUserManager sharedManager].user.nickName; } [conversation markAllMessagesAsRead:nil]; chatController.hidesBottomBarWhenPushed = YES; [self setupUnreadMessageCount]; [self.navigationController pushViewController:chatController animated:YES]; } } #pragma mark - Method - (void)configTableViewFrame { self.tableView.frame = CGRectMake(0, 0, UISCREENWIDTH, CGRectGetHeight(self.view.frame)); } @end