18
18
19
19
@interface TOPasscodeSettingsViewController ()
20
20
21
+ // Views
21
22
@property (nonatomic , strong ) UIView *containerView;
22
23
@property (nonatomic , strong ) UILabel *titleLabel;
23
24
@property (nonatomic , strong ) TOPasscodeNumberInputView *numberInputView;
@@ -32,11 +33,26 @@ - (instancetype)initWithStyle:(TOPasscodeSettingsViewStyle)style
32
33
{
33
34
if (self = [self initWithNibName: nil bundle: nil ]) {
34
35
_style = style;
36
+ [self setUp ];
35
37
}
36
38
37
39
return self;
38
40
}
39
41
42
+ - (instancetype )initWithNibName : (NSString *)nibNameOrNil bundle : (NSBundle *)nibBundleOrNil
43
+ {
44
+ if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]) {
45
+ [self setUp ];
46
+ }
47
+
48
+ return self;
49
+ }
50
+
51
+ - (void )setUp
52
+ {
53
+ _failedPasscodeAttemptCount = 0 ;
54
+ }
55
+
40
56
- (void )viewDidLoad {
41
57
[super viewDidLoad ];
42
58
@@ -64,6 +80,7 @@ - (void)viewDidLoad {
64
80
self.numberInputView = [[TOPasscodeNumberInputView alloc ] initWithRequiredLength: 4 ];
65
81
self.numberInputView .tintColor = [UIColor blackColor ];
66
82
self.numberInputView .autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
83
+ self.numberInputView .passcodeCompletedHandler = ^(NSString *passcode) { [weakSelf numberViewDidEnterPasscode: passcode]; };
67
84
[self .numberInputView sizeToFit ];
68
85
[self .containerView addSubview: self .numberInputView];
69
86
@@ -76,10 +93,11 @@ - (void)viewDidLoad {
76
93
self.warningLabel = [[TOPasscodeSettingsWarningLabel alloc ] initWithFrame: CGRectZero];
77
94
self.warningLabel .autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
78
95
self.warningLabel .hidden = YES ;
96
+ [self .warningLabel sizeToFit ];
79
97
[self .containerView addSubview: self .warningLabel];
80
98
81
99
// Add callbacks for the keypad view
82
- self.keypadView .numberButtonTappedHandler = ^(NSInteger number) {
100
+ self.keypadView .numberButtonTappedHandler = ^(NSInteger number) {
83
101
NSString *numberString = [NSString stringWithFormat: @" %ld " , number];
84
102
[weakSelf.numberInputView appendPasscodeCharacters: numberString animated: NO ];
85
103
};
@@ -114,6 +132,38 @@ - (void)viewDidLoad {
114
132
[self applyThemeForStyle: self .style];
115
133
}
116
134
135
+ - (void )viewWillAppear : (BOOL )animated
136
+ {
137
+ [super viewWillAppear: animated];
138
+
139
+ self.state = self.requireCurrentPasscode ? TOPasscodeSettingsViewStateEnterCurrentPassword : TOPasscodeSettingsViewStateEnterNewPassword;
140
+ [self updateViewsForState: self .state];
141
+ }
142
+
143
+ - (void )updateViewsForState : (TOPasscodeSettingsViewState)state
144
+ {
145
+ BOOL confirmingPasscode = state == TOPasscodeSettingsViewStateEnterCurrentPassword;
146
+
147
+ self.warningLabel .hidden = (confirmingPasscode && self.failedPasscodeAttemptCount == 0 );
148
+ self.warningLabel .numberOfWarnings = self.failedPasscodeAttemptCount ;
149
+
150
+ CGRect frame = self.warningLabel .frame ;
151
+ frame.origin .x = (CGRectGetWidth (self.view .frame ) - frame.size .width ) * 0 .5f ;
152
+ self.warningLabel .frame = frame;
153
+
154
+ switch (state) {
155
+ case TOPasscodeSettingsViewStateEnterCurrentPassword:
156
+ self.titleLabel .text = NSLocalizedString(@" Enter your passcode" , @" " );
157
+ break ;
158
+ case TOPasscodeSettingsViewStateEnterNewPassword:
159
+ self.titleLabel .text = NSLocalizedString(@" Enter a new passcode" , @" " );
160
+ break ;
161
+ case TOPasscodeSettingsViewStateConfirmNewPassword:
162
+ self.titleLabel .text = NSLocalizedString(@" Confirm new passcode" , @" " );
163
+ break ;
164
+ }
165
+ }
166
+
117
167
- (void )viewDidLayoutSubviews
118
168
{
119
169
[super viewDidLayoutSubviews ];
@@ -178,4 +228,20 @@ - (void)applyThemeForStyle:(TOPasscodeSettingsViewStyle)style
178
228
}
179
229
}
180
230
231
+ #pragma mark - Data Management -
232
+ - (void )numberViewDidEnterPasscode : (NSString *)passcode
233
+ {
234
+ if (![self .delegate respondsToSelector: @selector (passcodeSettingsViewController:didAttemptCurrentPasscode: )]) {
235
+ return ;
236
+ }
237
+
238
+ BOOL correct = [self .delegate passcodeSettingsViewController: self didAttemptCurrentPasscode: passcode];
239
+ if (!correct) {
240
+ self.failedPasscodeAttemptCount ++;
241
+ [self .numberInputView resetPasscodeAnimated: YES playImpact: YES ];
242
+ }
243
+
244
+ [self updateViewsForState: self .state];
245
+ }
246
+
181
247
@end
0 commit comments