ChatAddressBodyView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. //
  2. // ChatAddressBodyView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/24.
  6. //
  7. #import "ChatAddressBodyView.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 ChatAddressBodyView ()<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) ShareCallback callback;
  26. @end
  27. @implementation ChatAddressBodyView
  28. - (void)shareCallback:(ShareCallback)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, kScreenWidth, 10)];
  46. headView.backgroundColor = HexRGB(0xf6f8f9);
  47. self.tableView.tableHeaderView = headView;
  48. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 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 integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  76. NSArray *array = [dic 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 integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  107. NSArray *array = [dic 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 (_isShareImage) { // 分享
  254. [self sendMessageWithTargetId:model.imFriendId isGroup:NO];
  255. }
  256. else { // 聊天
  257. [self chatConversationWithTargetId:model.imFriendId targetName:model.friendNickname isGroup:NO];
  258. }
  259. }
  260. else { // 群聊
  261. GroupListModel *model = self.classArray[indexPath.row];
  262. if (_isShareImage) { // 群聊消息
  263. [self sendMessageWithTargetId:model.groupId isGroup:YES];
  264. }
  265. else {
  266. // 点击进入群聊
  267. [self chatConversationWithTargetId:model.groupId targetName:model.name isGroup:YES];
  268. }
  269. }
  270. }
  271. - (void)sendMessageWithTargetId:(NSString *)targetId isGroup:(BOOL)isGroup {
  272. RCImageMessage *imgMsg = [RCImageMessage messageWithImage:self.shareImage];
  273. imgMsg.full = YES;
  274. RCConversationType type = isGroup ? ConversationType_GROUP : ConversationType_PRIVATE;
  275. [[RCIMClient sharedRCIMClient] sendMediaMessage:type targetId:targetId content:imgMsg pushContent:nil pushData:nil progress:^(int progress, long messageId) {
  276. } success:^(long messageId) {
  277. dispatch_main_async_safe(^{
  278. if (self.callback) {
  279. self.callback(YES, @"发送成功");
  280. }
  281. });
  282. } error:^(RCErrorCode errorCode, long messageId) {
  283. dispatch_main_async_safe(^{
  284. if (self.callback) {
  285. self.callback(NO, @"发送失败");
  286. }
  287. });
  288. } cancel:^(long messageId) {
  289. dispatch_main_async_safe(^{
  290. if (self.callback) {
  291. self.callback(NO,@"已取消");
  292. }
  293. });
  294. }];
  295. }
  296. - (void)chatConversationWithTargetId:(NSString *)targetId targetName:(NSString *)targetName isGroup:(BOOL)isGroup {
  297. KSChatConversationViewController *conversationVC = [[KSChatConversationViewController alloc] init];
  298. conversationVC.targetId = targetId;
  299. conversationVC.conversationType = isGroup ? ConversationType_GROUP : ConversationType_PRIVATE;
  300. conversationVC.title = targetName;
  301. [self.naviController pushViewController:conversationVC animated:YES];
  302. }
  303. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  304. if (self.selectIndex == 1) {
  305. return CGFLOAT_MIN;
  306. }
  307. return 30;
  308. }
  309. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  310. if (self.selectIndex == 1) {
  311. return [UIView new];
  312. }
  313. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
  314. view.backgroundColor = HexRGB(0xf6f8f9);
  315. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, kScreenWidth - 48, 30)];
  316. label.textColor = HexRGB(0x999999);
  317. label.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
  318. [view addSubview:label];
  319. label.textAlignment = NSTextAlignmentLeft;
  320. NSString *titleStr = self.sourceIndexArray[section];
  321. label.text = titleStr;
  322. return view;
  323. }
  324. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  325. return CGFLOAT_MIN;
  326. }
  327. /**
  328. 设置没有数据时的显示
  329. @param promptString 提示语
  330. @param imgName 图片名称
  331. @param view 显示在什么地方
  332. */
  333. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  334. if (self.promptView != nil) {
  335. [self.promptView removeFromSuperview];
  336. }
  337. else {
  338. self.promptView = [[StateView alloc]init];
  339. self.promptView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 300);
  340. }
  341. _promptPlaceView = view;
  342. //当请求不到数据时 ,自定义提示view 将会出现;
  343. self.promptView.imageName = imgName;
  344. self.promptView.alpha = 0.0f;
  345. [self.promptView setText:promptString];
  346. [view addSubview:self.promptView];
  347. }
  348. // 结束刷新后调用方法
  349. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  350. NSInteger count;
  351. if (array.count) {
  352. count = array.count;
  353. } else {
  354. count = 0;
  355. }
  356. [UIView animateWithDuration:0.1 animations:^{
  357. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  358. }] ;
  359. }
  360. - (BOOL)networkAvaiable {
  361. return [self checkNetworkAvaiable];
  362. }
  363. - (BOOL)checkNetworkAvaiable {
  364. BOOL isExistenceNetwork = YES;
  365. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  366. switch ([reach currentReachabilityStatus]) {
  367. case NotReachable:
  368. isExistenceNetwork = NO;
  369. //NSLog(@"notReachable");
  370. break;
  371. case ReachableViaWiFi:
  372. isExistenceNetwork = YES;
  373. //NSLog(@"WIFI");
  374. break;
  375. case ReachableViaWWAN:
  376. isExistenceNetwork = YES;
  377. //NSLog(@"3G");
  378. break;
  379. }
  380. return isExistenceNetwork;
  381. }
  382. - (NSMutableArray *)classArray {
  383. if (!_classArray) {
  384. _classArray = [NSMutableArray array];
  385. }
  386. return _classArray;
  387. }
  388. - (NSMutableArray *)studentArray {
  389. if (!_studentArray) {
  390. _studentArray = [NSMutableArray array];
  391. }
  392. return _studentArray;
  393. }
  394. - (NSMutableArray *)sourceIndexArray {
  395. if (!_sourceIndexArray) {
  396. _sourceIndexArray = [NSMutableArray array];
  397. }
  398. return _sourceIndexArray;
  399. }
  400. - (NSMutableArray *)sourceArray {
  401. if (!_sourceArray) {
  402. _sourceArray = [NSMutableArray array];
  403. }
  404. return _sourceArray;
  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