RAChangePasswordViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  92. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  93. NSDictionary *json = [RADataProvider requestChange:oldpass password:newpass];
  94. int result = [[json objectForKey:@"result"] intValue];
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. [hud dismiss];
  97. self.changeBtn.enabled = YES;
  98. self.cancelBtn.enabled = YES;
  99. if (result == RESULT_TRUE) {
  100. [RASingleton.sharedInstance changePassword:newpass];
  101. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"change password success" preferredStyle:UIAlertControllerStyleAlert];
  102. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  103. }];
  104. [alertVC addAction:okAction];
  105. [self presentViewController:alertVC animated:YES completion:nil];
  106. } else {
  107. NSString *msg = [json objectForKey:@"err_msg"];
  108. if (msg.length == 0) {
  109. msg = @"Sorry,something is wrong";
  110. }
  111. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:msg preferredStyle:UIAlertControllerStyleAlert];
  112. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  113. }];
  114. [alertVC addAction:okAction];
  115. [self presentViewController:alertVC animated:YES completion:nil];
  116. }
  117. });
  118. });
  119. }
  120. #pragma mark - TextField Delegate
  121. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  122. [[self view] endEditing:YES];
  123. }
  124. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  125. [textField resignFirstResponder];
  126. return YES;
  127. }
  128. @end