FriendListViewController.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // FriendListViewController.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/29.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "FriendListViewController.h"
  9. #import "User.h"
  10. #import "FriendListTableViewCell.h"
  11. #import "NTESContactUtilItem.h"
  12. #import "NTESContactDataMember.h"
  13. #import "NTESContactUtilCell.h"
  14. #import "NTESContactDataCell.h"
  15. #import "UIView+Toast.h"
  16. #import "ContactViewController.h"
  17. #import "HttpRequest.h"
  18. @implementation GroupedContacts
  19. - (instancetype)initWithMembers:(NSMutableArray *)members
  20. {
  21. self = [super init];
  22. if(self) {
  23. self.groupTitleComparator = ^NSComparisonResult(NSString *title1, NSString *title2) {
  24. if ([title1 isEqualToString:@"#"]) {
  25. return NSOrderedDescending;
  26. }
  27. if ([title2 isEqualToString:@"#"]) {
  28. return NSOrderedAscending;
  29. }
  30. return [title1 compare:title2];
  31. };
  32. self.groupMemberComparator = ^NSComparisonResult(NSString *key1, NSString *key2) {
  33. return [key1 compare:key2];
  34. };
  35. [self setMembers:members];
  36. }
  37. return self;
  38. }
  39. @end
  40. @interface FriendListViewController ()<NTESContactUtilCellDelegate,
  41. NIMContactDataCellDelegate>
  42. @property (nonatomic, strong) GroupedContacts *contacts;
  43. @property (nonatomic, strong) NSMutableArray *requestArr;
  44. @end
  45. @implementation FriendListViewController
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. __weak typeof(self) wself = self;
  49. [[User sharedInfo] requestFollowList:^{
  50. [[HttpRequest shared] requestFriendList:^(NSMutableArray<FriendRequestData *> * _Nullable dataArr) {
  51. wself.requestArr = dataArr;
  52. [wself fetchUserInfos];
  53. } failure:^{
  54. [wself fetchUserInfos];
  55. }];
  56. }];
  57. }
  58. - (void)fetchUserInfos{
  59. __weak typeof(self) wself = self;
  60. NSArray *followList = [[User sharedInfo] followList];
  61. [[NIMSDK sharedSDK].userManager fetchUserInfos:followList completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
  62. [wself prepareData];
  63. }];
  64. }
  65. - (void)prepareData{
  66. NSMutableArray *infoArr = [NSMutableArray array];
  67. for (NSString *userId in [[User sharedInfo] followList]) {
  68. NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:userId option:nil];
  69. NTESContactDataMember *contact = [[NTESContactDataMember alloc] init];
  70. contact.info = info;
  71. [infoArr addObject:contact];
  72. }
  73. _contacts = [[GroupedContacts alloc] initWithMembers:infoArr];
  74. NSString *contactCellUtilIcon = @"icon";
  75. NSString *contactCellUtilVC = @"vc";
  76. NSString *contactCellUtilStory = @"story";
  77. NSString *contactCellUtilBadge = @"badge";
  78. NSString *contactCellUtilTitle = @"title";
  79. NSString *contactCellUtilUid = @"uid";
  80. NSString *contactCellUtilSelectorName = @"selName";
  81. NSMutableArray *utils =
  82. [@[
  83. @{
  84. contactCellUtilIcon:@"通讯录_03",
  85. contactCellUtilTitle:@"新的朋友",
  86. contactCellUtilStory:@"FriendRequest",
  87. contactCellUtilBadge:@(_requestArr.count)
  88. },
  89. @{
  90. contactCellUtilIcon:@"通讯录_06",
  91. contactCellUtilTitle:@"手机联系人",
  92. contactCellUtilStory:@"Contact"
  93. },
  94. @{
  95. contactCellUtilIcon:@"通讯录_08",
  96. contactCellUtilTitle:@"群组",
  97. contactCellUtilVC:@"NTESAdvancedTeamListViewController"
  98. },
  99. ] mutableCopy];
  100. //构造显示的数据模型
  101. NTESContactUtilItem *contactUtil = [[NTESContactUtilItem alloc] init];
  102. NSMutableArray * members = [[NSMutableArray alloc] init];
  103. for (NSDictionary *item in utils) {
  104. NTESContactUtilMember *utilItem = [[NTESContactUtilMember alloc] init];
  105. utilItem.nick = item[contactCellUtilTitle];
  106. utilItem.icon = [UIImage imageNamed:item[contactCellUtilIcon]];
  107. utilItem.vcName = item[contactCellUtilVC];
  108. utilItem.badge = [item[contactCellUtilBadge] stringValue];
  109. utilItem.userId = item[contactCellUtilUid];
  110. utilItem.selName = item[contactCellUtilSelectorName];
  111. utilItem.storyboardId = item[contactCellUtilStory];
  112. [members addObject:utilItem];
  113. }
  114. contactUtil.members = members;
  115. [_contacts addGroupAboveWithTitle:@"" members:contactUtil.members];
  116. [self.tableView reloadData];
  117. }
  118. - (void)didReceiveMemoryWarning {
  119. [super didReceiveMemoryWarning];
  120. // Dispose of any resources that can be recreated.
  121. }
  122. - (void)viewWillAppear:(BOOL)animated{
  123. [super viewWillAppear:animated];
  124. [self.navigationItem setTitle:@"通讯录"];
  125. }
  126. - (void)viewDidAppear:(BOOL)animated{
  127. [super viewDidAppear:animated];
  128. [self prepareData];
  129. }
  130. #pragma mark - Table view data source
  131. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  132. return [_contacts memberCountOfGroup:section];
  133. }
  134. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  135. return [_contacts groupCount];
  136. }
  137. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. id contactItem = [_contacts memberOfIndex:indexPath];
  139. NSString * cellId = [contactItem reuseId];
  140. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  141. if (!cell) {
  142. Class cellClazz = NSClassFromString([contactItem cellName]);
  143. cell = [[cellClazz alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  144. }
  145. if ([contactItem showAccessoryView]) {
  146. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  147. }else{
  148. cell.accessoryType = UITableViewCellAccessoryNone;
  149. }
  150. if ([cell isKindOfClass:[NTESContactUtilCell class]]) {
  151. [(NTESContactUtilCell *)cell refreshWithContactItem:contactItem];
  152. [(NTESContactUtilCell *)cell setDelegate:self];
  153. }else{
  154. [(NTESContactDataCell *)cell refreshUser:contactItem];
  155. [(NTESContactDataCell *)cell setDelegate:self];
  156. }
  157. return cell;
  158. }
  159. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  160. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  161. id<NTESContactItem> contactItem = (id<NTESContactItem>)[_contacts memberOfIndex:indexPath];
  162. if ([contactItem respondsToSelector:@selector(selName)] && [contactItem selName].length) {
  163. SEL sel = NSSelectorFromString([contactItem selName]);
  164. SuppressPerformSelectorLeakWarning([self performSelector:sel withObject:nil]);
  165. }
  166. else if (contactItem.vcName.length) {
  167. Class clazz = NSClassFromString(contactItem.vcName);
  168. UIViewController * vc = [[clazz alloc] initWithNibName:nil bundle:nil];
  169. [self.navigationController pushViewController:vc animated:YES];
  170. }
  171. else if ([contactItem isKindOfClass:[NTESContactUtilMember class]] && ((NTESContactUtilMember *)contactItem).storyboardId.length > 0){
  172. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  173. UIViewController *vc = [board instantiateViewControllerWithIdentifier:((NTESContactUtilMember *)contactItem).storyboardId];
  174. if([vc isKindOfClass:[FriendRequestViewController class]])
  175. {
  176. [((FriendRequestViewController *)vc) setDataArr:_requestArr];
  177. }
  178. [self.navigationController pushViewController:vc animated:YES];
  179. }
  180. else if([contactItem respondsToSelector:@selector(userId)]){
  181. NSString * friendId = contactItem.userId;
  182. [self onPressAvatar:friendId];
  183. }
  184. }
  185. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  186. id<NTESContactItem> contactItem = (id<NTESContactItem>)[_contacts memberOfIndex:indexPath];
  187. return contactItem.uiHeight;
  188. }
  189. - (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  190. return [_contacts titleOfGroup:section];
  191. }
  192. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  193. return _contacts.sortedGroupTitles;
  194. }
  195. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  196. return index + 1;
  197. }
  198. #pragma mark - NIMContactDataCellDelegate
  199. - (void)onPressAvatar:(NSString *)memberId{
  200. [User showUserInfo:memberId viewController:self];
  201. }
  202. #pragma mark - NTESContactUtilCellDelegate
  203. - (void)onPressUtilImage:(NSString *)content{
  204. [self.view makeToast:[NSString stringWithFormat:@"点我干嘛 我是<%@>",content] duration:2.0 position:CSToastPositionCenter];
  205. }
  206. @end