123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- //
- // ChatAddressBodyView.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/24.
- //
- #import "ChatAddressBodyView.h"
- #import "SCIndexView.h"
- #import "UITableView+SCIndexView.h"
- #import "KSChatConversationViewController.h"
- #import "GroupListModel.h"
- #import "GroupListViewCell.h"
- #import "ContractListCell.h"
- #import "StateView.h"
- #import "Reachability.h"
- #import "FriendListModel.h"
- @interface ChatAddressBodyView ()<UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic, strong) StateView *promptView;
- @property (nonatomic, strong) UIView *promptPlaceView;
- @property (nonatomic, assign) BOOL networkAvaiable; // 网络是否可用
- @property (nonatomic, strong) NSMutableArray *classArray; // 班级数据
- @property (nonatomic, strong) NSMutableArray *studentArray; // 学生数据
- @property (nonatomic, strong) NSMutableArray *sourceIndexArray;
- @property (nonatomic, strong) NSMutableArray *sourceArray;
- @property (nonatomic, copy) ShareCallback callback;
- @end
- @implementation ChatAddressBodyView
- - (void)shareCallback:(ShareCallback)callback {
- if (callback) {
- self.callback = callback;
- }
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = HexRGB(0xf6f8f9);
- self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
- self.tableView.backgroundColor = HexRGB(0xf6f8f9);
- self.tableView.showsVerticalScrollIndicator = NO;
- self.tableView.dataSource = self;
- self.tableView.delegate = self;
- self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- self.tableView.rowHeight = 70;
- [self addSubview:self.tableView];
-
-
- UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
- headView.backgroundColor = HexRGB(0xf6f8f9);
- self.tableView.tableHeaderView = headView;
-
- UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
- bottomView.backgroundColor = HexRGB(0xf6f8f9);
- self.tableView.tableFooterView = bottomView;
-
- [self.tableView registerNib:[UINib nibWithNibName:@"ContractListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ContractListCell"];
- [self.tableView registerNib:[UINib nibWithNibName:@"GroupListViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GroupListViewCell"];
- MJWeakSelf;
- self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
- [weakSelf resetParamenter];
- [weakSelf requestData];
- }];
- }
- return self;
- }
- - (void)endRefresh {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self.tableView.mj_header endRefreshing];
- });
- }
- - (void)refreshAndRequestData {
- [self resetParamenter];
- [self requestData];
- }
- - (void)requestData {
- if (self.selectIndex == 0) {
- [MBProgressHUD ksShowHUDWithText:@"数据加载中......"];
- [KSNetworkingManager imUserFriendRequest:KS_POST search:self.searchKey success:^(NSDictionary * _Nonnull dic) {
- [self endRefresh];
- if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
- NSArray *array = [dic arrayValueForKey:@"data"];
- for (NSDictionary *parm in array) {
- if ([parm isKindOfClass:[NSNull class]]) {
- continue;
- }
- FriendListModel *model = [[FriendListModel alloc] initWithDictionary:parm];
- [self.sourceArray addObject:model];
- }
- [self evaluateMessge];
- }
- else {
- [MBProgressHUD ksHideHUD];
- [self MBPShow:MESSAGEKEY];
- [self changePromptLabelStateWithArray:self.studentArray];
-
- }
- } faliure:^(NSError * _Nonnull error) {
- [MBProgressHUD ksHideHUD];
- [self endRefresh];
- if (self.networkAvaiable == NO) {
- [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
- }
- [self.studentArray removeAllObjects];
- [self.sourceIndexArray removeAllObjects];
- [self.tableView reloadData];
- [self changePromptLabelStateWithArray:self.studentArray];
- }];
-
- }
- else {
- [KSNetworkingManager imGroupQueryPage:KS_POST search:self.searchKey success:^(NSDictionary * _Nonnull dic) {
- [self endRefresh];
- if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
- NSArray *array = [dic arrayValueForKey:@"data"];
- for (NSDictionary *parm in array) {
- GroupListModel *model = [[GroupListModel alloc] initWithDictionary:parm];
- [self.classArray addObject:model];
- }
- }
- else {
- [self MBPShow:MESSAGEKEY];
- }
- [self.tableView reloadData];
- [self changePromptLabelStateWithArray:self.classArray];
- } faliure:^(NSError * _Nonnull error) {
- [self endRefresh];
- if (self.networkAvaiable == NO) {
- [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
- }
- [self.classArray removeAllObjects];
- [self.tableView reloadData];
- [self changePromptLabelStateWithArray:self.classArray];
- }];
- }
- }
- - (void)evaluateMessge {
- @autoreleasepool {
- NSMutableArray *sourceArray = [self.sourceArray mutableCopy];
- // 异步线程处理数据
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSMutableArray *sortArr = [NSMutableArray array];
- for (FriendListModel *model in sourceArray) {
- if ([NSString isEmptyString:model.friendNickname]) {
- model.friendNickname = [NSString stringWithFormat:@"游客%@",model.friendId];
- }
- NSString *firstLetter = [NSString getFirstLetterFromString:[NSString returnNoNullStringWithString:model.friendNickname]];
- model.firstLetter = firstLetter;
- if (![sortArr containsObject:firstLetter]) {
- [sortArr addObject:firstLetter];
- }
- }
- [sortArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
- return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
- }];
- // 将#移动到最后
- for (NSString *letterStr in sortArr) {
- if ([letterStr isEqualToString:@"#"]) {
- [sortArr removeObject:letterStr];
- [sortArr addObject:@"#"];
- break;
- }
- }
- for (NSString *sortStr in sortArr) {
- NSMutableArray *filterArray = [NSMutableArray array];
- for (FriendListModel *subModel in self.sourceArray) {
- if ([sortStr isEqualToString:subModel.firstLetter]) {
- [filterArray addObject:subModel];
- }
- }
- [self.studentArray addObject:filterArray];
- }
-
- self.sourceIndexArray = sortArr;
- // 主线程刷新
- dispatch_async(dispatch_get_main_queue(), ^{
- [MBProgressHUD ksHideHUD];
- self.tableView.sc_indexViewDataSource = self.sourceIndexArray;
- [self.tableView reloadData];
- [self changePromptLabelStateWithArray:self.studentArray];
- });
- });
- };
- }
- - (void)resetParamenter {
- [self setPromptString:@"暂无内容" imageName:@"wd_img_zwsj" inView:self.tableView];
- [self.classArray removeAllObjects];
- [self.sourceArray removeAllObjects];
- [self.studentArray removeAllObjects];
- self.sourceIndexArray = [NSMutableArray array];
- SCIndexViewConfiguration *indexConfiguration = [SCIndexViewConfiguration configuration];
- indexConfiguration.indexItemSelectedBackgroundColor = THEMECOLOR;
- indexConfiguration.indicatorBackgroundColor = THEMECOLOR;
- self.tableView.sc_indexViewConfiguration = indexConfiguration;
- self.tableView.sc_indexViewDataSource = self.sourceIndexArray;
- [self.tableView reloadData];
- }
- - (void)beginRefreshImmediately {
- [self.tableView.mj_header beginRefreshing];
- }
- - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
-
- if (self.lastSelectedIndexPath == indexPath) {
- return;
- }
- if (self.lastSelectedIndexPath != nil) {
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.lastSelectedIndexPath];
- [cell setSelected:NO animated:NO];
- }
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
- [cell setSelected:YES animated:NO];
- self.lastSelectedIndexPath = indexPath;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- self.tableView.frame = self.bounds;
- }
- - (void)beginFirstRefresh {
- if (!self.isHeaderRefreshed) {
- [self beginRefreshImmediately];
- }
- }
- #pragma mark - UITableViewDataSource, UITableViewDelegate
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (self.selectIndex == 0) {
- if (self.studentArray.count > section) {
- NSArray *filterArray = self.studentArray[section];
- return filterArray.count;
- }
- return 0;
- }
- else {
- return self.classArray.count;
- }
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (self.selectIndex == 0) {
- return self.sourceIndexArray.count;
- }
- else {
- return 1;
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- if (self.selectIndex == 0) {
- ContractListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContractListCell"];
- NSArray *filterArray = self.studentArray[indexPath.section];
- FriendListModel *model = filterArray[indexPath.row];
- [cell configWithSource:model];
- return cell;
- }
- else {
- GroupListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupListViewCell"];
- GroupListModel *model = self.classArray[indexPath.row];
- [cell configWithSource:model];
- return cell;
- }
-
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- if (self.selectIndex == 0) { // 联系人
- NSArray *filterArray = self.studentArray[indexPath.section];
- FriendListModel *model = filterArray[indexPath.row];
- if (_isShareImage) { // 分享
- [self sendMessageWithTargetId:model.imFriendId isGroup:NO];
- }
- else { // 聊天
- [self chatConversationWithTargetId:model.imFriendId targetName:model.friendNickname isGroup:NO];
- }
- }
- else { // 群聊
- GroupListModel *model = self.classArray[indexPath.row];
- if (_isShareImage) { // 群聊消息
- [self sendMessageWithTargetId:model.groupId isGroup:YES];
- }
- else {
- // 点击进入群聊
- [self chatConversationWithTargetId:model.groupId targetName:model.name isGroup:YES];
- }
- }
- }
- - (void)sendMessageWithTargetId:(NSString *)targetId isGroup:(BOOL)isGroup {
-
- RCImageMessage *imgMsg = [RCImageMessage messageWithImage:self.shareImage];
- imgMsg.full = YES;
- RCConversationType type = isGroup ? ConversationType_GROUP : ConversationType_PRIVATE;
- [[RCIMClient sharedRCIMClient] sendMediaMessage:type targetId:targetId content:imgMsg pushContent:nil pushData:nil progress:^(int progress, long messageId) {
-
- } success:^(long messageId) {
- dispatch_main_async_safe(^{
- if (self.callback) {
- self.callback(YES, @"发送成功");
- }
- });
-
- } error:^(RCErrorCode errorCode, long messageId) {
- dispatch_main_async_safe(^{
- if (self.callback) {
- self.callback(NO, @"发送失败");
- }
- });
-
- } cancel:^(long messageId) {
- dispatch_main_async_safe(^{
- if (self.callback) {
- self.callback(NO,@"已取消");
- }
- });
- }];
- }
- - (void)chatConversationWithTargetId:(NSString *)targetId targetName:(NSString *)targetName isGroup:(BOOL)isGroup {
- KSChatConversationViewController *conversationVC = [[KSChatConversationViewController alloc] init];
- conversationVC.targetId = targetId;
- conversationVC.conversationType = isGroup ? ConversationType_GROUP : ConversationType_PRIVATE;
- conversationVC.title = targetName;
- [self.naviController pushViewController:conversationVC animated:YES];
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- if (self.selectIndex == 1) {
- return CGFLOAT_MIN;
- }
- return 30;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- if (self.selectIndex == 1) {
- return [UIView new];
- }
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
- view.backgroundColor = HexRGB(0xf6f8f9);
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, kScreenWidth - 48, 30)];
- label.textColor = HexRGB(0x999999);
- label.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
- [view addSubview:label];
- label.textAlignment = NSTextAlignmentLeft;
- NSString *titleStr = self.sourceIndexArray[section];
- label.text = titleStr;
- return view;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
- return CGFLOAT_MIN;
- }
- /**
- 设置没有数据时的显示
-
- @param promptString 提示语
- @param imgName 图片名称
- @param view 显示在什么地方
- */
- - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
- if (self.promptView != nil) {
- [self.promptView removeFromSuperview];
- }
- else {
- self.promptView = [[StateView alloc]init];
- self.promptView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 300);
- }
- _promptPlaceView = view;
- //当请求不到数据时 ,自定义提示view 将会出现;
- self.promptView.imageName = imgName;
- self.promptView.alpha = 0.0f;
- [self.promptView setText:promptString];
- [view addSubview:self.promptView];
- }
- // 结束刷新后调用方法
- - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
- NSInteger count;
- if (array.count) {
- count = array.count;
- } else {
- count = 0;
- }
-
- [UIView animateWithDuration:0.1 animations:^{
- [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
-
- }] ;
-
- }
- - (BOOL)networkAvaiable {
- return [self checkNetworkAvaiable];
- }
- - (BOOL)checkNetworkAvaiable {
- BOOL isExistenceNetwork = YES;
- Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
- switch ([reach currentReachabilityStatus]) {
- case NotReachable:
- isExistenceNetwork = NO;
- //NSLog(@"notReachable");
- break;
- case ReachableViaWiFi:
- isExistenceNetwork = YES;
- //NSLog(@"WIFI");
- break;
- case ReachableViaWWAN:
- isExistenceNetwork = YES;
- //NSLog(@"3G");
- break;
- }
- return isExistenceNetwork;
- }
- - (NSMutableArray *)classArray {
- if (!_classArray) {
- _classArray = [NSMutableArray array];
- }
- return _classArray;
- }
- - (NSMutableArray *)studentArray {
- if (!_studentArray) {
- _studentArray = [NSMutableArray array];
- }
- return _studentArray;
- }
- - (NSMutableArray *)sourceIndexArray {
- if (!_sourceIndexArray) {
- _sourceIndexArray = [NSMutableArray array];
- }
- return _sourceIndexArray;
- }
- - (NSMutableArray *)sourceArray {
- if (!_sourceArray) {
- _sourceArray = [NSMutableArray array];
- }
- return _sourceArray;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|