// // ContactsViewController.m // watch // // Created by xTT on 2017/7/12. // Copyright © 2017年 xTT. All rights reserved. // #import "ContactsViewController.h" #import "Contacts.h" #import "ChoosePersonAddressVC.h" #import "ContactNEWPACell.h" #import "SelectContactVC.h" #import "MyUILabel.h" @interface ContactsViewController() { BOOL isDeletMode; ///删除模式 } @property (nonatomic,strong) UIView * vbottom; @property (nonatomic,strong) UIButton *leftBtn; @property (nonatomic,strong) UIButton *rightBtn; @property (nonatomic,strong) UIBarButtonItem *rightBarButton; @end @implementation ContactsViewController -(void)setRightBarBtnTitle:(NSString *)title RoImage:(UIImage *)image{ if(title){ [self.zx_navRightBtn setTitle:title forState:0]; [self.zx_navRightBtn setImage:ImageName_(@"ada") forState:0]; } if (image) { [self.zx_navRightBtn setTitle:@"" forState:0]; [self.zx_navRightBtn setImage:ImageName_(@"phoneBook_delete_nav") forState:0]; } // add by lsz 21-10-23 管理员去掉右边菜单 if ([self isSupportWhiteList]) { [self setZx_navRightBtn:nil]; } } - (void)viewDidLoad { [super viewDidLoad]; self.zx_navTitle = @"电话本"; self.view.backgroundColor = KKWhiteColorColor; self.zx_navRightBtn.zx_fixImageSize = CGSizeMake(24, 24); self.myTableView.frame = CGRectMake(0, iPhoneX_NavHeight, ScreenWidth, ScreenHeight-TabBarHeight-20); [self setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"phoneBook_delete_nav"]]; [self zx_rightClickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self setDeleteModel]; }]; // Do any additional setup after loading the view. [PPGetAddressBook requestAddressBookAuthorization]; //请求用户是否授权 CGRect newFrame = self.myTableView.frame; newFrame.size.height = newFrame.size.height - 45 - 22; self.myTableView.frame = newFrame; self.view.backgroundColor = KKWhiteColorColor; [self.myTableView registerNib:[UINib nibWithNibName:@"ContactNEWPACell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ContactNEWPACellID"]; //去线 self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; // add by lsz 2021-06-09 MyUILabel *header = [[MyUILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 56)]; header.edgeInsets = UIEdgeInsetsMake(8, 16, 8, 16); header.font = Font_(12); header.adjustsFontSizeToFitWidth = YES; header.backgroundColor = KKBackgroundGrey; header.numberOfLines = 2; header.baselineAdjustment = UIBaselineAdjustmentAlignCenters; [header setTextColor:KKGrey163]; header.attributedText = [UICommon labelLinesHeight:@"温馨提示:\n电话本最多保存50个号码(有短号的号码需要把长短号都保存)" height:3 align:NSTextAlignmentLeft]; if ([self isSupportWhiteList]) { self.zx_navTitle = @"管理员"; } else { self.myTableView.tableHeaderView = header; } } -(BOOL)isSupportWhiteList { return cUser.cDevice.support_whiteList && cUser.cDevice.support_whiteList.intValue!=0; } /** 编辑状态 */ //- (IBAction)editAction:(UIBarButtonItem *)sender { // // self.isEdit = !self.isEdit; // // [self.myTableView reloadData]; // if([self.myDataSource[0] count] == 0){ // WEAKSELF // LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"暂无联系人,是否从通讯录导入联系人?" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"容我想想" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // [weakSelf inputPersonAddressAction]; // } cancelHandler:nil destructiveHandler:nil]; // [alertView showAnimated:YES completionHandler:nil]; // return; // } // [self.myTableView setEditing:!self.myTableView.isEditing animated:YES]; // sender.title=[sender.title isEqualToString:@"编辑"]?@"保存":@"编辑"; // //} - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self setUI]; @try { [self loadData]; } @catch (NSException *exception) { NSLog(@"==联系人加载失败=="); } } -(void)loadData{ WEAKSELF [Contacts getObjsSuccess:^(NSMutableArray *arr) { //家庭成员 NSMutableArray *famliyArr = [NSMutableArray array]; //联系人 NSMutableArray *contactArr = [NSMutableArray array]; for (Contacts*item in arr) { if(item.openid){ [famliyArr addObject:item]; }else{ [contactArr addObject:item]; } } [weakSelf.myDataSource removeAllObjects]; [weakSelf.myDataSource addObject:famliyArr]; [weakSelf.myDataSource addObject:contactArr]; [weakSelf.myTableView reloadData]; if ([[weakSelf.myDataSource firstObject] count] == 0){ weakSelf.navigationItem.rightBarButtonItem = nil; [weakSelf clearBtnActoin]; }else{ //右侧导航栏添加个按钮 删除模式 if(self->isDeletMode){ [weakSelf setRightBarBtnTitle:@"取消" RoImage:nil]; }else{ [weakSelf setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]]; } } if (self->isDeletMode) { BOOL temp = NO; for (NSArray* secArr in self.myDataSource){ for (Contacts *contact in secArr) { if (contact.isSelect == NO){ temp = YES; } } } if(temp){ //有个一个没选到 [self.leftBtn setTitle:@"全选" forState:0]; }else{ [self.leftBtn setTitle:@"全不选" forState:0]; } } } failure:^(NSError *error){ if (!error) { } }]; } -(void)setUI { if(_vbottom){ [_vbottom removeFromSuperview]; _vbottom = nil; } _vbottom = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-TabBarHeight-15, ScreenWidth, TabBarHeight+15)]; _vbottom.backgroundColor = KKClearColor; _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_leftBtn setTitle:@"通讯录导入" forState:UIControlStateNormal]; [_leftBtn setImage:ImageName_(@"phoneBook_btn_t_icon") forState:0]; _leftBtn.layer.cornerRadius = 22; _leftBtn.layer.masksToBounds = YES; [_leftBtn setBackgroundColor:RGB(248, 247, 247)]; [_leftBtn setTitleColor:KKBlack20 forState:0]; [_leftBtn.titleLabel setFont:FontBold_(16)]; _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_rightBtn setTitle:@"手动添加" forState:UIControlStateNormal]; [_rightBtn setImage:ImageName_(@"phoneBook_btn_s_icon") forState:0]; _rightBtn.layer.cornerRadius = 22; _rightBtn.layer.masksToBounds = YES; [_rightBtn setBackgroundColor:RGB(248, 247, 247)]; [_rightBtn setTitleColor:KKBlack20 forState:0]; [_rightBtn.titleLabel setFont:FontBold_(16)]; // [_vbottom.layer setBorderColor:[UIColor lightGrayColor].CGColor]; // _vbottom.backgroundColor = RGBA(241, 241, 241, 1); _leftBtn.frame = CGRectMake(16, 1, (ScreenWidth-32-12)/2, 44); _rightBtn.frame = CGRectMake(_leftBtn.right+12, 1, (ScreenWidth-32-12)/2, 44); [_vbottom addSubview:_leftBtn]; [_vbottom addSubview:_rightBtn]; [self.view addSubview:_vbottom]; [self.view bringSubviewToFront:_vbottom]; //置顶 [_rightBtn addTarget:self action:@selector(rightBtnActoin:) forControlEvents:UIControlEventTouchUpInside]; [_leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside]; // Mark add by lsz 21-10-22 if ([self isSupportWhiteList]) { [_vbottom setHidden:YES]; } } -(void)rightBtnActoin:(UIButton*)sender { if ([sender.titleLabel.text isEqualToString:@"手动添加"]) { SelectContactVC * vc = [SelectContactVC new]; vc.isAddContact = YES; vc.isContact = YES; vc.deviceType = cUser.cDevice.deviceType; WEAKSELF vc.selectContactBack = ^(NSString *contact, NSNumber *contactId, NSString *phone, NSString *name) { //刷新数据 [weakSelf loadData]; }; [self.navigationController pushViewController:vc animated:YES]; } else { // 删除 NSMutableArray *deleteIDs = [NSMutableArray array]; for (NSArray * secArr in self.myDataSource) { for (Contacts *con in secArr) { if(con.isSelect){ [deleteIDs addObject: con.id]; } } } [self deleteContactFuntionWith:deleteIDs]; } } //MARK: 删除的方法 /** 删除 的 方法 */ -(void)deleteContactFuntionWith:(NSArray*)deleteIDs { if(deleteIDs.count == 0){ LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"注意" message:@"请选择要删除的联系人" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; return; } WEAKSELF NSString *content = [NSString stringWithFormat:@"确认要删除%lu个联系人?",(unsigned long)deleteIDs.count]; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"注意" message:content style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { [Contacts deleteManyPersonWithIDs:[deleteIDs copy] success:^{ [UICommon MessageSuccessText:@"删除联系人成功" isImg:YES]; xLog(@"删除联系人成功!") [weakSelf loadData]; // 取消删除模式 [weakSelf clearBtnActoin]; } failure:^{ // [UICommon MessageSuccessText:@"删除通讯录失败,请稍后重试..."]; xLog(@"删除通讯录失败"); }]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; } -(void)leftBtnAction:(UIButton*)sender { if ([sender.titleLabel.text isEqualToString:@"通讯录导入"]) { //系统导入 //弹出 导入联系人的选择页面 ChoosePersonAddressVC *vc = [ChoosePersonAddressVC new]; [self.navigationController pushViewController:vc animated:YES]; } else { //全选 if ([sender.titleLabel.text isEqualToString:@"全选"]) { for (NSArray* secArr in self.myDataSource) { for (Contacts *dCon in secArr) { //全选的不包括APP 的通讯录类型 if(!dCon.openid){ dCon.isSelect = YES; } } } [self.myTableView reloadData]; [sender setTitle:@"全不选" forState:0]; }else{ //全不选 for (NSArray* secArr in self.myDataSource){ for (Contacts *dCon in secArr) { dCon.isSelect = NO; } } [self.myTableView reloadData]; [sender setTitle:@"全选" forState:0]; } } } //MARK: 进入删除状态 -(void)setDeleteModel { if (!isDeletMode){ [self.leftBtn setImage:nil forState:0]; [self.rightBtn setImage:nil forState:0]; [self.leftBtn setTitle:@"全选" forState:0]; [self.rightBtn setTitle:@"删除" forState:0]; isDeletMode = true; [self.myTableView reloadData]; } else { [self clearBtnActoin]; } if (isDeletMode) { [self setRightBarBtnTitle:@"取消" RoImage:nil]; }else{ [self setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]]; } } //MARK : 取消删除状态 -(void)clearBtnActoin{ [self.leftBtn setTitle:@"通讯录导入" forState:0]; [self.rightBtn setTitle:@"手动添加" forState:0]; [self.leftBtn setImage:ImageName_(@"phoneBook_btn_t_icon") forState:0]; [self.rightBtn setImage:ImageName_(@"phoneBook_btn_s_icon") forState:0]; isDeletMode = NO; [self.myTableView reloadData]; } -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 31)]; view.backgroundColor = KKWhiteColorColor; UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, ScreenWidth, 31)]; titleLabel.font = [UIFont systemFontOfSize:12]; titleLabel.textColor = RGB(102, 102, 102); [view addSubview:titleLabel]; NSInteger tempCount = [self.myDataSource[section] count]; if(section == 0){ titleLabel.text = [NSString stringWithFormat:@"家庭成员(%ld人)",(long)tempCount]; }else if (section == 1){ if(tempCount == 0){ return nil; } titleLabel.text = [NSString stringWithFormat:@"联系人(%ld人)",(long)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 31; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Contacts *contacts = self.myDataSource[indexPath.section][indexPath.row]; ContactNEWPACell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactNEWPACellID" forIndexPath:indexPath]; cell.nameLabel.text = contacts.name; cell.phoneLabel.text = contacts.phone; [cell isCanEditStatus:YES]; if(![cUser.cDevice.identity isEqualToString:@"admin"] && ![contacts.openid isEqualToString:cUser.openid]){ //成员 且不是自己 [cell isCanEditStatus:NO]; } if(contacts.openid) { if([contacts.openid isEqualToString:cUser.openid]){ // [cell isCanEditStatus:YES]; //第一个是本账号 cell.nameLabel.text = @""; NSString *tmp = [NSString stringWithFormat:@"%@ (我) ",contacts.name]; if([contacts.identity isEqualToString:@"admin"]){ tmp = [tmp stringByAppendingString:@"管理员"]; } NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp]; [att addAttribute:NSForegroundColorAttributeName value:mainColor range:NSMakeRange(contacts.name.length,att.length-contacts.name.length)]; cell.nameLabel.attributedText = att; }else if([contacts.identity isEqualToString:@"admin"]){ cell.nameLabel.text = @""; NSString *tmp = [NSString stringWithFormat:@"%@ 管理员 ",contacts.name]; NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp]; [att addAttribute:NSForegroundColorAttributeName value:mainColor range:NSMakeRange(contacts.name.length,att.length-contacts.name.length)]; cell.nameLabel.attributedText = att; } // cell.characterImageView.image = [UIImage imageNamed:@"icon_app"]; //APP 联系人 } else { //普通联系人 出示箭头,有提示 [cell isCanEditStatus:YES]; [cell setDelectModel:isDeletMode]; if(isDeletMode){ [cell setSelectInImageShow:contacts.isSelect]; } // cell.characterImageView.image = [UIImage imageNamed:@"icon_small_phone"]; //普通联系人 if (contacts.phone.length == 0) { cell.phoneLabel.text = @"未设置"; }else{ cell.phoneLabel.text = contacts.phone; } } // //根据 关系 显示对应的头像 cell.headImageView.image = [myHelper getClickRelationshipImageWithCodeID:[contacts.relationship_image_id intValue] deviceType:cUser.cDevice.deviceType.integerValue]; NSInteger lastIndex = [self.myDataSource[indexPath.section] count] - 1; if(lastIndex < 0){ lastIndex = 0; } if(lastIndex > 0 && indexPath.row == lastIndex){ [cell.lineView setHidden:YES]; }else{ [cell.lineView setHidden:NO]; } return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80; } //MARK: 设置cell 的 交互 样式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ /** UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert */ //设置右滑 删除的 样式 // return UITableViewCellEditingStyleDelete; return UITableViewCellEditingStyleNone; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ Contacts *contacts = self.myDataSource[indexPath.section][indexPath.row]; if(isDeletMode){ //删除模式 if(contacts.openid){ //如果是成员或者管理员,不操作 return; } contacts.isSelect = !contacts.isSelect; [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; BOOL temp = NO; for (NSArray * secArr in self.myDataSource) { for (Contacts *con in secArr) { if(con.isSelect == NO){ temp = YES; } } } if(temp){ //有个一个没选到 [self.leftBtn setTitle:@"全选" forState:0]; }else{ [self.leftBtn setTitle:@"全不选" forState:0]; } }else{ if([cUser.cDevice.identity isEqualToString:@"admin"]){ //如果自己是管理员 WEAKSELF NSMutableArray* buttonArr = [NSMutableArray array]; if(contacts.openid){ //是成员 if(![contacts.identity isEqualToString:@"admin"]){ [buttonArr addObject:@"设置为管理员"]; } //是自己---直接跳 if([contacts.openid isEqualToString:cUser.openid]){ [self jumpContactVCWith:contacts]; return; } }else{ //普通 [buttonArr addObject:@"编辑联系人"]; } LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"联系人 %@",contacts.name] style:LGAlertViewStyleActionSheet buttonTitles:[buttonArr copy] cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { if([title isEqualToString:@"编辑联系人"]){ [weakSelf jumpContactVCWith:contacts]; }else if([title isEqualToString:@"设置为管理员"]){ NSString *msg = [NSString stringWithFormat:@"确认将管理员转给 %@ ?",contacts.name]; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:msg style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { contacts.transfer = @(1); [contacts saveSuccess:^{ [UICommon MessageSuccessText:@"转让管理员成功" isImg:YES]; xLog(@"转让管理员成功"); cUser.cDevice.identity = @"member"; [weakSelf loadData]; } failure:^{ xLog(@"转让管理员失败"); }]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; } } cancelHandler:nil destructiveHandler:^(LGAlertView *alertView) { //删除 [weakSelf deleteContactFuntionWith:@[contacts.id]]; }]; [alertView showAnimated:YES completionHandler:nil]; }else{ //成员 //是自己---直接跳 if([contacts.openid isEqualToString:cUser.openid]){ [self jumpContactVCWith:contacts]; return; }else if(contacts.openid){ // 非自己的成员(也包括管理员) return; }else{ //普通联系人 WEAKSELF LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"联系人 %@",contacts.name] style:LGAlertViewStyleActionSheet buttonTitles:@[@"编辑联系人"] cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { if([title isEqualToString:@"编辑联系人"]){ [weakSelf jumpContactVCWith:contacts]; } } cancelHandler:nil destructiveHandler:^(LGAlertView *alertView) { //删除 [weakSelf deleteContactFuntionWith:@[contacts.id]]; }]; [alertView showAnimated:YES completionHandler:nil]; } } } } //MARK: 跳转到 修改/添加 联系人 页面 -(void)jumpContactVCWith:(Contacts*)contacts { //跳转到选择关系页面 SelectContactVC * vc = [SelectContactVC new]; vc.isContact = YES; vc.deviceType = cUser.cDevice.deviceType; vc.contactId = contacts.relationship_image_id; vc.phone = contacts.phone; //本机电话号码 vc.name = contacts.name; vc.contactModel = contacts; vc.selectContactBack = ^(NSString *contact, NSNumber *contactId, NSString *phone, NSString *name) { //刷新数据 [self loadData]; }; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. if (sender) { [segue.destinationViewController setValue:sender forKey:@"infoContacts"]; } } @end