// // WhiteContactViewController.m // tongxin // // Created by ecell on 2021/10/19. // Copyright © 2021 xTT. All rights reserved. // #import "WhiteContactViewController.h" #import "WhiteContactModel.h" #import "WhiteContactTableViewCell.h" #import "AddWhiteContactViewController.h" #import "AppDelegate.h" @interface WhiteContactViewController () @property (nonatomic,strong) UIView * vbottom; @property (nonatomic,strong) UIButton *leftBtn; @property (nonatomic,strong) UIButton *rightBtn; @property (nonatomic,strong) UIBarButtonItem *rightBarButton; @property (nonatomic,strong) WhiteContactModel *model; /// 删除模式 @property (nonatomic ,assign) BOOL isDeletMode; @property (nonatomic ,strong) NSMutableArray *selectArr; @end @implementation WhiteContactViewController - (void)viewDidLoad { [super viewDidLoad]; self.isDeletMode = NO; [self setTitle:@"通讯录"]; CGRect newFrame = self.myTableView.frame; newFrame.size.height = newFrame.size.height - 45 - 22; self.myTableView.frame = newFrame; self.view.backgroundColor = self.myTableView.backgroundColor; [self.myTableView registerNib:[UINib nibWithNibName:@"WhiteContactTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"WhiteContactTableViewCellID"]; self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; [self.view addSubview:self.myTableView]; self.model = [[WhiteContactModel alloc] init]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setUI]; [self loadDataFromServer]; } -(void)loadDataFromServer{ WEAKSELF [self.model query:^(NSMutableArray *arr) { [weakSelf.myDataSource removeAllObjects]; [weakSelf.myDataSource addObject:arr]; if (weakSelf.myDataSource.count <= 0) { weakSelf.rightBtn.hidden = YES; self.isDeletMode = NO; [self.myTableView reloadData]; [self.selectArr removeAllObjects]; [weakSelf.leftBtn setTitle:@"添加" forState:UIControlStateNormal]; weakSelf.leftBtn.frame = CGRectMake(14, 1, (ScreenWidth-28), 44); } [weakSelf.myTableView reloadData]; } failure:^(NSError *error) { if (!error) { } }]; } -(void)setUI{ if(_vbottom){ [_vbottom removeFromSuperview]; _vbottom = nil; } CGFloat tempHeight = 45+11; if(ScreenHeight >= 812){ tempHeight = 45+11+15; } _vbottom = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-getRectNavAndStatusHight-tempHeight, ScreenWidth, tempHeight)]; _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_leftBtn setTitle:@"添加" forState:UIControlStateNormal]; [_leftBtn.layer setCornerRadius:3]; [_leftBtn.layer setMasksToBounds:YES]; [_leftBtn setBackgroundColor:mainColor]; [_leftBtn setTitleColor:[UIColor whiteColor] forState:0]; [_leftBtn.titleLabel setFont:[UIFont systemFontOfSize:15]]; _leftBtn.frame = CGRectMake(14, 1, (ScreenWidth-28), 44); [_vbottom addSubview:_leftBtn]; [self.view addSubview:_vbottom]; [self.view bringSubviewToFront:_vbottom]; //置顶 [_leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside]; _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_rightBtn setTitle:@"删除" forState:UIControlStateNormal]; [_rightBtn.layer setCornerRadius:3]; [_rightBtn.layer setMasksToBounds:YES]; [_rightBtn setBackgroundColor:mainColor]; [_rightBtn setTitleColor:[UIColor whiteColor] forState:0]; [_rightBtn.titleLabel setFont:[UIFont systemFontOfSize:15]]; _rightBtn.hidden = YES; _rightBtn.frame = CGRectMake(ScreenWidth/2+6, 1, (ScreenWidth-40)/2, 44); [_vbottom addSubview:_rightBtn]; [_rightBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside]; } -(void)leftBtnAction:(UIButton*)sender { WEAKSELF if ([sender.currentTitle isEqualToString:@"取消选择"]) { _rightBtn.hidden = YES; self.isDeletMode = NO; [self.myTableView reloadData]; [self.selectArr removeAllObjects]; [_leftBtn setTitle:@"添加" forState:UIControlStateNormal]; _leftBtn.frame = CGRectMake(14, 1, (ScreenWidth-28), 44); } else { AddWhiteContactViewController *vc = [AddWhiteContactViewController new]; vc.view.backgroundColor = [UIColor whiteColor]; NSLog(@"%@",self.myDataSource); vc.modelArr = self.myDataSource[0]; [self.navigationController pushViewController:vc animated:YES]; // UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加通讯录" message:nil preferredStyle:UIAlertControllerStyleAlert]; // //增加取消按钮; // [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]]; // //增加确定按钮; // [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // //获取第1个输入框; // UITextField *userNameTextField = alertController.textFields.firstObject; // //获取第2个输入框; // UITextField *phoneNumberTextField = alertController.textFields.lastObject; // // [weakSelf addWhiteContactToServer:userNameTextField.text phoneNumber:phoneNumberTextField.text]; // // }]]; // //定义第一个输入框; // [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { // textField.placeholder = @"请输入昵称"; // }]; // //定义第二个输入框; // [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { // textField.keyboardType = UIKeyboardTypePhonePad; // textField.placeholder = @"请输入手机号码"; // }]; // // [self presentViewController:alertController animated:true completion:nil]; } } -(void)addWhiteContactToServer:(NSString *)name phoneNumber:(NSString *)phone { //WEAKSELF if (!name || name.length<=0) { [UICommon MessageErrorText:@"昵称不能为空"]; return; } if (!phone || ![self isPhone:phone]) { [UICommon MessageErrorText:@"请输入正确的手机号码"]; return; } // self.model.imei = cUser.cDevice.imei; // self.model.name = name; // self.model.phone = phone; // // [self.model save:_model success:^{ // [weakSelf loadDataFromServer]; // } failure:^(NSError *error) { // // }]; // // NSLog(@"昵称 = %@,手机号码 = %@",name,phone); } -(BOOL)isPhone:(NSString*)phone { NSString * regex = @"^1([3-9])\\d{9}$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; return [predicate evaluateWithObject:phone]; } -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, Adapted(62))]; view.backgroundColor = RGB(248, 248, 254); UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 0, ScreenWidth-28, Adapted(62))]; titleLabel.font = [UIFont systemFontOfSize:12]; titleLabel.textColor = RGB(102, 102, 102); titleLabel.lineBreakMode = NSLineBreakByWordWrapping; titleLabel.numberOfLines = 0; [view addSubview:titleLabel]; NSInteger tempCount = [self.myDataSource[section] count]; if (cUser.cDevice.support_hht.integerValue == 1) titleLabel.attributedText = [self labelFontSize:[NSString stringWithFormat:@"温馨提示:\n为保护儿童通话安全,仅限设置的五组亲情号码之间进行通话,套餐余量不足时,仅支持接听;号码每月只能删除一次,一次限定两个号码。\n长按可删除白名单"]]; else titleLabel.text = [NSString stringWithFormat:@"温馨提示:\n通讯录最多设置5个号码,设置之后将同步到设备\n通讯录(%zd人)",tempCount]; return view; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ NSInteger tempCount = [self.myDataSource[section] count]; if (section == 1){ if(tempCount == 0){ return 0.01; } } return Adapted(62); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ WhiteContactModel *model = self.myDataSource[indexPath.section][indexPath.row]; WhiteContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WhiteContactTableViewCellID" forIndexPath:indexPath]; cell.name.text = model.name; cell.phone.text = model.phone; cell.isDeletMode = self.isDeletMode; cell.selectImg.image = model.isSelect ? [UIImage imageNamed:@"icon_selectImg_Yes"] : [UIImage imageNamed:@"icon_selectImg_No"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)]; longPressGesture.minimumPressDuration = 0.8;//设置长按 时间 [cell addGestureRecognizer:longPressGesture]; return cell; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleNone; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { WhiteContactModel *model = self.myDataSource[indexPath.section][indexPath.row]; if(self.isDeletMode) { model.isSelect = !model.isSelect; if (model.isSelect) { NSLog(@"选中%@",model.name); [self.selectArr addObject:model]; } else { NSLog(@"取消选中%@",model.name); [self.selectArr removeObject:model]; } if (self.selectArr.count > 2) { model.isSelect = NO; [self.selectArr removeObject:model]; [UICommon MessageErrorText:@"一次最多只能删2个白名单"]; return; } [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; } } -(void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer { if (cUser.cDevice.support_hht.integerValue == 1) { if (longRecognizer.state==UIGestureRecognizerStateBegan) { self.isDeletMode = YES; [self loadDataFromServer]; [self.myTableView reloadData]; _leftBtn.frame = CGRectMake(14, 1, (ScreenWidth-40)/2, 44); [_leftBtn setTitle:@"取消选择" forState:0]; _rightBtn.hidden = NO; // //成为第一响应者,需重写该方法 // [self becomeFirstResponder]; // CGPoint location = [longRecognizer locationInView:self.myTableView]; // NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:location]; // //可以得到此时你点击的哪一行 // // //在此添加你想要完成的功能 // NSLog(@"---%ld",indexPath.row); // WhiteContactModel *model = self.myDataSource[indexPath.section][indexPath.row]; // LGAlertView *alertViews = [[LGAlertView alloc] initWithTitle:@"删除白名单" message:[NSString stringWithFormat:@"您确定删除[%@]白名单吗?",model.name] style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // [alertView dismissAnimated:NO completionHandler:nil]; // [self deletemm:model]; // // } cancelHandler:nil destructiveHandler:nil]; // alertViews.cancelOnTouch = NO; // [alertViews showAnimated:YES completionHandler:nil]; } } } -(void)deleteBtnAction:(UIButton*)sender { AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate; if (!app.isTouch) { [UICommon MessageErrorText:@"操作频繁,操作间隔时间为1分钟"]; return; } WEAKSELF if (self.selectArr.count <= 0) { [UICommon MessageErrorText:@"请选择联系人"]; return; } [UICommon MessageUpload:@"正在删除"]; [self.model deleteModel:self.selectArr success:^{ [UICommon HidenLoading]; [UICommon MessageSuccessText:@"删除成功" isImg:YES]; [app stajishiqi]; [weakSelf loadDataFromServer]; } failure:^(NSError *error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[@"msg"]]; [app stajishiqi]; }]; } - (NSMutableArray *)selectArr { if (!_selectArr) _selectArr = [NSMutableArray new]; return _selectArr; } #pragma mark 实现成为第一响应者方法 -(BOOL)canBecomeFirstResponder { return YES; } /// 指定字符大小 /// @param text 内容 - (NSAttributedString *)labelFontSize:(NSString *)text { NSRange symbolRange = [text rangeOfString:@"长按可删除白名单"]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text]; if (symbolRange.location != NSNotFound) { // [string addAttribute:NSFontAttributeName value:FontADA_(13) range:NSMakeRange(symbolRange.location, symbolRange.length)]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(symbolRange.location, symbolRange.length)]; } return string; } @end