// // YOUPAIBBMessageVC.m // VQU // // Created by Elaine on 2021/10/22. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAIBBMessageVC.h" #import "NTESBundleSetting.h" #import "NTESSessionUtil.h" #import "YOUPAILCGiftAttachment.h" #import "YOUPAILCMultiImageAttachment.h" #import "YOUPAILCOneImageAttachment.h" #import "YOUPAILCMessageTxtAttachment.h" #import "YOUPAILCCallAttachment.h" #import "UIViewController+TFPresent.h" #import "YOUPAIZYUpdateRemarkWindow.h" #import "YOUPAILCReportVC.h" #import "LZAlertWindow.h" @interface YOUPAIBBMessageVC () @property (nonatomic,strong) UILabel *youpaiptitleLabel; @property (nonatomic,assign) BOOL youpaipsupportsForceTouch; @property (nonatomic,strong) NSMutableDictionary *youpaippreviews; @property (nonatomic,strong) NSArray *youpaiptop_listArray;//置顶列表 @end @implementation YOUPAIBBMessageVC - (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; // self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;////设置子视图的宽度、高度随着父视图变化 //消息列表适配暗黑模式 v1.5.6 [self.tableView setBackgroundColor:[UIColor whiteColor]]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(youpaiflongTapAction:)]; longPress.minimumPressDuration = 0.5; //定义按的时间 longPress.numberOfTouchesRequired = 1; [self.tableView addGestureRecognizer:longPress]; [self youpaifrefreshList]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.navigationController.navigationBar setDefaultBar]; } - (void)youpaifinitConfig{ //系统配置信息 [LCHttpHelper requestWithURLString:AppConfig parameters:nil needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 self->_youpaiptop_listArray = [[[dict objectForKey:@"data"]objectForKey:@"config"] objectForKey:@"top_list"]; for (NSString *sessionId in self->_youpaiptop_listArray) { NIMSession *session = [NIMSession session:sessionId type:NIMSessionTypeP2P]; [NTESSessionUtil addRecentSessionMark:session type:NTESRecentSessionMarkTypeTop]; } } } failure:^(NSError *error) { }]; } - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; self.youpaipsupportsForceTouch = [self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable; [self youpaifinitConfig]; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { _youpaippreviews = [[NSMutableDictionary alloc] init]; self.autoRemoveRemoteSession = [[NTESBundleSetting sharedConfig] autoRemoveRemoteSession]; } return self; } - (void)youpaifrefreshList{ if (self.recentSessions.count == 0) {//有时候会拿不到数据 // self.recentSessions = [[NIMSDK sharedSDK].conversationManager.allRecentSessions mutableCopy];//获取所有最近会话。 } self.recentSessions = [self customSortRecents:self.recentSessions]; //过虑系统通知和动态通知 for (NSInteger i = 0; i< self.recentSessions.count; i++) { NIMRecentSession *recentSession = self.recentSessions[i]; if ( [recentSession.session.sessionId isEqual:@"11"] || [recentSession.session.sessionId isEqual:@"2"]) { [self.recentSessions removeObject:recentSession]; i --; } } [self.tableView reloadData]; if (self.recentSessions.count != 0) { [self.tableView lz_hideEmptyView]; }else{ [self.tableView lz_showEmptyViewWithImage:[UIImage imageNamed:@"vqu_images_not_call_video_data"] content:@"暂无消息记录"]; } } //对最近会话重新排序 - (NSMutableArray *)customSortRecents:(NSMutableArray *)recentSessions { NSMutableArray *array = [[NSMutableArray alloc] initWithArray:recentSessions]; [array sortUsingComparator:^NSComparisonResult(NIMRecentSession *obj1, NIMRecentSession *obj2) { NSInteger score1 = [NTESSessionUtil recentSessionIsMark:obj1 type:NTESRecentSessionMarkTypeTop]? 10 : 0; NSInteger score2 = [NTESSessionUtil recentSessionIsMark:obj2 type:NTESRecentSessionMarkTypeTop]? 10 : 0; if (obj1.lastMessage.timestamp > obj2.lastMessage.timestamp) { score1 += 1; } else if (obj1.lastMessage.timestamp < obj2.lastMessage.timestamp) { score2 += 1; } if (score1 == score2) { return NSOrderedSame; } return score1 > score2? NSOrderedAscending : NSOrderedDescending; }]; return array; } #pragma mark - UITableViewDelegate //- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // return YES; //} //- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath //{ // return UITableViewCellEditingStyleDelete; //} //- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // NIMRecentSession *recentSession = self.recentSessions[indexPath.row]; // if (editingStyle == UITableViewCellEditingStyleDelete) { //// id manager = [[NIMSDK sharedSDK] conversationManager]; //删除最近会话 //// [manager deleteRecentSession:recentSession]; // //删除最近会话并且删除消息 // id manager = [[NIMSDK sharedSDK] conversationManager]; // NIMDeleteMessagesOption* deleteOption = [[NIMDeleteMessagesOption alloc]init]; // deleteOption.removeSession = YES; // deleteOption.removeTable = YES; // [manager deleteAllmessagesInSession:recentSession.session option:deleteOption]; // } //} - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (self.youpaipsupportsForceTouch) { id preview = [self registerForPreviewingWithDelegate:self sourceView:cell]; [self.youpaippreviews setObject:preview forKey:@(indexPath.row)]; } } - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (self.youpaipsupportsForceTouch) { id preview = [self.youpaippreviews objectForKey:@(indexPath.row)]; [self unregisterForPreviewingWithContext:preview]; [self.youpaippreviews removeObjectForKey:@(indexPath.row)]; } } - (void)onSelectedRecent:(NIMRecentSession *)recentSession atIndexPath:(NSIndexPath *)indexPath{ @weakify(self); [ZCHUDHelper show]; [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[recentSession.session.sessionId] completion:^(NSArray * _Nullable users, NSError * _Nullable error) { @strongify(self); [ZCHUDHelper dismiss]; YOUPAILCIMSessionVC *vc = [[YOUPAILCIMSessionVC alloc] initWithSession:recentSession.session]; [self.navigationController pushViewController:vc animated:YES]; }]; } - (NSString *)messageContent:(NIMMessage*)lastMessage{ if (lastMessage.messageType == NIMMessageTypeCustom) { NIMCustomObject *object = (NIMCustomObject*)lastMessage.messageObject; NSString *text = @""; if ([object.attachment isKindOfClass:[YOUPAILCMessageTxtAttachment class]]) { YOUPAILCMessageTxtAttachment* attachment = (YOUPAILCMessageTxtAttachment*)object.attachment; text = attachment.title; } if ([object.attachment isKindOfClass:[YOUPAILCOneImageAttachment class]]) { YOUPAILCOneImageAttachment* attachment = (YOUPAILCOneImageAttachment*)object.attachment; if (attachment.act_type==1) { text = attachment.act_string; }else{ text = attachment.title; } } if ([object.attachment isKindOfClass:[YOUPAILCMultiImageAttachment class]]) { YOUPAILCMultiImageAttachment* attachment = (YOUPAILCMultiImageAttachment*)object.attachment; if (attachment.imageArray.count != 0) { NSDictionary* topDict = attachment.imageArray[0]; text = [topDict objectForKey:@"title"]; } } if ([object.attachment isKindOfClass:[YOUPAILCGiftAttachment class]]) { YOUPAILCGiftAttachment* attachment = (YOUPAILCGiftAttachment*)object.attachment; if ([[NSString stringWithFormat:@"%zd",attachment.from_uid] isEqualToString:[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id]) { text = @"[赠送礼物]"; }else{ text = @"[收到礼物]"; } } if ([object.attachment isKindOfClass:[YOUPAILPFollowAttachment class]]){ //modify by leo v1.0.7 上线通知 YOUPAILPFollowAttachment* attachment = (YOUPAILPFollowAttachment *)object.attachment; if(attachment.type == 16) { text = @"新增粉丝"; } //modify by leo v1.0.8 动态更新 else if(attachment.type == 17) { NSString *nickeName = attachment.nickname; text = [NSString stringWithFormat:@"%@更新了动态",nickeName]; } else { NSString *nickeName = attachment.nickname; text = [NSString stringWithFormat:@"%@上线啦!",nickeName]; } } if ([object.attachment isKindOfClass:[YOUPAILCCallAttachment class]]) { YOUPAILCCallAttachment* attachment = (YOUPAILCCallAttachment*)object.attachment; if (attachment.calltype==1) { text = @"[视频通话]"; } NSString* txtStr = @""; if (attachment.status==3) { txtStr =@"[超时未接听]"; }else if (attachment.status==4){ txtStr = [NSString stringWithFormat:@"[通话时长:%@]",[ZCUtils getMinTimeStrFromSS:attachment.call_time]]; } NSLog(@"代表是自己发的%@-----%ld", [LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id , (long)attachment.from_uid); if ([[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id integerValue] == attachment.from_uid) {//代表是自己发的 // NSLog(@"代表是自己发的%@-----%@", [LCSaveModel getUserModel].userinfo.user_id , attachment.from_uid); if (attachment.status==1) { txtStr = @"[已取消]"; }else if (attachment.status==2){ txtStr = @"[对方已拒绝]"; } }else{ if (attachment.status==1) { txtStr = @"[对方已取消]"; }else if (attachment.status==2){ txtStr = @"[已拒绝]"; } } text = [NSString stringWithFormat:@"%@%@",text,txtStr]; } return text; }else{ return [super messageContent:lastMessage]; } } -(void)youpaiflongTapAction:(UILongPressGestureRecognizer *)longPress { if (longPress.state == UIGestureRecognizerStateBegan) { AudioServicesPlaySystemSound(1519); NSLog(@"long pressTap state :begin"); }else if (longPress.state == UIGestureRecognizerStateEnded){ CGPoint p = [longPress locationInView:self.tableView] ;// get longpress pt NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p] ; // get indexpath from table with point . NIMRecentSession *recentSession = self.recentSessions[indexPath.row]; if ([_youpaiptop_listArray containsObject:recentSession.session.sessionId]) {//置顶列表 return; } @weakify(self); ZCAlertAction* deleteAction = [ZCAlertAction actionWithTitle:@"删除" andblock:^{ @strongify(self); [self messagePopupWindowAction:@"deleteChat" andSession:recentSession.session]; }]; ZCAlertSheetView* alertSheet = [[ZCAlertSheetView alloc]initWithTitle:nil andShowCancelButton:YES andAction:@[deleteAction]]; alertSheet.alertWindow.hidden = NO; [alertSheet show]; } } -(void)dealloc { NSLog(@"lcmessage dealloc"); } #pragma mark VQ_ZYMessagePopupWindowDelegate -(void)messagePopupWindowAction:(NSString *)type andSession:(NIMSession *)session { if ([type isEqualToString:@"updateRemark"]) {//设置备注名 YOUPAIZYUpdateRemarkWindow *VC= [[YOUPAIZYUpdateRemarkWindow alloc]init]; VC.vWidth = KScreenWidth -68; VC.vHeight = ScaleHeight(187)+10; VC.youpaipsession = session; VC.isTouchDismiss = YES; [kAppDelegate.window.rootViewController TFPresentVC:VC completion:^{ }]; } if ([type isEqualToString:@"deleteChat"]) {//删除聊天 WeakSelf; LZAlertAction *cancelAction = [LZAlertAction actionWithTitle:@"取消" handler:^(LZAlertAction *action) { }]; cancelAction.cornerRadius = 5.0f; cancelAction.bgColor = HexColorFromRGB(0x9F9DA5); LZAlertAction *confimAction = [LZAlertAction actionWithTitle:@"删除" handler:^(LZAlertAction *action) { [weakSelf deleteChatWithSession:session]; }]; confimAction.cornerRadius = 5.0f; confimAction.bgColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake((KScreenWidth - 105.0f) / 2.0f, 48.0f) FromColors:@[ZYGradientOneColor,ZYGradientTwoColor] ByGradientType:GradientLeftToRight]]; LZAlertWindow *alert = [LZAlertWindow alertWithTitle:@"提示" content:@"确定删除该聊天?" action:@[cancelAction,confimAction]]; alert.contentTextAlignment = NSTextAlignmentCenter; [self TFPresentVC:alert completion:^{}]; } if ([type isEqualToString:@"top"]) {//置顶该聊天 } if ([type isEqualToString:@"blacklist"]) {//加入黑名单 } if ([type isEqualToString:@"report"]) {//举报 YOUPAILCReportVC* report = [[YOUPAILCReportVC alloc]init]; report.youpaipreport_uid = session.sessionId; report.youpaiptype = 4; [self.navigationController pushViewController:report animated:YES]; } } #pragma mark ZYDeleteChatWindowDelegate -(void)deleteChatWithSession:(NIMSession *)session { id manager = [[NIMSDK sharedSDK] conversationManager]; NIMDeleteMessagesOption* deleteOption = [[NIMDeleteMessagesOption alloc]init]; deleteOption.removeSession = YES; deleteOption.removeTable = YES; [manager deleteAllmessagesInSession:session option:deleteOption]; } #pragma mark - JXCategoryListContainerView - (UIView *)listView{ return self.view; } @end