|
//
// editMailController.m
// ThePaperBase
//
// Created by Huixin on 15/8/26.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "editMailController.h"
@interface editMailController () <UITextFieldDelegate> {
NSString *mail;
NSString *verificationCode;
}
@property(nonatomic, strong)UIImageView *mailImageView;
@property(nonatomic, strong)UITextField *mailField;
@property(nonatomic, strong)UIView *mailLine;
@property(nonatomic, strong)UIImageView *verificationCodeImageView;
@property(nonatomic, strong)UITextField *verificationCodeField;
@property(nonatomic, strong)UIView *verificationCodeLine;
@property(nonatomic, strong)UIButton *getCodeBtn;
@property(nonatomic, strong)TPCustomButton *confirmBtn;
@end
@implementation editMailController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.titleStr = @"修改邮箱";
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tapGesture];
[self.view addSubview:self.mailField];
[self.view addSubview:self.mailImageView];
[self.view addSubview:self.mailLine];
[self.view addSubview:self.getCodeBtn];
[self.view addSubview:self.verificationCodeImageView];
[self.view addSubview:self.verificationCodeField];
[self.view addSubview:self.verificationCodeLine];
[self.view addSubview:self.confirmBtn];
[self.mailField becomeFirstResponder];
[self layoutViews];
}
- (void)layoutViews {
[self.mailImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(20);
make.centerY.equalTo(self.mailField.centerY);
make.width.mas_equalTo(@20);
make.height.mas_equalTo(@15);
}];
[self.mailField makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.bottom).offset(38);
make.left.equalTo(self.view.left).offset(50);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@19);
}];
[self.mailLine makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mailField.bottom).offset(7);
make.left.equalTo(self.mailImageView.left);
make.right.equalTo(self.mailField.right);
make.height.mas_equalTo(@1);
}];
[self.getCodeBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mailLine.bottom).offset(30);
make.centerX.equalTo(self.view.centerX);
make.width.mas_equalTo(@80);
make.height.mas_equalTo(@20);
}];
[self.verificationCodeImageView makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.verificationCodeField.centerY);
make.left.equalTo(self.view.left).offset(20);
make.width.mas_equalTo(@20);
make.height.mas_equalTo(@15);
}];
[self.verificationCodeField makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.getCodeBtn.bottom).offset(38);
make.left.equalTo(self.view.left).offset(50);
make.right.equalTo(self.view.right).offset(-20);
make.height.mas_equalTo(@19);
}];
[self.verificationCodeLine makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.verificationCodeField.bottom).offset(7);
make.left.equalTo(self.verificationCodeImageView.left);
make.right.equalTo(self.verificationCodeField.right);
make.height.mas_equalTo(@1);
}];
[self.confirmBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.verificationCodeLine.bottom).offset(35);
make.left.equalTo(self.view.left).offset(20);
make.right.equalTo(self.view.right).offset(-20);
make.height.equalTo(self.confirmBtn.width).multipliedBy(0.15);
}];
}
#pragma mark - get method
- (UIImageView*)mailImageView {
if (!_mailImageView) {
_mailImageView = [[UIImageView alloc] init];
_mailImageView.image = Image(@"setting/leak_mail.png");
}
return _mailImageView;
}
- (UITextField*)mailField {
if (!_mailField) {
_mailField = [[UITextField alloc] init];
_mailField.backgroundColor = [UIColor clearColor];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入新邮箱" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}];
_mailField.attributedPlaceholder = atrString;
_mailField.textColor = [UIColor colorWithHexString:TextBlack];
_mailField.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_mailField.textAlignment = NSTextAlignmentLeft;
_mailField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_mailField.keyboardAppearance = UIKeyboardAppearanceDark;
}
_mailField.keyboardType = UIKeyboardTypeEmailAddress;
_mailField.clearButtonMode = UITextFieldViewModeWhileEditing;
_mailField.delegate = self;
[_mailField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _mailField;
}
- (UIView*)mailLine {
if (!_mailLine) {
_mailLine = [[UIView alloc] init];
_mailLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _mailLine;
}
- (UIButton*)getCodeBtn {
if (!_getCodeBtn) {
_getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
[_getCodeBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateNormal];
[_getCodeBtn setTitleColor:[UIColor colorWithHexString:BUTTONSELECTBACK] forState:UIControlStateHighlighted];
_getCodeBtn.titleLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO);
_getCodeBtn.layer.borderColor = [UIColor colorWithHexString:BLUECOLOR].CGColor;
_getCodeBtn.layer.cornerRadius = 4.f;
_getCodeBtn.layer.borderWidth = 1;
[_getCodeBtn addTarget:self action:@selector(getVerificationCode:) forControlEvents:UIControlEventTouchUpInside];
}
return _getCodeBtn;
}
- (UIImageView*)verificationCodeImageView {
if (!_verificationCodeImageView) {
_verificationCodeImageView = [[UIImageView alloc] init];
_verificationCodeImageView.image = Image(@"login/pin.png");
}
return _verificationCodeImageView;
}
- (UITextField*)verificationCodeField {
if (!_verificationCodeField) {
_verificationCodeField = [[UITextField alloc] init];
_verificationCodeField.backgroundColor = [UIColor clearColor];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"请输入邮箱中的验证码" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextLightGray]}];
_verificationCodeField.attributedPlaceholder = atrString;
_verificationCodeField.textColor = [UIColor colorWithHexString:TextBlack];
_verificationCodeField.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
_verificationCodeField.textAlignment = NSTextAlignmentLeft;
_verificationCodeField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_verificationCodeField.keyboardAppearance = UIKeyboardAppearanceDark;
}
_verificationCodeField.keyboardType = UIKeyboardTypeNumberPad;
_verificationCodeField.clearButtonMode = UITextFieldViewModeWhileEditing;
_verificationCodeField.delegate = self;
[_verificationCodeField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _verificationCodeField;
}
- (UIView*)verificationCodeLine {
if (!_verificationCodeLine) {
_verificationCodeLine = [[UIView alloc] init];
_verificationCodeLine.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _verificationCodeLine;
}
- (TPCustomButton*)confirmBtn {
if (!_confirmBtn) {
_confirmBtn = [[TPCustomButton alloc] init];
_confirmBtn.title = @"确认";
[_confirmBtn addTarget:self action:@selector(confirmEvent) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
#pragma mark - textfield delegate
- (void)textFieldDidChange:(UITextField *)textField {
if ([textField.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) {
textField.text = disable_emoji(textField.text);
}
if (textField == self.mailField) {
mail = textField.text;
}else if (textField == self.verificationCodeField) {
verificationCode = textField.text;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == _mailField) {
[_verificationCodeField becomeFirstResponder];
}
else if (textField == _verificationCodeField) {
[self confirmEvent];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isFirstResponder]) {
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
return NO;
}
}
return YES;
}
#pragma mark - btn event handler
- (void)getVerificationCode:(UIButton*)btn {
[self closeKeyBoard];
if (isBlankString(mail)) {
ShowTextMessage(@"请输入邮箱地址");
return;
}
NSDictionary *dic = @{@"verType":@"2",@"mail":mail};
[Remote doJsonActionWithBlock:0 requestUrl:getVerCodeURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
if ([responseData[@"resultCode"] intValue] == 1) {
ShowTextMessage(responseData[@"resultMsg"]);
}
}else {
ShowTextMessage(message);
}
}];
}
- (void)confirmEvent {
[self closeKeyBoard];
if (isBlankString(mail)) {
ShowTextMessage(@"请输入邮箱地址");
return;
}
if (isBlankString(verificationCode)) {
ShowTextMessage(@"请输入验证码");
return;
}
NSDictionary *dic = @{@"verType":@"2",@"mail":mail,@"verCode":verificationCode};
[Remote doJsonAction:0 requestUrl:checkVerCodeURL parameter:dic delegate:self];
}
#pragma mark - remote delegate
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responseData {
if (actionTag == 0) {
if ([responseData[@"resultCode"] intValue] == 1) {
userBO *user = setJsonDicToDataModel(responseData[@"userInfo"], [userBO class]);
userBO *oriUser = [TPUserDefault instance].userBO;
user.loginType = oriUser.loginType;
[TPUserDefault instance].userBO = user;
[self.navigationController popViewControllerAnimated:YES];
}else {
ShowTextMessage(responseData[@"resultMsg"]);
}
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
ShowTextMessage(message);
}
#pragma mark - tap handler
- (void)tap:(UITapGestureRecognizer*)sender {
[self closeKeyBoard];
}
#pragma mark - keyBoard event
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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
|