// // AttentionMessageViewController.m // tongxin // // Created by WeiChaoZheng on 2018/6/27. // Copyright © 2018年 xTT. All rights reserved. // #import "AttentionMessageViewController.h" #import "DMessage.h" #import "MessageTableViewCell.h" #import "CommonPopView.h" @interface AttentionMessageViewController () @property (nonatomic ,strong) NSMutableArray *listDataSoure; @property (nonatomic ,strong) NSMutableArray *dateArr; @end @implementation AttentionMessageViewController - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self loadMoreDataWithDMessage:nil]; } - (void)viewDidLoad { [super viewDidLoad]; self.myTableView.frame = CGRectMake(-4, 0, SWIDTH+8, self.myTableView.bounds.size.height-40); self.view.backgroundColor = KKBackgroundGrey; [UICommon setTavleViewlink:self.myTableView distance:16]; [self.view addSubview:self.myTableView]; self.zx_hideBaseNavBar = YES; // Do any additional setup after loading the view. self.listDataSoure = [[NSMutableArray alloc] init]; self.dateArr = [[NSMutableArray alloc] init]; WEAKSELF self.myTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{ [weakSelf loadMoreDataWithDMessage:[weakSelf.listDataSoure lastObject]]; }]; [self.myTableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MessageTableViewCellID"]; // [self.myTableView.mj_footer beginRefreshing]; self.myTableView.tableFooterView = [[UIView alloc] init]; // iOS 11 系统 tableView 会自动调整高度 在 Gounp 模式下 头部的高度会有问题 ,需要对 预设的高度设置为0 self.myTableView.estimatedRowHeight = 0; self.myTableView.estimatedSectionHeaderHeight = 0; self.myTableView.estimatedSectionFooterHeight = 0; } //MARK :刪除方法 -(void)deletAction{ // NSMutableArray * messageIds = [NSMutableArray array]; // for (NSArray* secArr in self.myDataSource){ // for (DMessage *dMsg in secArr) { // [messageIds addObject: dMsg.id]; // } // } WEAKSELF CommonPopView *popView = [CommonPopView new]; [popView subPopView:popViewType_binding PopTitle:@"注意" PopText:@"确认要清空关注消息?" okTouchBlock:^{ [DMessage delectAttentionsWithMessageIds:nil success:^(id responseObject) { [weakSelf.myDataSource removeAllObjects]; [weakSelf.listDataSoure removeAllObjects]; [weakSelf.dateArr removeAllObjects]; [weakSelf loadMoreDataWithDMessage:nil]; xLog(@"删除记录成功!") } failure:^{ [UICommon MessageSuccessText:@"删除记录失败,请稍后重试..." isImg:YES]; } ]; }]; } - (void)loadMoreDataWithDMessage:(DMessage *)dMessage{ WEAKSELF NSString *ID; if(dMessage){ ID = dMessage.id; } // add by lsz 21-12-28 @try{}@catch{} @try { [DMessage getAttentionsWithID:ID success:^(NSMutableArray *arr) { if(!ID){ [weakSelf.listDataSoure removeAllObjects]; [weakSelf.dateArr removeAllObjects]; [weakSelf.myDataSource removeAllObjects]; } [weakSelf.listDataSoure addObjectsFromArray:arr]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [weakSelf.listDataSoure enumerateObjectsUsingBlock:^(DMessage *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSDate *date = [NSDate dateWithTimeIntervalSince1970:[obj.timestamp doubleValue]]; NSString *dateStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" date:date]; NSMutableArray *arr = dic[dateStr]; if (!arr) { arr = [NSMutableArray array]; } [arr addObject:obj]; [dic setObject:arr forKey:dateStr]; if (![weakSelf.dateArr containsObject:dateStr]) { [weakSelf.dateArr addObject:dateStr]; } }]; [weakSelf.myDataSource removeAllObjects]; [weakSelf.dateArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [weakSelf.myDataSource addObject:dic[obj]]; }]; [weakSelf.myTableView reloadData]; if ([[weakSelf.myDataSource firstObject] count] == 0){ weakSelf.navigationItem.rightBarButtonItem = nil; } [weakSelf.myTableView.mj_footer endRefreshing]; } failure:^(NSError *error){ if (!error) { } [weakSelf.myTableView.mj_footer endRefreshing]; }]; } @catch (NSException *exception) { NSLog(@"DMessage Error = %@", exception); } } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleNone; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ // if(section == 0){ // return 30; // } // return 10; return 40; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return CGFLOAT_MIN; } //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 30)]; // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, SWIDTH, 30)]; // label.font = [UIFont systemFontOfSize:13]; // label.textColor = RGB(109, 109, 113); // label.text = self.dateArr[section]; // [view addSubview:label]; // return view; //} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ [[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setTextAlignment:NSTextAlignmentCenter]; //[[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setFont:[UIFont systemFontOfSize:12.0 weight:UIFontWeightThin]]; return self.dateArr[section]; } - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if([view isKindOfClass:[UITableViewHeaderFooterView class]]) { UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row]; NSString *str ; if([dMessage.type isEqualToString:@"applicationRrecord"]){ //申请记录的消息 str = [NSString stringWithFormat:@"用户(%@)申请关注【%@】的设备",dMessage.phone,dMessage.name]; CGFloat cellHeight = [self getLineHeight:str withWidth:SWIDTH*(280.0/320) withFont:DefineFontSize]+25; return cellHeight; }else if ([dMessage.type isEqualToString:@"checkResults"]) { //审核结果消息 NSString *checkStr = [NSString stringWithFormat:@"您已%@用户(%@)关注设备",@"同意",dMessage.phone]; str = [NSString stringWithFormat:@"用户(%@)申请关注【%@】的设备 \n %@",dMessage.phone,dMessage.name,checkStr]; }else if ([dMessage.type isEqualToString:@"checkedResults"]) { //被审核结果消息 str = [NSString stringWithFormat:@"设备(%@)的管理员同意您关注设备",dMessage.imei]; }else if ([dMessage.type isEqualToString:@"applicationOvertime"]) { //记录失效消息 str = [NSString stringWithFormat:@"用户(%@)申请关注【%@】设备,记录未及时处理,已失效",dMessage.phone,dMessage.name]; } CGFloat cellHeight = [self getLineHeight:str withWidth:SWIDTH*(280.0/320) withFont:DefineFontSize]; return cellHeight; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row]; MessageTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCellID" forIndexPath:indexPath]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dMessage.timestamp doubleValue]]; cell.timeLabel.text = [myHelper getDateFormatWithStr:@"HH:mm" date:date]; //时间 //信息 NSString * titleStr ; cell.isApplicationRrecord = NO; if([dMessage.type isEqualToString:@"applicationRrecord"]){ //申请记录的消息 // 用户(17328339622)申请关注【宝贝】设备 titleStr = @"申请关注设备记录"; cell.isApplicationRrecord = YES; NSString *tmp = [NSString stringWithFormat:@"用户(%@)",dMessage.phone]; NSInteger lenght = tmp.length; tmp = [NSString stringWithFormat:@"%@申请关注【%@】的设备",tmp,dMessage.name]; NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp]; [att addAttribute:NSForegroundColorAttributeName value:mainColor range:NSMakeRange(0,lenght)]; cell.messageLabel.attributedText = att; WEAKSELF cell.agreeOrNOBlock = ^(BOOL ifAgree) { NSString *msg = [NSString stringWithFormat:@"确认%@用户关注【%@】?",ifAgree?@"同意":@"拒绝",dMessage.name]; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:msg style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { NSMutableDictionary *params = [NSMutableDictionary dictionary]; [params setValue:dMessage.imei forKey:@"imei"]; [params setValue:dMessage.id forKey:@"id"]; [params setValue:@(ifAgree) forKey:@"agree"]; [DMessage manageAttentionWithParams:params success:^{ //刷新数据 [weakSelf.myDataSource removeAllObjects]; [weakSelf.listDataSoure removeAllObjects]; [weakSelf.dateArr removeAllObjects]; [weakSelf loadMoreDataWithDMessage:nil]; [UICommon MessageSuccessText:@"处理成功" isImg:YES]; } failure:^{ [UICommon MessageErrorText:@"处理失败"]; }]; [weakSelf updateMsg2Read:dMessage]; } cancelHandler:^(LGAlertView *alertView) { [weakSelf updateMsg2Read:dMessage]; } destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; }; }else if ([dMessage.type isEqualToString:@"checkResults"]) { //审核结果消息 titleStr = @"申请关注设备记录"; NSString *tmp = [NSString stringWithFormat:@"用户(%@)",dMessage.phone]; NSInteger lenght = tmp.length; NSString *results; if([dMessage.action isEqualToString:@"agree"]){ results = @"同意"; }else{ results = @"拒绝"; } NSString *checkStr = [NSString stringWithFormat:@"您已%@用户(%@)关注设备",results,dMessage.phone]; tmp = [NSString stringWithFormat:@"%@申请关注【%@】的设备 \n %@",tmp,dMessage.name,checkStr]; NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp]; [att addAttribute:NSForegroundColorAttributeName value:mainColor range:NSMakeRange(0,lenght)]; [att addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(att.length-checkStr.length,checkStr.length)]; cell.messageLabel.attributedText = att; }else if ([dMessage.type isEqualToString:@"checkedResults"]) { //被审核结果消息 titleStr = @"申请关注设备记录"; NSString *tmp = [NSString stringWithFormat:@"设备(%@)",dMessage.imei]; NSInteger lenght = tmp.length; NSString *results; if([dMessage.action isEqualToString:@"agree"]){ results = @"同意"; }else{ results = @"拒绝"; } tmp = [NSString stringWithFormat:@"%@的管理员%@您关注设备",tmp,results]; NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp]; [att addAttribute:NSForegroundColorAttributeName value:mainColor range:NSMakeRange(0,lenght)]; cell.messageLabel.attributedText = att; }else if ([dMessage.type isEqualToString:@"applicationOvertime"]) { //记录失效消息 titleStr = @"申请记录失效"; //用户(17328339622)申请关注【宝贝】设备,记录未及时处理,已失效 cell.messageLabel.text = [NSString stringWithFormat:@"用户(%@)申请关注【%@】设备,记录未及时处理,已失效",dMessage.phone,dMessage.name]; } cell.aTitleLabel.text = titleStr; if (dMessage.read && [dMessage.read intValue] == 0) { cell.readLabel.hidden = NO; } else { cell.readLabel.hidden = YES; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row]; [self updateMsg2Read:dMessage]; } -(void)updateMsg2Read:(DMessage *)dMessage{ if (dMessage.read && [dMessage.read intValue] == 0) { WEAKSELF [cUser updateMsg2Read:dMessage.msgId MsgType:@"attention" Success:^(id responseObject) { [weakSelf refreshTableView:dMessage]; NSLog(@"%@", responseObject); } failure:^{ }]; } } -(void)refreshTableView:(DMessage *)message { message.read = @1; [self.myTableView reloadData]; } - (CGFloat)getLineHeight:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font{ UILabel *label = [[UILabel alloc]init]; label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; label.textAlignment = NSTextAlignmentLeft; label.text = string; label.font = font; CGSize labelSize = [label sizeThatFits:CGSizeMake(width, MAXFLOAT)]; CGFloat height = ceil(labelSize.height) + 1 + 34 + 10 +8; // xLog(@"文本框应该高 Height :%f ", height); if(height < 66){ height = 66; } return height; } @end