// // MessageListViewController.m // FirstLink // // Created by mac on 14-9-10. // Copyright (c) 2014年 FirstLink. All rights reserved. // #import "SessionListController.h" #import "FKSessionListViewModel.h" #import "FirstLinkAppDelegate.h" #import "PindanMessageController.h" #import "UIImageView+FLAddition.h" #import "FKMessageManager.h" @interface SessionListController () @property (nonatomic, strong) FKSessionListViewModel *listViewModel; @property (nonatomic, strong) UILabel *badgeView; @end @implementation SessionListController - (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self configUnreadRefreshCallBackAfterViewDidLoad]; [self refreshUsersAndUnreadMessage]; } - (void)refreshUsersAndUnreadMessage { [self requestUsersForHuanxin]; [[FKMessageManager sharedInstance] refreshMessageUnreadMessage]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"消息"; [UIApplication sharedApplication].statusBarHidden = NO; [self.navigationController setNavigationBarHidden:NO animated:NO]; [self.tableView reloadData]; if (![FKMessageManager sharedInstance].remoteMessageBadge) { [self refreshUsersAndUnreadMessage]; } } - (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 NSInteger chatterID; __block EMMessage *latestCustomMessage; __weak FKMessageManager *weakMessageManager = [FKMessageManager sharedInstance]; NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB]; [conversations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { EMConversation *conversation = (EMConversation*)obj; chatterID = conversation.conversationId.integerValue; if (conversation.type != EMConversationTypeGroupChat) { if (chatterID >= MinCustomerConversationID && chatterID <= MaxCustomerConversationID) { EMMessage *message = [weakSelf realLastMessage:conversation]; if (latestCustomMessage == nil) { latestCustomMessage = message; } else { if (latestCustomMessage.timestamp < message.timestamp) { latestCustomMessage = message; } } } else { [conversation markAllMessagesAsRead:nil]; } } }]; if (latestCustomMessage) { EMConversation *latestConversation = [[EMClient sharedClient].chatManager getConversation:latestCustomMessage.conversationId type:EMConversationTypeChat createIfNotExist:NO]; FKLatestMessage *message = [[FKLatestMessage alloc] init]; message.userID = latestCustomMessage.from; message.text = [self subTitleMessageByConversation:latestConversation]; weakMessageManager.latestCustomerMessage = message; } return nil; } + (NSInteger)localUnreadMessageCount { __block NSInteger chatterID; __block NSInteger unreadCount = 0; NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB]; [conversations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { EMConversation *conversation = (EMConversation*)obj; chatterID = conversation.conversationId.integerValue; if (conversation.type != EMConversationTypeGroupChat) { if (chatterID >= MinCustomerConversationID && chatterID <= MaxCustomerConversationID) { unreadCount += conversation.unreadMessagesCount; } else { [conversation markAllMessagesAsRead:nil]; } } }]; return unreadCount; } #pragma mark - Unread Message - (void)configUnreadRefreshCallBackBeforeViewDidLoad { WeakSelf(weakSelf); __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance]; [FKMessageManager sharedInstance].unreadRefreshCallBackBeforeViewDidLoad = ^() { NSInteger localBadge = [SessionListController localUnreadMessageCount]; [weakSelf setBadgeViewNumber:(localBadge + weakMessageManage.remoteMessageBadge.totalBadge)]; if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { [weakSelf setBadgeViewNumber:0]; } }; } - (void)configUnreadRefreshCallBackAfterViewDidLoad { WeakSelf(weakSelf); __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance]; [FKMessageManager sharedInstance].unreadRefreshCallBackAfterViewDidLoad = ^() { NSInteger localBadge = [SessionListController localUnreadMessageCount]; [weakSelf setBadgeViewNumber:(localBadge + weakMessageManage.remoteMessageBadge.totalBadge)]; if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { [weakSelf setBadgeViewNumber:0]; } [weakSelf.tableView reloadData]; }; } #pragma mark - Method - (void)configBadgeView { FirstLinkAppDelegate *delegate = (FirstLinkAppDelegate *)[[UIApplication sharedApplication] delegate]; UITabBarController *tabbarController = (UITabBarController*)delegate.window.rootViewController; if ([tabbarController isKindOfClass:[UITabBarController class]]) { [tabbarController.tabBar addSubview:self.badgeView]; } } - (void)setBadgeViewNumber:(NSInteger)count { if (count <= 0) { self.badgeView.hidden = YES; } else { self.badgeView.hidden = NO; self.badgeView.text = [NSString stringWithFormat:@"%lu", (long)count]; } } #pragma mark - TableView Delegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 64; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SessionListCell *cell = [tableView dequeueReusableCellWithIdentifier:SessionListCellIndetifier]; if (cell) { cell.iconView.image = [self.listViewModel imageAtIndex:indexPath.row]; cell.tagLabel.text = [self.listViewModel titleAtIndex:indexPath.row]; FKMessageManager *messageManage = [FKMessageManager sharedInstance]; switch (indexPath.row) { case 0: { [cell setCellType:SessionListCellTypeDefault]; [cell setUnreadNumberString:messageManage.remoteMessageBadge.systemBadge]; break; } case 1: { [cell setCellType:SessionListCellTypeDefault]; [cell setUnreadNumberString:messageManage.remoteMessageBadge.pindanBadge]; break; } case 2: { [cell setCellType:SessionListCellTypeMessage]; [cell setUnreadNumberString:messageManage.remoteMessageBadge.commentBadge]; [cell setMessageText:messageManage.latestCommentMessage.text]; cell.headView.hidden = cell.messageLabel.hidden; if (messageManage.latestCommentMessage.headURL) { [cell.headView setImageWithURL:messageManage.latestCommentMessage.headURL placeholderImage:nil width:HEAD_PHOTO_LENGTH_32 height:HEAD_PHOTO_LENGTH_32]; } break; } case 3: { [cell setCellType:SessionListCellTypeMessage]; [cell setUnreadNumberString:[NSString stringWithFormat:@"%lu", (long)[SessionListController localUnreadMessageCount]]]; [cell setMessageText:messageManage.latestCustomerMessage.text]; [self setHeadView:cell userID:messageManage.latestCustomerMessage.userID]; break; } default: break; } } if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { cell.unreadLabel.hidden = YES; } if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } return cell; } - (void)setHeadView:(SessionListCell *)cell userID:(NSString*)userID { NSString *urlString = self.nicknameAndHearurlDict[userID][@"headURL"]; [cell.headView setImageWithURL:urlString placeholderImage:nil width:HEAD_PHOTO_LENGTH_32 height:HEAD_PHOTO_LENGTH_32]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { [self showLoginActionMenu]; return; } UIViewController *viewController; FKMessageManager *messageManager = [FKMessageManager sharedInstance]; switch (indexPath.row) { case 0: { messageManager.remoteMessageBadge.systemBadge = @"0"; viewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"SysMSGViewController"]; break; } case 1: { messageManager.remoteMessageBadge.pindanBadge = @"0"; viewController = [[PindanMessageController alloc] init]; break; } case 2: { messageManager.remoteMessageBadge.commentBadge = @"0"; viewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"CommentMessageController"]; break; } case 3: { CustomerListController *cusController = [[CustomerListController alloc] init]; cusController.nicknameAndHearurlDict = self.nicknameAndHearurlDict; cusController.hidesBottomBarWhenPushed = YES; viewController = cusController; break; } default: break; } if (viewController) { [self.navigationController pushViewController:viewController animated:YES]; } } #pragma mark - - (void)setupReadPindanMessageBlock:(PindanMessageController*)controller { WeakSelf(weakSelf); __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance]; controller.readPindanMessageBlock = ^() { int count = weakMessageManage.remoteMessageBadge.pindanBadge.intValue; weakMessageManage.remoteMessageBadge.pindanBadge = [NSString stringWithFormat:@"%d", MAX(0, --count)]; [weakSelf.tableView reloadData]; }; } #pragma mark - IChatMangerDelegate // 统计未读消息数 -(void)setupUnreadMessageCount { NSInteger unreadCount = [SessionListController localUnreadMessageCount]; __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance]; if (weakMessageManage.remoteMessageBadge) { unreadCount += weakMessageManage.remoteMessageBadge.totalBadge; } [self setBadgeViewNumber:unreadCount]; } // 收到消息回调 -(void)messagesDidReceive:(NSArray *)aMessages { FKMessageManager *messageManage = [FKMessageManager sharedInstance]; EMMessage *message = aMessages.lastObject; BOOL needShowNotification = (message.chatType == EMChatTypeGroupChat) ? [self needShowNotification:message.conversationId] : YES; if (needShowNotification) { #if !TARGET_IPHONE_SIMULATOR [self playSoundAndVibration]; NSInteger unreadCount = [SessionListController localUnreadMessageCount]; if (messageManage.remoteMessageBadge) { unreadCount += messageManage.remoteMessageBadge.totalBadge; } UIApplicationState state = [[UIApplication sharedApplication] applicationState]; if (state == UIApplicationStateBackground) { [self showNotificationWithMessage:message]; } else { if (unreadCount > 0) { [self setBadgeViewNumber:(int)(unreadCount)]; } else { [self setBadgeViewNumber:0]; } } #endif } [self loadDataSource]; [self.tableView reloadData]; } #pragma mark - Property - (FKSessionListViewModel *)listViewModel { if (!_listViewModel) { _listViewModel = [[FKSessionListViewModel alloc] init]; } return _listViewModel; } - (UILabel*)badgeView { if (!_badgeView) { _badgeView = [[UILabel alloc]init]; _badgeView.textAlignment = NSTextAlignmentCenter; _badgeView.frame = CGRectMake(UISCREENWIDTH*7/10 + 4, 4, 16, 16); _badgeView.layer.cornerRadius = 8; _badgeView.textColor = [UIColor whiteColor]; _badgeView.backgroundColor = UIColorFromRGB(0xff676b); _badgeView.font = [UIFont systemFontOfSize:11]; _badgeView.clipsToBounds = YES; _badgeView.hidden = YES; } return _badgeView; } @end