-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordRecoveryVC.m
More file actions
executable file
·241 lines (192 loc) · 7.63 KB
/
PasswordRecoveryVC.m
File metadata and controls
executable file
·241 lines (192 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// PasswordRecoveryVC.m
//
// Copyright © 2015 Lodossteam. All rights reserved.
//
#import "PasswordRecoveryVC.h"
#import "NetworkManager.h"
#import "NSString+Extensions.h"
#import "ColorsManager.h"
#import "DataValidator.h"
@interface PasswordRecoveryVC () <UITextViewDelegate, UITextFieldDelegate>
- (IBAction)backButtonPress:(UIButton *)sender;
- (IBAction)resetPasswordButtonPress:(UIButton *)sender;
- (IBAction)editFieldTextChanged:(UITextField *)sender;
@property (strong, nonatomic) IBOutlet UITextField *emailAdressTextField;
@property (strong, nonatomic) IBOutlet UIButton *resetPasswordButton;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *errorMessagesOutputLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainTopLabel;
@property (strong, nonatomic) IBOutlet UIView *emailAdressTextFieldUnderliner;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *resetPasswordButtonTopMargin;
@property (strong, nonatomic) IBOutlet UIButton *emailConfirmationButton;
@property (strong, nonatomic) IBOutlet UILabel *emailWarningLabel;
@end
@implementation PasswordRecoveryVC
#pragma mark - VC Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self setupButtonsStyle];
[self setupInputIndicatorColors];
[self setupFieldsTextChangeHandlers];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
#pragma mark - Init
- (void)setupButtonsStyle {
[self setupResetPasswordButtonStyle];
}
- (void)setupResetPasswordButtonStyle {
self.resetPasswordButton.layer.cornerRadius = self.resetPasswordButton.frame.size.height / 6;
}
- (void)setupInputIndicatorColors {
UIColor *inputFieldInputIndicatorColor = [ColorsManager blue2D87D7AppMainColor];
self.emailAdressTextField.tintColor = inputFieldInputIndicatorColor;
}
- (void)setupFieldsTextChangeHandlers {
[self.emailAdressTextField addTarget:self
action:@selector(editFieldTextChanged:)
forControlEvents:UIControlEventEditingChanged];
[self editFieldTextChanged:nil];
}
#pragma mark - IBActions
- (IBAction)backButtonPress:(UIButton *)sender {
[self dismissView];
}
- (IBAction)resetPasswordButtonPress:(UIButton *)sender {
[self resetErrorMessagesOutput];
[self resetPassword];
}
- (IBAction)editFieldTextChanged:(UITextField *)sender {
BOOL isCanEnableMainButton = (![NSString isNilOrEmpty:self.emailAdressTextField.text]);
if (isCanEnableMainButton) {
isCanEnableMainButton = [DataValidator stringIsValidEmail:_emailAdressTextField.text];
}
if (isCanEnableMainButton) {
if (!_emailWarningLabel.hidden) {
_emailWarningLabel.hidden = YES;
[self updateConstraintsAndColor];
[self updateConfirmImages];
}
self.resetPasswordButton.enabled = YES;
self.resetPasswordButton.alpha = 1;
} else {
self.resetPasswordButton.enabled = NO;
self.resetPasswordButton.alpha = 0.5;
}
}
#pragma mark - Actions
- (void)resetPassword {
NSString *email = self.emailAdressTextField.text;
[self resetPasswordForEmail:email];
}
- (void)resetPasswordForEmail:(NSString *)email {
UIActivityIndicatorView *indicator = [self disableLoginControls];
__weak __typeof(self)weakSelf = self;
[NetworkManager passwordRecoveryForEmail:email success:^(NSDictionary *resonseDict) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf enableLoginControls:indicator];
[strongSelf successfullySendRecovery:^{
}];
});
} error:^(NSString *errorDescriptionText, NSInteger HTTPstatusCode, NSString *serverErrorCode, NSDictionary *serverResponseDict) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf enableLoginControls:indicator];
[strongSelf setErrorMessagesText:errorDescriptionText];
});
} cleanup:^{}];
}
- (void)successfullySendRecovery:(void (^)())cleanup {
self.emailAdressTextField.enabled = NO;
self.resetPasswordButton.hidden = YES;
self.emailAdressTextFieldUnderliner.hidden = YES;
self.mainTopLabel.text = kSuccessfulSendTiEMailMessage;
}
- (void)resetErrorMessagesOutput {
self.errorMessagesOutputLabel.text = @"";
}
- (void)setErrorMessagesText:(NSString *)errorMessage {
if ([errorMessage isEqualToString:kNoSuchUserErrorMsg]) {
_emailWarningLabel.text = kNotRegisteredEmailWarning;
_emailWarningLabel.hidden = NO;
self.resetPasswordButton.enabled = NO;
self.resetPasswordButton.alpha = 0.5;
[self updateConstraintsAndColor];
[self updateConfirmImages];
}
}
- (UIActivityIndicatorView *)disableLoginControls {
self.emailAdressTextField.enabled = NO;
self.resetPasswordButton.enabled = NO;
self.resetPasswordButton.alpha = 0.5;
CGRect defFrame = CGRectMake(0, 0, 50, 50);
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:defFrame];
indicator.center = CGPointMake((self.resetPasswordButton.center.x / 2), self.resetPasswordButton.center.y);
indicator.hidesWhenStopped = YES;
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.scrollView addSubview:indicator];
[indicator startAnimating];
return indicator;
}
- (void)enableLoginControls:(UIActivityIndicatorView *)indicator {
self.emailAdressTextField.enabled = YES;
self.resetPasswordButton.enabled = YES;
self.resetPasswordButton.alpha = 1;
[indicator stopAnimating];
[indicator removeFromSuperview];
}
- (void) updateConstraintsAndColor {
if (_emailWarningLabel.hidden) {
_emailAdressTextFieldUnderliner.backgroundColor = [ColorsManager grayE1E3E0TextViewBorder];
} else {
if ([_emailWarningLabel.text isEqualToString:kNotRegisteredEmailWarning]) {
_emailAdressTextFieldUnderliner.backgroundColor = [ColorsManager redEC6A5D];
} else {
_emailAdressTextFieldUnderliner.backgroundColor = [ColorsManager redEC6A5D];
}
}
}
- (void) updateConfirmImages {
if (_emailWarningLabel.hidden) {
if ([NSString isNilOrEmpty:_emailAdressTextField.text]) {
_emailConfirmationButton.hidden = YES;
} else {
[_emailConfirmationButton setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateNormal];
_emailConfirmationButton.hidden = NO;
}
} else {
[_emailConfirmationButton setImage:[UIImage imageNamed:@"Warning"] forState:UIControlStateNormal];
_emailConfirmationButton.hidden = NO;
}
}
#pragma mark - Other
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if ([_emailWarningLabel.text isEqualToString:kNotRegisteredEmailWarning]) {
_emailWarningLabel.hidden = YES;
[self updateConstraintsAndColor];
[self updateConfirmImages];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (![_emailAdressTextField.text isEqualToString:@""]) {
if ([DataValidator stringIsValidEmail:_emailAdressTextField.text]) {
_emailWarningLabel.hidden = YES;
} else {
_emailWarningLabel.text = kInvalidEmailWarning;
_emailWarningLabel.hidden = NO;
}
}
[self updateConstraintsAndColor];
[self updateConfirmImages];
}
#pragma mark - Segues
- (void)dismissView {
[self.navigationController popViewControllerAnimated:YES];
}
@end