| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- /************************************************************
- * * Hyphenate CONFIDENTIAL
- * __________________
- * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
- *
- * NOTICE: All information contained herein is, and remains
- * the property of Hyphenate Inc.
- * Dissemination of this information or reproduction of this material
- * is strictly forbidden unless prior written permission is obtained
- * from Hyphenate Inc.
- */
- #import "EaseConversationListViewController.h"
- #import "EaseEmotionEscape.h"
- #import "EaseConversationCell.h"
- #import "EaseConvertToCommonEmoticonsHelper.h"
- #import "EaseMessageViewController.h"
- #import "NSDate+Category.h"
- #import "EaseLocalDefine.h"
- @interface EaseConversationListViewController ()<EMChatManagerDelegate>
- @end
- @implementation EaseConversationListViewController
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- [self registerNotifications];
- }
- -(void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [self unregisterNotifications];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:dispatch_get_main_queue()];
- [self tableViewDidTriggerHeaderRefresh];
- }
- - (void)messagesDidReceive:(NSArray *)aMessages
- {
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- // Return the number of sections.
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // Return the number of rows in the section.
- return [self.dataArray count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSString *CellIdentifier = [EaseConversationCell cellIdentifierWithModel:nil];
- EaseConversationCell *cell = (EaseConversationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
-
- // Configure the cell...
- if (cell == nil) {
- cell = [[EaseConversationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
-
- if ([self.dataArray count] <= indexPath.row) {
- return cell;
- }
-
- id<IConversationModel> model = [self.dataArray objectAtIndex:indexPath.row];
- cell.model = model;
-
- if (_dataSource && [_dataSource respondsToSelector:@selector(conversationListViewController:latestMessageTitleForConversationModel:)]) {
- NSMutableAttributedString *attributedText = [[_dataSource conversationListViewController:self latestMessageTitleForConversationModel:model] mutableCopy];
- [attributedText addAttributes:@{NSFontAttributeName : cell.detailLabel.font} range:NSMakeRange(0, attributedText.length)];
- cell.detailLabel.attributedText = attributedText;
- } else {
- cell.detailLabel.attributedText = [[EaseEmotionEscape sharedInstance] attStringFromTextForChatting:[self _latestMessageTitleForConversationModel:model]textFont:cell.detailLabel.font];
- }
-
- if (_dataSource && [_dataSource respondsToSelector:@selector(conversationListViewController:latestMessageTimeForConversationModel:)]) {
- cell.timeLabel.text = [_dataSource conversationListViewController:self latestMessageTimeForConversationModel:model];
- } else {
- cell.timeLabel.text = [self _latestMessageTimeForConversationModel:model];
- }
-
- return cell;
- }
- #pragma mark - Table view delegate
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return [EaseConversationCell cellHeightWithModel:nil];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- if (_delegate && [_delegate respondsToSelector:@selector(conversationListViewController:didSelectConversationModel:)]) {
- EaseConversationModel *model = [self.dataArray objectAtIndex:indexPath.row];
- [_delegate conversationListViewController:self didSelectConversationModel:model];
- } else {
- EaseConversationModel *model = [self.dataArray objectAtIndex:indexPath.row];
- EaseMessageViewController *viewController = [[EaseMessageViewController alloc] initWithConversationChatter:model.conversation.conversationId conversationType:model.conversation.type];
- viewController.title = model.title;
- [self.navigationController pushViewController:viewController animated:YES];
- }
- }
- -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
- return YES;
- }
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
-
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- EaseConversationModel *model = [self.dataArray objectAtIndex:indexPath.row];
- [[EMClient sharedClient].chatManager deleteConversation:model.conversation.conversationId isDeleteMessages:YES completion:nil];
- [self.dataArray removeObjectAtIndex:indexPath.row];
- [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- }
- }
- #pragma mark - data
- -(void)refreshAndSortView
- {
- if ([self.dataArray count] > 1) {
- if ([[self.dataArray objectAtIndex:0] isKindOfClass:[EaseConversationModel class]]) {
- NSArray* sorted = [self.dataArray sortedArrayUsingComparator:
- ^(EaseConversationModel *obj1, EaseConversationModel* obj2){
- EMMessage *message1 = [obj1.conversation latestMessage];
- EMMessage *message2 = [obj2.conversation latestMessage];
- if(message1.timestamp > message2.timestamp) {
- return(NSComparisonResult)NSOrderedAscending;
- }else {
- return(NSComparisonResult)NSOrderedDescending;
- }
- }];
- [self.dataArray removeAllObjects];
- [self.dataArray addObjectsFromArray:sorted];
- }
- }
- [self.tableView reloadData];
- }
- /*!
- @method
- @brief 加载会话列表
- @discussion
- @result
- */
- - (void)tableViewDidTriggerHeaderRefresh
- {
- NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
- NSArray* sorted = [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;
- }
- }];
-
-
-
- [self.dataArray removeAllObjects];
- for (EMConversation *converstion in sorted) {
- EaseConversationModel *model = nil;
- if (self.dataSource && [self.dataSource respondsToSelector:@selector(conversationListViewController:modelForConversation:)]) {
- model = [self.dataSource conversationListViewController:self
- modelForConversation:converstion];
- }
- else{
- model = [[EaseConversationModel alloc] initWithConversation:converstion];
- }
-
- if (model) {
- [self.dataArray addObject:model];
- }
- }
-
- [self.tableView reloadData];
- [self tableViewDidFinishTriggerHeader:YES reload:NO];
- }
- #pragma mark - EMGroupManagerDelegate
- - (void)didUpdateGroupList:(NSArray *)groupList
- {
- [self tableViewDidTriggerHeaderRefresh];
- }
- #pragma mark - registerNotifications
- -(void)registerNotifications{
- [self unregisterNotifications];
- [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
- [[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
- }
- -(void)unregisterNotifications{
- [[EMClient sharedClient].chatManager removeDelegate:self];
- [[EMClient sharedClient].groupManager removeDelegate:self];
- }
- - (void)dealloc{
- [self unregisterNotifications];
- }
- #pragma mark - private
- /*!
- @method
- @brief 获取会话最近一条消息内容提示
- @discussion
- @param conversationModel 会话model
- @result 返回传入会话model最近一条消息提示
- */
- - (NSString *)_latestMessageTitleForConversationModel:(id<IConversationModel>)conversationModel
- {
- NSString *latestMessageTitle = @"";
- EMMessage *lastMessage = [conversationModel.conversation latestMessage];
- if (lastMessage) {
- EMMessageBody *messageBody = lastMessage.body;
- switch (messageBody.type) {
- case EMMessageBodyTypeImage:{
- latestMessageTitle = NSEaseLocalizedString(@"message.image1", @"[image]");
- } break;
- case EMMessageBodyTypeText:{
- NSString *didReceiveText = [EaseConvertToCommonEmoticonsHelper
- convertToSystemEmoticons:((EMTextMessageBody *)messageBody).text];
- latestMessageTitle = didReceiveText;
- } break;
- case EMMessageBodyTypeVoice:{
- latestMessageTitle = NSEaseLocalizedString(@"message.voice1", @"[voice]");
- } break;
- case EMMessageBodyTypeLocation: {
- latestMessageTitle = NSEaseLocalizedString(@"message.location1", @"[location]");
- } break;
- case EMMessageBodyTypeVideo: {
- latestMessageTitle = NSEaseLocalizedString(@"message.video1", @"[video]");
- } break;
- case EMMessageBodyTypeFile: {
- latestMessageTitle = NSEaseLocalizedString(@"message.file1", @"[file]");
- } break;
- default: {
- } break;
- }
- }
- return latestMessageTitle;
- }
- /*!
- @method
- @brief 获取会话最近一条消息时间
- @discussion
- @param conversationModel 会话model
- @result 返回传入会话model最近一条消息时间
- */
- - (NSString *)_latestMessageTimeForConversationModel:(id<IConversationModel>)conversationModel
- {
- NSString *latestMessageTime = @"";
- EMMessage *lastMessage = [conversationModel.conversation latestMessage];;
- if (lastMessage) {
- double timeInterval = lastMessage.timestamp ;
- if(timeInterval > 140000000000) {
- timeInterval = timeInterval / 1000;
- }
- NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"YYYY-MM-dd"];
- latestMessageTime = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
- }
- return latestMessageTime;
- }
- @end
|