IMAContactManager+SubGroup.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // IMAContactManager+SubGroup.m
  3. // TIMChat
  4. //
  5. // Created by AlexiChen on 16/3/2.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMAContactManager+SubGroup.h"
  9. @implementation IMAContactManager (SubGroup)
  10. #define kSubGroupConfig @"kSubGroupConfig"
  11. //static NSString *const kCurrentSubGroupListTime = @"kCurrentSubGroupListTime";
  12. //
  13. //- (NSInteger)currentSubGroupListTime
  14. //{
  15. //// [NSUserDefaults standardUserDefaults]
  16. //}
  17. //
  18. //- (void)setCurrentSubGroupListTime
  19. //{
  20. // objc_setAssociatedObject(self, (__bridge const void *)kPresentedTransitionKey, kPresentingTransitionKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  21. //}
  22. - (void)createSubGroupList:(NSArray *)array
  23. {
  24. NSString * log1 = [NSString stringWithFormat:@"array = %ld,fun = %s",(long)array.count, __func__];
  25. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
  26. if (!_subGroupList)
  27. {
  28. _subGroupList = [[CLSafeMutableArray alloc] init];
  29. }
  30. else
  31. {
  32. [_subGroupList removeAllObjects];
  33. }
  34. for (TIMFriendGroupWithProfiles *group in array)
  35. {
  36. IMASubGroup *sg = [[IMASubGroup alloc] initWith:group];
  37. [_subGroupList addObject:sg];
  38. }
  39. // 从本地加载分组展开状态
  40. [self syncSubGroupStateInLocal];
  41. //刷新一下所有分组
  42. if (self.contactChangedCompletion)
  43. {
  44. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_SubGroupReloadAll];
  45. self.contactChangedCompletion(item);
  46. }
  47. // 更新会话列表中的内容
  48. [[IMAPlatform sharedInstance].conversationMgr updateOnAsyncLoadContactComplete];
  49. NSString * log2 = [NSString stringWithFormat:@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  50. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log2];
  51. }
  52. - (void)syncSubGroupStateInLocal
  53. {
  54. // 用帐号作存储名
  55. NSString *subGroupConfig = [self contactStateSaveKey];
  56. NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:subGroupConfig];
  57. if (data)
  58. {
  59. NSArray *ja = [data objectFromJSONData];
  60. NSMutableArray *array = [NSObject loadItem:[IMASubGroup class] fromArrayDictionary:ja];
  61. for (IMASubGroup *sg in array)
  62. {
  63. NSInteger idx = [_subGroupList indexOfObject:sg];
  64. if (idx >= 0 && idx < _subGroupList.count)
  65. {
  66. IMASubGroup *sgr = [_subGroupList objectAtIndex:idx];
  67. [sgr syncFoldOf:sg];
  68. }
  69. }
  70. NSLog(@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__);
  71. }
  72. }
  73. - (NSString *)contactStateSaveKey
  74. {
  75. return [NSString stringWithFormat:@"%@_SubGroupState", [[IMAPlatform sharedInstance].host userId]];
  76. }
  77. - (void)saveSubGroupStateInfoToLocal
  78. {
  79. NSInteger count = _subGroupList.count;
  80. NSMutableArray *array = [NSMutableArray array];
  81. for (NSInteger i = 0; i < count; i++)
  82. {
  83. IMASubGroup *gr = [_subGroupList objectAtIndex:i];
  84. NSDictionary *dic = @{@"name" : gr.name, @"isFold" : @(gr.isFold)};
  85. [array addObject:dic];
  86. }
  87. if ([NSJSONSerialization isValidJSONObject:array])
  88. {
  89. NSError *error = nil;
  90. NSData *data = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
  91. if(error)
  92. {
  93. DebugLog(@"[%@] save Json Error: %@", [self class], array);
  94. }
  95. else
  96. {
  97. [[NSUserDefaults standardUserDefaults] setObject:data forKey:[self contactStateSaveKey]];
  98. [[NSUserDefaults standardUserDefaults] synchronize];
  99. }
  100. }
  101. else
  102. {
  103. DebugLog(@"[%@] Post Json is not valid: %@", [self class], array);
  104. }
  105. }
  106. - (IMASubGroup *)defaultAddToSubGroup
  107. {
  108. // 返回我的好友分组
  109. IMASubGroup *temp = [[IMASubGroup alloc] initWithName:nil];
  110. NSInteger idx = [_subGroupList indexOfObject:temp];
  111. if (idx >= 0 && idx < _subGroupList.count)
  112. {
  113. return [_subGroupList objectAtIndex:idx];
  114. }
  115. return nil;
  116. }
  117. - (IMASubGroup *)subGroupOf:(IMAUser *)user
  118. {
  119. for (NSInteger i = 0; i < _subGroupList.count; i++)
  120. {
  121. IMASubGroup *sg = [_subGroupList objectAtIndex:i];
  122. NSInteger index = [sg.friends indexOfObject:user];
  123. if (index >= 0 && index < sg.friends.count)
  124. {
  125. return sg;
  126. }
  127. }
  128. return nil;
  129. }
  130. // 异步请求分组列表
  131. - (void)asyncSubGroupList
  132. {
  133. // 获取分组列表
  134. NSArray *list = [[TIMFriendshipManager sharedInstance] getFriendGroup:nil];
  135. NSString * log1 = [NSString stringWithFormat:@"list.count = %ld,fun = %s",(long)list.count, __func__];
  136. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
  137. [self createSubGroupList:list];
  138. NSString * log2 = [NSString stringWithFormat:@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  139. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log2];
  140. }
  141. - (BOOL)isValidSubGroupName:(NSString *)sbName
  142. {
  143. for (NSInteger i = 0; i < _subGroupList.count; i++)
  144. {
  145. IMASubGroup *sg = [_subGroupList objectAtIndex:i];
  146. if ([sg.name isEqualToString:sbName])
  147. {
  148. return NO;
  149. }
  150. }
  151. return YES;
  152. }
  153. - (void)asyncCreateSubGroup:(NSString *)sgName succ:(IMASubGroupCompletion)succ fail:(TIMFail)fail
  154. {
  155. if ([NSString isEmpty:sgName])
  156. {
  157. DebugLog(@"参数sgName不能为空");
  158. return;
  159. }
  160. __weak IMAContactManager *ws = self;
  161. [[TIMFriendshipManager sharedInstance] createFriendGroup:@[sgName] users:nil succ:^(NSArray *array) {
  162. IMASubGroup *sub = [[IMASubGroup alloc] initWithName:sgName];
  163. [ws onAddNewSubGroup:sub];
  164. if (succ)
  165. {
  166. succ(sub);
  167. }
  168. } fail:^(int code, NSString *msg) {
  169. DebugLog(@"Fail:-->code=%d,msg=%@,fun=%s", code, msg,__func__);
  170. [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, err)];
  171. if (fail)
  172. {
  173. fail(code,msg);
  174. }
  175. }];
  176. }
  177. - (void)asyncDeleteSubGroup:(IMASubGroup *)sg succ:(TIMSucc)succ fail:(TIMFail)fail
  178. {
  179. if (!sg)
  180. {
  181. DebugLog(@"分组不能为空");
  182. return;
  183. }
  184. if ([NSString isEmpty:sg.name]) {
  185. DebugLog(@"不能删除默认分组");
  186. return;
  187. }
  188. NSArray *groups = @[sg.name];
  189. __weak IMAContactManager *ws = self;
  190. [[TIMFriendshipManager sharedInstance] deleteFriendGroup:groups succ:^{
  191. [ws onDeleteSubGroup:sg];
  192. if (succ)
  193. {
  194. succ();
  195. }
  196. } fail:fail];
  197. }
  198. @end