123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- //
- // 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 ()
- <SRRefreshDelegate, UITableViewDataSource, UITableViewDelegate>
- @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
|