NIMMemberGroupView.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // NIMMemberGroupView.m
  3. // NIMKit
  4. //
  5. // Created by chris on 15/10/15.
  6. // Copyright © 2015年 NetEase. All rights reserved.
  7. //
  8. #import "NIMMemberGroupView.h"
  9. #import "NIMTeamCardHeaderCell.h"
  10. #import "UIView+NIM.h"
  11. #import "NIMTeamCardOperationItem.h"
  12. #import "NIMCardMemberItem.h"
  13. #define CollectionItemWidth 58
  14. #define CollectionItemHeight 80
  15. #define CollectionMinLeft 20 //防止计算后有左右贴边的情况
  16. #define CollectionEdgeInsetTopFirstLine 25
  17. #define CollectionEdgeInsetTop 15
  18. #define CollectionCellReuseId @"collectionCell"
  19. #pragma mark - NIMMemebrGroupData
  20. @interface NIMMemebrGroupData : NSObject
  21. @property (nonatomic,strong) id data;
  22. @property (nonatomic,assign) NIMKitCardHeaderOpeator operator;
  23. @end
  24. @implementation NIMMemebrGroupData
  25. @end
  26. #pragma mark - NIMMemberGroupView
  27. @interface NIMMemberGroupView()<UICollectionViewDataSource,UICollectionViewDelegate,NIMTeamCardHeaderCellDelegate>
  28. @property (nonatomic,strong) NSMutableArray *uids;
  29. @property (nonatomic,strong) NSMutableArray *data;
  30. @property (nonatomic,strong) NSMutableDictionary *operatorTitle;
  31. @end
  32. @implementation NIMMemberGroupView
  33. - (instancetype)initWithFrame:(CGRect)frame{
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. self.collectionView.delegate = self;
  37. self.collectionView.dataSource = self;
  38. [self addSubview:self.collectionView];
  39. }
  40. return self;
  41. }
  42. - (void)refreshUids:(NSArray *)uids operators:(NIMKitCardHeaderOpeator)operators{
  43. _uids = [uids mutableCopy];
  44. _showAddOperator = (operators & CardHeaderOpeatorAdd) != 0;
  45. _showRemoveOperator = (operators & CardHeaderOpeatorRemove) != 0;
  46. [self makeData];
  47. [self.collectionView reloadData];
  48. }
  49. - (void)setTitle:(NSString *)title forOperator:(NIMKitCardHeaderOpeator)opera{
  50. if (!self.operatorTitle) {
  51. self.operatorTitle = [[NSMutableDictionary alloc] init];
  52. }
  53. self.operatorTitle[@(opera)] = title;
  54. }
  55. - (CGSize)sizeThatFits:(CGSize)size{
  56. CGFloat width = size.width;
  57. NSInteger sectionNumber = [self numberOfSections:width];
  58. CGFloat height = CollectionItemHeight * sectionNumber + CollectionEdgeInsetTop * (sectionNumber-1) + CollectionEdgeInsetTopFirstLine * 2;
  59. return CGSizeMake(width, height);
  60. }
  61. - (void)layoutSubviews{
  62. [super layoutSubviews];
  63. self.collectionView.contentInset = self.sectionInsets;
  64. }
  65. #pragma mark - UICollectionViewDataSource
  66. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  67. NSInteger lastTotal = self.collectionItemNumber * section;
  68. NSInteger remain = self.data.count - lastTotal;
  69. return remain < self.collectionItemNumber ? remain:self.collectionItemNumber;
  70. }
  71. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  72. NSInteger sections = self.data.count / self.collectionItemNumber;
  73. return self.data.count % self.collectionItemNumber ? sections + 1 : sections;
  74. }
  75. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  76. NIMTeamCardHeaderCell *cell;
  77. NIMMemebrGroupData *data = [self dataAtIndexPath:indexPath];
  78. if (data.operator == CardHeaderOpeatorAdd || data.operator == CardHeaderOpeatorRemove) {
  79. cell = [self buildOperatorCell:data.operator indexPath:indexPath];
  80. }else{
  81. cell = [self buildUserCell:data.data indexPath:indexPath];
  82. }
  83. cell.delegate = self;
  84. return cell;
  85. }
  86. - (NIMMemebrGroupData *)dataAtIndexPath:(NSIndexPath*)indexpath{
  87. NSInteger index = indexpath.section * self.collectionItemNumber;
  88. index += indexpath.row;
  89. return self.data[index];
  90. }
  91. #pragma mark - NIMTeamCardHeaderCellDelegate
  92. - (void)cellDidSelected:(NIMTeamCardHeaderCell *)cell{
  93. NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
  94. NIMMemebrGroupData *groupData = [self dataAtIndexPath:indexPath];
  95. if (groupData.operator == CardHeaderOpeatorNone && [self.delegate respondsToSelector:@selector(didSelectMemberId:)]) {
  96. [self.delegate didSelectMemberId:groupData.data];
  97. }else if ([self.delegate respondsToSelector:@selector(didSelectOperator:)]){
  98. [self.delegate didSelectOperator:groupData.operator];
  99. }
  100. }
  101. - (void)cellShouldBeRemoved:(NIMTeamCardHeaderCell*)cell{
  102. NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
  103. NIMMemebrGroupData *groupData = [self dataAtIndexPath:indexPath];
  104. if (groupData.operator == CardHeaderOpeatorNone && [self.delegate respondsToSelector:@selector(didSelectRemoveButtonWithMemberId:)]) {
  105. [self.delegate didSelectRemoveButtonWithMemberId:groupData.data];
  106. }
  107. }
  108. #pragma mark - UICollectionViewDelegateFlowLayout
  109. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  110. return CGSizeMake(CollectionItemWidth, CollectionItemHeight);
  111. }
  112. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  113. if (section == 0) {
  114. return UIEdgeInsetsMake(CollectionEdgeInsetTopFirstLine, 0, 0, 0);
  115. }
  116. return UIEdgeInsetsMake(CollectionEdgeInsetTop, 0, 0, 0);
  117. }
  118. #pragma mark - Getter & Setter
  119. - (UICollectionView *)collectionView{
  120. if (!_collectionView) {
  121. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  122. layout.minimumInteritemSpacing = self.collectionEdgeInsetLeftRight;
  123. _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
  124. _collectionView.backgroundColor = [UIColor colorWithRed:236.0/255.0 green:241.0/255.0 blue:245.0/255.0 alpha:1];
  125. _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  126. _collectionView.delegate = self;
  127. _collectionView.dataSource = self;
  128. [_collectionView registerClass:[NIMTeamCardHeaderCell class] forCellWithReuseIdentifier:CollectionCellReuseId];
  129. }
  130. return _collectionView;
  131. }
  132. - (CGFloat)collectionEdgeInsetLeftRight{
  133. return CollectionMinLeft;
  134. }
  135. #pragma mark - Private
  136. - (void)makeData{
  137. self.data = [[NSMutableArray alloc] init];
  138. for (NSString *uid in self.uids) {
  139. NIMMemebrGroupData *groupData = [[NIMMemebrGroupData alloc] init];
  140. groupData.operator = CardHeaderOpeatorNone;
  141. groupData.data = uid;
  142. [self.data addObject:groupData];
  143. }
  144. if (self.showAddOperator) {
  145. NIMMemebrGroupData *groupData = [[NIMMemebrGroupData alloc] init];
  146. groupData.operator = CardHeaderOpeatorAdd;
  147. [self.data addObject:groupData];
  148. }
  149. if (self.showRemoveOperator) {
  150. NIMMemebrGroupData *groupData = [[NIMMemebrGroupData alloc] init];
  151. groupData.operator = CardHeaderOpeatorRemove;
  152. [self.data addObject:groupData];
  153. }
  154. }
  155. - (NIMTeamCardHeaderCell *)buildUserCell:(NSString *)uid indexPath:(NSIndexPath *)indexPath{
  156. NIMTeamCardHeaderCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellReuseId forIndexPath:indexPath];
  157. NIMMemebrGroupData *data = [self dataAtIndexPath:indexPath];
  158. NIMUserCardMemberItem *item = [[NIMUserCardMemberItem alloc] initWithUserId:data.data];
  159. [cell refreshData:item];
  160. cell.removeBtn.hidden = !self.enableRemove;
  161. return cell;
  162. }
  163. - (NIMTeamCardHeaderCell *)buildOperatorCell:(NIMKitCardHeaderOpeator)operator indexPath:(NSIndexPath *)indexPath{
  164. NIMTeamCardHeaderCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellReuseId forIndexPath:indexPath];
  165. NIMTeamCardOperationItem *item = [[NIMTeamCardOperationItem alloc] initWithOperation:operator];
  166. if (self.operatorTitle[@(operator)]) {
  167. item.title = self.operatorTitle[@(operator)];
  168. }
  169. [cell refreshData:item];
  170. cell.removeBtn.hidden = YES;
  171. return cell;
  172. }
  173. - (UIEdgeInsets)sectionInsets {
  174. CGFloat left = (self.collectionView.nim_width - ((CollectionItemWidth + self.collectionEdgeInsetLeftRight)) * self.collectionItemNumber - self.collectionEdgeInsetLeftRight) * 0.5;
  175. left = left > CollectionMinLeft ? left : CollectionMinLeft;
  176. return UIEdgeInsetsMake(self.collectionView.contentInset.top, left, self.collectionView.contentInset.bottom, left);
  177. }
  178. - (NSInteger)collectionItemNumber{
  179. return [self collectionItemNumber:self.collectionView.nim_width];
  180. }
  181. - (NSInteger)collectionItemNumber:(CGFloat)width{
  182. CGFloat minSpace = CollectionMinLeft; //防止计算到最后出现左右贴边的情况
  183. return (int)((width - minSpace)/ (CollectionItemWidth + self.collectionEdgeInsetLeftRight));
  184. }
  185. - (NSInteger)numberOfSections:(CGFloat)width{
  186. NSInteger collectionNumber = [self collectionItemNumber:width];
  187. NSInteger sections = self.data.count / collectionNumber;
  188. return self.data.count % collectionNumber ? sections + 1 : sections;
  189. }
  190. @end