EMConversationHelper.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // EMConversationHelper.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/14.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMConversationHelper.h"
  9. #import "EMMulticastDelegate.h"
  10. @implementation EMConversationModel
  11. - (instancetype)initWithEMModel:(EMConversation *)aModel
  12. {
  13. self = [super init];
  14. if (self) {
  15. _emModel = aModel;
  16. _name = aModel.conversationId;
  17. }
  18. return self;
  19. }
  20. @end
  21. static EMConversationHelper *shared = nil;
  22. @interface EMConversationHelper()
  23. @property (nonatomic, strong) EMMulticastDelegate<EMConversationsDelegate> *delegates;
  24. @end
  25. @implementation EMConversationHelper
  26. - (instancetype)init
  27. {
  28. self = [super init];
  29. if (self) {
  30. _delegates = (EMMulticastDelegate<EMConversationsDelegate> *)[[EMMulticastDelegate alloc] init];
  31. }
  32. return self;
  33. }
  34. + (instancetype)shared
  35. {
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. shared = [[EMConversationHelper alloc] init];
  39. });
  40. return shared;
  41. }
  42. - (void)dealloc
  43. {
  44. [self.delegates removeAllDelegates];
  45. }
  46. #pragma mark - Delegate
  47. - (void)addDelegate:(id<EMConversationsDelegate>)aDelegate
  48. {
  49. [self.delegates addDelegate:aDelegate delegateQueue:dispatch_get_main_queue()];
  50. }
  51. - (void)removeDelegate:(id<EMConversationsDelegate>)aDelegate
  52. {
  53. [self.delegates removeDelegate:aDelegate];
  54. }
  55. #pragma mark - Class Methods
  56. + (NSArray<EMConversationModel *> *)modelsFromEMConversations:(NSArray<EMConversation *> *)aConversations
  57. {
  58. NSMutableArray *retArray = [[NSMutableArray alloc] init];
  59. NSArray *groupArray = [[EMClient sharedClient].groupManager getJoinedGroups];
  60. for (int i = 0; i < [aConversations count]; i++) {
  61. EMConversation *conversation = aConversations[i];
  62. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  63. if (conversation.type == EMConversationTypeGroupChat || conversation.type == EMConversationTypeChatRoom) {
  64. NSString *name = [conversation.ext objectForKey:@"subject"];
  65. if ([name length] == 0 && conversation.type == EMConversationTypeGroupChat) {
  66. for (EMGroup *group in groupArray) {
  67. if ([group.groupId isEqualToString:conversation.conversationId]) {
  68. NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:conversation.ext];
  69. [ext setObject:group.subject forKey:@"subject"];
  70. [ext setObject:[NSNumber numberWithBool:group.isPublic] forKey:@"isPublic"];
  71. conversation.ext = ext;
  72. name = group.subject;
  73. break;
  74. }
  75. }
  76. }
  77. model.name = name;
  78. }
  79. [retArray addObject:model];
  80. }
  81. return retArray;
  82. }
  83. + (EMConversationModel *)modelFromContact:(NSString *)aContact
  84. {
  85. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:aContact type:EMConversationTypeChat createIfNotExist:YES];
  86. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  87. return model;
  88. }
  89. + (EMConversationModel *)modelFromGroup:(EMGroup *)aGroup
  90. {
  91. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:aGroup.groupId type:EMConversationTypeGroupChat createIfNotExist:YES];
  92. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  93. model.name = aGroup.subject;
  94. NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:conversation.ext];
  95. [ext setObject:aGroup.subject forKey:@"subject"];
  96. [ext setObject:[NSNumber numberWithBool:aGroup.isPublic] forKey:@"isPublic"];
  97. conversation.ext = ext;
  98. return model;
  99. }
  100. + (EMConversationModel *)modelFromChatroom:(EMChatroom *)aChatroom
  101. {
  102. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:aChatroom.chatroomId type:EMConversationTypeChatRoom createIfNotExist:YES];
  103. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  104. model.name = aChatroom.subject;
  105. NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:conversation.ext];
  106. [ext setObject:aChatroom.subject forKey:@"subject"];
  107. conversation.ext = ext;
  108. return model;
  109. }
  110. + (void)markAllAsRead:(EMConversationModel *)aConversationModel
  111. {
  112. [aConversationModel.emModel markAllMessagesAsRead:nil];
  113. EMConversationHelper *helper = [EMConversationHelper shared];
  114. [helper.delegates didConversationUnreadCountToZero:aConversationModel];
  115. }
  116. + (void)resortConversationsLatestMessage
  117. {
  118. EMConversationHelper *helper = [EMConversationHelper shared];
  119. [helper.delegates didResortConversationsLatestMessage];
  120. }
  121. @end