IMAConversationManager.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. //
  2. // IMAConversationManager.m
  3. // TIMAdapter
  4. //
  5. // Created by AlexiChen on 16/2/18.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMAConversationManager.h"
  9. @implementation IMAConversationChangedNotifyItem
  10. - (instancetype)initWith:(IMAConversationChangedNotifyType)type
  11. {
  12. if (self = [super init])
  13. {
  14. _type = type;
  15. }
  16. return self;
  17. }
  18. - (NSNotification *)changedNotification
  19. {
  20. NSNotification *notify = [NSNotification notificationWithName:[self notificationName] object:self];
  21. return notify;
  22. }
  23. - (NSString *)notificationName
  24. {
  25. return [NSString stringWithFormat:@"IMAConversationChangedNotification_%d", (int)_type];
  26. }
  27. @end
  28. @implementation IMAConversationManager
  29. - (instancetype)init
  30. {
  31. if (self = [super init])
  32. {
  33. _conversationList = [[CLSafeSetArray alloc] init];
  34. }
  35. return self;
  36. }
  37. - (void)releaseChattingConversation
  38. {
  39. // [_chattingConversation releaseConversation];
  40. _chattingConversation = nil;
  41. }
  42. - (void)asyncUpdateConversationListComplete
  43. {
  44. if (_refreshStyle == EIMARefresh_Wait)
  45. {
  46. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  47. // 说明有更新过来,再更新一次
  48. [self asyncConversationList];
  49. _refreshStyle = EIMARefresh_None;
  50. });
  51. }
  52. else
  53. {
  54. _refreshStyle = EIMARefresh_None;
  55. [self updateOnLocalMsgComplete];
  56. }
  57. }
  58. - (void)asyncUpdateConversationList
  59. {
  60. NSInteger unRead = 0;
  61. // NSMutableArray *conversationList = [NSMutableArray array];
  62. //2.0之前的版本不支持 getConversationList 接口
  63. NSArray *conversationList = [[TIMManager sharedInstance] getConversationList];
  64. for (TIMConversation *conversation in conversationList)
  65. {
  66. IMAConversation *conv = nil;
  67. if ([conversation getType] == TIM_SYSTEM)
  68. {
  69. #if kSupportCustomConversation
  70. // 可能返回空
  71. conv = [[IMACustomConversation alloc] initWith:conversation];
  72. #else
  73. continue;
  74. #endif
  75. }
  76. else
  77. {
  78. conv = [[IMAConversation alloc] initWith:conversation];
  79. }
  80. if (conv)
  81. {
  82. [_conversationList addObject:conv];
  83. }
  84. if (_chattingConversation && [_chattingConversation isEqual:conv])
  85. {
  86. [conv copyConversationInfo:_chattingConversation];
  87. // 防止因中途在聊天时,出现onrefresh回调
  88. _chattingConversation = conv;
  89. }
  90. else
  91. {
  92. if (conv)
  93. {
  94. unRead += [conversation getUnReadMessageNum];
  95. }
  96. }
  97. }
  98. [self asyncUpdateConversationListComplete];
  99. if (unRead != _unReadMessageCount)
  100. {
  101. self.unReadMessageCount = unRead;
  102. }
  103. DebugLog(@"==========>>>>>>>>>asyncUpdateConversationList Complete");
  104. }
  105. - (void)asyncConversationList
  106. {
  107. DebugLog(@"==========>>>>>>>>>asyncConversationList");
  108. // 因OnRefresh里面不定期有回调,能过这种处理,避免_conversationList修改导致界面Crash
  109. if (_refreshStyle == EIMARefresh_None)
  110. {
  111. _refreshStyle = EIMARefresh_ING;
  112. [self asyncUpdateConversationList];
  113. }
  114. else
  115. {
  116. _refreshStyle = EIMARefresh_Wait;
  117. }
  118. }
  119. - (void)addConversationChangedObserver:(id)observer handler:(SEL)selector forEvent:(NSUInteger)eventID
  120. {
  121. NSUInteger op = EIMAContact_AddNewSubGroup;
  122. do
  123. {
  124. if (op & eventID)
  125. {
  126. NSString *notification = [NSString stringWithFormat:@"IMAConversationChangedNotification_%d", (int)op];
  127. [[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:notification object:nil];
  128. eventID -= op;
  129. }
  130. op = op << 1;
  131. } while (eventID > 0);
  132. }
  133. - (void)removeConversationChangedObser:(id)observer
  134. {
  135. [[NSNotificationCenter defaultCenter] removeObserver:observer];
  136. }
  137. - (void)deleteConversation:(IMAConversation *)conv needUIRefresh:(BOOL)need
  138. {
  139. NSInteger index = [_conversationList indexOfObject:conv];
  140. if (index >= 0 && index < [_conversationList count])
  141. {
  142. [conv setReadAllMsg];
  143. self.unReadMessageCount -= [conv unReadCount];
  144. [_conversationList removeObject:conv];
  145. if ([conv type] == TIM_C2C)
  146. {
  147. [[TIMManager sharedInstance] deleteConversation:[conv type] receiver:[conv receiver]];
  148. }
  149. else if ([conv type] == TIM_GROUP)
  150. {
  151. [[TIMManager sharedInstance] deleteConversationAndMessages:[conv type] receiver:[conv receiver]];
  152. }
  153. if (need)
  154. {
  155. [self updateOnDelete:conv atIndex:index];
  156. }
  157. }
  158. }
  159. - (IMAConversation *)chatWith:(IMAUser *)user
  160. {
  161. TIMConversation *conv = nil;
  162. if ([user isC2CType])
  163. {
  164. conv = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:[user userId]];
  165. }
  166. else if([user isGroupType])
  167. {
  168. conv = [[TIMManager sharedInstance] getConversation:TIM_GROUP receiver:[user userId]];
  169. }
  170. else if ([user isSystemType])
  171. {
  172. // 暂不支持System消息
  173. return nil;
  174. }
  175. self.unReadMessageCount -= [conv getUnReadMessageNum];
  176. // [conv setReadMessage];
  177. [conv setReadMessage:nil succ:^{
  178. } fail:^(int code, NSString *msg) {
  179. }];
  180. if (conv)
  181. {
  182. IMAConversation *temp = [[IMAConversation alloc] initWith:conv];
  183. NSInteger index = [_conversationList indexOfObject:temp];
  184. if (index >= 0 && index < _conversationList.count)
  185. {
  186. IMAConversation *ret = [_conversationList objectAtIndex:index];
  187. _chattingConversation = ret;
  188. // _chattingConversation.lastMessage = _chattingConversation.lastMessage;
  189. return ret;
  190. }
  191. _chattingConversation = temp;
  192. return temp;
  193. }
  194. return nil;
  195. }
  196. // 主用要于自定义类型
  197. - (IMAConversation *)queryConversationWithType:(IMAConType)user
  198. {
  199. // for (NSInteger i = 0; i < [_conversationList count]; i++)
  200. // {
  201. // IMAConversation *conv = [_conversationList objectAtIndex:i];
  202. // if ([conv isChatWith:user])
  203. // {
  204. // return conv;
  205. // }
  206. // }
  207. return nil;
  208. }
  209. - (IMAConversation *)queryConversationWith:(IMAUser *)user
  210. {
  211. if (user)
  212. {
  213. for (NSInteger i = 0; i < [_conversationList count]; i++)
  214. {
  215. IMAConversation *conv = [_conversationList objectAtIndex:i];
  216. if ([conv isChatWith:user])
  217. {
  218. return conv;
  219. }
  220. }
  221. }
  222. return nil;
  223. }
  224. - (void)removeConversationWith:(IMAUser *)user
  225. {
  226. for (NSInteger i = 0; i < [_conversationList count]; i++)
  227. {
  228. V2TIMConversation *conv = [_conversationList objectAtIndex:i];
  229. if ([conv.userID isEqualToString:user.userId] || [conv.groupID isEqualToString:user.userId])
  230. {
  231. if (conv == _chattingConversation)
  232. {
  233. // TODO:通知界面
  234. [[AppDelegate sharedAppDelegate] popToRootViewController];
  235. }
  236. [_conversationList removeObjectAtIndex:i];
  237. // self.unReadMessageCount -= [conv unReadCount];
  238. self.unReadMessageCount -= [conv unreadCount];
  239. // if ([conv type] == TIM_C2C)
  240. // {
  241. [[V2TIMManager sharedInstance] deleteConversation:conv.userID succ:^{
  242. } fail:^(int code, NSString *desc) {
  243. }];
  244. // [[TIMManager sharedInstance] deleteConversation:[conv type] receiver:[conv receiver]];
  245. // }
  246. // else if ([conv type] == TIM_GROUP)
  247. // {
  248. // [[TIMManager sharedInstance] deleteConversationAndMessages:[conv type] receiver:[conv receiver]];
  249. // }
  250. [self updateOnDelete:conv atIndex:i];
  251. break;
  252. }
  253. }
  254. }
  255. //- (void)removeConversationWithConv:(IMAConversation *)conv
  256. //{
  257. // if (conv == nil)
  258. // {
  259. // return;
  260. // }
  261. // [[TIMManager sharedInstance] deleteConversation:[conv type] receiver:[conv receiver]];
  262. //}
  263. - (void)updateConversationWith:(IMAUser *)user
  264. {
  265. IMAConversation *conv = [self queryConversationWith:user];
  266. if (conv)
  267. {
  268. [self updateOnConversationChanged:conv];
  269. }
  270. }
  271. - (NSInteger)insertPosition
  272. {
  273. IMAPlatform *mp = [IMAPlatform sharedInstance];
  274. if (!mp.isConnected)
  275. {
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. //消息撤回通知
  281. - (void)onRevokeMessage:(TIMMessageLocator*)locator
  282. {
  283. [[NSNotificationCenter defaultCenter] postNotificationName:kIMAMSG_RevokeNotification object:locator];
  284. [self changeLastMsg:locator];
  285. }
  286. //判断撤销的是不是最后一条消息,如果是,则最近回话列表中的显示,需要改成“xxx撤销了一条消息”
  287. - (void)changeLastMsg:(TIMMessageLocator*)locator
  288. {
  289. for (int i = 0; i < [_conversationList count]; i++)
  290. {
  291. IMAConversation *imaconv = [_conversationList objectAtIndex:i];
  292. if ([imaconv.lastMessage.msg respondsToLocator:locator]) {
  293. IMAMsg *imamsg = [IMAMsg msgWithRevoked:imaconv.lastMessage.msg.sender];
  294. imaconv.lastMessage = imamsg;
  295. }
  296. }
  297. }
  298. /**
  299. * 新消息通知
  300. *
  301. * @param msgs 新消息列表,TIMMessage 类型数组
  302. */
  303. - (void)onNewMessage:(NSArray *)msgs
  304. {
  305. for (TIMMessage *msg in msgs)
  306. {
  307. IMAMsg *imamsg = [IMAMsg msgWith:msg];
  308. TIMConversation *conv = [msg getConversation];
  309. BOOL isSystemMsg = [conv getType] == TIM_SYSTEM;
  310. BOOL isAddGroupReq = NO;
  311. BOOL isAddFriendReq = NO;
  312. BOOL isContinue = YES;
  313. if (isSystemMsg)
  314. {
  315. int elemCount = [msg elemCount];
  316. for (int i = 0; i < elemCount; i++)
  317. {
  318. TIMElem* elem = [msg getElem:i];
  319. if ([elem isKindOfClass:[TIMGroupSystemElem class]])
  320. {
  321. TIMGroupSystemElem *gse = (TIMGroupSystemElem *)elem;
  322. if (gse.type == TIM_GROUP_SYSTEM_ADD_GROUP_REQUEST_TYPE)
  323. {
  324. isContinue = NO;
  325. isAddGroupReq = YES;
  326. }
  327. }
  328. else if ([elem isKindOfClass:[TIMSNSSystemElem class]])
  329. {
  330. TIM_SNS_SYSTEM_TYPE type = ((TIMSNSSystemElem *)elem).type;
  331. if (type == TIM_SNS_SYSTEM_ADD_FRIEND_REQ)
  332. {
  333. if (!msg.isSelf)
  334. {
  335. isContinue = NO;
  336. isAddFriendReq = YES;
  337. }
  338. }
  339. }
  340. }
  341. if (isContinue)
  342. {
  343. continue;
  344. }
  345. }
  346. BOOL updateSucc = NO;
  347. for (int i = 0; i < [_conversationList count]; i++)
  348. {
  349. IMAConversation *imaconv = [_conversationList objectAtIndex:i];
  350. NSString *imaconvReceiver = [imaconv receiver];
  351. if (imaconv.type == [conv getType] && ([imaconvReceiver isEqualToString:[conv getReceiver]] || [imaconvReceiver isEqualToString:[IMACustomConversation getCustomConversationID:imamsg]]))
  352. {
  353. if (imaconv == _chattingConversation)
  354. {
  355. //如果是c2c会话,则更新“对方正在输入...”状态
  356. BOOL isInputStatus = NO;
  357. if (!msg.isSelf)
  358. {
  359. // if ([_chattingConversation imaType] == TIM_C2C)
  360. // {
  361. // int elemCount = [imamsg.msg elemCount];
  362. // for (int i = 0; i < elemCount; i++)
  363. // {
  364. // TIMElem* elem = [msg getElem:i];
  365. // CustomElemCmd *elemCmd = [self isOnlineMsg:elem];
  366. //
  367. // if (elemCmd)
  368. // {
  369. // isInputStatus = YES;
  370. // //[[NSNotificationCenter defaultCenter] postNotificationName:kUserInputStatus object:elemCmd];
  371. // }
  372. // }
  373. // }
  374. //
  375. // if (!isInputStatus)
  376. // {
  377. //// [conv setReadMessage];
  378. // imaconv.lastMessage = imamsg;
  379. // [_chattingConversation onReceiveNewMessage:imamsg];
  380. // }
  381. }
  382. }
  383. else
  384. {
  385. TIMElem* elem = [msg getElem:0];
  386. CustomElemCmd *elemCmd = [self isOnlineMsg:elem];
  387. if ([elem isKindOfClass:[TIMCustomElem class]])
  388. {
  389. TIMCustomElem *elemcd = (TIMCustomElem *)elem;
  390. NSString *dataStr = [[NSString alloc] initWithData:elemcd.data encoding:NSUTF8StringEncoding];
  391. NSDictionary *dic = [dataStr objectFromJSONString];
  392. CustomMessageModel *customMessageModel = [CustomMessageModel mj_objectWithKeyValues:dic];
  393. NSLog(@"收到广播消息%d",customMessageModel.type);
  394. BGTabBarController *tabBar = [BGTabBarController sharedInstance];
  395. UITabBarItem *item = [tabBar
  396. .tabBar.items objectAtIndex:3];
  397. if (customMessageModel.type == MSG_RECEIVE_GUILD_REFRESH_TYPE) {
  398. NSLog(@"收到消息");
  399. [[NSNotificationCenter defaultCenter]postNotificationName:@"RECEIVE_GUILD_REFRESH_TYPE" object:nil];
  400. }
  401. if (customMessageModel.type == MSG_RECEIVE_BROADCAST || customMessageModel.type == MSG_OPEN_VIP_TYPE || customMessageModel.type == MSG_OPEN_GUARD_SUCCESS) {
  402. if (customMessageModel.sender.guardian_broadcast.integerValue != 0) {
  403. [[BGIMMsgHandler sharedInstance]showGlobalViewWithModel:customMessageModel];
  404. }
  405. }else if(customMessageModel.type == MSG_PRIVATE_TEXT ||customMessageModel.type == MSG_PRIVATE_VOICE ||customMessageModel.type == MSG_PRIVATE_IMAGE ||customMessageModel.type == MSG_PRIVATE_GIFT){
  406. // [AppDelegate sharedAppDelegate].entertabBar.
  407. item.badgeValue = [NSString stringWithFormat:@"%d", [item.badgeValue intValue] + 1];
  408. }
  409. }
  410. if (!elemCmd)
  411. {
  412. imaconv.lastMessage = imamsg;
  413. if (isSystemMsg)
  414. {
  415. __weak IMAConversationManager *ws = self;
  416. // 系统消息
  417. IMACustomConversation *customConv = (IMACustomConversation *)imaconv;
  418. [customConv saveMessage:imamsg succ:^(int newUnRead) {
  419. ws.unReadMessageCount += newUnRead;
  420. [ws updateOnChat:imaconv moveFromIndex:i];
  421. }];
  422. }
  423. else
  424. {
  425. //如果是自己发出去的消息,一定是已读(这里判断主要是用在多终端登录的情况下)
  426. if (![imamsg isMineMsg])
  427. {
  428. self.unReadMessageCount++;
  429. }
  430. [self updateOnChat:imaconv moveFromIndex:i];
  431. }
  432. }
  433. }
  434. updateSucc = YES;
  435. break;
  436. }
  437. }
  438. if (!updateSucc && _refreshStyle == EIMARefresh_None)
  439. {
  440. if (isSystemMsg)
  441. {
  442. NSString *receiverid = [IMACustomConversation getCustomConversationID:imamsg];
  443. TIMConversation *imconv = [[TIMManager sharedInstance] getConversation:TIM_SYSTEM receiver:receiverid];
  444. [imconv setReadMessage:nil succ:^{
  445. } fail:^(int code, NSString *msg) {
  446. }];
  447. // 说明会话列表中没有该会话,新生建会话,并更新到
  448. IMACustomConversation *temp = [[IMACustomConversation alloc] initWith:imconv andMsg:imamsg];
  449. if (temp)
  450. {
  451. __weak IMAConversationManager *ws = self;
  452. [temp saveMessage:imamsg succ:^(int newUnRead) {
  453. ws.unReadMessageCount += newUnRead;
  454. }];
  455. [_conversationList insertObject:temp atIndex:[self insertPosition]];
  456. [self updateOnNewConversation:temp];
  457. }
  458. }
  459. else
  460. {
  461. // 说明会话列表中没有该会话,新生建会话,并更新到
  462. IMAConversation *temp = [[IMAConversation alloc] initWith:conv];
  463. temp.lastMessage = imamsg;
  464. [_conversationList insertObject:temp atIndex:[self insertPosition]];
  465. self.unReadMessageCount++;
  466. [self updateOnNewConversation:temp];
  467. }
  468. }
  469. }
  470. }
  471. - (CustomElemCmd *)isOnlineMsg:(TIMElem *) elem
  472. {
  473. if ([elem isKindOfClass:[TIMCustomElem class]])
  474. {
  475. CustomElemCmd *elemCmd = [CustomElemCmd parseCustom:(TIMCustomElem *)elem];
  476. if (elemCmd)
  477. {
  478. return elemCmd;
  479. }
  480. }
  481. return nil;
  482. }
  483. //申请加群请求
  484. - (void)onAddGroupRequest:(TIMGroupSystemElem *)item
  485. {
  486. // IMAGroup *tempGroup = [[IMAGroup alloc] initWith:item.group];
  487. // IMAGroup *group = (IMAGroup *)[[IMAPlatform sharedInstance].contactMgr isContainUser:tempGroup];
  488. // NSString *message = [NSString stringWithFormat:ASLocalizedString(@"%@申请加入群:%@\n申请理由:%@"), item.user, group.groupInfo.groupName, item.msg];
  489. //
  490. // UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:ASLocalizedString(@"加群申请")message:message cancelButtonTitle:ASLocalizedString(@"忽略")otherButtonTitles:@[ASLocalizedString(@"同意"),ASLocalizedString(@"拒绝")] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  491. // if (buttonIndex == 1)//同意
  492. // {
  493. // [[IMAPlatform sharedInstance] asyncAcceptAddGroup:item succ:^{
  494. //
  495. // } fail:nil];
  496. // }
  497. // else if (buttonIndex == 2)//拒绝
  498. // {
  499. // [[IMAPlatform sharedInstance] asyncRefuseAddGroup:item succ:^{
  500. //
  501. // } fail:nil];
  502. // }
  503. // }];
  504. // [alert show];
  505. }
  506. - (void)onAddFreindRequest:(TIMSNSSystemElem *)elem
  507. {
  508. }
  509. @end
  510. @implementation IMAConversationManager (Protected)
  511. - (void)onConnect
  512. {
  513. // 删除
  514. IMAConnectConversation *conv = [[IMAConnectConversation alloc] init];
  515. NSInteger index = [_conversationList indexOfObject:conv];
  516. if (index >= 0 && index < [_conversationList count])
  517. {
  518. [_conversationList removeObject:conv];
  519. [self updateOnDelete:conv atIndex:index];
  520. }
  521. }
  522. - (void)onDisConnect
  523. {
  524. // 插入一个网络断开的fake conversation
  525. IMAConnectConversation *conv = [[IMAConnectConversation alloc] init];
  526. NSInteger index = [_conversationList indexOfObject:conv];
  527. if (!(index >= 0 && index < [_conversationList count]))
  528. {
  529. [_conversationList insertObject:conv atIndex:0];
  530. [self updateOnNewConversation:conv];
  531. }
  532. }
  533. - (void)updateOnChat:(IMAConversation *)conv moveFromIndex:(NSUInteger)index
  534. {
  535. NSInteger toindex = [self insertPosition];
  536. if (index != toindex)
  537. {
  538. [_conversationList removeObjectAtIndex:index];
  539. [_conversationList insertObject:conv atIndex:toindex];
  540. // 更新界面
  541. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_BecomeActiveTop];
  542. item.conversation = conv;
  543. item.index = index;
  544. item.toIndex = toindex;
  545. if (_conversationChangedCompletion)
  546. {
  547. _conversationChangedCompletion(item);
  548. }
  549. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  550. }
  551. }
  552. - (void)updateOnDelete:(IMAConversation *)conv atIndex:(NSUInteger)index
  553. {
  554. // 更新界面
  555. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_DeleteConversation];
  556. item.conversation = conv;
  557. item.index = index;
  558. if (_conversationChangedCompletion)
  559. {
  560. _conversationChangedCompletion(item);
  561. }
  562. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  563. }
  564. - (void)updateOnAsyncLoadContactComplete
  565. {
  566. // 通知更新界面
  567. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_SyncLocalConversation];
  568. if (_conversationChangedCompletion)
  569. {
  570. _conversationChangedCompletion(item);
  571. }
  572. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  573. }
  574. - (void)updateOnLocalMsgComplete
  575. {
  576. // 更新界面
  577. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_SyncLocalConversation];
  578. if (_conversationChangedCompletion)
  579. {
  580. _conversationChangedCompletion(item);
  581. }
  582. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  583. }
  584. - (void)updateOnLastMessageChanged:(IMAConversation *)conv
  585. {
  586. if ([_chattingConversation isEqual:conv])
  587. {
  588. NSInteger index = [_conversationList indexOfObject:conv];
  589. NSInteger toindex = [self insertPosition];
  590. if (index > 0 && index < [_conversationList count])
  591. {
  592. [_conversationList removeObject:conv];
  593. [_conversationList insertObject:conv atIndex:toindex];
  594. [self updateOnChat:conv moveFromIndex:index toIndex:toindex];
  595. }
  596. else if (index < 0 || index > [_conversationList count])
  597. {
  598. [_conversationList insertObject:conv atIndex:[self insertPosition]];
  599. [self updateOnNewConversation:conv];
  600. }
  601. else
  602. {
  603. // index == 0 不作处理
  604. }
  605. }
  606. }
  607. - (void)updateOnChat:(IMAConversation *)conv moveFromIndex:(NSUInteger)index toIndex:(NSInteger)toIdx
  608. {
  609. // 更新界面
  610. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_BecomeActiveTop];
  611. item.conversation = conv;
  612. item.index = index;
  613. item.toIndex = toIdx;
  614. if (_conversationChangedCompletion)
  615. {
  616. _conversationChangedCompletion(item);
  617. }
  618. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  619. }
  620. - (void)updateOnNewConversation:(IMAConversation *)conv
  621. {
  622. // 更新界面
  623. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_NewConversation];
  624. item.conversation = conv;
  625. item.index = [_conversationList indexOfObject:conv];
  626. if (_conversationChangedCompletion)
  627. {
  628. _conversationChangedCompletion(item);
  629. }
  630. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  631. }
  632. - (void)updateOnConversationChanged:(IMAConversation *)conv
  633. {
  634. IMAConversationChangedNotifyItem *item = [[IMAConversationChangedNotifyItem alloc] initWith:EIMAConversation_ConversationChanged];
  635. item.conversation = conv;
  636. item.index = [_conversationList indexOfObject:conv];
  637. if (_conversationChangedCompletion)
  638. {
  639. _conversationChangedCompletion(item);
  640. }
  641. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  642. }
  643. @end