NIMContactSelectViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // NIMContactSelectViewController.m
  3. // NIMKit
  4. //
  5. // Created by chris on 15/9/14.
  6. // Copyright (c) 2015年 NetEase. All rights reserved.
  7. //
  8. #import "NIMContactSelectViewController.h"
  9. #import "NIMContactSelectTabView.h"
  10. #import "NIMContactPickedView.h"
  11. #import "NIMGroupedUsrInfo.h"
  12. #import "NIMGroupedData.h"
  13. #import "NIMContactDataCell.h"
  14. #import "UIView+NIM.h"
  15. #import "NIMKit.h"
  16. #import "NIMKitDependency.h"
  17. #import "NIMGlobalMacro.h"
  18. @interface NIMContactSelectViewController ()<UITableViewDataSource, UITableViewDelegate, NIMContactPickedViewDelegate>{
  19. NSMutableArray *_selectecContacts;
  20. }
  21. @property (strong, nonatomic) UITableView *tableView;
  22. @property (strong, nonatomic) NIMContactSelectTabView *selectIndicatorView;
  23. @property (nonatomic, assign) NSInteger maxSelectCount;
  24. @property(nonatomic, strong) NSDictionary *contentDic;
  25. @property(nonatomic, strong) NSArray *sectionTitles;
  26. @end
  27. @implementation NIMContactSelectViewController
  28. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  29. {
  30. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  31. if(self) {
  32. _maxSelectCount = NSIntegerMax;
  33. }
  34. return self;
  35. }
  36. - (instancetype)initWithConfig:(id<NIMContactSelectConfig>) config{
  37. self = [self initWithNibName:nil bundle:nil];
  38. if (self) {
  39. self.config = config;
  40. }
  41. return self;
  42. }
  43. - (void)viewDidLoad
  44. {
  45. [super viewDidLoad];
  46. self.view.backgroundColor = NIMKit_UIColorFromRGB(0x383836);
  47. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  48. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  49. if (@available(iOS 15.0, *)) {
  50. self.tableView.sectionHeaderTopPadding = 0;
  51. }
  52. [self.view addSubview:self.tableView];
  53. [self.view addSubview:self.selectIndicatorView];
  54. self.tableView.dataSource = self;
  55. self.tableView.delegate = self;
  56. [self youpaifsetupNav];
  57. self.selectIndicatorView.pickedView.delegate = self;
  58. [self.selectIndicatorView.doneButton addTarget:self action:@selector(onDoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  59. }
  60. - (void)youpaifsetupNav
  61. {
  62. self.navigationItem.title = [self.config respondsToSelector:@selector(title)] ? [self.config title] : @"选择联系人";
  63. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancelBtnClick:)];
  64. if ([self.config respondsToSelector:@selector(showSelectDetail)] && self.config.showSelectDetail) {
  65. UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  66. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:label];
  67. [label setText:self.detailTitle];
  68. [label sizeToFit];
  69. }
  70. }
  71. - (void)refreshDetailTitle
  72. {
  73. UILabel *label = (UILabel *)self.navigationItem.rightBarButtonItem.customView;
  74. [label setText:self.detailTitle];
  75. [label sizeToFit];
  76. }
  77. - (NSString *)detailTitle
  78. {
  79. NSString *detail = @"";
  80. if ([self.config respondsToSelector:@selector(maxSelectedNum)])
  81. {
  82. detail = [NSString stringWithFormat:@"%zd/%zd",_selectecContacts.count,_maxSelectCount];
  83. }
  84. else
  85. {
  86. detail = [NSString stringWithFormat:@"已选%zd人",_selectecContacts.count];
  87. }
  88. return detail;
  89. }
  90. - (void)viewDidLayoutSubviews{
  91. [super viewDidLayoutSubviews];
  92. UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
  93. if (@available(iOS 11.0, *))
  94. {
  95. safeAreaInsets = self.view.safeAreaInsets;
  96. }
  97. self.selectIndicatorView.nim_width = self.view.nim_width;
  98. self.tableView.nim_height = self.view.nim_height - self.selectIndicatorView.nim_height - safeAreaInsets.bottom;
  99. self.selectIndicatorView.nim_bottom = self.view.nim_height - safeAreaInsets.bottom;
  100. }
  101. - (void)show{
  102. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  103. [vc presentViewController:[[UINavigationController alloc] initWithRootViewController:self] animated:YES completion:nil];
  104. }
  105. - (void)setConfig:(id<NIMContactSelectConfig>)config{
  106. _config = config;
  107. if ([config respondsToSelector:@selector(maxSelectedNum)]) {
  108. _maxSelectCount = [config maxSelectedNum];
  109. _contentDic = @{}.mutableCopy;
  110. _sectionTitles = @[].mutableCopy;
  111. }
  112. [self makeData];
  113. }
  114. - (void)onCancelBtnClick:(id)sender {
  115. [self dismissViewControllerAnimated:YES completion:^() {
  116. if (self.cancelBlock) {
  117. self.cancelBlock();
  118. self.cancelBlock = nil;
  119. }
  120. if([_delegate respondsToSelector:@selector(didCancelledSelect)]) {
  121. [_delegate didCancelledSelect];
  122. }
  123. }];
  124. }
  125. - (IBAction)onDoneBtnClick:(id)sender {
  126. [self dismissViewControllerAnimated:YES completion:nil];
  127. if (_selectecContacts.count) {
  128. if ([self.delegate respondsToSelector:@selector(didFinishedSelect:)]) {
  129. [self.delegate didFinishedSelect:_selectecContacts];
  130. }
  131. if (self.finshBlock) {
  132. self.finshBlock(_selectecContacts);
  133. self.finshBlock = nil;
  134. }
  135. }
  136. else {
  137. if([_delegate respondsToSelector:@selector(didCancelledSelect)]) {
  138. [_delegate didCancelledSelect];
  139. }
  140. if (self.cancelBlock) {
  141. self.cancelBlock();
  142. self.cancelBlock = nil;
  143. }
  144. }
  145. }
  146. - (void)makeData{
  147. NIMKit_WEAK_SELF(weakSelf);
  148. [self.config getContactData:^(NSDictionary *contentDic, NSArray *titles) {
  149. self.contentDic = contentDic;
  150. self.sectionTitles = titles;
  151. dispatch_async(dispatch_get_main_queue(), ^{
  152. [weakSelf.tableView reloadData];
  153. });
  154. }];
  155. if ([self.config respondsToSelector:@selector(alreadySelectedMemberId)])
  156. {
  157. _selectecContacts = [[self.config alreadySelectedMemberId] mutableCopy];
  158. }
  159. _selectecContacts = _selectecContacts.count ? _selectecContacts : [NSMutableArray array];
  160. for (NSString *selectId in _selectecContacts) {
  161. NIMKitInfo *info;
  162. info = [self.config getInfoById:selectId];
  163. [self.selectIndicatorView.pickedView addMemberInfo:info];
  164. }
  165. }
  166. #pragma mark - UITableViewDataSource
  167. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  168. {
  169. return self.sectionTitles.count;
  170. }
  171. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  172. {
  173. NSArray *arr = [self.contentDic valueForKey:self.sectionTitles[section]];
  174. return arr.count;
  175. }
  176. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  177. {
  178. if ([self.sectionTitles[0] isEqualToString:@"$"] && section == 0) {
  179. return @"机器人";
  180. }else {
  181. return self.sectionTitles[section];
  182. }
  183. }
  184. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  185. {
  186. NSString *title = self.sectionTitles[indexPath.section];
  187. NSMutableArray *arr = [self.contentDic valueForKey:title];
  188. id<NIMGroupMemberProtocol> contactItem = arr[indexPath.row];
  189. NIMContactDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectContactCellID"];
  190. if (cell == nil) {
  191. cell = [[NIMContactDataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SelectContactCellID"];
  192. }
  193. cell.accessoryBtn.hidden = NO;
  194. cell.accessoryBtn.selected = [_selectecContacts containsObject:[contactItem memberId]];
  195. NIMKitInfo *info = [self.config getInfoById:[contactItem memberId]];
  196. [cell refreshItem:contactItem withMemberInfo:info];
  197. return cell;
  198. }
  199. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  200. return [self.sectionTitles mutableCopy];
  201. }
  202. #pragma mark - UITableViewDelegate
  203. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. return 50;
  206. }
  207. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  208. {
  209. NSString *title = self.sectionTitles[indexPath.section];
  210. NSMutableArray *arr = [self.contentDic valueForKey:title];
  211. id<NIMGroupMemberProtocol> member = arr[indexPath.row];
  212. NSString *memberId = [(id<NIMGroupMemberProtocol>)member memberId];
  213. NIMContactDataCell *cell = (NIMContactDataCell *)[tableView cellForRowAtIndexPath:indexPath];
  214. NIMKitInfo *info;
  215. info = [self.config getInfoById:memberId];
  216. if([_selectecContacts containsObject:memberId]) {
  217. [_selectecContacts removeObject:memberId];
  218. cell.accessoryBtn.selected = NO;
  219. [self.selectIndicatorView.pickedView removeMemberInfo:info];
  220. } else if(_selectecContacts.count >= _maxSelectCount) {
  221. if ([self.config respondsToSelector:@selector(selectedOverFlowTip)]) {
  222. NSString *tip = [self.config selectedOverFlowTip];
  223. [self.view makeToast:tip duration:2.0 position:CSToastPositionCenter];
  224. }
  225. cell.accessoryBtn.selected = NO;
  226. } else {
  227. [_selectecContacts addObject:memberId];
  228. cell.accessoryBtn.selected = YES;
  229. [self.selectIndicatorView.pickedView addMemberInfo:info];
  230. }
  231. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  232. [self refreshDetailTitle];
  233. }
  234. #pragma mark - ContactPickedViewDelegate
  235. - (void)removeUser:(NSString *)userId {
  236. [_selectecContacts removeObject:userId];
  237. [_tableView reloadData];
  238. [self refreshDetailTitle];
  239. }
  240. #pragma mark - Private
  241. - (NIMContactSelectTabView *)selectIndicatorView{
  242. if (_selectIndicatorView) {
  243. return _selectIndicatorView;
  244. }
  245. CGFloat tabHeight = 50.f;
  246. CGFloat tabWidth = 320.f;
  247. _selectIndicatorView = [[NIMContactSelectTabView alloc] initWithFrame:CGRectMake(0, 0, tabWidth, tabHeight)];
  248. return _selectIndicatorView;
  249. }
  250. @end