GroupMemberViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // GroupMemberViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/24.
  6. //
  7. #import "GroupMemberViewController.h"
  8. #import <SCIndexView.h>
  9. #import <UITableView+SCIndexView.h>
  10. #import "GroupMemberListCell.h"
  11. #import "GroupMemberModel.h"
  12. #import "KSChatConversationViewController.h"
  13. #import "KSUserDetailViewController.h"
  14. #import "UserInfoManager.h"
  15. @interface GroupMemberViewController ()<UITableViewDelegate,UITableViewDataSource>
  16. @property (nonatomic, strong) UITableView *tableView;
  17. @property (nonatomic, strong) NSMutableArray *sourceIndexArray;
  18. @property (nonatomic, strong) NSMutableArray *sourceArray;
  19. @end
  20. @implementation GroupMemberViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. [self allocTitle:@"群成员"];
  25. [self configUI];
  26. [self requestData];
  27. }
  28. - (void)resetSource {
  29. [self setPromptString:@"暂无内容" imageName:@"wd_img_zwsj" inView:self.tableView];
  30. [self.sourceArray removeAllObjects];
  31. [self.dataArray removeAllObjects];
  32. self.sourceIndexArray = [NSMutableArray array];
  33. self.tableView.sc_indexViewDataSource = self.sourceIndexArray;
  34. [self.tableView reloadData];
  35. }
  36. - (void)requestData {
  37. [self.sourceArray removeAllObjects];
  38. [KSNetworkingManager imGroupMemberAllRequest:KS_POST groupId:self.groupId success:^(NSDictionary * _Nonnull dic) {
  39. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  40. NSArray *sourceArray = [dic ks_arrayValueForKey:@"data"];
  41. for (NSDictionary *parm in sourceArray) {
  42. if ([parm isKindOfClass:[NSNull class]]) {
  43. continue;
  44. }
  45. GroupMemberModel *model = [[GroupMemberModel alloc] initWithDictionary:parm];
  46. [self.sourceArray addObject:model];
  47. }
  48. [self evaluateMessge];
  49. }
  50. else {
  51. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  52. }
  53. } faliure:^(NSError * _Nonnull error) {
  54. }];
  55. }
  56. - (void)evaluateMessge {
  57. // 异步线程处理数据
  58. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  59. NSMutableArray *sortArr = [NSMutableArray array];
  60. for (GroupMemberModel *model in self.sourceArray) {
  61. NSString *firstLetter = [NSString getFirstLetterFromString:model.nickname];
  62. model.firstLetter = firstLetter;
  63. if (![sortArr containsObject:firstLetter]) {
  64. [sortArr addObject:firstLetter];
  65. }
  66. }
  67. if (sortArr) {
  68. // 排序
  69. [sortArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  70. return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
  71. }];
  72. }
  73. // 将#移动到最后
  74. NSMutableArray *tempArray = [NSMutableArray array];
  75. for (NSString *letterStr in sortArr) {
  76. if ([letterStr isEqualToString:@"#"]) {
  77. [tempArray addObject:@"#"];
  78. }
  79. }
  80. // 删除和添加操作在遍历完成后进行
  81. if (tempArray.count) {
  82. [sortArr removeObjectsInArray:tempArray];
  83. [sortArr addObjectsFromArray:tempArray];
  84. }
  85. for (NSString *sortStr in sortArr) {
  86. NSMutableArray *filterArray = [NSMutableArray array];
  87. for (GroupMemberModel *subModel in self.sourceArray) {
  88. if ([sortStr isEqualToString:subModel.firstLetter]) {
  89. [filterArray addObject:subModel];
  90. }
  91. }
  92. [self.dataArray addObject:filterArray];
  93. }
  94. self.sourceIndexArray = sortArr;
  95. // 主线程刷新
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. self.tableView.sc_indexViewDataSource = self.sourceIndexArray;
  98. [self.tableView reloadData];
  99. [self changePromptLabelState];
  100. });
  101. });
  102. }
  103. - (void)configUI {
  104. [self.scrollView removeFromSuperview];
  105. [self.view addSubview:self.tableView];
  106. SCIndexViewConfiguration *indexConfiguration = [SCIndexViewConfiguration configuration];
  107. indexConfiguration.indexItemSelectedBackgroundColor = THEMECOLOR;
  108. indexConfiguration.indicatorBackgroundColor = THEMECOLOR;
  109. self.tableView.sc_indexViewConfiguration = indexConfiguration;
  110. self.tableView.sc_translucentForTableViewInNavigationBar = YES;
  111. }
  112. #pragma mark -- table data source
  113. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  114. if (self.dataArray.count > section) {
  115. NSArray *filterArray = self.dataArray[section];
  116. return filterArray.count;
  117. }
  118. return 0;
  119. }
  120. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  121. return self.sourceIndexArray.count;
  122. }
  123. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  124. GroupMemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupMemberListCell"];
  125. NSArray *filterArray = self.dataArray[indexPath.section];
  126. GroupMemberModel *model = filterArray[indexPath.row];
  127. MJWeakSelf;
  128. [cell configWithSource:model callback:^(NSString * _Nonnull targetId, NSString * _Nonnull name, BOOL showDetail) {
  129. if (showDetail) {
  130. [weakSelf showUserDetail:targetId];
  131. }
  132. else {
  133. [weakSelf chatUser:targetId];
  134. }
  135. }];
  136. return cell;
  137. }
  138. - (void)showUserDetail:(NSString *)targetId {
  139. if (USER_MANAGER.userInfo.customerService) {
  140. KSUserDetailViewController *ctrl = [[KSUserDetailViewController alloc] init];
  141. ctrl.imUserId = targetId;
  142. ctrl.fromSingleChat = NO;
  143. [self.navigationController pushViewController:ctrl animated:YES];
  144. }
  145. }
  146. - (void)chatUser:(NSString *)targetId {
  147. if ([NSString isEmptyString:targetId]) {
  148. return;
  149. }
  150. TUIChatConversationModel *model = [[TUIChatConversationModel alloc] init];
  151. model.userID = targetId;
  152. KSChatConversationViewController *ctrl = [[KSChatConversationViewController alloc] init];
  153. ctrl.conversation = model;
  154. [self.navigationController pushViewController:ctrl animated:YES];
  155. }
  156. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  157. }
  158. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  159. return 30;
  160. }
  161. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  162. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
  163. view.backgroundColor = [UIColor clearColor];
  164. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, kScreenWidth - 48, 30)];
  165. label.textColor = HexRGB(0x999999);
  166. label.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
  167. [view addSubview:label];
  168. label.textAlignment = NSTextAlignmentLeft;
  169. NSString *titleStr = self.sourceIndexArray[section];
  170. label.text = titleStr;
  171. return view;
  172. }
  173. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  174. return CGFLOAT_MIN;
  175. }
  176. #pragma mark ---- lazying
  177. - (UITableView *)tableView {
  178. if (!_tableView) {
  179. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin) style:UITableViewStylePlain];
  180. _tableView.delegate = self;
  181. _tableView.dataSource = self;
  182. _tableView.rowHeight = 80;
  183. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  184. _tableView.showsVerticalScrollIndicator = NO;
  185. _tableView.showsHorizontalScrollIndicator = NO;
  186. _tableView.backgroundColor = [UIColor clearColor];
  187. [_tableView registerNib:[UINib nibWithNibName:@"GroupMemberListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GroupMemberListCell"];
  188. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  189. bottomView.backgroundColor = [UIColor clearColor];
  190. _tableView.tableFooterView = bottomView;
  191. }
  192. return _tableView;
  193. }
  194. - (NSMutableArray *)sourceIndexArray {
  195. if (!_sourceIndexArray) {
  196. _sourceIndexArray = [NSMutableArray array];
  197. }
  198. return _sourceIndexArray;
  199. }
  200. - (NSMutableArray *)sourceArray {
  201. if (!_sourceArray) {
  202. _sourceArray = [NSMutableArray array];
  203. }
  204. return _sourceArray;
  205. }
  206. /*
  207. #pragma mark - Navigation
  208. // In a storyboard-based application, you will often want to do a little preparation before navigation
  209. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  210. // Get the new view controller using [segue destinationViewController].
  211. // Pass the selected object to the new view controller.
  212. }
  213. */
  214. @end