Skip to content
Merged
24 changes: 17 additions & 7 deletions OneTimePasswordExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
<viewController modalPresentationStyle="fullScreen" id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand All @@ -35,7 +35,7 @@
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="filled" title="設定"/>
<connections>
<segue destination="maI-u2-Zio" kind="show" identifier="to setting" id="iaU-Pc-RNd"/>
<segue destination="maI-u2-Zio" kind="show" identifier="viewSegue" id="Cdg-sN-cDv"/>
</connections>
</button>
</subviews>
Expand All @@ -53,23 +53,33 @@
<constraint firstItem="YFg-3N-DhZ" firstAttribute="top" secondItem="aqW-F6-Yvt" secondAttribute="bottom" constant="150" id="s5y-KA-9sU"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="vdE-z6-yiA"/>
<connections>
<outlet property="oneTimePasswordLabel" destination="JX5-NB-jZf" id="cql-S7-e6Z"/>
<outlet property="oneTimePasswordProgressView" destination="aqW-F6-Yvt" id="zK4-rs-OWS"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="18.840579710144929" y="78.348214285714278"/>
<point key="canvasLocation" x="2749.275362318841" y="78.348214285714278"/>
</scene>
<!--Setting Controller-->
<scene sceneID="KmQ-w1-cif">
<objects>
<viewController id="maI-u2-Zio" customClass="SettingController" sceneMemberID="viewController">
<viewController modalPresentationStyle="fullScreen" id="maI-u2-Zio" customClass="SettingController" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" id="DgX-Op-7fC">
<rect key="frame" x="0.0" y="0.0" width="414" height="842"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<button key="tableFooterView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" id="L2b-dO-OP0">
<rect key="frame" x="0.0" y="110.5" width="414" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="filled" title="戻る"/>
<connections>
<segue destination="BYZ-38-t0r" kind="show" identifier="settingSegue" id="ayo-nI-n3c"/>
</connections>
</button>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="SettingsTableViewCell" id="6DC-et-efa" customClass="SettingsTableViewCell">
<rect key="frame" x="0.0" y="44.5" width="414" height="43.5"/>
Expand Down Expand Up @@ -105,11 +115,11 @@
<outlet property="delegate" destination="maI-u2-Zio" id="O1g-UV-F2h"/>
</connections>
</tableView>
<navigationItem key="navigationItem" id="gZv-0X-QNQ"/>
<navigationItem key="navigationItem" id="Q8C-HW-VYN"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="fZU-ge-jmc" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-197.10144927536234" y="-595.3125"/>
<point key="canvasLocation" x="2533.3333333333335" y="-595.3125"/>
</scene>
</scenes>
<resources>
Expand Down
92 changes: 56 additions & 36 deletions OneTimePasswordExample/SettingController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

#import "SettingController.h"
#import "SettingsTableViewCell.h"
#import "ViewController.h"

#pragma mark - Settings item

typedef NS_ENUM(UInt8, SettingsItem) {
SettingsItem1,
SettingsItem2,
SettingsItme3,
AlgorithmItem,
DigitsItem,
PeriodItme,
};

extern SettingsItem SettingsItemUnknown;
Expand All @@ -27,17 +28,19 @@ @interface SettingController () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic) UIPickerView *pickerView;

@property (nonatomic) NSArray *test1Patterns;
@property (nonatomic) NSArray *test2Patterns;
@property (nonatomic) NSArray *test3Patterns;
@property (nonatomic) NSArray *algorithmPatterns;
@property (nonatomic) NSArray *digitsPatterns;
@property (nonatomic) NSArray *periodPatterns;

@property (nonatomic) NSString *test1;
@property (nonatomic) NSString *test2;
@property (nonatomic) NSString *test3;
@property (nonatomic) NSString *algorithm;
@property (nonatomic) NSString *digits;
@property (nonatomic) NSString *period;

@end

@implementation SettingController
@implementation SettingController {
NSIndexPath * pickerIndexPath;
}

- (void)viewDidLoad {
[super viewDidLoad];
Expand All @@ -48,13 +51,13 @@ - (void)viewDidLoad {
self.pickerView.delegate = self;
[self.view addSubview: self.pickerView];

self.test1Patterns = @[@"1", @"2", @"3"];
self.test2Patterns = @[@"4", @"5", @"6"];
self.test3Patterns = @[@"7", @"8", @"9"];
self.algorithmPatterns = @[@"SHA1", @"SHA256", @"SHA512"];
self.digitsPatterns = @[@"4", @"5", @"6", @"7", @"8", @"9", @"10"];
self.periodPatterns = @[@"30", @"60"];

self.test1 = self.test1Patterns[0];
self.test2 = self.test2Patterns[0];
self.test3 = self.test3Patterns[0];
self.algorithm = self.algorithmPatterns[0];
self.digits = self.digitsPatterns[0];
self.period = self.periodPatterns[0];
}

#pragma mark - Table view data source
Expand All @@ -72,16 +75,16 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

switch (indexPath.row) {
case 0:
cell.titleLabel.text = @"TEST1";
cell.valueLabel.text = self.test1;
cell.titleLabel.text = @"アルゴリズム";
cell.valueLabel.text = self.algorithm;
break;
case 1:
cell.titleLabel.text = @"TEST2";
cell.valueLabel.text = self.test2;
cell.titleLabel.text = @"OTP桁数";
cell.valueLabel.text = self.digits;
break;
case 2:
cell.titleLabel.text = @"TEST3";
cell.valueLabel.text = self.test3;
cell.titleLabel.text = @"タイムステップ数";
cell.valueLabel.text = self.period;
break;
}

Expand All @@ -91,9 +94,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.settingsItem = indexPath.row;
[self.pickerView selectRow:0 inComponent:0 animated:true];
[self.pickerView reloadAllComponents];
self.settingsItem = indexPath.row;
}

#pragma mark - Picker view data source
Expand All @@ -103,38 +106,55 @@ - (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
}

- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
switch (self.settingsItem) {
case AlgorithmItem:
return self.algorithmPatterns.count;
case DigitsItem:
return self.digitsPatterns.count;
case PeriodItme:
return self.periodPatterns.count;
default:
return 0;
}
}

#pragma mark - Picker view delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (self.settingsItem) {
case SettingsItem1:
return self.test1Patterns[row];
case SettingsItem2:
return self.test2Patterns[row];
case SettingsItme3:
return self.test3Patterns[row];
case AlgorithmItem:
return self.algorithmPatterns[row];
case DigitsItem:
return self.digitsPatterns[row];
case PeriodItme:
return self.periodPatterns[row];
}

return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (self.settingsItem) {
case SettingsItem1:
self.test1 = self.test1Patterns[row];
case AlgorithmItem:
self.algorithm = self.algorithmPatterns[row];
break;
case SettingsItem2:
self.test2 = self.test2Patterns[row];
case DigitsItem:
self.digits = self.digitsPatterns[row];
break;
case SettingsItme3:
self.test3 = self.test3Patterns[row];
case PeriodItme:
self.period = self.periodPatterns[row];
break;
}

[self.tableView reloadData];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewController *viewController = segue.destinationViewController;
viewController.algorithm = self.algorithm;
viewController.digits = self.digits;
viewController.period = self.period;
}

@end
4 changes: 3 additions & 1 deletion OneTimePasswordExample/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits-badge

逆に意図して入れてある空行は残しておくと良いです。

@property (nonatomic) NSString *algorithm;
@property (nonatomic) NSString *digits;
@property (nonatomic) NSString *period;

@end

24 changes: 20 additions & 4 deletions OneTimePasswordExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import "ViewController.h"
#import "OneTimePassword.h"
#import "SettingController.h"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits-badge

ViewController から SettingController は見てないので、 import しなくて良いはずです。

#import <Base32/MF_Base32Additions.h>
#import <UAProgressView.h>

Expand All @@ -16,20 +17,20 @@ @interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *oneTimePasswordLabel;
@property (weak, nonatomic) IBOutlet UAProgressView *oneTimePasswordProgressView;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits-badge

逆に意図して入れてある空行は残しておくと良いです。

@property (nonatomic, assign) CGFloat localProgress;

@property (nonatomic) float scheduledTimerWithTimeInterval;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits-badge

逆に意図して入れてある空行は残しておくと良いです。

@end

@implementation ViewController


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits-badge

余計な空行はなるべく増やさないでおきましょう。

- (void)viewDidLoad {
[super viewDidLoad];

[self initOneTimePasswordToken];
[self initOneTimePasswordView];

[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(updateOneTimePasswordView:) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:self.scheduledTimerWithTimeInterval target:self selector:@selector(updateOneTimePasswordView:) userInfo:nil repeats:YES];
}

- (void)initOneTimePasswordToken {
Expand All @@ -39,7 +40,21 @@ - (void)initOneTimePasswordToken {

NSData *secretData = [NSData dataWithBase32String:secretString];

self.token = [OTPToken tokenWithType:OTPTokenTypeTimer secret:secretData name:name issuer:issuer ];
self.token = [OTPToken tokenWithType:OTPTokenTypeTimer secret:secretData name:name issuer:issuer];
self.scheduledTimerWithTimeInterval = 0.03f;
if (_algorithm != nil) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next-badge

元がそうなのですが、なるべく self を使うと良いです。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

また nil のケースは考慮したくないので、デフォルト値で初期化できておくと良いと思います。

NSArray *OTPAlgorithmStrings = @[@"SHA1", @"SHA256", @"SHA512"];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next-badge

この辺りは、 SettingController と冗長なので Model を利用して表現できると良いです。

self.token.algorithm = [OTPAlgorithmStrings indexOfObject:_algorithm];
}
if (_digits != nil) {
self.token.digits = [_digits intValue];
}
if (_period != nil) {
self.token.period = [_period intValue];
if([_period isEqualToString:@"60"]) {
self.scheduledTimerWithTimeInterval = 0.06f;
}
Comment on lines +54 to +56
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo-badge

文字列よりも intValue から算出可能なので、以下のように求めつつ、プロパティにせずに直接 NSTimer に渡しても良さそうです。

Suggested change
if([_period isEqualToString:@"60"]) {
self.scheduledTimerWithTimeInterval = 0.06f;
}
self.scheduledTimerWithTimeInterval = self.token.period / 1000;

}
}

- (void)initOneTimePasswordView {
Expand All @@ -64,6 +79,7 @@ - (void)updateOneTimePasswordView:(NSTimer *)timer {

_localProgress = newProgress;
[self.oneTimePasswordProgressView setProgress:_localProgress animated:NO];

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits-badge

余計な空行はなるべく増やさないでおきましょう。

}

@end