ShareChooseMainView.m 14 KB

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