Commit 51658bd1 authored by Gleb Pinigin's avatar Gleb Pinigin

Replace passcode view controller with a nice one

parent 6f68321d
......@@ -7,11 +7,13 @@
//
#import <UIKit/UIKit.h>
#import "VLCPlaylistViewController.h"
@class VLCPlaylistViewController;
@class PAPasscodeViewController;
@interface VLCAppDelegate : UIResponder <UIApplicationDelegate>
{
NSURL *_tempURL;
PAPasscodeViewController *_passcodeLockController;
}
- (void)updateMediaList;
......@@ -22,4 +24,6 @@
@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, retain) NSDate *nextPasscodeCheckDate;
@end
......@@ -9,8 +9,14 @@
#import "VLCAppDelegate.h"
#import "VLCPlaylistViewController.h"
#import "VLCMovieViewController.h"
#import "PAPasscodeViewController.h"
@interface VLCAppDelegate () <PAPasscodeViewControllerDelegate>
@property (nonatomic) BOOL passcodeValidated;
@end
@implementation VLCAppDelegate
......@@ -77,13 +83,7 @@
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults objectForKey:@"Passcode"] isEqualToString:@""])
self.playlistViewController.passcodeValidated = NO;
else
self.playlistViewController.passcodeValidated = YES;
NSLog(@"applicationWillEnterForeground: %i", self.playlistViewController.passcodeValidated);
NSLog(@"applicationWillEnterForeground: %i", self.passcodeValidated);
}
- (void)applicationDidBecomeActive:(UIApplication *)application
......@@ -91,6 +91,11 @@
[self updateMediaList];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self validatePasscode]; // Lock library when going to background
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
......@@ -121,4 +126,43 @@
[_playlistViewController updateViewContents];
}
#pragma mark - pass code validation
- (void)validatePasscode
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
if ([passcode isEqualToString:@""]) {
self.passcodeValidated = YES;
return;
}
if (!self.passcodeValidated) {
if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
_passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
_passcodeLockController.delegate = self;
_passcodeLockController.passcode = passcode;
self.window.rootViewController = _passcodeLockController;
} else
self.passcodeValidated = YES;
}
}
- (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
{
// TODO remove cancel button for Enter action
}
- (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
{
// TODO add transition animation
self.window.rootViewController = self.navigationController;
}
- (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
{
// TODO handle error attempts
}
@end
//
// VLCPasscodeLockViewController.h
// VLC for iOS
//
// Created by Felix Paul Kühne on 18.05.13.
// Copyright (c) 2013 VideoLAN. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface VLCPasscodeLockViewController : UIViewController
{
NSString *_passcode;
NSString *_tmpPasscode;
NSUInteger _resetStage;
}
@property (nonatomic, strong) IBOutlet UILabel *enterPasscodeLabel;
@property (nonatomic, strong) IBOutlet UITextField *enterCodeField;
- (IBAction)textFieldValueChanged:(id)sender;
- (void)resetPasscode;
@end
//
// VLCPasscodeLockViewController.m
// VLC for iOS
//
// Created by Felix Paul Kühne on 18.05.13.
// Copyright (c) 2013 VideoLAN. All rights reserved.
//
#import "VLCPasscodeLockViewController.h"
#import "VLCAppDelegate.h"
#import "VLCPlaylistViewController.h"
@implementation VLCPasscodeLockViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.enterCodeField.secureTextEntry = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
_passcode = [defaults objectForKey:@"Passcode"];
self.enterPasscodeLabel.text = NSLocalizedString(@"ENTER_PASSCODE", @"");
[self.navigationController setNavigationBarHidden:YES animated:NO];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
self.enterCodeField.text = @"";
[self.enterCodeField becomeFirstResponder];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
[super viewWillDisappear:animated];
}
- (IBAction)textFieldValueChanged:(id)sender
{
if (self.enterCodeField.text.length == 4) {
VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
if (_resetStage == 1) {
_tmpPasscode = self.enterCodeField.text;
self.enterCodeField.text = @"";
self.enterPasscodeLabel.text = NSLocalizedString(@"REENTER_PASSCODE", @"");
_resetStage = 2;
} else if (_resetStage == 2) {
if ([self.enterCodeField.text isEqualToString:_tmpPasscode]) {
NSUserDefaults *defaults;
[defaults setObject:@1 forKey:@"PasscodeProtection"];
[defaults setObject:_tmpPasscode forKey:@"Passcode"];
[defaults synchronize];
_passcode = _tmpPasscode;
_resetStage = 0;
appDelegate.playlistViewController.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300]; // five min
appDelegate.playlistViewController.passcodeValidated = YES;
[self.view removeFromSuperview];
}
} else if ([self.enterCodeField.text isEqualToString:_passcode]) {
appDelegate.playlistViewController.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300]; // five min
appDelegate.playlistViewController.passcodeValidated = YES;
[self.navigationController popViewControllerAnimated:YES];
}
}
}
- (void)resetPasscode
{
_resetStage = 1;
}
@end
......@@ -8,6 +8,7 @@
#import "VLCPlaylistGridView.h"
#import "VLCAppDelegate.h"
#import "VLCPlaylistViewController.h"
@interface VLCPlaylistGridView (Hack)
@property (nonatomic, retain) NSString *reuseIdentifier;
......
......@@ -11,7 +11,6 @@
@class VLCMovieViewController;
@class VLCAboutViewController;
@class VLCPasscodeLockViewController;
@class VLCAddMediaViewController;
@interface VLCPlaylistViewController : UIViewController <AQGridViewDataSource, AQGridViewDelegate, UITableViewDataSource, UITableViewDelegate, UITabBarDelegate, UIPopoverControllerDelegate>
......@@ -19,12 +18,8 @@
BOOL _editMode;
}
@property (nonatomic, retain) NSDate *nextPasscodeCheckDate;
@property (nonatomic) BOOL passcodeValidated;
@property (nonatomic, strong) VLCMovieViewController *movieViewController;
@property (nonatomic, strong) VLCAboutViewController *aboutViewController;
@property (nonatomic, strong) VLCPasscodeLockViewController *passcodeLockViewController;
@property (nonatomic, strong) VLCAddMediaViewController *addMediaViewController;
@property (nonatomic, strong) UIPopoverController *addMediaPopoverController;
......@@ -38,7 +33,6 @@
- (IBAction)leftButtonAction:(id)sender;
- (void)validatePasscode;
- (void)updateViewContents;
- (void)openMovieFromURL:(NSURL *)url;
- (void)removeMediaObject:(MLFile *)mediaObject;
......
......@@ -11,7 +11,6 @@
#import "VLCPlaylistTableViewCell.h"
#import "VLCPlaylistGridView.h"
#import "VLCAboutViewController.h"
#import "VLCPasscodeLockViewController.h"
#import "VLCAddMediaViewController.h"
@interface VLCPlaylistViewController () {
......@@ -65,8 +64,6 @@
self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
[self.emptyLibraryLongDescriptionLabel sizeToFit];
self.passcodeLockViewController = [[VLCPasscodeLockViewController alloc] initWithNibName:@"VLCPasscodeLockViewController" bundle:nil];
}
- (void)viewWillAppear:(BOOL)animated
......@@ -84,7 +81,6 @@
- (void)viewDidAppear:(BOOL)animated
{
[self validatePasscode];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
self.tableView.hidden = NO;
else
......@@ -123,21 +119,6 @@
}
}
- (void)validatePasscode
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:@"Passcode"] isEqualToString:@""]) {
self.passcodeValidated = YES;
return;
}
if (!self.passcodeValidated) {
if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate)
[self.navigationController pushViewController:self.passcodeLockViewController animated:YES];
else
self.passcodeValidated = YES;
}
}
- (void)updateViewContents
{
......
......@@ -8,10 +8,10 @@
#import "VLCSettingsViewController.h"
#import "VLCPlaylistViewController.h"
#import "VLCPasscodeLockViewController.h"
#import "PAPasscodeViewController.h"
#import "VLCAppDelegate.h"
@interface VLCSettingsViewController ()
@interface VLCSettingsViewController () <PAPasscodeViewControllerDelegate>
{
NSArray *_userFacingTextEncodingNames;
NSArray *_textEncodingNames;
......@@ -67,14 +67,9 @@
if (sender == self.passcodeLockSwitch) {
if (self.passcodeLockSwitch.on) {
VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGRect frame = self.view.frame;
frame.size.height -= 44.;
appDelegate.playlistViewController.passcodeLockViewController.view.frame = frame;
}
[self.view addSubview:appDelegate.playlistViewController.passcodeLockViewController.view];
[appDelegate.playlistViewController.passcodeLockViewController resetPasscode];
PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
passcodeLockController.delegate = self;
[self presentModalViewController:passcodeLockController animated:YES];
} else {
[defaults setObject:@0 forKey:kVLCSettingPasscodeOnKey];
}
......@@ -125,4 +120,24 @@
return _textEncodingNames.count;
}
#pragma mark - PAPasscode delegate
- (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
{
self.passcodeLockSwitch.on = NO;
[controller dismissModalViewControllerAnimated:YES];
}
- (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@(1) forKey:kVLCSettingPasscodeOnKey];
[defaults setObject:controller.passcode forKey:kVLCSettingPasscodeKey];
[defaults synchronize];
VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
[controller dismissModalViewControllerAnimated:YES];
}
@end
//
// PAPasscodeViewController.h
// PAPasscode
//
// Created by Denis Hennessy on 15/10/2012.
// Copyright (c) 2012 Peer Assembly. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum {
PasscodeActionSet,
PasscodeActionEnter,
PasscodeActionChange
} PasscodeAction;
@class PAPasscodeViewController;
@protocol PAPasscodeViewControllerDelegate <NSObject>
@optional
- (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller;
- (void)PAPasscodeViewControllerDidChangePasscode:(PAPasscodeViewController *)controller;
- (void)PAPasscodeViewControllerDidEnterAlternativePasscode:(PAPasscodeViewController *)controller;
- (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller;
- (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller;
- (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts;
@end
@interface PAPasscodeViewController : UIViewController {
UIView *contentView;
NSInteger phase;
UILabel *promptLabel;
UILabel *messageLabel;
UIImageView *failedImageView;
UILabel *failedAttemptsLabel;
UITextField *passcodeTextField;
UIImageView *digitImageViews[4];
UIImageView *snapshotImageView;
}
@property (strong) UIView *backgroundView;
@property (readonly) PasscodeAction action;
@property (weak) id<PAPasscodeViewControllerDelegate> delegate;
@property (strong) NSString *alternativePasscode;
@property (strong) NSString *passcode;
@property (assign) BOOL simple;
@property (assign) NSInteger failedAttempts;
@property (strong) NSString *enterPrompt;
@property (strong) NSString *confirmPrompt;
@property (strong) NSString *changePrompt;
@property (strong) NSString *message;
- (id)initForAction:(PasscodeAction)action;
@end
//
// PAPasscodeViewController.m
// PAPasscode
//
// Created by Denis Hennessy on 15/10/2012.
// Copyright (c) 2012 Peer Assembly. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "PAPasscodeViewController.h"
#define NAVBAR_HEIGHT 44
#define PROMPT_HEIGHT 74
#define DIGIT_SPACING 10
#define DIGIT_WIDTH 61
#define DIGIT_HEIGHT 53
#define MARKER_WIDTH 16
#define MARKER_HEIGHT 16
#define MARKER_X 22
#define MARKER_Y 18
#define MESSAGE_HEIGHT 74
#define FAILED_LCAP 19
#define FAILED_RCAP 19
#define FAILED_HEIGHT 26
#define FAILED_MARGIN 10
#define TEXTFIELD_MARGIN 8
#define SLIDE_DURATION 0.3
@interface PAPasscodeViewController ()
- (void)cancel:(id)sender;
- (void)handleFailedAttempt;
- (void)handleCompleteField;
- (void)passcodeChanged:(id)sender;
- (void)resetFailedAttempts;
- (void)showFailedAttempts;
- (void)showScreenForPhase:(NSInteger)phase animated:(BOOL)animated;
@end
@implementation PAPasscodeViewController
- (id)initForAction:(PasscodeAction)action {
self = [super init];
if (self) {
_action = action;
switch (action) {
case PasscodeActionSet:
self.title = NSLocalizedString(@"Set Passcode", nil);
_enterPrompt = NSLocalizedString(@"Enter a passcode", nil);
_confirmPrompt = NSLocalizedString(@"Re-enter your passcode", nil);
break;
case PasscodeActionEnter:
self.title = NSLocalizedString(@"Enter Passcode", nil);
_enterPrompt = NSLocalizedString(@"Enter your passcode", nil);
break;
case PasscodeActionChange:
self.title = NSLocalizedString(@"Change Passcode", nil);
_changePrompt = NSLocalizedString(@"Enter your old passcode", nil);
_enterPrompt = NSLocalizedString(@"Enter your new passcode", nil);
_confirmPrompt = NSLocalizedString(@"Re-enter your new passcode", nil);
break;
}
self.modalPresentationStyle = UIModalPresentationFormSheet;
_simple = YES;
}
return self;
}
- (void)loadView {
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, NAVBAR_HEIGHT)];
navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
navigationBar.items = @[self.navigationItem];
[view addSubview:navigationBar];
contentView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT, view.bounds.size.width, view.bounds.size.height-NAVBAR_HEIGHT)];
contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
if (_backgroundView) {
[contentView addSubview:_backgroundView];
}
contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[view addSubview:contentView];
CGFloat panelWidth = DIGIT_WIDTH*4+DIGIT_SPACING*3;
if (_simple) {
UIView *digitPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, panelWidth, DIGIT_HEIGHT)];
digitPanel.frame = CGRectOffset(digitPanel.frame, (contentView.bounds.size.width-digitPanel.bounds.size.width)/2, PROMPT_HEIGHT);
digitPanel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[contentView addSubview:digitPanel];
UIImage *backgroundImage = [UIImage imageNamed:@"papasscode_background"];
UIImage *markerImage = [UIImage imageNamed:@"papasscode_marker"];
CGFloat xLeft = 0;
for (int i=0;i<4;i++) {
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundImageView.frame = CGRectOffset(backgroundImageView.frame, xLeft, 0);
[digitPanel addSubview:backgroundImageView];
digitImageViews[i] = [[UIImageView alloc] initWithImage:markerImage];
digitImageViews[i].autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
digitImageViews[i].frame = CGRectOffset(digitImageViews[i].frame, backgroundImageView.frame.origin.x+MARKER_X, MARKER_Y);
[digitPanel addSubview:digitImageViews[i]];
xLeft += DIGIT_SPACING + backgroundImage.size.width;
}
passcodeTextField = [[UITextField alloc] initWithFrame:digitPanel.frame];
passcodeTextField.hidden = YES;
} else {
UIView *passcodePanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, panelWidth, DIGIT_HEIGHT)];
passcodePanel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
passcodePanel.frame = CGRectOffset(passcodePanel.frame, (contentView.bounds.size.width-passcodePanel.bounds.size.width)/2, PROMPT_HEIGHT);
passcodePanel.frame = CGRectInset(passcodePanel.frame, TEXTFIELD_MARGIN, TEXTFIELD_MARGIN);
passcodePanel.layer.borderColor = [UIColor colorWithRed:0.65 green:0.67 blue:0.70 alpha:1.0].CGColor;
passcodePanel.layer.borderWidth = 1.0;
passcodePanel.layer.cornerRadius = 5.0;
passcodePanel.layer.shadowColor = [UIColor whiteColor].CGColor;
passcodePanel.layer.shadowOffset = CGSizeMake(0, 1);
passcodePanel.layer.shadowOpacity = 1.0;
passcodePanel.layer.shadowRadius = 1.0;
passcodePanel.backgroundColor = [UIColor whiteColor];
[contentView addSubview:passcodePanel];
passcodeTextField = [[UITextField alloc] initWithFrame:CGRectInset(passcodePanel.frame, 6, 6)];
}
passcodeTextField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
passcodeTextField.borderStyle = UITextBorderStyleNone;
passcodeTextField.secureTextEntry = YES;
passcodeTextField.textColor = [UIColor colorWithRed:0.23 green:0.33 blue:0.52 alpha:1.0];
passcodeTextField.keyboardType = UIKeyboardTypeNumberPad;
[passcodeTextField addTarget:self action:@selector(passcodeChanged:) forControlEvents:UIControlEventEditingChanged];
[contentView addSubview:passcodeTextField];
promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, contentView.bounds.size.width, PROMPT_HEIGHT)];
promptLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
promptLabel.backgroundColor = [UIColor clearColor];
promptLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
promptLabel.font = [UIFont boldSystemFontOfSize:17];
promptLabel.shadowColor = [UIColor whiteColor];
promptLabel.shadowOffset = CGSizeMake(0, 1);
promptLabel.textAlignment = UITextAlignmentCenter;
promptLabel.numberOfLines = 0;
[contentView addSubview:promptLabel];
messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, PROMPT_HEIGHT+DIGIT_HEIGHT, contentView.bounds.size.width, MESSAGE_HEIGHT)];
messageLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
messageLabel.font = [UIFont systemFontOfSize:14];
messageLabel.shadowColor = [UIColor whiteColor];
messageLabel.shadowOffset = CGSizeMake(0, 1);
messageLabel.textAlignment = UITextAlignmentCenter;
messageLabel.numberOfLines = 0;
messageLabel.text = _message;
[contentView addSubview:messageLabel];
UIImage *failedBg = [[UIImage imageNamed:@"papasscode_failed_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, FAILED_LCAP, 0, FAILED_RCAP)];
failedImageView = [[UIImageView alloc] initWithImage:failedBg];
failedImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
failedImageView.hidden = YES;
[contentView addSubview:failedImageView];
failedAttemptsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
failedAttemptsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
failedAttemptsLabel.backgroundColor = [UIColor clearColor];
failedAttemptsLabel.textColor = [UIColor whiteColor];
failedAttemptsLabel.font = [UIFont boldSystemFontOfSize:15];
failedAttemptsLabel.shadowColor = [UIColor blackColor];
failedAttemptsLabel.shadowOffset = CGSizeMake(0, -1);
failedAttemptsLabel.textAlignment = UITextAlignmentCenter;
failedAttemptsLabel.hidden = YES;
[contentView addSubview:failedAttemptsLabel];
self.view = view;
}
- (void)viewDidLoad {
[super viewDidLoad];
if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidCancel:)]) {
if (_simple) {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
} else {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
}
}
if (_failedAttempts > 0) {
[self showFailedAttempts];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self showScreenForPhase:0 animated:NO];
[passcodeTextField becomeFirstResponder];
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (void)cancel:(id)sender {
[_delegate PAPasscodeViewControllerDidCancel:self];
}
#pragma mark - implementation helpers
- (void)handleCompleteField {
NSString *text = passcodeTextField.text;
switch (_action) {
case PasscodeActionSet:
if (phase == 0) {
_passcode = text;
messageLabel.text = @"";
[self showScreenForPhase:1 animated:YES];
} else {
if ([text isEqualToString:_passcode]) {
if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidSetPasscode:)]) {
[_delegate PAPasscodeViewControllerDidSetPasscode:self];
}
} else {
[self showScreenForPhase:0 animated:YES];
messageLabel.text = NSLocalizedString(@"Passcodes did not match. Try again.", nil);
}
}
break;
case PasscodeActionEnter:
if ([text isEqualToString:_passcode]) {
[self resetFailedAttempts];
if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidEnterPasscode:)]) {
[_delegate PAPasscodeViewControllerDidEnterPasscode:self];
}
} else {
if (_alternativePasscode && [text isEqualToString:_alternativePasscode]) {
[self resetFailedAttempts];
if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidEnterAlternativePasscode:)]) {
[_delegate PAPasscodeViewControllerDidEnterAlternativePasscode:self];
}
} else {
[self handleFailedAttempt];
[self showScreenForPhase:0 animated:NO];
}
}
break;
case PasscodeActionChange:
if (phase == 0) {
if ([text isEqualToString:_passcode]) {
[self resetFailedAttempts];
[self showScreenForPhase:1 animated:YES];
} else {
[self handleFailedAttempt];
[self showScreenForPhase:0 animated:NO];
}
} else if (phase == 1) {
_passcode = text;
messageLabel.text = @"";
[self showScreenForPhase:2 animated:YES];
} else {
if ([text isEqualToString:_passcode]) {
if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidChangePasscode:)]) {
[_delegate PAPasscodeViewControllerDidChangePasscode:self];
}
} else {
[self showScreenForPhase:1 animated:YES];
messageLabel.text = NSLocalizedString(@"Passcodes did not match. Try again.", nil);
}
}
break;
}
}
- (void)handleFailedAttempt {
_failedAttempts++;