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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//
// ICRModifyPwdViewController.m
// XFFruit
//
// Created by Lili Wang on 15/4/3.
// Copyright (c) 2015年 Xummer. All rights reserved.
//
typedef NS_ENUM(NSUInteger, _InputTextTag) {
kInputOldPwd = 0,
kInputNewPwd,
kInputConfirmPwd,
};
typedef NS_ENUM(NSInteger, ChangePWDError) {
kNoError = 0,
kOldPwdNotMatch,
kNewPwdIsEqualToOld,
kNewPwdILLegal,
kConfirmPwdIsNotMatchNewPwd,
kConfirmPwdILLegal,
kCustomError,
};
static NSString * const ChangePWDErrorDescription[] = {
[ kNoError ] = @"",
[ kOldPwdNotMatch ] = @"Old password is not correct.",
[ kNewPwdIsEqualToOld ] = @"New password is equal to old.",
[ kNewPwdILLegal ] = @"New password is illegal.",
[ kConfirmPwdIsNotMatchNewPwd ] = @"Confirm password is not equal to new.",
[ kConfirmPwdILLegal ] = @"Confirm password is illegal."
};
static NSString *ChangePwdCellID = @"ChangePwdCellID";
#import "ICRModifyPwdViewController.h"
#import "IBTTextFieldCell.h"
#import "IBTTableView.h"
@interface ICRModifyPwdViewController ()
<
UITableViewDataSource,
UITextFieldDelegate
>
{
BOOL m_bIsNewPwdLegal;
BOOL m_bIsConfirmPwdLegal;
BOOL m_bNewIsEqualToOld;
BOOL m_bNewIllegal;
BOOL m_bConfirmNotMatch;
BOOL m_bConfirmIllegal;
ChangePWDError m_eLocalError;
}
@property (weak, nonatomic) UIButton *m_submitBtn;
@property (strong, nonatomic) UILabel *m_indicatorLabel;
@property (weak, nonatomic) UITextField *m_oldPwdTextF;
@property (weak, nonatomic) UITextField *m_newPwdTextF;
@property (weak, nonatomic) UITextField *m_confirmPwdTextF;
@property (strong, nonatomic) NSString *m_nsOldPassword;
@property (strong, nonatomic) NSString *m_nsNewPassword;
@property (strong, nonatomic) NSString *m_nsConfirmPassword;
@property (strong, nonatomic) IBTTableView *m_tableView;
@property (strong, nonatomic) IBTUIView *m_indicatorView;
@end
@implementation ICRModifyPwdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupSubviews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateSubmitButtonForSending:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self addTextFieldObserver];
}
- (void)viewWillDisappear:(BOOL)animated {
[self removeTextFieldObserver];
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Action
- (void)onSubmitAction:(id)sender {
[self updateSubmitButtonForSending:YES];
ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
__weak typeof(self)weakSelf = self;
[httpCtrl doChangePassword:[_m_nsOldPassword uppercaseMD5String]
newPassword:[_m_nsNewPassword uppercaseMD5String]
success:^(id data) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
[[ICRUserUtil sharedInstance] updatePassword:_m_nsNewPassword];
[strongSelf updateSubmitButtonForSending:NO];
[self.navigationController popViewControllerAnimated:YES];
}
failure:^(id data) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
m_eLocalError = kCustomError;
[strongSelf showTopMessage:data];
[strongSelf updateSubmitButtonForSending:NO];
}];
}
#pragma mark - Private Method
- (void)setupSubviews {
self.title = [IBTCommon localizableString:@"Change Password"];
self.m_tableView = [[IBTTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_m_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_m_tableView registerClass:[IBTTextFieldCell class] forCellReuseIdentifier:ChangePwdCellID];
_m_tableView.dataSource = self;
[self.view addSubview:_m_tableView];
UIButton *btn = nil;
UIView *tableFooter = [_m_tableView buttonViewWithTitle:[IBTCommon localizableString:@"Submit"]
color:nil
topGap:32
pointer:&btn
target:self
action:@selector(onSubmitAction:)];
_m_tableView.tableFooterView = tableFooter;
self.m_submitBtn = btn;
}
- (void)showLocalInfoWithErrror:(ChangePWDError)newError {
if (m_eLocalError == newError) {
return;
}
m_eLocalError = newError;
[self showTopMessage:[IBTCommon localizableString:ChangePWDErrorDescription[ m_eLocalError ]]];
}
- (void)updateSubmitButtonForSending:(BOOL)isSending {
_m_submitBtn.enabled = m_bIsNewPwdLegal && m_bIsConfirmPwdLegal;
if (_m_submitBtn.enabled) {
[self showLocalInfoWithErrror:kNoError];
}
_m_submitBtn.userInteractionEnabled = !isSending;
[_m_submitBtn setTitle:[IBTCommon localizableString:isSending ? @"Submiting..." :@"Submit"]
forState:UIControlStateNormal];
}
- (BOOL)checkNewPassword {
m_bNewIsEqualToOld = [self.m_nsNewPassword isEqualToString:self.m_nsOldPassword];
if (m_bNewIsEqualToOld) {
return m_bIsNewPwdLegal = NO;
}
m_bNewIllegal = ![IBTCommon isLegalString:_m_nsNewPassword WithRegex:IBT_PASSWORD_REGEX];
if (m_bNewIllegal) {
return m_bIsConfirmPwdLegal = NO;
}
return m_bIsNewPwdLegal = YES;
}
- (BOOL)checkConfirmPassword {
m_bConfirmNotMatch = ![self.m_nsConfirmPassword isEqualToString:self.m_nsNewPassword];
if (m_bConfirmNotMatch) {
return m_bIsConfirmPwdLegal = NO;
}
m_bConfirmIllegal = ![IBTCommon isLegalString:_m_nsConfirmPassword WithRegex:IBT_PASSWORD_REGEX];
if (m_bConfirmIllegal) {
return m_bIsConfirmPwdLegal = NO;
}
return m_bIsConfirmPwdLegal = YES;
}
- (void)showTopMessage:(NSString *)message {
if (![message isKindOfClass:[NSString class]]) {
return;
}
if ([message length] == 0) {
_m_tableView.tableHeaderView = nil;
return;
}
if (!_m_indicatorView) {
self.m_indicatorView = [[IBTUIView alloc] init];
_m_indicatorView.backgroundColor = [UIColor clearColor];
self.m_indicatorLabel = [[UILabel alloc] init];
_m_indicatorLabel.textColor = [UIColor redColor];
_m_indicatorLabel.textAlignment = NSTextAlignmentLeft;
_m_indicatorLabel.font = [UIFont systemFontOfSize:17];
_m_indicatorLabel.numberOfLines = 0;
[_m_indicatorView addSubview:_m_indicatorLabel];
}
_m_indicatorLabel.text = message;
CGFloat xPadding = 10;
CGFloat yPadding = 5;
CGFloat w = _m_tableView.width - 2 * xPadding;
_m_indicatorLabel.frame = (CGRect){
.origin.x = xPadding,
.origin.y = yPadding,
.size.width = w,
.size.height = [UILabel getHeightWithText:_m_indicatorLabel.text
font:_m_indicatorLabel.font
andWidth:w]
};
CGFloat h = CGRectGetHeight(_m_indicatorLabel.frame) + 2 * yPadding;
w = _m_tableView.width;
if (IBT_IOS7_OR_LATER) {
h = MAX(h-35, 1);
}
_m_indicatorView.frame = (CGRect){
.origin.x = 0,
.origin.y = 0,
.size.width = w,
.size.height = h
};
_m_tableView.tableHeaderView = _m_indicatorView;
}
- (IBTTextFieldCell *)cellForInputTextTag:(_InputTextTag)tag {
NSIndexPath *indexP = [NSIndexPath indexPathForRow:tag inSection:0];
UITableViewCell *cell = [_m_tableView cellForRowAtIndexPath:indexP];
IBTTextFieldCell *mCell = nil;
if ([cell isKindOfClass:[IBTTextFieldCell class]]) {
mCell = (IBTTextFieldCell *)cell;
}
return mCell;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:ChangePwdCellID forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
IBTTextFieldCell *mCell = (IBTTextFieldCell *)cell;
UITextField *inputTxtF = mCell.textField;
inputTxtF.secureTextEntry = YES;
inputTxtF.tag = indexPath.row;
switch (indexPath.row) {
case kInputOldPwd:
mCell.textLabel.text = [IBTCommon localizableString:@"Old"];
inputTxtF.placeholder = [IBTCommon localizableString:@"Required"];
inputTxtF.text = _m_nsOldPassword;
inputTxtF.delegate = self;
inputTxtF.returnKeyType = UIReturnKeyNext;
self.m_oldPwdTextF = inputTxtF;
if ([_m_nsOldPassword length] > 0) {
mCell.textIsIllegal = NO;
}
break;
case kInputNewPwd:
mCell.textLabel.text = [IBTCommon localizableString:@"New"];
inputTxtF.placeholder = [IBTCommon localizableString:@"Required"];
inputTxtF.text = _m_nsNewPassword;
inputTxtF.delegate = self;
inputTxtF.returnKeyType = UIReturnKeyNext;
self.m_newPwdTextF = inputTxtF;
if ([_m_nsNewPassword length] > 0) {
mCell.textIsIllegal = ![self checkNewPassword];
}
break;
case kInputConfirmPwd:
mCell.textLabel.text = [IBTCommon localizableString:@"Confirm"];
inputTxtF.placeholder = [IBTCommon localizableString:@"Required"];
inputTxtF.text = _m_nsConfirmPassword;
inputTxtF.delegate = self;
inputTxtF.returnKeyType = UIReturnKeyDone;
self.m_confirmPwdTextF = inputTxtF;
if ([_m_nsConfirmPassword length] > 0) {
mCell.textIsIllegal = ![self checkConfirmPassword];
}
break;
default:
break;
}
}
#pragma mark - TextObserver
- (void)addTextFieldObserver {
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
[notiCenter addObserver:self
selector:@selector(inputTextEditChanged:)
name:UITextFieldTextDidChangeNotification
object:nil];
}
- (void)removeTextFieldObserver {
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
[notiCenter removeObserver:self
name:UITextFieldTextDidChangeNotification
object:nil];
}
- (void)inputTextEditChanged:(NSNotification *)obj{
UITextField *textField = obj.object;
IBTTextFieldCell *mCell = [self cellForInputTextTag:textField.tag];
switch (textField.tag) {
case kInputOldPwd:
{
self.m_nsOldPassword = textField.text;
mCell.textIsIllegal = NO;
}
break;
case kInputNewPwd:
{
self.m_nsNewPassword = textField.text;
mCell.textIsIllegal = ![self checkNewPassword];
if ([self.m_nsConfirmPassword length] > 0) {
IBTTextFieldCell *confirmCell = [self cellForInputTextTag:kInputConfirmPwd];
confirmCell.textIsIllegal = ![self checkConfirmPassword];
}
}
break;
case kInputConfirmPwd:
{
self.m_nsConfirmPassword = textField.text;
mCell.textIsIllegal = ![self checkConfirmPassword];
}
break;
default:
break;
}
[self updateSubmitButtonForSending:NO];
}
#pragma mark - Keyboard Notification
- (void)keyboardWillShow:(NSNotification *)note {
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
[_m_tableView setContentInsetTop:_m_tableView.contentInset.top andBottom:CGRectGetHeight(keyboardBounds)];
// commit animations
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)note {
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
[_m_tableView setContentInsetTop:_m_tableView.contentInset.top andBottom:0];
// commit animations
[UIView commitAnimations];
}
@end