Skip to content

Commit 0ebf058

Browse files
committed
Merge branch 'advanced-options'
2 parents 4a5c2f0 + 9c5b2e6 commit 0ebf058

File tree

2 files changed

+192
-27
lines changed

2 files changed

+192
-27
lines changed

Authenticator/Classes/OTPTokenEditViewController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ - (void)updateToken
8787

8888
#pragma mark - UITableViewDataSource
8989

90+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
91+
{
92+
return 1;
93+
}
94+
9095
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
9196
{
9297
return 2;

Authenticator/Classes/OTPTokenEntryViewController.m

Lines changed: 187 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,44 @@
2929
#import <Base32/MF_Base32Additions.h>
3030

3131

32+
typedef enum : NSUInteger {
33+
OTPTokenEntrySectionBasic,
34+
OTPTokenEntrySectionAdvanced,
35+
OTPNumberOfTokenEntrySections,
36+
} OTPTokenEntrySection;
37+
38+
typedef enum : NSUInteger {
39+
OTPTokenEntryBasicRowType,
40+
OTPTokenEntryBasicRowIssuer,
41+
OTPTokenEntryBasicRowName,
42+
OTPTokenEntryBasicRowSecret,
43+
OTPNumberOfTokenEntryBasicRows,
44+
} OTPTokenEntryBasicRow;
45+
46+
typedef enum : NSUInteger {
47+
OTPTokenEntryAdvancedRowDigits,
48+
OTPTokenEntryAdvancedRowAlgorithm,
49+
OTPNumberOfTokenEntryAdvancedRows,
50+
} OTPTokenEntryAdvancedRow;
51+
52+
typedef enum : NSUInteger {
53+
OTPTokenTypeIndexTimer,
54+
OTPTokenTypeIndexCounter,
55+
} OTPTokenTypeIndex;
56+
57+
typedef enum : NSUInteger {
58+
OTPTokenDigitsIndex6,
59+
OTPTokenDigitsIndex7,
60+
OTPTokenDigitsIndex8,
61+
} OTPTokenDigitsIndex;
62+
63+
typedef enum : NSUInteger {
64+
OTPTokenAlgorithmIndexSHA1,
65+
OTPTokenAlgorithmIndexSHA256,
66+
OTPTokenAlgorithmIndexSHA512,
67+
} OTPTokenAlgorithmIndex;
68+
69+
3270
@interface OTPTokenEntryViewController ()
3371
<UITextFieldDelegate>
3472

@@ -39,11 +77,20 @@ @interface OTPTokenEntryViewController ()
3977
@property (nonatomic, strong) OTPTextFieldCell *accountNameCell;
4078
@property (nonatomic, strong) OTPTextFieldCell *secretKeyCell;
4179

80+
@property (nonatomic) BOOL showsAdvancedOptions;
81+
@property (nonatomic, strong) OTPSegmentedControlCell *digitCountCell;
82+
@property (nonatomic, strong) OTPSegmentedControlCell *algorithmCell;
83+
4284
@end
4385

4486

4587
@implementation OTPTokenEntryViewController
4688

89+
- (instancetype)init
90+
{
91+
return [super initWithStyle:UITableViewStyleGrouped];
92+
}
93+
4794
- (void)viewDidLoad
4895
{
4996
[super viewDidLoad];
@@ -79,12 +126,36 @@ - (void)createToken
79126
NSData *secret = [NSData dataWithBase32String:self.secretKeyCell.textField.text];
80127

81128
if (secret.length) {
82-
OTPTokenType tokenType = (self.tokenTypeCell.segmentedControl.selectedSegmentIndex == 0) ? OTPTokenTypeTimer : OTPTokenTypeCounter;
129+
OTPTokenType tokenType = (self.tokenTypeCell.segmentedControl.selectedSegmentIndex == OTPTokenTypeIndexTimer) ? OTPTokenTypeTimer : OTPTokenTypeCounter;
83130
OTPToken *token = [OTPToken tokenWithType:tokenType
84131
secret:secret
85132
name:self.accountNameCell.textField.text
86133
issuer:self.issuerCell.textField.text];
87134

135+
switch (self.digitCountCell.segmentedControl.selectedSegmentIndex) {
136+
case OTPTokenDigitsIndex6:
137+
token.digits = 6;
138+
break;
139+
case OTPTokenDigitsIndex7:
140+
token.digits = 7;
141+
break;
142+
case OTPTokenDigitsIndex8:
143+
token.digits = 8;
144+
break;
145+
}
146+
147+
switch (self.algorithmCell.segmentedControl.selectedSegmentIndex) {
148+
case OTPTokenAlgorithmIndexSHA1:
149+
token.algorithm = OTPAlgorithmSHA1;
150+
break;
151+
case OTPTokenAlgorithmIndexSHA256:
152+
token.algorithm = OTPAlgorithmSHA256;
153+
break;
154+
case OTPTokenAlgorithmIndexSHA512:
155+
token.algorithm = OTPAlgorithmSHA512;
156+
break;
157+
}
158+
88159
if (token.password) {
89160
id <OTPTokenSourceDelegate> delegate = self.delegate;
90161
[delegate tokenSource:self didCreateToken:token];
@@ -99,37 +170,69 @@ - (void)createToken
99170

100171
#pragma mark - UITableViewDataSource
101172

173+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
174+
{
175+
return OTPNumberOfTokenEntrySections;
176+
}
177+
102178
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
103179
{
104-
return 4;
180+
switch (section) {
181+
case OTPTokenEntrySectionBasic:
182+
return OTPNumberOfTokenEntryBasicRows;
183+
case OTPTokenEntrySectionAdvanced:
184+
return self.showsAdvancedOptions ? OTPNumberOfTokenEntryAdvancedRows : 0;
185+
}
186+
return 0;
105187
}
106188

107189
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
108190
{
109-
switch (indexPath.row) {
110-
case 0:
111-
return self.tokenTypeCell;
112-
case 1:
113-
return self.issuerCell;
114-
case 2:
115-
return self.accountNameCell;
116-
case 3:
117-
return self.secretKeyCell;
191+
switch (indexPath.section) {
192+
case OTPTokenEntrySectionBasic:
193+
switch (indexPath.row) {
194+
case OTPTokenEntryBasicRowType:
195+
return self.tokenTypeCell;
196+
case OTPTokenEntryBasicRowIssuer:
197+
return self.issuerCell;
198+
case OTPTokenEntryBasicRowName:
199+
return self.accountNameCell;
200+
case OTPTokenEntryBasicRowSecret:
201+
return self.secretKeyCell;
202+
}
203+
break;
204+
case OTPTokenEntrySectionAdvanced:
205+
switch (indexPath.row) {
206+
case OTPTokenEntryAdvancedRowDigits:
207+
return self.digitCountCell;
208+
case OTPTokenEntryAdvancedRowAlgorithm:
209+
return self.algorithmCell;
210+
}
211+
break;
118212
}
119213
return nil;
120214
}
121215

122216
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
123217
{
124-
switch (indexPath.row) {
125-
case 0:
126-
return 44;
127-
case 1:
128-
return 74;
129-
case 2:
130-
return 74;
131-
case 3:
132-
return 74;
218+
switch (indexPath.section) {
219+
case OTPTokenEntrySectionBasic:
220+
switch (indexPath.row) {
221+
case OTPTokenEntryBasicRowType:
222+
return 44;
223+
case OTPTokenEntryBasicRowIssuer:
224+
case OTPTokenEntryBasicRowName:
225+
case OTPTokenEntryBasicRowSecret:
226+
return 74;
227+
}
228+
break;
229+
case OTPTokenEntrySectionAdvanced:
230+
switch (indexPath.row) {
231+
case OTPTokenEntryAdvancedRowDigits:
232+
case OTPTokenEntryAdvancedRowAlgorithm:
233+
return 54;
234+
}
235+
break;
133236
}
134237
return 0;
135238
}
@@ -140,18 +243,18 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
140243
- (OTPSegmentedControlCell *)tokenTypeCell
141244
{
142245
if (!_tokenTypeCell) {
143-
_tokenTypeCell = [OTPSegmentedControlCell cellForTableView:self.tableView];
144-
[_tokenTypeCell.segmentedControl insertSegmentWithTitle:@"Time Based" atIndex:0 animated:NO];
145-
[_tokenTypeCell.segmentedControl insertSegmentWithTitle:@"Counter Based" atIndex:1 animated:NO];
146-
_tokenTypeCell.segmentedControl.selectedSegmentIndex = 0;
246+
_tokenTypeCell = [OTPSegmentedControlCell cell];
247+
[_tokenTypeCell.segmentedControl insertSegmentWithTitle:@"Time Based" atIndex:OTPTokenTypeIndexTimer animated:NO];
248+
[_tokenTypeCell.segmentedControl insertSegmentWithTitle:@"Counter Based" atIndex:OTPTokenTypeIndexCounter animated:NO];
249+
_tokenTypeCell.segmentedControl.selectedSegmentIndex = OTPTokenTypeIndexTimer;
147250
}
148251
return _tokenTypeCell;
149252
}
150253

151254
- (OTPTextFieldCell *)issuerCell
152255
{
153256
if (!_issuerCell) {
154-
_issuerCell = [OTPTextFieldCell cellForTableView:self.tableView];
257+
_issuerCell = [OTPTextFieldCell cell];
155258
_issuerCell.textLabel.text = @"Issuer";
156259
_issuerCell.textField.placeholder = @"Some Website";
157260
_issuerCell.textField.delegate = self;
@@ -163,7 +266,7 @@ - (OTPTextFieldCell *)issuerCell
163266
- (OTPTextFieldCell *)accountNameCell
164267
{
165268
if (!_accountNameCell) {
166-
_accountNameCell = [OTPTextFieldCell cellForTableView:self.tableView];
269+
_accountNameCell = [OTPTextFieldCell cell];
167270
_accountNameCell.textLabel.text = @"Account Name";
168271
_accountNameCell.textField.placeholder = @"user@example.com";
169272
_accountNameCell.textField.delegate = self;
@@ -178,7 +281,7 @@ - (OTPTextFieldCell *)accountNameCell
178281
- (OTPTextFieldCell *)secretKeyCell
179282
{
180283
if (!_secretKeyCell) {
181-
_secretKeyCell = [OTPTextFieldCell cellForTableView:self.tableView];
284+
_secretKeyCell = [OTPTextFieldCell cell];
182285
_secretKeyCell.textLabel.text = @"Secret Key";
183286
_secretKeyCell.textField.placeholder = @"•••• •••• •••• ••••";
184287
_secretKeyCell.textField.delegate = self;
@@ -189,6 +292,30 @@ - (OTPTextFieldCell *)secretKeyCell
189292
return _secretKeyCell;
190293
}
191294

295+
- (OTPSegmentedControlCell *)digitCountCell
296+
{
297+
if (!_digitCountCell) {
298+
_digitCountCell = [OTPSegmentedControlCell cell];
299+
[_digitCountCell.segmentedControl insertSegmentWithTitle:@"6 Digits" atIndex:OTPTokenDigitsIndex6 animated:NO];
300+
[_digitCountCell.segmentedControl insertSegmentWithTitle:@"7 Digits" atIndex:OTPTokenDigitsIndex7 animated:NO];
301+
[_digitCountCell.segmentedControl insertSegmentWithTitle:@"8 Digits" atIndex:OTPTokenDigitsIndex8 animated:NO];
302+
_digitCountCell.segmentedControl.selectedSegmentIndex = OTPTokenDigitsIndex6;
303+
}
304+
return _digitCountCell;
305+
}
306+
307+
- (OTPSegmentedControlCell *)algorithmCell
308+
{
309+
if (!_algorithmCell) {
310+
_algorithmCell = [OTPSegmentedControlCell cell];
311+
[_algorithmCell.segmentedControl insertSegmentWithTitle:@"SHA-1" atIndex:OTPTokenAlgorithmIndexSHA1 animated:NO];
312+
[_algorithmCell.segmentedControl insertSegmentWithTitle:@"SHA-256" atIndex:OTPTokenAlgorithmIndexSHA256 animated:NO];
313+
[_algorithmCell.segmentedControl insertSegmentWithTitle:@"SHA-512" atIndex:OTPTokenAlgorithmIndexSHA512 animated:NO];
314+
_algorithmCell.segmentedControl.selectedSegmentIndex = OTPTokenAlgorithmIndexSHA1;
315+
}
316+
return _algorithmCell;
317+
}
318+
192319

193320
#pragma mark - UITableViewDelegate
194321

@@ -204,6 +331,39 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce
204331
}
205332
}
206333

334+
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
335+
{
336+
if (section == OTPTokenEntrySectionAdvanced) {
337+
return 54;
338+
}
339+
return 1;
340+
}
341+
342+
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
343+
{
344+
if (section == OTPTokenEntrySectionAdvanced) {
345+
UIButton *headerView = [UIButton new];
346+
[headerView setTitle:@"Advanced Options" forState:UIControlStateNormal];
347+
headerView.titleLabel.textAlignment = NSTextAlignmentCenter;
348+
headerView.titleLabel.textColor = [UIColor otpForegroundColor];
349+
headerView.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:16];
350+
[headerView addTarget:self action:@selector(revealAdvancedOptions) forControlEvents:UIControlEventTouchUpInside];
351+
return headerView;
352+
}
353+
return nil;
354+
}
355+
356+
- (void)revealAdvancedOptions
357+
{
358+
if (!self.showsAdvancedOptions) {
359+
self.showsAdvancedOptions = YES;
360+
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:OTPTokenEntrySectionAdvanced] withRowAnimation:UITableViewRowAnimationAutomatic];
361+
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(OTPNumberOfTokenEntryAdvancedRows - 1)
362+
inSection:OTPTokenEntrySectionAdvanced]
363+
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
364+
}
365+
}
366+
207367

208368
#pragma mark - UITextFieldDelegate
209369

0 commit comments

Comments
 (0)