//  Created by Kitten Yang on 2/8/15.
//  Copyright (c) 2015 Kitten Yang. All rights reserved.
//


#import "MessageViewController.h"
#import "EMChatViewCell.h"
#import "MessageModel.h"
#import "Constant.h"
#import "ChatViewController.h"
#import "ChatSendHelper.h"

@interface MessageViewController ()<UITableViewDataSource,UITableViewDelegate,IChatManagerDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation MessageViewController

-(NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reloadDataSource];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = '消息';
    //初始化聊天管理器
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    [self setupTableView];
    [self reloadDataSource];
}

- (void)setupTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-64-49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

-(void)reloadDataSource
{
    [self.dataSource removeAllObjects];
    
    NSArray *conversations = [[EaseMob sharedInstance].chatManager conversations];
    NSArray* sorte = [conversations sortedArrayUsingComparator:
                      ^(EMConversation *obj1, EMConversation* obj2){
                          EMMessage *message1 = [obj1 latestMessage];
                          EMMessage *message2 = [obj2 latestMessage];
                          if(message1.timestamp > message2.timestamp) {
                              return(NSComparisonResult)NSOrderedAscending;
                          }else {
                              return(NSComparisonResult)NSOrderedDescending;
                          }
                      }];
    
    for (EMConversation *conversation in sorte) {
        MessageModel *model = [[MessageModel alloc] init];
        model.conversation = conversation;
        model.isGroup = conversation.conversationType == eConversationTypeChat ? NO : YES;
        if (conversation.conversationType == eConversationTypeChat) {
            model.title = conversation.chatter;
            if (!model.title) {
                model.title = conversation.chatter;
            }
        } else {
            NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList];
            for (EMGroup *group in groupArray) {
                if ([group.groupId isEqualToString:conversation.chatter]) {
                    model.title = group.groupSubject;
                    break;
                }
            }
        }
        
        EMMessage *lastMessage = [conversation latestMessage];
        if (lastMessage) {
            model.date = lastMessage.timestamp;
            id<IEMMessageBody> messageBody = lastMessage.messageBodies.lastObject;
            switch (messageBody.messageBodyType) {
                case eMessageBodyType_Image:{
                    model.messageType = eMessageBodyType_Image;
                } break;
                case eMessageBodyType_Text:{
                    // 表情映射。
                    NSString *didReceiveText = [ConvertToCommonEmoticonsHelper
                                                convertToSystemEmoticons:((EMTextMessageBody *)messageBody).text];
                    model.content = didReceiveText;
                    model.messageType = eMessageBodyType_Text;
                } break;
                case eMessageBodyType_Voice:{
                    model.messageType = eMessageBodyType_Voice;
                    model.content = '[声音]';
                } break;
                case eMessageBodyType_Location: {
                    model.messageType = eMessageBodyType_Location;
                    model.content = '[位置]';
                } break;
                case eMessageBodyType_Video: {
                    model.messageType = eMessageBodyType_Video;
                    model.content = '[视频]';
                } break;
                default:
                    break;
            }
        }
        
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}


#pragma mark - tableView delegate & dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = 'cellID';
    EMChatViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[EMChatViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    MessageModel *model = [self.dataSource objectAtIndex:indexPath.row];
    cell.nameLabel.text = model.title;
    cell.messageLabel.text = model.content;
    cell.timeLabel.text = [ChatSendHelper stringFromDateFormat:model.date];
    if (model.conversation.unreadMessagesCount > 0) {
        cell.unreadLabel.text = [NSString stringWithFormat:'%i',model.conversation.unreadMessagesCount];
    }
    if (model.isGroup) {
        NSString *imageName = 'groupPublicHeader';
        cell.headerImageView.image = [UIImage imageNamed:imageName];
    } else {
        NSString *imageName = 'chatListCellHead';
        cell.headerImageView.image = [UIImage imageNamed:imageName];
    }
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel *model = [self.dataSource objectAtIndex:indexPath.row];
    ChatViewController *chatVC = [[ChatViewController alloc]init];
    chatVC.title = model.title;
    chatVC.chatter = model.conversation.chatter;
    chatVC.isGroup = model.isGroup;
    [self.navigationController pushViewController:chatVC animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        MessageModel *model = [self.dataSource objectAtIndex:indexPath.row];
        EMConversation *conversation = model.conversation;
        [[EaseMob sharedInstance].chatManager removeConversationByChatter:conversation.chatter deleteMessages:YES append2Chat:YES];
        [self.dataSource removeObject

原文地址: https://www.cveoy.top/t/topic/lhYj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录