ShareChooseMainView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. //
  2. // ShareChooseMainView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/6/9.
  6. //
  7. #import "ShareChooseMainView.h"
  8. #import <SCIndexView.h>
  9. #import <UITableView+SCIndexView.h>
  10. #import "GroupListModel.h"
  11. #import "GroupListViewCell.h"
  12. #import "ContractListCell.h"
  13. #import "StateView.h"
  14. #import "Reachability.h"
  15. #import "FriendListModel.h"
  16. #import "KSChatListSearchView.h"
  17. @interface ShareChooseMainView ()<UITableViewDelegate,UITableViewDataSource>
  18. @property (nonatomic, strong) StateView *promptView;
  19. @property (nonatomic, strong) UIView *promptPlaceView;
  20. @property (nonatomic, assign) BOOL networkAvaiable; // 网络是否可用
  21. @property (nonatomic, strong) NSMutableArray *classArray; // 班级数据
  22. @property (nonatomic, strong) NSMutableArray *studentArray; // 学生数据
  23. @property (nonatomic, strong) NSMutableArray *sourceIndexArray;
  24. @property (nonatomic, strong) NSMutableArray *sourceArray;
  25. @property (nonatomic, copy) ShareChooseCallback callback;
  26. @property (nonatomic, strong) KSChatListSearchView *searchView;
  27. @end
  28. @implementation ShareChooseMainView
  29. - (void)shareCallback:(ShareChooseCallback)callback {
  30. if (callback) {
  31. self.callback = callback;
  32. }
  33. }
  34. - (instancetype)initWithFrame:(CGRect)frame {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. self.backgroundColor = HexRGB(0xf8f9fc);
  38. self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  39. self.tableView.backgroundColor = HexRGB(0xf8f9fc);
  40. self.tableView.showsVerticalScrollIndicator = NO;
  41. self.tableView.dataSource = self;
  42. self.tableView.delegate = self;
  43. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  44. self.tableView.rowHeight = 70;
  45. [self addSubview:self.tableView];
  46. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.right.top.bottom.mas_equalTo(self);
  48. }];
  49. UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KPortraitWidth, 10)];
  50. headView.backgroundColor = HexRGB(0xf8f9fc);
  51. self.tableView.tableHeaderView = headView;
  52. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KPortraitWidth, 10)];
  53. bottomView.backgroundColor = HexRGB(0xf8f9fc);
  54. self.tableView.tableFooterView = bottomView;
  55. [self.tableView registerNib:[UINib nibWithNibName:@"ContractListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ContractListCell"];
  56. [self.tableView registerNib:[UINib nibWithNibName:@"GroupListViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GroupListViewCell"];
  57. MJWeakSelf;
  58. self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  59. [weakSelf resetParamenter];
  60. [weakSelf requestData];
  61. }];
  62. }
  63. return self;
  64. }
  65. - (void)endRefresh {
  66. @weakObj(self);
  67. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  68. @strongObj(self);
  69. [self.tableView.mj_header endRefreshing];
  70. });
  71. }
  72. - (void)refreshAndRequestData {
  73. [self resetParamenter];
  74. [self requestData];
  75. }
  76. - (void)requestData {
  77. if (self.selectIndex == 1) {
  78. [LOADING_MANAGER MBShowInWindow:@"数据加载中..."];
  79. [KSNetworkingManager imUserFriendRequest:KS_POST search:self.searchKey success:^(NSDictionary * _Nonnull dic) {
  80. [self endRefresh];
  81. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  82. NSArray *array = [dic ks_arrayValueForKey:@"data"];
  83. for (NSDictionary *parm in array) {
  84. if ([parm isKindOfClass:[NSNull class]]) {
  85. continue;
  86. }
  87. FriendListModel *model = [[FriendListModel alloc] initWithDictionary:parm];
  88. [self.sourceArray addObject:model];
  89. }
  90. [self evaluateMessge];
  91. }
  92. else {
  93. [LOADING_MANAGER removeCustomLoading];
  94. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  95. [self changePromptLabelStateWithArray:self.studentArray];
  96. }
  97. } faliure:^(NSError * _Nonnull error) {
  98. [LOADING_MANAGER removeCustomLoading];
  99. [self endRefresh];
  100. if (self.networkAvaiable == NO) {
  101. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  102. }
  103. [self.studentArray removeAllObjects];
  104. [self.sourceIndexArray removeAllObjects];
  105. [self.tableView reloadData];
  106. [self changePromptLabelStateWithArray:self.studentArray];
  107. }];
  108. }
  109. else {
  110. [KSNetworkingManager imGroupQueryPage:KS_POST search:self.searchKey success:^(NSDictionary * _Nonnull dic) {
  111. [self endRefresh];
  112. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  113. NSArray *array = [dic ks_arrayValueForKey:@"data"];
  114. for (NSDictionary *parm in array) {
  115. GroupListModel *model = [[GroupListModel alloc] initWithDictionary:parm];
  116. [self.classArray addObject:model];
  117. }
  118. }
  119. else {
  120. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  121. }
  122. [self.tableView reloadData];
  123. [self changePromptLabelStateWithArray:self.classArray];
  124. } faliure:^(NSError * _Nonnull error) {
  125. [self endRefresh];
  126. if (self.networkAvaiable == NO) {
  127. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  128. }
  129. [self.classArray removeAllObjects];
  130. [self.tableView reloadData];
  131. [self changePromptLabelStateWithArray:self.classArray];
  132. }];
  133. }
  134. }
  135. - (void)evaluateMessge {
  136. @autoreleasepool {
  137. NSMutableArray *sourceArray = [self.sourceArray mutableCopy];
  138. // 异步线程处理数据
  139. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  140. NSMutableArray *sortArr = [NSMutableArray array];
  141. for (FriendListModel *model in sourceArray) {
  142. if ([NSString isEmptyString:model.friendNickname]) {
  143. model.friendNickname = [NSString stringWithFormat:@"游客%@",model.friendId];
  144. }
  145. NSString *firstLetter = [NSString getFirstLetterFromString:[NSString returnNoNullStringWithString:model.friendNickname]];
  146. model.firstLetter = firstLetter;
  147. if (![sortArr containsObject:firstLetter]) {
  148. [sortArr addObject:firstLetter];
  149. }
  150. }
  151. if (sortArr) {
  152. // 排序
  153. [sortArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  154. return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
  155. }];
  156. }
  157. // 将#移动到最后
  158. NSMutableArray *tempArray = [NSMutableArray array];
  159. for (NSString *letterStr in sortArr) {
  160. if ([letterStr isEqualToString:@"#"]) {
  161. [tempArray addObject:@"#"];
  162. }
  163. }
  164. // 删除和添加操作在遍历完成后进行
  165. if (tempArray.count) {
  166. [sortArr removeObjectsInArray:tempArray];
  167. [sortArr addObjectsFromArray:tempArray];
  168. }
  169. for (NSString *sortStr in sortArr) {
  170. NSMutableArray *filterArray = [NSMutableArray array];
  171. for (FriendListModel *subModel in self.sourceArray) {
  172. if ([sortStr isEqualToString:subModel.firstLetter]) {
  173. [filterArray addObject:subModel];
  174. }
  175. }
  176. [self.studentArray addObject:filterArray];
  177. }
  178. self.sourceIndexArray = sortArr;
  179. // 主线程刷新
  180. dispatch_async(dispatch_get_main_queue(), ^{
  181. [LOADING_MANAGER removeCustomLoading];
  182. self.tableView.sc_indexViewDataSource = self.sourceIndexArray;
  183. [self.tableView reloadData];
  184. [self changePromptLabelStateWithArray:self.studentArray];
  185. });
  186. });
  187. };
  188. }
  189. - (void)resetParamenter {
  190. [self setPromptString:@"暂无内容" imageName:@"wd_img_zwsj" inView:self.tableView];
  191. [self.classArray removeAllObjects];
  192. [self.sourceArray removeAllObjects];
  193. [self.studentArray removeAllObjects];
  194. self.sourceIndexArray = [NSMutableArray array];
  195. SCIndexViewConfiguration *indexConfiguration = [SCIndexViewConfiguration configuration];
  196. indexConfiguration.indexItemSelectedBackgroundColor = THEMECOLOR;
  197. indexConfiguration.indicatorBackgroundColor = THEMECOLOR;
  198. self.tableView.sc_indexViewConfiguration = indexConfiguration;
  199. self.tableView.sc_indexViewDataSource = self.sourceIndexArray;
  200. [self.tableView reloadData];
  201. }
  202. - (void)beginRefreshImmediately {
  203. [self.tableView.mj_header beginRefreshing];
  204. }
  205. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  206. if (self.lastSelectedIndexPath == indexPath) {
  207. return;
  208. }
  209. if (self.lastSelectedIndexPath != nil) {
  210. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.lastSelectedIndexPath];
  211. [cell setSelected:NO animated:NO];
  212. }
  213. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  214. [cell setSelected:YES animated:NO];
  215. self.lastSelectedIndexPath = indexPath;
  216. }
  217. - (void)layoutSubviews {
  218. [super layoutSubviews];
  219. CGFloat height = [self.searchView getViewHeight];
  220. if (![self.subviews containsObject:self.searchView]) {
  221. [self addSubview:self.searchView];
  222. self.searchView.frame = CGRectMake(0, 0, self.bounds.size.width, height);
  223. }
  224. if (self.selectIndex == 0) {
  225. [self.searchView configText:@"请输入群聊名称"];
  226. }
  227. else {
  228. [self.searchView configText:@"请输入联系人名称"];
  229. }
  230. [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
  231. make.top.mas_equalTo(self).mas_offset(height);
  232. }];
  233. }
  234. - (void)beginFirstRefresh {
  235. if (!self.isHeaderRefreshed) {
  236. [self beginRefreshImmediately];
  237. }
  238. }
  239. #pragma mark - UITableViewDataSource, UITableViewDelegate
  240. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  241. if (self.selectIndex == 1) {
  242. if (self.studentArray.count > section) {
  243. NSArray *filterArray = self.studentArray[section];
  244. return filterArray.count;
  245. }
  246. return 0;
  247. }
  248. else {
  249. return self.classArray.count;
  250. }
  251. }
  252. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  253. if (self.selectIndex == 1) {
  254. return self.sourceIndexArray.count;
  255. }
  256. else {
  257. return 1;
  258. }
  259. }
  260. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  261. if (self.selectIndex == 1) {
  262. ContractListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContractListCell"];
  263. NSArray *filterArray = self.studentArray[indexPath.section];
  264. FriendListModel *model = filterArray[indexPath.row];
  265. [cell configWithSource:model callback:^(NSString *ImUserId) {
  266. }];
  267. return cell;
  268. }
  269. else {
  270. GroupListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupListViewCell"];
  271. GroupListModel *model = self.classArray[indexPath.row];
  272. [cell configWithSource:model];
  273. return cell;
  274. }
  275. }
  276. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  277. if (self.selectIndex == 1) { // 联系人
  278. NSArray *filterArray = self.studentArray[indexPath.section];
  279. FriendListModel *model = filterArray[indexPath.row];
  280. if (self.callback) {
  281. self.callback(model.imFriendId, NO);
  282. }
  283. }
  284. else { // 群聊
  285. GroupListModel *model = self.classArray[indexPath.row];
  286. self.callback(model.groupId, YES);
  287. }
  288. }
  289. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  290. if (self.selectIndex == 0) {
  291. return CGFLOAT_MIN;
  292. }
  293. return 30;
  294. }
  295. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  296. if (self.selectIndex == 0) {
  297. return [UIView new];
  298. }
  299. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KPortraitWidth, 30)];
  300. view.backgroundColor = HexRGB(0xf8f9fc);
  301. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, KPortraitWidth - 48, 30)];
  302. label.textColor = HexRGB(0x999999);
  303. label.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
  304. [view addSubview:label];
  305. label.textAlignment = NSTextAlignmentLeft;
  306. NSString *titleStr = self.sourceIndexArray[section];
  307. label.text = titleStr;
  308. return view;
  309. }
  310. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  311. return CGFLOAT_MIN;
  312. }
  313. /**
  314. 设置没有数据时的显示
  315. @param promptString 提示语
  316. @param imgName 图片名称
  317. @param view 显示在什么地方
  318. */
  319. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  320. if (self.promptView != nil) {
  321. [self.promptView removeFromSuperview];
  322. }
  323. else {
  324. self.promptView = [[StateView alloc]init];
  325. self.promptView.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight - 300);
  326. }
  327. _promptPlaceView = view;
  328. //当请求不到数据时 ,自定义提示view 将会出现;
  329. self.promptView.imageName = imgName;
  330. self.promptView.alpha = 0.0f;
  331. [self.promptView setText:promptString];
  332. [view addSubview:self.promptView];
  333. }
  334. // 结束刷新后调用方法
  335. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  336. NSInteger count;
  337. if (array.count) {
  338. count = array.count;
  339. } else {
  340. count = 0;
  341. }
  342. [UIView animateWithDuration:0.1 animations:^{
  343. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  344. }] ;
  345. }
  346. - (BOOL)networkAvaiable {
  347. return [self checkNetworkAvaiable];
  348. }
  349. - (BOOL)checkNetworkAvaiable {
  350. BOOL isExistenceNetwork = YES;
  351. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  352. switch ([reach currentReachabilityStatus]) {
  353. case NotReachable:
  354. isExistenceNetwork = NO;
  355. //NSLog(@"notReachable");
  356. break;
  357. case ReachableViaWiFi:
  358. isExistenceNetwork = YES;
  359. //NSLog(@"WIFI");
  360. break;
  361. case ReachableViaWWAN:
  362. isExistenceNetwork = YES;
  363. //NSLog(@"3G");
  364. break;
  365. }
  366. return isExistenceNetwork;
  367. }
  368. - (NSMutableArray *)classArray {
  369. if (!_classArray) {
  370. _classArray = [NSMutableArray array];
  371. }
  372. return _classArray;
  373. }
  374. - (NSMutableArray *)studentArray {
  375. if (!_studentArray) {
  376. _studentArray = [NSMutableArray array];
  377. }
  378. return _studentArray;
  379. }
  380. - (NSMutableArray *)sourceIndexArray {
  381. if (!_sourceIndexArray) {
  382. _sourceIndexArray = [NSMutableArray array];
  383. }
  384. return _sourceIndexArray;
  385. }
  386. - (NSMutableArray *)sourceArray {
  387. if (!_sourceArray) {
  388. _sourceArray = [NSMutableArray array];
  389. }
  390. return _sourceArray;
  391. }
  392. - (KSChatListSearchView *)searchView {
  393. if (!_searchView) {
  394. _searchView = [KSChatListSearchView shareInstance];
  395. MJWeakSelf;
  396. [_searchView topSearchCallback:^{
  397. [weakSelf searchAction];
  398. }];
  399. }
  400. return _searchView;
  401. }
  402. - (void)searchAction {
  403. self.searchKey = self.searchView.searchField.text;
  404. [self refreshAndRequestData];
  405. }
  406. /*
  407. // Only override drawRect: if you perform custom drawing.
  408. // An empty implementation adversely affects performance during animation.
  409. - (void)drawRect:(CGRect)rect {
  410. // Drawing code
  411. }
  412. */
  413. @end