// // RAChangePasswordViewController.m // Apex And Drivers // // Created by Jack on 2018/9/13. // Copyright © 2018年 USAI. All rights reserved. // #import "RAChangePasswordViewController.h" #import "RAProgressHUD.h" @interface RAChangePasswordViewController () @property (nonatomic,strong) IBOutlet UITextField *oldTextField; @property (nonatomic,strong) IBOutlet UITextField *changeTextField; @property (nonatomic,strong) IBOutlet UITextField *confirmTextField; @property (nonatomic,strong) IBOutlet UIButton *cancelBtn; @property (nonatomic,strong) IBOutlet UIButton *changeBtn; @property (nonatomic,assign) CGFloat offset; @end @implementation RAChangePasswordViewController + (instancetype)viewControllerFromStoryboard { RAChangePasswordViewController *vc = [[UIStoryboard storyboardWithName:@"setting" bundle:nil] instantiateViewControllerWithIdentifier:@"RAChangePasswordViewController"]; vc.preferredContentSize = CGSizeMake(300, 210); return vc; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self registKeyboardListener]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self unregistKeyboardListener]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)registKeyboardListener { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)unregistKeyboardListener { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - Keyboard Listener - (void)keyboardWillChangeFrame:(NSNotification *)notification { CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds); // CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end); CGRect frame = self.view.frame; CGFloat maxY = CGRectGetMaxY(frame); if (end.origin.y >= screenHeight) { frame.origin.y -= self.offset; self.offset = 0; } else { self.offset += end.origin.y - maxY; frame.origin.y += self.offset; } self.view.frame = frame; } #pragma mark - Action - (IBAction)cancelBtnClick:(id)sender { [self.view endEditing:YES]; [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)changeBtnClick:(id)sender { NSString* oldpass = self.oldTextField.text; NSString* newpass = self.changeTextField.text; NSString* confirmpass = self.confirmTextField.text; if(oldpass.length==0||oldpass.length==0||confirmpass.length==0) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Required fields can not be empty!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:action]; [self presentViewController:alertVC animated:YES completion:nil]; return; } if(! [newpass isEqualToString:confirmpass]) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Waning" message:@"New password not equal confirm password!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:action]; [self presentViewController:alertVC animated:YES completion:nil]; return; } self.changeBtn.enabled = NO; self.cancelBtn.enabled = NO; NSString *encryptOldpass = [RASingleton.sharedInstance encryptString:oldpass]; NSString *encryptNewpass = [RASingleton.sharedInstance encryptString:newpass]; RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *json = [RADataProvider requestChange:encryptOldpass password:encryptNewpass]; int result = [[json objectForKey:@"result"] intValue]; dispatch_async(dispatch_get_main_queue(), ^{ [hud dismiss]; self.changeBtn.enabled = YES; self.cancelBtn.enabled = YES; if (result == RESULT_TRUE) { [RASingleton.sharedInstance changePassword:newpass]; UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"change password success" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self dismissViewControllerAnimated:YES completion:nil]; }]; [alertVC addAction:okAction]; [self presentViewController:alertVC animated:YES completion:nil]; } else { NSString *msg = [json objectForKey:@"err_msg"]; if (msg.length == 0) { msg = @"Sorry,something is wrong"; } UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:okAction]; [self presentViewController:alertVC animated:YES completion:nil]; } }); }); } #pragma mark - TextField Delegate - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end