RAChangePasswordViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // RAChangePasswordViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/9/13.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAChangePasswordViewController.h"
  9. #import "RAProgressHUD.h"
  10. @interface RAChangePasswordViewController () <UITextFieldDelegate>
  11. @property (nonatomic,strong) IBOutlet UITextField *oldTextField;
  12. @property (nonatomic,strong) IBOutlet UITextField *changeTextField;
  13. @property (nonatomic,strong) IBOutlet UITextField *confirmTextField;
  14. @property (nonatomic,strong) IBOutlet UIButton *cancelBtn;
  15. @property (nonatomic,strong) IBOutlet UIButton *changeBtn;
  16. @property (nonatomic,assign) CGFloat offset;
  17. @end
  18. @implementation RAChangePasswordViewController
  19. + (instancetype)viewControllerFromStoryboard {
  20. RAChangePasswordViewController *vc = [[UIStoryboard storyboardWithName:@"setting" bundle:nil] instantiateViewControllerWithIdentifier:@"RAChangePasswordViewController"];
  21. vc.preferredContentSize = CGSizeMake(300, 210);
  22. return vc;
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. }
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. [self registKeyboardListener];
  31. }
  32. - (void)viewWillDisappear:(BOOL)animated {
  33. [super viewWillDisappear:animated];
  34. [self unregistKeyboardListener];
  35. }
  36. - (void)didReceiveMemoryWarning {
  37. [super didReceiveMemoryWarning];
  38. // Dispose of any resources that can be recreated.
  39. }
  40. - (void)registKeyboardListener {
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  42. }
  43. - (void)unregistKeyboardListener {
  44. [[NSNotificationCenter defaultCenter] removeObserver:self];
  45. }
  46. #pragma mark - Keyboard Listener
  47. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  48. CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  49. CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
  50. // CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
  51. CGRect frame = self.view.frame;
  52. CGFloat maxY = CGRectGetMaxY(frame);
  53. if (end.origin.y >= screenHeight) {
  54. frame.origin.y -= self.offset;
  55. self.offset = 0;
  56. } else {
  57. self.offset += end.origin.y - maxY;
  58. frame.origin.y += self.offset;
  59. }
  60. self.view.frame = frame;
  61. }
  62. #pragma mark - Action
  63. - (IBAction)cancelBtnClick:(id)sender {
  64. [self.view endEditing:YES];
  65. [self dismissViewControllerAnimated:YES completion:nil];
  66. }
  67. - (IBAction)changeBtnClick:(id)sender {
  68. NSString* oldpass = self.oldTextField.text;
  69. NSString* newpass = self.changeTextField.text;
  70. NSString* confirmpass = self.confirmTextField.text;
  71. if(oldpass.length==0||oldpass.length==0||confirmpass.length==0)
  72. {
  73. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Required fields can not be empty!" preferredStyle:UIAlertControllerStyleAlert];
  74. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  75. }];
  76. [alertVC addAction:action];
  77. [self presentViewController:alertVC animated:YES completion:nil];
  78. return;
  79. }
  80. if(! [newpass isEqualToString:confirmpass])
  81. {
  82. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Waning" message:@"New password not equal confirm password!" preferredStyle:UIAlertControllerStyleAlert];
  83. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  84. }];
  85. [alertVC addAction:action];
  86. [self presentViewController:alertVC animated:YES completion:nil];
  87. return;
  88. }
  89. self.changeBtn.enabled = NO;
  90. self.cancelBtn.enabled = NO;
  91. NSString *encryptOldpass = [RASingleton.sharedInstance encryptString:oldpass];
  92. NSString *encryptNewpass = [RASingleton.sharedInstance encryptString:newpass];
  93. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  94. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  95. NSDictionary *json = [RADataProvider requestChange:encryptOldpass password:encryptNewpass];
  96. int result = [[json objectForKey:@"result"] intValue];
  97. dispatch_async(dispatch_get_main_queue(), ^{
  98. [hud dismiss];
  99. self.changeBtn.enabled = YES;
  100. self.cancelBtn.enabled = YES;
  101. if (result == RESULT_TRUE) {
  102. [RASingleton.sharedInstance changePassword:newpass];
  103. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"change password success" preferredStyle:UIAlertControllerStyleAlert];
  104. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  105. [self dismissViewControllerAnimated:YES completion:nil];
  106. }];
  107. [alertVC addAction:okAction];
  108. [self presentViewController:alertVC animated:YES completion:nil];
  109. } else {
  110. NSString *msg = [json objectForKey:@"err_msg"];
  111. if (msg.length == 0) {
  112. msg = @"Sorry,something is wrong";
  113. }
  114. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:msg preferredStyle:UIAlertControllerStyleAlert];
  115. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  116. }];
  117. [alertVC addAction:okAction];
  118. [self presentViewController:alertVC animated:YES completion:nil];
  119. }
  120. });
  121. });
  122. }
  123. #pragma mark - TextField Delegate
  124. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  125. [[self view] endEditing:YES];
  126. }
  127. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  128. [textField resignFirstResponder];
  129. return YES;
  130. }
  131. @end