// // SelectContactViewController.m // LekangGuard // // Created by ecell on 2022/10/12. // #import "SelectContactViewController.h" #import "SelectContactCollectionViewCell.h" #import "AddWatchViewController.h" @interface SelectContactViewController () /// 关系Field @property (nonatomic ,weak) UITextField *contactField; /// 手机号Field @property (nonatomic ,weak) UITextField *phoneField; /// 号码View @property (nonatomic ,weak) UIView *phoneView; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, assign) WSLWaterFlowLayoutStyle flowLayoutStyle; /// 昵称Arr @property (nonatomic ,strong) NSArray *nameArr; /// 头像Arr @property (nonatomic ,strong) NSArray *imgArr; /// 关系ID @property (nonatomic ,assign) NSInteger relationshipId; /// 关系 @property (nonatomic ,strong) NSString *relationship; @end @implementation SelectContactViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = GJText(@"选择关系"); if (self.model.binding_status == 1) { EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:nil subtitle:F(GJText(@"设备已被%@绑定,请选择关系后向管理员申请关注!"), self.model.admin_phone) AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"确定") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { }]; }]; [alertV showAlertView]; } [self zx_setRightBtnWithText:GJText(self.model.binding_status == 1 ? @"申请关注" : @"下一步") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { if (self.phoneField.text.length <= 1) { [UICommon MessageErrorText:@"请输入号码"]; return; } if (self.model.binding_status == 1) { [self SetAttention]; } else { AddWatchViewController *vc = [AddWatchViewController new]; vc.relationshipId = self.relationshipId; vc.relationship = self.relationship; vc.phone = self.phoneField.text; vc.imei = self.imei; vc.model = self.model; [self.navigationController pushViewController:vc animated:YES]; } }]; self.imgArr = [UICommon getUnclickRelationshipImageArr:self.model.device_type]; self.nameArr = [UICommon getRelationshipNameArr:self.model.device_type]; for (int i = 0; i < 2; i++) { UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:RGB(245, 245, 245) cornerRadius:5 borderWidth:0 borderColor:KKClearColor]; if (i == 1) self.phoneView = bgView; [self.view addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view).inset(15); make.top.equalTo(self.view).offset(iPhoneX_NavHeight+Adapted(20)+Adapted(50*i)+10*i); make.height.mas_equalTo(Adapted(50)); }]; UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(13) textColor:KKTextBlackColor text:i == 0 ? GJText(@"与我的关系") : GJText(@"我的号码") Radius:0]; CGFloat width = [(NSString *)titleLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FontADA_(13)} context:nil].size.width; [bgView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(10); make.centerY.equalTo(bgView); make.width.mas_equalTo(width); }]; UITextField *textfield = [UICommon ui_textField:CGRectZero textColor:KKTextBlackColor backColor:KKClearColor font:FontADA_(13) maxTextNum:i == 0 ? 20 : 11 placeholderColor:KKGrey143 placeholder:i == 0 ? GJText(@"请输入关系") : GJText(@"请输入号码") toMaxNum:^(UITextField *textField) { } change:^(UITextField *textField) { }]; textfield.textAlignment = NSTextAlignmentRight; if (i == 0) self.contactField = textfield; else { textfield.keyboardType = UIKeyboardTypePhonePad; textfield.delegate = self; self.phoneField = textfield; } [bgView addSubview:textfield]; [textfield mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(titleLabel.mas_right).offset(7); make.right.equalTo(bgView).inset(10); make.centerY.equalTo(bgView); make.height.mas_equalTo(Adapted(44)); }]; } UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(14) textColor:KKTextBlackColor text:GJText(@"选择头像") Radius:0]; [self.view addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(15); make.top.equalTo(self.phoneView.mas_bottom).offset(Adapted(15)); }]; WSLWaterFlowLayout *FlowLayout = [[WSLWaterFlowLayout alloc] init]; FlowLayout.delegate = self; FlowLayout.flowLayoutStyle = self.flowLayoutStyle; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:FlowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.alwaysBounceVertical = YES; _collectionView.allowsMultipleSelection = NO; _collectionView.scrollEnabled = NO; _collectionView.backgroundColor = KKClearColor; if (@available(iOS 11.0, *)) _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; [_collectionView registerClass:SelectContactCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass(SelectContactCollectionViewCell.class)]; [self.view addSubview:_collectionView]; [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(titleLabel.mas_bottom).offset(0); make.bottom.equalTo(self.view.mas_bottom); }]; kWeakSelf(self) AfterDispatch(1, ^{ NSIndexPath *indexPaths = [NSIndexPath indexPathForRow:0 inSection:0]; [weakself collectionView:weakself.collectionView didSelectItemAtIndexPath:indexPaths]; }); } #pragma mark - WSLWaterFlowLayoutDelegate //返回每个item大小 - (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake((SCREEN_WIDTH-75)/4, (SCREEN_WIDTH-75)/4+Adapted(25)); } /** 头视图Size */ -(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section { return CGSizeMake(SCREEN_WIDTH, 0.1); } /** 列数*/ -(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 4; } /** 列间距*/ -(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 15; } /** 行间距*/ -(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 10; } /** 边缘之间的间距*/ -(UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return UIEdgeInsetsMake(0, 15, 10, 15); } #pragma mark - UICollectionView数据源 //组个数 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } //组内成员个数 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.imgArr.count; } //设置元素内容 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { SelectContactCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(SelectContactCollectionViewCell.class) forIndexPath:indexPath]; cell.tag = indexPath.item+1; cell.deviceType = self.model.device_type; cell.iconImgName = self.imgArr[indexPath.row]; cell.nameStr = self.nameArr[indexPath.row]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [UICommon resignKeyboard]; [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone]; self.contactField.text = [UICommon getRelationshipNameWithCodeID:indexPath.item+1 deviceType:self.model.device_type]; self.relationship = self.contactField.text; self.relationshipId = indexPath.item+1; } #pragma mark 申请关注 /// 申请关注 - (void)SetAttention { [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:self.model.adminUserId forKey:@"adminUserId"]; [parameters setValue:self.imei forKey:@"imei"]; [parameters setValue:self.phoneField.text forKey:@"phone"]; [parameters setValue:self.relationship forKey:@"relationship"]; [parameters setValue:@(self.relationshipId) forKey:@"relationshipImageId"]; [parameters setValue:@(self.model.virtualFlag) forKey:@"virtualFlag"]; [[[APIManager sharedManager] APPOST:SendAccount_URL parameters:parameters isJson:YES resultClass:nil] subscribeNext:^(id _Nullable x) { [UICommon HidenLoading]; [UICommon MessageSuccessText:@"申请关注成功"]; [[NSNotificationCenter defaultCenter] postNotificationName:UPDATA_DeviceList object:nil]; AfterDispatch(1, ^{ [self.navigationController popToRootViewControllerAnimated:YES]; }); } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField != self.phoneField) return NO; if([@"0123456789" containsString:string]) { // 最终的结果 NSString *str = textField.text; NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string]; if(resultStr.length > 15){ return NO; }else{ return YES; } } else { if([string isEqualToString:@""]){ return YES; } return NO; } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [UICommon resignKeyboard]; return 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. } */ @end