// // EditDeviceViewController.m // LekangGuard // // Created by ecell on 2022/12/6. // #import "EditDeviceViewController.h" #import "QRCodeMsgViewController.h" #import "EditSelectContactViewController.h" #define View_height Adapted(55) @interface EditDeviceViewController () /// 头像Btn @property (nonatomic ,weak) UIButton *iconBtn; /// 姓名Field @property (nonatomic ,weak) UITextField *nameField; /// 性别 @property (nonatomic ,weak) UIButton *sexBtn; /// 设备电话号码Field @property (nonatomic ,weak) UITextField *phoneField; /// 关系Label @property (nonatomic ,weak) UILabel *relationLabel; /// 头像Btn @property (nonatomic ,weak) UIImageView *iconImg; /// 生日Btn @property (nonatomic ,weak) UIButton *birthdayBtn; /// 身高Btn @property (nonatomic ,weak) UIButton *heightBtn; /// 体重Btn @property (nonatomic ,weak) UIButton *weightBtn; /// 像头文件 @property (nonatomic ,strong) NSData *iconFile; @property (nonatomic ,strong) EditDeviceModel *devModel; /// 是否编辑 @property (nonatomic ,assign) BOOL isEdit; @property (nonatomic ,assign) NSInteger Sex; @property (nonatomic ,strong) NSString *Weight; @property (nonatomic ,strong) NSString *Height; @property (nonatomic ,assign) BOOL isSelectIcon; @end @implementation EditDeviceViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.zx_popGestureCoverRatio = 0; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.zx_popGestureCoverRatio = 1; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = GJText(@"设备资料"); self.isEdit = NO; self.isSelectIcon = NO; [self zx_setRightBtnWithImg:ImageName_(@"icon_or_code") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { QRCodeMsgViewController *vc = [QRCodeMsgViewController new]; [self.navigationController pushViewController:vc animated:YES]; }]; [self zx_setSubRightBtnWithImg:ImageName_(@"icon_data_editor_orange") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { btn.selected = !btn.selected; self.isEdit = btn.selected; [self setIsEditMsg]; [btn setImage:ImageName_(@"icon_data_editor_orange") forState:UIControlStateNormal]; [btn setImage:ImageName_(@"icon_equipment_information_save") forState:UIControlStateSelected]; if (!self.isEdit) [self UpdateDeviceInfo]; }]; [self zx_leftClickedBlock:^(ZXNavItemBtn * _Nonnull btn) { if (self.isEdit) { if (APIManager.sharedManager.deviceModel.identity == 2) { if (!self.isEditing && [self.nameField.text isEqualToString:self.devModel.name] && self.Sex == self.devModel.sex && [self.phoneField.text isEqualToString:self.devModel.phone] && [self.relationLabel.text isEqualToString:self.devModel.relation] && [self.Height isEqualToString:self.devModel.height] && [self.Weight isEqualToString:self.devModel.weight]) { [self.navigationController popViewControllerAnimated:YES]; return; } } else { if ([self.relationLabel.text isEqualToString:self.devModel.relation] && [self.Height isEqualToString:self.devModel.height] && [self.Weight isEqualToString:self.devModel.weight]) { [self.navigationController popViewControllerAnimated:YES]; return; } } EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:nil subtitle:GJText(@"是否保存修改") AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"取消") type:AlertItemTypeSystemCancel callback:^(EasyAlertView *showview, long index) { [self.navigationController popViewControllerAnimated:YES]; }]; }]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"保存") type:AlertItemTypeSystemDestructive callback:^(EasyAlertView *showview, long index) { [self UpdateDeviceInfo]; }]; }]; [alertV showAlertView]; } else [self.navigationController popViewControllerAnimated:YES]; }]; [self QueryDeviceInfo]; } #pragma mark 获取设备资料 /// 获取设备资料 - (void)QueryDeviceInfo { [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:APIManager.sharedManager.deviceModel.imei forKey:@"imei"]; [[[APIManager sharedManager] APGET:QueryDeviceInfo_URL parameters:parameters resultClass:EditDeviceModel.class] subscribeNext:^(EditDeviceModel *model) { [UICommon HidenLoading]; self.devModel = model; [self DeviceMsgView]; } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } #pragma mark 设备信息View /// 设备信息View - (void)DeviceMsgView { NSArray *titleArr = @[GJText(@"头像"), GJText(@"名称"), GJText(@"性别"), GJText(@"学校"), GJText(@"年级"), GJText(@"班级"), GJText(@"设备电话号码")]; UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:0 borderWidth:0 borderColor:KKClearColor]; [self.view addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.left.right.equalTo(self.view); make.height.mas_equalTo(titleArr.count*View_height); }]; for (int i = 0; i < titleArr.count; i++) { UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(14) textColor:KKTextBlackColor text:titleArr[i] Radius:0]; [bgView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(15); make.top.equalTo(bgView).offset(View_height*i); make.height.mas_equalTo(View_height); }]; if (i < titleArr.count-1) { UILabel *line = [UILabel new]; line.backgroundColor = KKLineColor; [bgView addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView).inset(10); make.top.equalTo(titleLabel.mas_bottom); make.height.mas_equalTo(0.5); }]; } if (i == 0) { /// 头像 UIButton *iconBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(0) normalColor:KKWhiteColorColor normalText:@"" click:^(id x) { [UICommon resignKeyboard]; if (self.devModel.identity != 2) return; if (!self.isEdit) return; [self selectIcon]; }]; [iconBtn sd_setImageWithURL:[NSURL URLWithString:self.devModel.image] forState:0 placeholderImage:ImageName_(@"icon_girl_head_portrait")]; self.iconBtn = iconBtn; [bgView addSubview:iconBtn]; [iconBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.width.height.mas_equalTo(Adapted(42)); }]; } else if (i != 0 && i != 2) { /// 姓名和电话号码 UITextField *textfield = [UICommon ui_textField:CGRectZero textColor:KKGrey121 backColor:KKClearColor font:FontADA_(13) maxTextNum:i == titleArr.count-1 ? 11 : 20 placeholderColor:KKGrey143 placeholder:GJText(@"请输入设备电话号码") toMaxNum:^(UITextField *textField) { } change:^(UITextField *textField) { }]; textfield.textAlignment = NSTextAlignmentRight; if (self.devModel.identity != 2) textfield.enabled = NO; if (i == 1) { textfield.text = self.devModel.name; self.nameField = textfield; } if (i == 3) { textfield.text = self.devModel.schName; } if (i == 4) { textfield.text = self.devModel.gradeName; } if (i == 5) { textfield.text = self.devModel.className; } if (i == titleArr.count-1) { textfield.text = self.devModel.phone; textfield.keyboardType = UIKeyboardTypePhonePad; self.phoneField = textfield; } [bgView addSubview:textfield]; [textfield mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-150, Adapted(44))); }]; } else { /// 性别 NSArray *sexArr = @[GJText(@"男"), GJText(@"女"), GJText(@"保密")]; NSInteger sexs = self.devModel.sex-1 < 0 ? 2 : self.devModel.sex-1; self.Sex = self.devModel.sex; UIButton *sexBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKGrey121 normalText:sexArr[sexs] click:^(UIButton *btn) { [UICommon resignKeyboard]; if (self.devModel.identity != 2) return; if (!self.isEdit) return; [BRStringPickerView showPickerWithTitle:GJText(@"请选择性别") dataSourceArr:sexArr selectIndex:self.devModel.sex-1 resultBlock:^(BRResultModel * _Nullable resultModel) { [btn setTitle:resultModel.value forState:0]; self.Sex = resultModel.index+1; }]; }]; sexBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; self.sexBtn = sexBtn; [bgView addSubview:sexBtn]; [sexBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(Adapted(150), Adapted(44))); }]; } } [self UserMsgView:bgView]; } #pragma mark 关系信息View /// 关系信息View - (void)UserMsgView:(UIView *)view { NSInteger h = 50; NSInteger w = 5; NSMutableArray *hArr = [NSMutableArray new]; NSMutableArray *wArr = [NSMutableArray new]; for (int i = 0; i < 20; i++) { h+=5; w+=5; [hArr addObject:F(@"%ld",h)]; [wArr addObject:F(@"%ld",w)]; } NSArray *titleArr = @[GJText(@"与我的关系"), GJText(@"生日"), GJText(@"身高(cm)"), GJText(@"体重(kg)")]; UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:0 borderWidth:0 borderColor:KKClearColor]; [self.view addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(view.mas_bottom).offset(15); make.left.right.equalTo(self.view); make.height.mas_equalTo(titleArr.count*View_height); }]; for (int i = 0; i < titleArr.count; i++) { UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(14) textColor:KKTextBlackColor text:titleArr[i] Radius:0]; [bgView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(15); make.top.equalTo(bgView).offset(View_height*i); make.height.mas_equalTo(View_height); }]; if (i < titleArr.count-1) { UILabel *line = [UILabel new]; line.backgroundColor = KKLineColor; [bgView addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView).inset(15); make.top.equalTo(titleLabel.mas_bottom); make.height.mas_equalTo(0.5); }]; } if (i == 0) { UILabel *relationLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(13) textColor:KKTextBlackColor text:GJText(self.devModel.relation) Radius:0]; self.relationLabel = relationLabel; [bgView addSubview:relationLabel]; [relationLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); }]; UIImageView *iconImg = [UICommon ui_imageView:CGRectZero fileName:@""]; iconImg.image = [UICommon getClickRelationshipImageWithCodeID:self.devModel.relationImageId deviceType:APIManager.sharedManager.deviceModel.deviceType]; self.iconImg = iconImg; [bgView addSubview:iconImg]; [iconImg mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(relationLabel.mas_left).inset(15); make.centerY.equalTo(titleLabel); make.width.height.mas_equalTo(Adapted(42)); }]; } kWeakSelf(self) UIButton *commBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKTextBlackColor normalText:@"" click:^(UIButton *btn) { [UICommon resignKeyboard]; if (!weakself.isEdit) return; if (btn.tag == 0) { EditSelectContactViewController *vc = [EditSelectContactViewController new]; vc.relationImageId = weakself.devModel.relationImageId; vc.isSelectMsg = ^(NSInteger relationImageId, NSString * _Nonnull name) { weakself.relationLabel.text = name; weakself.iconImg.image = [UICommon getClickRelationshipImageWithCodeID:relationImageId deviceType:APIManager.sharedManager.deviceModel.deviceType]; weakself.devModel.relationImageId = relationImageId; }; [weakself.navigationController pushViewController:vc animated:YES]; } else if (btn.tag == 1) { [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:GJText(@"请选择生日") selectValue:self.devModel.birthday minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) { [weakself.birthdayBtn setTitle:selectValue forState:0]; weakself.devModel.birthday = selectValue; }]; } else if (i == 2) { [hArr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isEqualToString:self.heightBtn.currentTitle]) { [BRStringPickerView showPickerWithTitle:GJText(@"请选择身高") dataSourceArr:hArr selectIndex:idx resultBlock:^(BRResultModel * _Nullable resultModel) { [weakself.heightBtn setTitle:resultModel.value forState:0]; weakself.Height = resultModel.value; }]; } }]; } else { [wArr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isEqualToString:self.weightBtn.currentTitle]) { [BRStringPickerView showPickerWithTitle:GJText(@"请选择体重") dataSourceArr:wArr selectIndex:idx resultBlock:^(BRResultModel * _Nullable resultModel) { [weakself.weightBtn setTitle:resultModel.value forState:0]; weakself.Weight = resultModel.value; }]; } }]; } }]; commBtn.tag = i; if (i == 1) { [commBtn setTitleColor:KKGrey121 forState:0]; [commBtn setTitle:self.devModel.birthday forState:0]; commBtn.enabled = NO; self.birthdayBtn = commBtn; } else if (i == 2) { [commBtn setTitle:self.devModel.height forState:0]; self.Height = self.devModel.height; self.heightBtn = commBtn; } else if (i == 3) { [commBtn setTitle:self.devModel.weight forState:0]; self.Weight = self.devModel.weight; self.weightBtn = commBtn; } commBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [bgView addSubview:commBtn]; [commBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-150, Adapted(44))); }]; } UILabel *tiLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(13) textColor:KKGrey121 text:@"" Radius:0]; tiLabel.text = GJText(@"温馨提示:设备资料可以设置更改设备头像,设备号码"); [self.view addSubview:tiLabel]; [tiLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view).inset(15); make.top.equalTo(bgView.mas_bottom).offset(Adapted(20)); }]; [self setIsEditMsg]; } - (void)selectIcon { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:GJText(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = sourceType; [self presentViewController:picker animated:YES completion:nil]; }else { NSLog(@"模拟其中无法打开照相机,请在真机中使用"); } }]]; [alertController addAction:[UIAlertAction actionWithTitle:GJText(@"从相册选择") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType]; } picker.delegate = self; [self presentViewController:picker animated:YES completion:nil]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:GJText(@"取消") style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; return; } #pragma mark 协议的方法 //用户点击图像选取器中的“cancel”按钮时被调用,这说明用户想要中止选取图像的操作 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } //用户点击选取器中的“choose”按钮时被调用,告知委托对象,选取操作已经完成,同时将返回选取图片的实例 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *curImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; [picker dismissViewControllerAnimated:YES completion:nil]; [self.iconBtn setImage:[UICommon reduceImage:curImg percent:0.5] forState:0]; self.isSelectIcon = YES; } - (void)setIsEditMsg { if (self.devModel.identity == 2) { self.nameField.enabled = self.isEdit; self.phoneField.enabled = self.isEdit; self.nameField.textColor = self.isEdit ? KKTextBlackColor : KKGrey121; self.phoneField.textColor = self.isEdit ? KKTextBlackColor : KKGrey121; [self.sexBtn setTitleColor:self.isEdit ? KKTextBlackColor : KKGrey121 forState:0]; } //[self.birthdayBtn setTitleColor:self.isEdit ? KKTextBlackColor : KKGrey121 forState:0]; [self.heightBtn setTitleColor:self.isEdit ? KKTextBlackColor : KKGrey121 forState:0]; [self.weightBtn setTitleColor:self.isEdit ? KKTextBlackColor : KKGrey121 forState:0]; self.relationLabel.textColor = self.isEdit ? KKTextBlackColor : KKGrey121 ; } #pragma mark 修改手表资料 /// 修改手表资料 - (void)UpdateDeviceInfo { if (self.phoneField.text.length <= 0) { [UICommon MessageErrorText:@"请输入号码"]; return; } if (self.nameField.text.length <= 0) { [UICommon MessageErrorText:@"请输入姓名"]; return; } [UICommon MessageUpload:@"加载中"]; self.iconFile = UIImageJPEGRepresentation(self.iconBtn.currentImage, 1.0f); NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:APIManager.sharedManager.deviceModel.imei forKey:@"imei"]; [parameters setValue:self.Height forKey:@"height"]; [parameters setValue:self.nameField.text forKey:@"name"]; [parameters setValue:self.phoneField.text forKey:@"phone"]; [parameters setValue:self.relationLabel.text forKey:@"relation"]; [parameters setValue:@(self.devModel.relationImageId) forKey:@"relationImageId"]; [parameters setValue:@(self.Sex) forKey:@"sex"]; [parameters setValue:self.Weight forKey:@"weight"]; [parameters setValue:self.devModel.birthday forKey:@"birthday"]; [[[APIManager sharedManager] uploadImageWithURL:UpdateDeviceInfo_URL parameters:parameters images:@[self.iconBtn.currentImage]] subscribeNext:^(id _Nullable x) { [super updataNotificat]; [UICommon HidenLoading]; [UICommon MessageSuccessText:@"修改成功"]; [self.navigationController popViewControllerAnimated:YES]; } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } /* #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