// // SPSBEditMailAddressViewController.m // 我的社保 // // Created by shanp on 2021/5/14. // #import "SPSBEditMailAddressViewController.h" #import "SPSBUIGeneralHeader.h" #import "UIViewController+SPSBNetworkManager.h" #import "SPSBMailingAddressListModel.h" #import #import "SPSBVerificationProfile.h" #import "SPSBTextField.h" #import "SPSBPickerViewManager.h" #import #define spsb_editMailTextFieldTag 3000 @interface SPSBEditMailAddressViewController () { UIView *_bgView; UITextView *_detailsAddressTextView; SPSBPickerViewManager *_pickerManager; } @end @implementation SPSBEditMailAddressViewController - (void)viewDidLoad { [super viewDidLoad]; [self initData]; [self setupUI]; } - (void)initData { _pickerManager = [[SPSBPickerViewManager alloc] initWithTypes:@[SPSBPickerViewTypeMailAddress]]; _pickerManager.spsb_controller = self; } #pragma mark - Action - (void)selectDistrist { jxh_endEdit(); @weakify(self) [_pickerManager showPickerViewWithType:SPSBPickerViewTypeMailAddress defaultValue:nil complete:^(SPSBPickerViewResult result) { @strongify(self) UITextField *districtTextField = [self->_bgView viewWithTag:spsb_editMailTextFieldTag + 2]; districtTextField.text = result.spsb_showData; self->_spsb_data.spsb_area = result.spsb_uploadData; }]; } - (void)saveAction { jxh_endEdit(); if ([_spsb_data.spsb_u_name isEqualToString:@""]) { [self.view showToastWithTitle:@"请输入收货人姓名"]; } else if (_spsb_data.spsb_phone.length != SPSBPhoneNumLength || !jxh_isNumber(_spsb_data.spsb_phone)) { [self.view showToastWithTitle:@"请输入正确的手机号"]; } else if ([_spsb_data.spsb_area isEqualToString:@""]) { [self.view showToastWithTitle:@"请选择所在地区"]; } else if ([_spsb_data.spsb_address isEqualToString:@""]) { [self.view showToastWithTitle:@"请填写详细地址"]; } else { [self uploadMailAddress]; } } #pragma mark - Network Action - (void)uploadMailAddress { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:_spsb_editType == SPSBEditMailAddressTypeAdd ? @"正在添加" : @"正在修改" isLogin:true url:spsb_appUrl(SPSBUrlAddAndUpdateMailingAddress) urlParameters:nil parameters:^NSDictionary * _Nonnull{ return [self->_spsb_data getDictionary]; } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) if (self->_spsb_completion) { self->_spsb_completion(self->_spsb_data); } [self.navigationController popViewControllerAnimated:true]; return @""; } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) return self->_spsb_editType == SPSBEditMailAddressTypeAdd ? @"添加失败" : @"修改失败"; }]; } #pragma mark - UITextFieldDelegate - (void)textFieldDidEndEditing:(UITextField *)textField { if (textField.tag == spsb_editMailTextFieldTag) { _spsb_data.spsb_u_name = textField.text; } else if (textField.tag == spsb_editMailTextFieldTag + 1) { _spsb_data.spsb_phone = textField.text; } } #pragma mark - UITextViewDelegate - (void)textViewDidEndEditing:(UITextView *)textView { _spsb_data.spsb_address = textView.text; } - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { jxh_handleInputTextView(textView, textView.font, 4, 0, NSTextAlignmentLeft); return true; } - (void)textViewDidChange:(UITextView *)textView { UITextRange *selectedRange = [textView markedTextRange]; if (!selectedRange) { if (textView.text.length > SPSBMaxMailAddressLength) { textView.text = [textView.text substringToIndex:SPSBMaxMailAddressLength]; } } } #pragma mark - UI - (void)setupUI { self.title = _spsb_editType == SPSBEditMailAddressTypeAdd ? @"添加新地址" : @"修改信息"; self.view.backgroundColor = spsb_F5F5F5_color(); [self createBg]; [self createTextFields]; [self createTextView]; [self createSaveButton]; } - (void)createSaveButton { UIButton *saveButton = [UIButton convenienceWithFont:spsb_semiboldFont(16) target:self action:@selector(saveAction)]; [saveButton setTitle:@"保存" titleColor:spsb_FFFFFF_color(1.f) image:nil backgroundImage:jxh_getImage(confirm_n) state:JXHButtonControlStateNormal]; [saveButton setBackgroundImage:jxh_getImage(confirm_h) state:JXHButtonControlStateHighlighted]; [saveButton setLayerCornerRadius:22.5 clipToBounds:true]; [self.view addSubview:saveButton]; [saveButton makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self -> _detailsAddressTextView.bottom).offset(24); make.leading.equalTo(16); make.trailing.equalTo(-16); make.height.equalTo(45); }]; } - (void)createBg { _bgView = UIView.new; _bgView.backgroundColor = spsb_FFFFFF_color(1.f); [self.view addSubview:_bgView]; [_bgView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.view.safetop).offset(12); make.leading.and.trailing.equalTo(0); make.height.equalTo(50 * 3 + 100); }]; } - (void)createTextView { _detailsAddressTextView = UITextView.new; _detailsAddressTextView.showsVerticalScrollIndicator = false; _detailsAddressTextView.font = spsb_font(16); _detailsAddressTextView.textColor = spsb_666666_color(); _detailsAddressTextView.delegate = self; _detailsAddressTextView.text = _spsb_data.spsb_address; [_detailsAddressTextView setPlaceholder:@"请填写详细地址" placeholdColor:spsb_CCCCCC_color()]; [_bgView addSubview:_detailsAddressTextView]; [_detailsAddressTextView makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(10); make.trailing.equalTo(-15); make.top.equalTo(50 * 3 + 5); make.bottom.equalTo(0); }]; } - (void)createTextFields { NSArray *contents = @[_spsb_data.spsb_u_name, _spsb_data.spsb_phone, _spsb_data.spsb_area]; NSArray *maxCount = @[@(SPSBMaxNameLength), @(SPSBPhoneNumLength), @50]; NSArray *titleArray = @[@"收货人", @"联系电话", @"所在地区"]; NSArray *placeholderArray = @[@"收货人姓名", @"收货人联系电话", @"省份 城市 区县"]; for (NSInteger i = 0; i < 3; i ++) { UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(16) text:titleArray[i] textColor:spsb_333333_color()]; [_bgView addSubview:titleLabel]; [titleLabel makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(15); make.centerY.equalTo(titleLabel.superview.top).offset(25 + i * 50); }]; [_bgView createLineWithLocation:JXHLineLocationTop locationOffset:50 + i * 50 headOffset:15 footOffset:0]; SPSBTextField *textField = [SPSBTextField convenienceWithFont:spsb_font(16) placeholder:placeholderArray[i] placeholderColor:spsb_CCCCCC_color() textColor:spsb_666666_color() clearButtonMode:UITextFieldViewModeWhileEditing returnType:UIReturnKeyDone keyboardType:i == 1 ? 4 : 0 delegate:self]; textField.spsb_maxCount = [maxCount[i] integerValue]; textField.tag = spsb_editMailTextFieldTag + i; textField.text = contents[i]; [_bgView addSubview:textField]; [textField makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(119); make.centerY.equalTo(titleLabel); make.trailing.equalTo(-15); }]; if (i == 2) { textField.userInteractionEnabled = false; UIView *touchView = UIView.new; [_bgView addSubview:touchView]; [touchView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(textField); }]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectDistrist)]; [touchView addGestureRecognizer:tap]; } } } @end