YOUPAIBBUCIMP2PSessionVC.m 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. //
  2. // YOUPAIBBUCIMP2PSessionVC.m
  3. // VQU
  4. // Created by Elaine on 2021/10/26.
  5. // Copyright © 2021 MS. All rights reserved.
  6. //
  7. #import "YOUPAIBBUCIMP2PSessionVC.h"
  8. #import "NIMSessionConfigurateProtocol.h"
  9. #import "NIMKit.h"
  10. #import "NIMMessageCellProtocol.h"
  11. #import "NIMMessageModel.h"
  12. #import "NIMKitUtil.h"
  13. #import "NIMCustomLeftBarView.h"
  14. #import "NIMBadgeView.h"
  15. #import "UITableView+NIMScrollToBottom.h"
  16. #import "NIMMessageMaker.h"
  17. #import "UIView+NIM.h"
  18. #import "NIMKitTitleView.h"
  19. #import "NIMKitKeyboardInfo.h"
  20. #import "YOUPAIBBNIMSessionConfigurator.h"
  21. #import "NIMKitInfoFetchOption.h"
  22. #import "YOUPAIUCIMP2PSessionConfig.h"
  23. #import "NTESTimerHolder.h"
  24. #import "NSDictionary+NTESJson.h"
  25. #import "NTESCustomSysNotificationSender.h"
  26. #import "UIViewController+TFPresent.h"
  27. #import "YOUPAIZYIMSessionDetailWindow.h"
  28. #import "YOUPAIZYUpdateRemarkWindow.h"
  29. #import "YOUPAILCReportVC.h"
  30. #import "YOUPAILZUserShowVC.h"
  31. #import "LZAlertWindow.h"
  32. @interface YOUPAIBBUCIMP2PSessionVC ()<NIMMediaManagerDelegate,UCBaseTouchTableViewDelegate,NIMSystemNotificationManagerDelegate,NTESTimerHolderDelegate,ZYIMSessionDetailWindowDelegate>
  33. @property (nonatomic,readwrite) NIMMessage *messageForMenu;
  34. @property (nonatomic,strong) UILabel *youpaiptitleLabel;
  35. @property (nonatomic,strong) UILabel *youpaipsubTitleLabel;
  36. @property (nonatomic,strong) NSIndexPath *youpaiplastVisibleIndexPathBeforeRotation;
  37. @property (nonatomic,strong) YOUPAIUCIMP2PSessionConfig* youpaipsessionConfig;
  38. @property (nonatomic,strong) NIMCustomLeftBarView* youpaipbadgeBarView;
  39. @property (nonatomic,strong) NTESCustomSysNotificationSender *youpaipnotificaionSender;
  40. @property (nonatomic,strong) NTESTimerHolder *youpaiptitleTimer;
  41. @end
  42. @implementation YOUPAIBBUCIMP2PSessionVC
  43. #pragma mark - 子类实现
  44. - (void)youpaifprofileClick{
  45. NSLog(@"头像点击啦");
  46. }
  47. - (instancetype)initWithSession:(NIMSession *)session{
  48. self = [super initWithNibName:nil bundle:nil];
  49. if (self) {
  50. _session = session;
  51. }
  52. return self;
  53. }
  54. - (void)dealloc
  55. {
  56. [self removeListener];
  57. [[NIMKit sharedKit].robotTemplateParser clean];
  58. _youpaiptableView.delegate = nil;
  59. _youpaiptableView.dataSource = nil;
  60. }
  61. - (void)viewDidAppear:(BOOL)animated{
  62. [super viewDidAppear:animated];
  63. //判断是否有本地消息记录 用来判断是否是第一次聊天
  64. [self youpaifcheckLocalMessage];
  65. }
  66. - (void)viewDidLoad {
  67. [super viewDidLoad];
  68. self.view.backgroundColor = LCBkgColor;
  69. _youpaipnotificaionSender = [[NTESCustomSysNotificationSender alloc] init];
  70. BOOL youpaipdisableCommandTyping = self.youpaipdisableCommandTyping || (self.session.sessionType == NIMSessionTypeP2P &&[[NIMSDK sharedSDK].userManager isUserInBlackList:self.session.sessionId]);
  71. if (!youpaipdisableCommandTyping) {
  72. _youpaiptitleTimer = [[NTESTimerHolder alloc] init];
  73. [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
  74. }
  75. //导航栏
  76. [self youpaifsetupNav];
  77. //消息 tableView
  78. [self youpaifsetupTableView];
  79. //输入框 inputView
  80. [self youpaifsetupInputView];
  81. //会话相关逻辑配置器安装
  82. [self youpaifsetupConfigurator];
  83. //添加监听
  84. [self addListener];
  85. //进入会话时,标记所有消息已读,并发送已读回执
  86. [self markRead];
  87. //更新已读位置
  88. [self uiCheckReceipts:nil];
  89. }
  90. - (void)youpaifsetupNav
  91. {
  92. //子类实现
  93. [self setUpTitleView];
  94. //导航栏设置
  95. NIMCustomLeftBarView *leftBarView = [[NIMCustomLeftBarView alloc] initWithFrame:CGRectMake(0, 0, 70, 44)];
  96. leftBarView.badgeView.badgeTextColor = [UIColor whiteColor];
  97. leftBarView.backImgV.image = [UIImage imageNamed:@"vqu_images_IM_session_Back"];
  98. [leftBarView addTarget:self action:@selector(youpaifbackClick) forControlEvents:UIControlEventTouchUpInside];
  99. self.navigationController.navigationBar.barTintColor = LCNavColor;
  100. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarView];
  101. // if (@available(iOS 11.0, *)) {
  102. // leftBarView.translatesAutoresizingMaskIntoConstraints = NO;
  103. // }
  104. self.youpaipbadgeBarView = leftBarView;
  105. self.navigationItem.leftBarButtonItem = leftItem;
  106. //设置不进入聊天详情的页面号
  107. if (@available(iOS 15.0, *)) {
  108. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  109. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  110. [appearance setBackgroundColor:LCNavColor];
  111. appearance.backgroundEffect = nil;
  112. appearance.shadowColor = [UIColor clearColor];
  113. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  114. textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
  115. appearance.titleTextAttributes = textAttrs;
  116. self.navigationController.navigationBar.standardAppearance = appearance;
  117. self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
  118. }
  119. // if (![self.session.sessionId isEqualToString:[LCSaveData getSysId]] && (![self.session.sessionId isEqualToString:[LCSaveData getServerId]])) {
  120. // self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifuserSetClick) image:[UIImage imageNamed:@"vqu_images_IM_im_details"]];
  121. // }
  122. [LCHttpHelper requestWithURLString:AppConfig parameters:nil needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  123. NSDictionary* dict = (NSDictionary*)responseObject;
  124. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  125. if (code==0) {//成功
  126. NSArray *array = [[[dict objectForKey:@"data"]objectForKey:@"config"] objectForKey:@"no_info"];
  127. if (![array containsObject:self.session.sessionId]) {
  128. self.navigationItem.rightBarButtonItems = @[[UIBarButtonItem itemWithTarget:self action:@selector(youpaifuserSetClick) image:[UIImage imageNamed:@"vqu_images_IM_im_details"]]] ;
  129. }
  130. }
  131. } failure:^(NSError *error) {}];
  132. }
  133. - (void)youpaifbackClick{
  134. [self.navigationController popViewControllerAnimated:YES];
  135. }
  136. #pragma mark-更多
  137. - (void)youpaifuserSetClick{
  138. YOUPAIZYIMSessionDetailWindow *vc= [[YOUPAIZYIMSessionDetailWindow alloc]init];
  139. vc.vWidth = KScreenWidth -68;
  140. vc.vHeight = 315+25+30;
  141. vc.youpaipsession = self.session;
  142. vc.youpaipdelegate = self;
  143. vc.isTouchDismiss = YES;
  144. [self TFPresentVC:vc completion:^{}];
  145. }
  146. - (void)youpaifsetupTableView
  147. {
  148. self.view.backgroundColor = LCBkgColor;
  149. self.youpaiptableView = [[YOUPAIUCBaseTouchTableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight) style:UITableViewStylePlain];
  150. self.youpaiptableView.touchDelegate = self;
  151. self.youpaiptableView.backgroundColor = LCBkgColor;
  152. self.youpaiptableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  153. self.youpaiptableView.estimatedRowHeight = 0;
  154. self.youpaiptableView.estimatedSectionHeaderHeight = 0;
  155. self.youpaiptableView.estimatedSectionFooterHeight = 0;
  156. self.youpaiptableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  157. if ([self.sessionConfig respondsToSelector:@selector(sessionBackgroundImage)] && [self.sessionConfig sessionBackgroundImage]) {
  158. UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  159. imgView.image = [self.sessionConfig sessionBackgroundImage];
  160. imgView.contentMode = UIViewContentModeScaleAspectFill;
  161. self.youpaiptableView.backgroundView = imgView;
  162. }
  163. if (@available(iOS 15.0, *)) {
  164. self.youpaiptableView.sectionHeaderTopPadding = 0;
  165. }
  166. [self.view addSubview:self.youpaiptableView];
  167. }
  168. - (void)youpaifsetupInputView
  169. {
  170. if ([self shouldShowInputView])
  171. {
  172. self.youpaipsessionInputView = [[YOUPAIBBNIMInputView alloc] initWithFrame:CGRectMake(0, 0, self.view.nim_width,0) config:self.sessionConfig];
  173. self.youpaipsessionInputView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  174. [self.youpaipsessionInputView setSession:self.session];
  175. [self.youpaipsessionInputView youpaifsetInputDelegate:self];
  176. [self.youpaipsessionInputView youpaifsetInputActionDelegate:self];
  177. [self.youpaipsessionInputView youpaifrefreshStatus:BBNIMInputStatusText];
  178. [self.view addSubview:_youpaipsessionInputView];
  179. // self.youpaipsessionInputView
  180. self.youpaiptableView.nim_height -= self.youpaipsessionInputView.youpaiptoolBar.nim_height;
  181. }
  182. }
  183. - (void)youpaifsetupConfigurator
  184. {
  185. _youpaipconfigurator = [[YOUPAIBBNIMSessionConfigurator alloc] init];
  186. [_youpaipconfigurator youpaifuc_setup:self];
  187. BOOL youpaipneedProximityMonitor = [self needProximityMonitor];
  188. [[NIMSDK sharedSDK].mediaManager setNeedProximityMonitor:youpaipneedProximityMonitor];
  189. }
  190. - (void)viewWillAppear:(BOOL)animated
  191. {
  192. [super viewWillAppear:animated];
  193. [self.youpaipinteractor onViewWillAppear];
  194. [self.navigationController.navigationBar setBackgroundColor:LCNavColor];
  195. if (@available(iOS 15.0, *)) {
  196. [self.navigationController.navigationBar.scrollEdgeAppearance setBackgroundColor:LCNavColor];
  197. [self.navigationController.navigationBar.standardAppearance setBackgroundColor:LCNavColor];
  198. }
  199. }
  200. - (void)viewWillDisappear:(BOOL)animated
  201. {
  202. [super viewWillDisappear:animated];
  203. [self.youpaipsessionInputView endEditing:YES];
  204. // [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
  205. // if (@available(iOS 15.0, *)) {
  206. // [self.navigationController.navigationBar.scrollEdgeAppearance setBackgroundColor:[UIColor clearColor]];
  207. // [self.navigationController.navigationBar.standardAppearance setBackgroundColor:[UIColor clearColor]];
  208. // }
  209. }
  210. - (void)viewDidDisappear:(BOOL)animated
  211. {
  212. [super viewDidDisappear:animated];
  213. [self.youpaipinteractor onViewDidDisappear];
  214. }
  215. - (void)viewDidLayoutSubviews
  216. {
  217. [self changeLeftBarBadge:self.conversationManager.allUnreadCount];
  218. [self.youpaipinteractor resetLayout];
  219. }
  220. #pragma mark - 第一次聊天的提示信息
  221. - (void)youpaifcheckLocalMessage{
  222. NIMMessageSearchOption* option = [[NIMMessageSearchOption alloc]init];
  223. option.allMessageTypes = YES;
  224. option.order = NIMMessageSearchOrderDesc;
  225. [[[NIMSDK sharedSDK] conversationManager] searchMessages:self.session option:option result:^(NSError * _Nullable error, NSArray<NIMMessage *> * _Nullable messages) {
  226. if (messages.count==0 && [LCSaveModel getUserModel].youpaipuserinfo.youpaipgender==2) {
  227. //不是系统和客服才请求
  228. if (![self.session.sessionId isEqualToString:[LCSaveData getSysId]] && ![self.session.sessionId isEqualToString:[LCSaveData getServerId]]) {//请求提示信息
  229. }
  230. }
  231. }];
  232. }
  233. #pragma mark - 对方输入状态
  234. - (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification
  235. {
  236. if (!notification.sendToOnlineUsersOnly) {
  237. return;
  238. }
  239. NSData *data = [[notification content] dataUsingEncoding:NSUTF8StringEncoding];
  240. if (data) {
  241. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
  242. options:0
  243. error:nil];
  244. if ([dict jsonInteger:NTESNotifyID] == NTESCommandTyping && self.session.sessionType == NIMSessionTypeP2P && [notification.sender isEqualToString:self.session.sessionId])
  245. {
  246. [self refreshSessionTitle:@"对方正在输入..."];
  247. [_youpaiptitleTimer startTimer:5
  248. delegate:self
  249. repeats:NO];
  250. }
  251. }
  252. }
  253. - (void)onNTESTimerFired:(NTESTimerHolder *)holder
  254. {
  255. [self refreshSessionTitle:self.youpaifsessionTitle];
  256. }
  257. #pragma mark - 消息收发接口
  258. - (void)sendMessage:(NIMMessage *)message
  259. {
  260. [self.youpaipinteractor sendMessage:message];
  261. }
  262. #pragma mark - Touch Event
  263. - (void)didTouchesBegan:(YOUPAIUCBaseTouchTableView *)tableView{
  264. [_youpaipsessionInputView endEditing:YES];
  265. }
  266. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  267. {
  268. [super touchesBegan:touches withEvent:event];
  269. [_youpaipsessionInputView endEditing:YES];
  270. }
  271. #pragma mark - NIMSessionConfiguratorDelegate
  272. - (void)didFetchMessageData
  273. {
  274. [self uiCheckReceipts:nil];
  275. [self.youpaiptableView reloadData];
  276. [self.youpaiptableView nim_scrollToBottom:NO];
  277. }
  278. - (void)didRefreshMessageData
  279. {
  280. [self refreshSessionTitle:self.youpaifsessionTitle];
  281. [self refreshSessionSubTitle:self.sessionSubTitle];
  282. [self.youpaiptableView reloadData];
  283. }
  284. - (void)didPullUpMessageData {}
  285. #pragma mark - 会话title
  286. - (NSString *)youpaifsessionTitle
  287. {
  288. NSString *title = @"";
  289. NIMSessionType type = self.session.sessionType;
  290. switch (type) {
  291. case NIMSessionTypeTeam:{
  292. NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:self.session.sessionId];
  293. title = [NSString stringWithFormat:@"%@(%zd)",[team teamName],[team memberNumber]];
  294. }
  295. break;
  296. case NIMSessionTypeP2P:{
  297. title = [NIMKitUtil showNick:self.session.sessionId inSession:self.session];
  298. }
  299. break;
  300. default:
  301. break;
  302. }
  303. return title;
  304. }
  305. - (NSString *)sessionSubTitle{return @"";};
  306. #pragma mark - NIMChatManagerDelegate
  307. //开始发送
  308. - (void)willSendMessage:(NIMMessage *)message
  309. {
  310. id<NIMSessionInteractor> youpaipinteractor = self.youpaipinteractor;
  311. if ([message.session isEqual:self.session]) {
  312. if ([youpaipinteractor findMessageModel:message]) {
  313. [youpaipinteractor updateMessage:message];
  314. }else{
  315. [youpaipinteractor addMessages:@[message]];
  316. }
  317. }
  318. }
  319. //上传资源文件成功
  320. - (void)uploadAttachmentSuccess:(NSString *)urlString
  321. forMessage:(NIMMessage *)message
  322. {
  323. //如果需要使用富文本推送,可以在这里进行 message apns payload 的设置
  324. }
  325. //发送结果
  326. - (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error
  327. {
  328. if ([message.session isEqual:_session])
  329. {
  330. [self.youpaipinteractor updateMessage:message];
  331. if (message.session.sessionType == NIMSessionTypeTeam)
  332. {
  333. //如果是群的话需要检查一下回执显示情况
  334. NIMMessageReceipt *receipt = [[NIMMessageReceipt alloc] initWithMessage:message];
  335. [self.youpaipinteractor checkReceipts:@[receipt]];
  336. }
  337. }
  338. }
  339. //发送进度
  340. -(void)sendMessage:(NIMMessage *)message progress:(float)progress
  341. {
  342. if ([message.session isEqual:_session]) {
  343. [self.youpaipinteractor updateMessage:message];
  344. }
  345. }
  346. //接收消息
  347. - (void)onRecvMessages:(NSArray *)messages
  348. {
  349. if ([self shouldAddListenerForNewMsg])
  350. {
  351. NIMMessage *message = messages.firstObject;
  352. NIMSession *session = message.session;
  353. if (![session isEqual:self.session] || !messages.count)
  354. {
  355. return;
  356. }
  357. [self uiAddMessages:messages];
  358. [self.youpaipinteractor markRead];
  359. }
  360. }
  361. - (void)fetchMessageAttachment:(NIMMessage *)message progress:(float)progress
  362. {
  363. if ([message.session isEqual:_session])
  364. {
  365. [self.youpaipinteractor updateMessage:message];
  366. }
  367. }
  368. - (void)fetchMessageAttachment:(NIMMessage *)message didCompleteWithError:(NSError *)error
  369. {
  370. if ([message.session isEqual:_session])
  371. {
  372. NIMMessageModel *model = [self.youpaipinteractor findMessageModel:message];
  373. //下完缩略图之后,因为比例有变化,重新刷下宽高。
  374. [model cleanCache];
  375. [self.youpaipinteractor updateMessage:message];
  376. }
  377. }
  378. - (void)onRecvMessageReceipts:(NSArray<NIMMessageReceipt *> *)receipts
  379. {
  380. if ([self shouldAddListenerForNewMsg])
  381. {
  382. NSMutableArray *handledReceipts = [[NSMutableArray alloc] init];
  383. for (NIMMessageReceipt *receipt in receipts) {
  384. if ([receipt.session isEqual:self.session])
  385. {
  386. [handledReceipts addObject:receipt];
  387. }
  388. }
  389. if (handledReceipts.count)
  390. {
  391. [self uiCheckReceipts:handledReceipts];
  392. }
  393. }
  394. }
  395. #pragma mark - NIMConversationManagerDelegate
  396. - (void)messagesDeletedInSession:(NIMSession *)session{
  397. [self.youpaipinteractor resetMessages:nil];
  398. [self.youpaiptableView reloadData];
  399. }
  400. - (void)didAddRecentSession:(NIMRecentSession *)recentSession
  401. totalUnreadCount:(NSInteger)totalUnreadCount{
  402. [self changeUnreadCount:recentSession totalUnreadCount:totalUnreadCount];
  403. }
  404. - (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
  405. totalUnreadCount:(NSInteger)totalUnreadCount{
  406. [self changeUnreadCount:recentSession totalUnreadCount:totalUnreadCount];
  407. }
  408. - (void)didRemoveRecentSession:(NIMRecentSession *)recentSession
  409. totalUnreadCount:(NSInteger)totalUnreadCount{
  410. [self changeUnreadCount:recentSession totalUnreadCount:totalUnreadCount];
  411. }
  412. - (void)changeUnreadCount:(NIMRecentSession *)recentSession
  413. totalUnreadCount:(NSInteger)totalUnreadCount{
  414. if ([recentSession.session isEqual:self.session]) {
  415. return;
  416. }
  417. [self changeLeftBarBadge:totalUnreadCount];
  418. }
  419. #pragma mark - NIMMediaManagerDelegate
  420. - (void)recordAudio:(NSString *)filePath didBeganWithError:(NSError *)error {
  421. if (!filePath || error) {
  422. _youpaipsessionInputView.youpaiprecording = NO;
  423. [self onRecordFailed:error];
  424. }
  425. }
  426. - (void)recordAudio:(NSString *)filePath didCompletedWithError:(NSError *)error {
  427. if(!error) {
  428. if ([self recordFileCanBeSend:filePath]) {
  429. [self sendMessage:[NIMMessageMaker msgWithAudio:filePath]];
  430. }else{
  431. [self showRecordFileNotSendReason];
  432. }
  433. } else {
  434. [self onRecordFailed:error];
  435. }
  436. _youpaipsessionInputView.youpaiprecording = NO;
  437. }
  438. - (void)recordAudioDidCancelled {
  439. _youpaipsessionInputView.youpaiprecording = NO;
  440. }
  441. - (void)recordAudioProgress:(NSTimeInterval)currentTime {
  442. [_youpaipsessionInputView youpaifupdateAudioRecordTime:currentTime];
  443. }
  444. - (void)recordAudioInterruptionBegin {
  445. [[NIMSDK sharedSDK].mediaManager cancelRecord];
  446. }
  447. #pragma mark - 录音相关接口
  448. - (void)onRecordFailed:(NSError *)error{}
  449. - (BOOL)recordFileCanBeSend:(NSString *)filepath
  450. {
  451. return YES;
  452. }
  453. - (void)showRecordFileNotSendReason{}
  454. #pragma mark - BBNIMInputDelegate
  455. - (void)didChangeInputHeight:(CGFloat)inputHeight
  456. {
  457. [self.youpaipinteractor changeLayout:inputHeight];
  458. }
  459. #pragma mark - NIMInputActionDelegate
  460. - (BOOL)onTapMediaItem:(NIMMediaItem *)item{
  461. SEL sel = item.selctor;
  462. BOOL youpaiphandled = sel && [self respondsToSelector:sel];
  463. if (youpaiphandled) {
  464. NIMKit_SuppressPerformSelectorLeakWarning([self performSelector:sel withObject:item]);
  465. youpaiphandled = YES;
  466. }
  467. return youpaiphandled;
  468. }
  469. - (void)onTextChanged:(id)sender{
  470. [_youpaipnotificaionSender sendTypingState:self.session];
  471. }
  472. - (void)onSendText:(NSString *)text atUsers:(NSArray *)atUsers
  473. {
  474. NSMutableArray *users = [NSMutableArray arrayWithArray:atUsers];
  475. if (self.session.sessionType == NIMSessionTypeP2P)
  476. {
  477. [users addObject:self.session.sessionId];
  478. }
  479. NSString *robotsToSend = [self robotsToSend:users];
  480. NIMMessage *message = nil;
  481. if (robotsToSend.length)
  482. {
  483. message = [NIMMessageMaker msgWithRobotQuery:text toRobot:robotsToSend];
  484. }
  485. else
  486. {
  487. message = [NIMMessageMaker msgWithText:text];
  488. }
  489. if (atUsers.count)
  490. {
  491. NIMMessageApnsMemberOption *apnsOption = [[NIMMessageApnsMemberOption alloc] init];
  492. apnsOption.userIds = atUsers;
  493. apnsOption.forcePush = YES;
  494. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  495. option.session = self.session;
  496. NSString *me = [[NIMKit sharedKit].provider infoByUser:[NIMSDK sharedSDK].loginManager.currentAccount option:option].showName;
  497. apnsOption.apnsContent = [NSString stringWithFormat:@"%@在群里@了你",me];
  498. message.apnsMemberOption = apnsOption;
  499. }
  500. [self sendMessage:message];
  501. }
  502. - (NSString *)robotsToSend:(NSArray *)atUsers
  503. {
  504. for (NSString *userId in atUsers)
  505. {
  506. if ([[NIMSDK sharedSDK].robotManager isValidRobot:userId])
  507. {
  508. return userId;
  509. }
  510. }
  511. return nil;
  512. }
  513. - (void)onSelectChartlet:(NSString *)chartletId
  514. catalog:(NSString *)catalogId{}
  515. - (void)onCancelRecording
  516. {
  517. [[NIMSDK sharedSDK].mediaManager cancelRecord];
  518. }
  519. - (void)onStopRecording
  520. {
  521. [[NIMSDK sharedSDK].mediaManager stopRecord];
  522. }
  523. - (void)onStartRecording
  524. {
  525. _youpaipsessionInputView.youpaiprecording = YES;
  526. NIMAudioType type = [self recordAudioType];
  527. NSTimeInterval duration = [NIMKit sharedKit].config.recordMaxDuration;
  528. [[NIMSDK sharedSDK].mediaManager addDelegate:self];
  529. [[NIMSDK sharedSDK].mediaManager record:type
  530. duration:duration];
  531. }
  532. #pragma mark - NIMMessageCellDelegate
  533. - (BOOL)onTapCell:(NIMKitEvent *)event{
  534. BOOL youpaiphandle = NO;
  535. NSString *eventName = event.eventName;
  536. if ([eventName isEqualToString:NIMKitEventNameTapAudio])
  537. {
  538. [self.youpaipinteractor mediaAudioPressed:event.messageModel];
  539. youpaiphandle = YES;
  540. }
  541. if ([eventName isEqualToString:NIMKitEventNameTapRobotBlock]) {
  542. NSDictionary *param = event.data;
  543. NIMMessage *message = [NIMMessageMaker msgWithRobotSelect:param[@"text"] target:param[@"target"] params:param[@"param"] toRobot:param[@"robotId"]];
  544. [self sendMessage:message];
  545. youpaiphandle = YES;
  546. }
  547. if ([eventName isEqualToString:NIMKitEventNameTapRobotContinueSession]) {
  548. NIMRobotObject *robotObject = (NIMRobotObject *)event.messageModel.message.messageObject;
  549. NIMRobot *robot = [[NIMSDK sharedSDK].robotManager robotInfo:robotObject.robotId];
  550. NSString *text = [NSString stringWithFormat:@"%@%@%@",NIMInputAtStartChar,robot.nickname,NIMInputAtEndChar];
  551. NIMInputAtItem *item = [[NIMInputAtItem alloc] init];
  552. item.uid = robot.userId;
  553. item.name = robot.nickname;
  554. [self.youpaipsessionInputView.youpaipatCache addAtItem:item];
  555. [self.youpaipsessionInputView.youpaiptoolBar youpaifinsertText:text];
  556. youpaiphandle = YES;
  557. }
  558. return youpaiphandle;
  559. }
  560. - (void)onRetryMessage:(NIMMessage *)message
  561. {
  562. if (message.isReceivedMsg) {
  563. [[[NIMSDK sharedSDK] chatManager] fetchMessageAttachment:message
  564. error:nil];
  565. }else{
  566. [[[NIMSDK sharedSDK] chatManager] resendMessage:message
  567. error:nil];
  568. }
  569. }
  570. - (BOOL)onLongPressCell:(NIMMessage *)message
  571. inView:(UIView *)view
  572. {
  573. BOOL youpaiphandle = NO;
  574. NSArray *items = [self menusItems:message];
  575. if ([items count] && [self becomeFirstResponder]) {
  576. UIMenuController *controller = [UIMenuController sharedMenuController];
  577. controller.menuItems = items;
  578. _messageForMenu = message;
  579. [controller setTargetRect:view.bounds inView:view];
  580. [controller setMenuVisible:YES animated:YES];
  581. youpaiphandle = YES;
  582. }
  583. return youpaiphandle;
  584. }
  585. - (BOOL)disableAudioPlayedStatusIcon:(NIMMessage *)message
  586. {
  587. BOOL youpaipdisable = NO;
  588. if ([self.sessionConfig respondsToSelector:@selector(disableAudioPlayedStatusIcon)])
  589. {
  590. youpaipdisable = [self.sessionConfig disableAudioPlayedStatusIcon];
  591. }
  592. return youpaipdisable;
  593. }
  594. #pragma mark - 配置项
  595. - (id<NIMSessionConfig>)sessionConfig
  596. {
  597. return nil; //使用默认配置
  598. }
  599. #pragma mark - 配置项列表
  600. //是否需要监听新消息通知 : 某些场景不需要监听新消息,如浏览服务器消息历史界面
  601. - (BOOL)shouldAddListenerForNewMsg
  602. {
  603. BOOL youpaipshould = YES;
  604. if ([self.sessionConfig respondsToSelector:@selector(disableReceiveNewMessages)]) {
  605. youpaipshould = ![self.sessionConfig disableReceiveNewMessages];
  606. }
  607. return youpaipshould;
  608. }
  609. //是否需要显示输入框 : 某些场景不需要显示输入框,如使用 3D touch 的场景预览会话界面内容
  610. - (BOOL)shouldShowInputView
  611. {
  612. BOOL youpaipshould = YES;
  613. if ([self.sessionConfig respondsToSelector:@selector(disableInputView)]) {
  614. youpaipshould = ![self.sessionConfig disableInputView];
  615. }
  616. if ([[LCSaveData getSysId] isEqualToString:self.session.sessionId] || [self.session.sessionId isEqualToString:@"11"]) {
  617. youpaipshould = NO;
  618. }
  619. return youpaipshould;
  620. }
  621. //当前录音格式 : NIMSDK 支持 aac 和 amr 两种格式
  622. - (NIMAudioType)recordAudioType
  623. {
  624. NIMAudioType type = NIMAudioTypeAAC;
  625. if ([self.sessionConfig respondsToSelector:@selector(recordType)]) {
  626. type = [self.sessionConfig recordType];
  627. }
  628. return type;
  629. }
  630. //是否需要监听感应器事件
  631. - (BOOL)needProximityMonitor
  632. {
  633. BOOL youpaipneedProximityMonitor = YES;
  634. if ([self.sessionConfig respondsToSelector:@selector(disableProximityMonitor)]) {
  635. youpaipneedProximityMonitor = !self.sessionConfig.disableProximityMonitor;
  636. }
  637. return youpaipneedProximityMonitor;
  638. }
  639. #pragma mark - 菜单
  640. - (NSArray *)menusItems:(NIMMessage *)message
  641. {
  642. NSMutableArray *items = [NSMutableArray array];
  643. BOOL youpaipcopyText = NO;
  644. if (message.messageType == NIMMessageTypeText)
  645. {
  646. youpaipcopyText = YES;
  647. }
  648. if (message.messageType == NIMMessageTypeRobot)
  649. {
  650. NIMRobotObject *robotObject = (NIMRobotObject *)message.messageObject;
  651. youpaipcopyText = !robotObject.isFromRobot;
  652. }
  653. if (youpaipcopyText) {
  654. [items addObject:[[UIMenuItem alloc] initWithTitle:@"复制"
  655. action:@selector(youpaipcopyText:)]];
  656. }
  657. [items addObject:[[UIMenuItem alloc] initWithTitle:@"删除"
  658. action:@selector(deleteMsg:)]];
  659. return items;
  660. }
  661. - (NIMMessage *)messageForMenu
  662. {
  663. return _messageForMenu;
  664. }
  665. - (BOOL)canBecomeFirstResponder
  666. {
  667. return YES;
  668. }
  669. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  670. {
  671. NSArray *items = [[UIMenuController sharedMenuController] menuItems];
  672. for (UIMenuItem *item in items) {
  673. if (action == [item action]){
  674. return YES;
  675. }
  676. }
  677. return NO;
  678. }
  679. - (void)youpaipcopyText:(id)sender
  680. {
  681. NIMMessage *message = [self messageForMenu];
  682. if (message.text.length) {
  683. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  684. [pasteboard setString:message.text];
  685. }
  686. }
  687. - (void)deleteMsg:(id)sender
  688. {
  689. NIMMessage *message = [self messageForMenu];
  690. [self uiDeleteMessage:message];
  691. [self.conversationManager deleteMessage:message];
  692. }
  693. - (void)menuDidHide:(NSNotification *)notification
  694. {
  695. [UIMenuController sharedMenuController].menuItems = nil;
  696. }
  697. #pragma mark - 操作接口
  698. - (void)uiAddMessages:(NSArray *)messages
  699. {
  700. [self.youpaipinteractor addMessages:messages];
  701. }
  702. - (void)uiInsertMessages:(NSArray *)messages
  703. {
  704. [self.youpaipinteractor insertMessages:messages];
  705. }
  706. - (NIMMessageModel *)uiDeleteMessage:(NIMMessage *)message{
  707. NIMMessageModel *model = [self.youpaipinteractor deleteMessage:message];
  708. if (model.shouldShowReadLabel && model.message.session.sessionType == NIMSessionTypeP2P)
  709. {
  710. [self uiCheckReceipts:nil];
  711. }
  712. return model;
  713. }
  714. - (void)uiUpdateMessage:(NIMMessage *)message{
  715. [self.youpaipinteractor updateMessage:message];
  716. }
  717. - (void)uiCheckReceipts:(NSArray<NIMMessageReceipt *> *)receipts
  718. {
  719. [self.youpaipinteractor checkReceipts:receipts];
  720. }
  721. #pragma mark - NIMMeidaButton
  722. - (void)onTapMediaItemPicture:(NIMMediaItem *)item
  723. {
  724. [self.youpaipinteractor mediaPicturePressed];
  725. }
  726. - (void)onTapMediaItemShoot:(NIMMediaItem *)item
  727. {
  728. [self.youpaipinteractor mediaShootPressed];
  729. }
  730. - (void)onTapMediaItemLocation:(NIMMediaItem *)item
  731. {
  732. [self.youpaipinteractor mediaLocationPressed];
  733. }
  734. #pragma mark - 旋转处理 (iOS8 or above)
  735. - (void)viewWillTransitionToSize:(CGSize)size
  736. withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  737. {
  738. self.youpaiplastVisibleIndexPathBeforeRotation = [self.youpaiptableView indexPathsForVisibleRows].lastObject;
  739. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  740. if (self.view.window) {
  741. __weak typeof(self) wself = self;
  742. [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context)
  743. {
  744. [[NIMSDK sharedSDK].mediaManager cancelRecord];
  745. [wself.youpaipinteractor cleanCache];
  746. [wself.youpaipsessionInputView youpaifreset];
  747. [wself.youpaiptableView reloadData];
  748. [wself.youpaiptableView scrollToRowAtIndexPath:wself.youpaiplastVisibleIndexPathBeforeRotation atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  749. } completion:nil];
  750. }
  751. }
  752. #pragma mark - 标记已读
  753. - (void)markRead
  754. {
  755. [self.youpaipinteractor markRead];
  756. }
  757. #pragma mark - Private
  758. - (void)addListener
  759. {
  760. [[NIMSDK sharedSDK].chatManager addDelegate:self];
  761. [[NIMSDK sharedSDK].conversationManager addDelegate:self];
  762. }
  763. - (void)removeListener
  764. {
  765. [[NIMSDK sharedSDK].chatManager removeDelegate:self];
  766. [[NIMSDK sharedSDK].conversationManager removeDelegate:self];
  767. }
  768. - (void)changeLeftBarBadge:(NSInteger)unreadCount
  769. {
  770. // NIMCustomLeftBarView *leftBarView = (NIMCustomLeftBarView *)self.navigationItem.leftBarButtonItem.customView;
  771. self.youpaipbadgeBarView.badgeView.badgeValue = [NSString stringWithFormat:@"%ld", unreadCount];
  772. // self.youpaipbadgeBarView.badgeView.backgroundColor = [UIColor yellowColor];
  773. self.youpaipbadgeBarView.badgeView.hidden = !unreadCount;
  774. }
  775. - (id<NIMConversationManager>)conversationManager{
  776. switch (self.session.sessionType) {
  777. case NIMSessionTypeChatroom:
  778. return nil;
  779. break;
  780. case NIMSessionTypeP2P:
  781. case NIMSessionTypeTeam:
  782. default:
  783. return [NIMSDK sharedSDK].conversationManager;
  784. }
  785. }
  786. - (void)setUpTitleView
  787. {
  788. NIMKitTitleView *titleView = (NIMKitTitleView *)self.navigationItem.titleView;
  789. if (!titleView || ![titleView isKindOfClass:[NIMKitTitleView class]])
  790. {
  791. titleView = [[NIMKitTitleView alloc] initWithFrame:CGRectZero];
  792. self.navigationItem.titleView = titleView;
  793. titleView.titleLabel.text = self.youpaifsessionTitle;
  794. titleView.subtitleLabel.text = self.sessionSubTitle;
  795. titleView.titleLabel.textColor = [UIColor whiteColor];
  796. self.youpaiptitleLabel = titleView.titleLabel;
  797. self.youpaipsubTitleLabel = titleView.subtitleLabel;
  798. }
  799. [titleView sizeToFit];
  800. }
  801. - (void)refreshSessionTitle:(NSString *)title
  802. {
  803. self.youpaiptitleLabel.text = title;
  804. [self setUpTitleView];
  805. }
  806. - (void)refreshSessionSubTitle:(NSString *)title
  807. {
  808. self.youpaipsubTitleLabel.text = title;
  809. [self setUpTitleView];
  810. }
  811. #pragma mark ZYIMSessionDetailWindowDelegate
  812. -(void)imSessionDetailWindowAction:(NSString *)type andSession:(NIMSession *)session
  813. {
  814. if ([type isEqualToString:@"updateRemark"]) {//设置备注名
  815. YOUPAIZYUpdateRemarkWindow *VC= [[YOUPAIZYUpdateRemarkWindow alloc]init];
  816. VC.vWidth = KScreenWidth -68;
  817. VC.vHeight = ScaleHeight(187)+10;
  818. VC.youpaipsession = session;
  819. VC.isTouchDismiss = YES;
  820. [kAppDelegate.window.rootViewController TFPresentVC:VC completion:^{
  821. }];
  822. }
  823. if ([type isEqualToString:@"clearChat"]) {//清除聊天记录
  824. WeakSelf;
  825. LZAlertAction *cancelAction = [LZAlertAction actionWithTitle:@"取消" handler:^(LZAlertAction *action) {
  826. }];
  827. cancelAction.cornerRadius = 5.0f;
  828. cancelAction.bgColor = HexColorFromRGB(0x9F9DA5);
  829. LZAlertAction *confimAction = [LZAlertAction actionWithTitle:@"删除" handler:^(LZAlertAction *action) {
  830. [weakSelf clearChatSession:session];
  831. }];
  832. confimAction.cornerRadius = 5.0f;
  833. confimAction.bgColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake((KScreenWidth - 105.0f) / 2.0f, 48.0f) FromColors:@[ZYGradientOneColor,ZYGradientTwoColor] ByGradientType:GradientLeftToRight]];
  834. LZAlertWindow *alert = [LZAlertWindow alertWithTitle:@"提示" content:@"确定删除该聊天?" action:@[cancelAction,confimAction]];
  835. alert.contentTextAlignment = NSTextAlignmentCenter;
  836. [self TFPresentVC:alert completion:^{}];
  837. }
  838. if ([type isEqualToString:@"report"]) {//举报
  839. YOUPAILCReportVC* report = [[YOUPAILCReportVC alloc]init];
  840. report.youpaipreport_uid = session.sessionId;
  841. report.youpaiptype = 4;
  842. [self.navigationController pushViewController:report animated:YES];
  843. }
  844. }
  845. -(void)showUserVCWithSession:(NIMSession *)session
  846. {
  847. YOUPAILZUserShowVC *vc = [[YOUPAILZUserShowVC alloc] init];
  848. vc.youpaipuserId = session.sessionId;
  849. [self.navigationController pushViewController:vc animated:YES];
  850. }
  851. -(void)clearChatSession:(NIMSession *)session
  852. {
  853. id<NIMConversationManager> manager = [[NIMSDK sharedSDK] conversationManager];
  854. NIMDeleteMessagesOption* deleteOption = [[NIMDeleteMessagesOption alloc]init];
  855. deleteOption.removeSession = NO;
  856. deleteOption.removeTable = YES;
  857. [manager deleteAllmessagesInSession:session option:deleteOption];
  858. [ZCHUDHelper showTitle:@"已清空"];
  859. }
  860. @end