Нет описания

CustomerListController.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // CustomerViewController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/6/8.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "CustomerListController.h"
  9. #import "FKMessageManager.h"
  10. #import "UIImageView+FLAddition.h"
  11. @interface CustomerListController ()
  12. <UITableViewDataSource, UITableViewDelegate, EMChatManagerDelegate>
  13. @end
  14. @implementation CustomerListController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. // Do any additional setup after loading the view from its nib.
  18. }
  19. - (void)viewWillAppear:(BOOL)animated {
  20. [super viewWillAppear:animated];
  21. self.navigationItem.title = @"客服消息";
  22. [self.navigationController.tabBarController.tabBar setHidden:YES];
  23. [self.navigationController setNavigationBarHidden:NO animated:YES];
  24. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  25. [self configTableViewFrame];
  26. }
  27. - (void)viewDidAppear:(BOOL)animated {
  28. [super viewDidAppear:animated];
  29. [[FKMessageManager sharedInstance] addEaseMobDelegate:self
  30. observerType:FKEaseMobObserverTypeService];
  31. }
  32. - (void)viewWillDisappear:(BOOL)animated {
  33. [super viewWillDisappear:animated];
  34. }
  35. - (void)viewDidDisappear:(BOOL)animated {
  36. [super viewDidDisappear:animated];
  37. [[FKMessageManager sharedInstance] removeEaseMobDelegate:self
  38. observerType:FKEaseMobObserverTypeService];
  39. }
  40. - (void)didReceiveMemoryWarning {
  41. [super didReceiveMemoryWarning];
  42. // Dispose of any resources that can be recreated.
  43. }
  44. /*
  45. #pragma mark - Navigation
  46. // In a storyboard-based application, you will often want to do a little preparation before navigation
  47. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  48. // Get the new view controller using [segue destinationViewController].
  49. // Pass the selected object to the new view controller.
  50. }
  51. */
  52. #pragma mark -
  53. - (NSMutableArray *)loadDataSource {
  54. WeakSelf(weakSelf);
  55. __block NSMutableArray *customerConversations = [NSMutableArray array];
  56. NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB];
  57. [conversations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  58. EMConversation *con = (EMConversation*)obj;
  59. if (con.conversationId.integerValue >= MinCustomerConversationID
  60. && con.conversationId.integerValue <= MaxCustomerConversationID) {
  61. [customerConversations addObject:obj];
  62. }
  63. }];
  64. NSArray *sorteConversations = [customerConversations sortedArrayUsingComparator:
  65. ^(EMConversation *obj1, EMConversation* obj2){
  66. EMMessage *message1 = [weakSelf realLastMessage:obj1];
  67. EMMessage *message2 = [weakSelf realLastMessage:obj2];
  68. if (message1.timestamp > message2.timestamp) {
  69. return (NSComparisonResult)NSOrderedAscending;
  70. } else {
  71. return (NSComparisonResult)NSOrderedDescending;
  72. }
  73. }];
  74. [self.statusView removeFromSuperview];
  75. if (sorteConversations.count == 0) {
  76. [weakSelf showStatusTipInView:self.view
  77. image:[UIImage imageNamed:@"StatusNoMessageIcon"]
  78. title:NoMessageTip];
  79. }
  80. return [NSMutableArray arrayWithArray:sorteConversations];
  81. }
  82. #pragma mark - ChatManager Delegate
  83. - (void)messagesDidReceive:(NSArray *)aMessages {
  84. [self refreshDataSource];
  85. }
  86. #pragma mark - TableView Delegate
  87. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  88. return self.dataSource.count;
  89. }
  90. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  91. return 64.0;
  92. }
  93. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  94. CustomerListCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomerListCellIndetifier];
  95. if (indexPath.row < self.dataSource.count) {
  96. EMConversation *conversation = self.dataSource[indexPath.row];
  97. [self setHeadView:cell userID:conversation.conversationId];
  98. cell.nickLabel.text = self.nicknameAndHearurlDict[conversation.conversationId][@"nickname"];
  99. cell.messageLabel.text = [self subTitleMessageByConversation:conversation];
  100. cell.timeLabel.text = [self lastMessageTimeByConversation:conversation];
  101. NSInteger unreadCount = conversation.unreadMessagesCount;
  102. if (unreadCount <= 0) {
  103. cell.unreadLabel.hidden = YES;
  104. } else {
  105. cell.unreadLabel.hidden = NO;
  106. cell.unreadLabel.text = [NSString stringWithFormat:@"%lu", unreadCount];
  107. }
  108. }
  109. return cell;
  110. }
  111. - (void)setHeadView:(CustomerListCell *)cell userID:(NSString*)userID {
  112. [cell.headView setImageWithURL:self.nicknameAndHearurlDict[userID][@"headURL"]
  113. placeholderImage:nil
  114. width:HEAD_PHOTO_LENGTH_35
  115. height:HEAD_PHOTO_LENGTH_35];
  116. }
  117. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  118. if (indexPath.row < self.dataSource.count) {
  119. EMConversation *conversation = self.dataSource[indexPath.row];
  120. BOOL isGroup = (conversation.type == EMConversationTypeGroupChat);
  121. ChatViewController *chatController = [[ChatViewController alloc] initWithChatter:conversation.conversationId
  122. isGroup:isGroup];
  123. chatController.naviTitle = conversation.conversationId;
  124. if ([[self.nicknameAndHearurlDict allKeys] containsObject:conversation.conversationId]) {
  125. chatController.naviTitle = self.nicknameAndHearurlDict[conversation.conversationId][@"nickname"];
  126. chatController.receiverHeadURL = self.nicknameAndHearurlDict[conversation.conversationId][@"headURL"];
  127. chatController.myNickname = [FKUserManager sharedManager].user.nickName;
  128. }
  129. [conversation markAllMessagesAsRead:nil];
  130. chatController.hidesBottomBarWhenPushed = YES;
  131. [self setupUnreadMessageCount];
  132. [self.navigationController pushViewController:chatController animated:YES];
  133. }
  134. }
  135. #pragma mark - Method
  136. - (void)configTableViewFrame {
  137. self.tableView.frame = CGRectMake(0, 0, UISCREENWIDTH, CGRectGetHeight(self.view.frame));
  138. }
  139. @end