PAPasscodeViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //Copyright (c) 2012, Denis Hennessy (Peer Assembly - http://peerassembly.com)
  2. //All rights reserved.
  3. //
  4. //Redistribution and use in source and binary forms, with or without
  5. //modification, are permitted provided that the following conditions are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in the
  10. // documentation and/or other materials provided with the distribution.
  11. // * Neither the name of Peer Assembly, Denis Hennessy nor the
  12. // names of its contributors may be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. //DISCLAIMED. IN NO EVENT SHALL PEER ASSEMBLY OR DENIS HENNESSY BE LIABLE FOR ANY
  19. //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #import <QuartzCore/QuartzCore.h>
  26. #import "PAPasscodeViewController.h"
  27. #define NAVBAR_HEIGHT 66
  28. #define PROMPT_HEIGHT 90
  29. #define DIGIT_SPACING 25
  30. #define DIGIT_WIDTH 19
  31. #define DIGIT_HEIGHT 1
  32. #define MARKER_WIDTH 16
  33. #define MARKER_HEIGHT 1
  34. #define MARKER_X 1.5
  35. #define MARKER_Y -18
  36. #define MESSAGE_HEIGHT 90
  37. #define FAILED_LCAP 19
  38. #define FAILED_RCAP 19
  39. #define FAILED_HEIGHT 26
  40. #define FAILED_MARGIN 10
  41. #define TEXTFIELD_MARGIN 8
  42. #define SLIDE_DURATION 0.3
  43. @interface PAPasscodeViewController ()
  44. - (void)cancel:(id)sender;
  45. - (void)handleFailedAttempt;
  46. - (void)handleCompleteField;
  47. - (void)passcodeChanged:(id)sender;
  48. - (void)resetFailedAttempts;
  49. - (void)showFailedAttempts;
  50. - (void)showScreenForPhase:(NSInteger)phase animated:(BOOL)animated;
  51. @end
  52. @implementation PAPasscodeViewController
  53. - (id)initForAction:(PasscodeAction)action {
  54. self = [super init];
  55. if (self) {
  56. _action = action;
  57. switch (action) {
  58. case PasscodeActionSet:
  59. self.title = NSLocalizedString(@"PASSCODE_SET", nil);
  60. _enterPrompt = NSLocalizedString(@"ENTER_PASSCODE", nil);
  61. _confirmPrompt = NSLocalizedString(@"REENTER_PASSCODE", nil);
  62. break;
  63. case PasscodeActionEnter:
  64. self.title = NSLocalizedString(@"ENTER_PASSCODE", nil);
  65. _enterPrompt = NSLocalizedString(@"ENTER_PASSCODE", nil);
  66. break;
  67. case PasscodeActionChange:
  68. self.title = NSLocalizedString(@"PASSCODE_CHANGE", nil);
  69. _changePrompt = NSLocalizedString(@"PASSCODE_ENTER_OLD", nil);
  70. _enterPrompt = NSLocalizedString(@"PASSCODE_ENTER_NEW", nil);
  71. _confirmPrompt = NSLocalizedString(@"PASSCODE_REENTER_NEW", nil);
  72. break;
  73. }
  74. self.modalPresentationStyle = UIModalPresentationFormSheet;
  75. _simple = YES;
  76. }
  77. return self;
  78. }
  79. - (void)loadView {
  80. UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
  81. view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  82. self.navigationController.navigationBarHidden = YES;
  83. UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, NAVBAR_HEIGHT)];
  84. navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  85. navigationBar.items = @[self.navigationItem];
  86. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  87. navigationBar.barTintColor = [UIColor VLCOrangeTintColor];
  88. navigationBar.tintColor = [UIColor whiteColor];
  89. navigationBar.titleTextAttributes = @{ UITextAttributeTextColor : [UIColor whiteColor] };
  90. } else {
  91. [navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  92. navigationBar.translucent = NO;
  93. navigationBar.opaque = YES;
  94. }
  95. [view addSubview:navigationBar];
  96. contentView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT, view.bounds.size.width, view.bounds.size.height-NAVBAR_HEIGHT)];
  97. contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  98. if (_backgroundView) {
  99. [contentView addSubview:_backgroundView];
  100. }
  101. contentView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  102. [view addSubview:contentView];
  103. CGFloat panelWidth = DIGIT_WIDTH*4+DIGIT_SPACING*3;
  104. if (_simple) {
  105. UIView *digitPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, panelWidth, DIGIT_HEIGHT)];
  106. NSLog(@"cw width %f, panel width %f",contentView.bounds.size.width, panelWidth);
  107. digitPanel.frame = CGRectOffset(digitPanel.frame, (contentView.bounds.size.width - panelWidth) / 2., PROMPT_HEIGHT);
  108. digitPanel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  109. [contentView addSubview:digitPanel];
  110. UIImage *backgroundImage = [UIImage imageNamed:@"papasscode_background"];
  111. UIImage *markerImage = [UIImage imageNamed:@"papasscode_marker"];
  112. CGFloat xLeft = 0;
  113. for (int i=0;i<4;i++) {
  114. UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
  115. backgroundImageView.frame = CGRectOffset(backgroundImageView.frame, xLeft, 0);
  116. [digitPanel addSubview:backgroundImageView];
  117. digitImageViews[i] = [[UIImageView alloc] initWithImage:markerImage];
  118. digitImageViews[i].autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  119. digitImageViews[i].frame = CGRectOffset(digitImageViews[i].frame, backgroundImageView.frame.origin.x+MARKER_X, MARKER_Y);
  120. [digitPanel addSubview:digitImageViews[i]];
  121. xLeft += DIGIT_SPACING + backgroundImage.size.width;
  122. }
  123. passcodeTextField = [[UITextField alloc] initWithFrame:digitPanel.frame];
  124. passcodeTextField.hidden = YES;
  125. } else {
  126. UIView *passcodePanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, panelWidth, DIGIT_HEIGHT)];
  127. passcodePanel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  128. passcodePanel.frame = CGRectOffset(passcodePanel.frame, (contentView.bounds.size.width-passcodePanel.bounds.size.width)/2, PROMPT_HEIGHT);
  129. passcodePanel.frame = CGRectInset(passcodePanel.frame, TEXTFIELD_MARGIN, TEXTFIELD_MARGIN);
  130. passcodePanel.layer.borderColor = [UIColor colorWithRed:0.65 green:0.67 blue:0.70 alpha:1.0].CGColor;
  131. passcodePanel.layer.borderWidth = 1.0;
  132. passcodePanel.layer.cornerRadius = 5.0;
  133. passcodePanel.layer.shadowColor = [UIColor whiteColor].CGColor;
  134. passcodePanel.layer.shadowOffset = CGSizeMake(0, 1);
  135. passcodePanel.layer.shadowOpacity = 1.0;
  136. passcodePanel.layer.shadowRadius = 1.0;
  137. passcodePanel.backgroundColor = [UIColor whiteColor];
  138. [contentView addSubview:passcodePanel];
  139. passcodeTextField = [[UITextField alloc] initWithFrame:CGRectInset(passcodePanel.frame, 6, 6)];
  140. }
  141. passcodeTextField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  142. passcodeTextField.borderStyle = UITextBorderStyleNone;
  143. passcodeTextField.secureTextEntry = YES;
  144. passcodeTextField.textColor = [UIColor colorWithRed:0.23 green:0.33 blue:0.52 alpha:1.0];
  145. passcodeTextField.keyboardType = UIKeyboardTypeNumberPad;
  146. [passcodeTextField addTarget:self action:@selector(passcodeChanged:) forControlEvents:UIControlEventEditingChanged];
  147. [contentView addSubview:passcodeTextField];
  148. promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, contentView.bounds.size.width, PROMPT_HEIGHT)];
  149. promptLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  150. promptLabel.backgroundColor = [UIColor clearColor];
  151. promptLabel.textColor = [UIColor VLCLightTextColor];
  152. promptLabel.font = [UIFont boldSystemFontOfSize:17];
  153. promptLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  154. promptLabel.shadowOffset = CGSizeMake(0, 1);
  155. promptLabel.textAlignment = NSTextAlignmentCenter;
  156. promptLabel.numberOfLines = 0;
  157. [contentView addSubview:promptLabel];
  158. messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, PROMPT_HEIGHT+DIGIT_HEIGHT, contentView.bounds.size.width, MESSAGE_HEIGHT)];
  159. messageLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  160. messageLabel.backgroundColor = [UIColor clearColor];
  161. messageLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
  162. messageLabel.font = [UIFont systemFontOfSize:14];
  163. messageLabel.shadowColor = [UIColor whiteColor];
  164. messageLabel.shadowOffset = CGSizeMake(0, 1);
  165. messageLabel.textAlignment = NSTextAlignmentCenter;
  166. messageLabel.numberOfLines = 0;
  167. messageLabel.text = _message;
  168. [contentView addSubview:messageLabel];
  169. UIImage *failedBg = [[UIImage imageNamed:@"papasscode_failed_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, FAILED_LCAP, 0, FAILED_RCAP)];
  170. failedImageView = [[UIImageView alloc] initWithImage:failedBg];
  171. failedImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  172. failedImageView.hidden = YES;
  173. [contentView addSubview:failedImageView];
  174. failedAttemptsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  175. failedAttemptsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  176. failedAttemptsLabel.backgroundColor = [UIColor clearColor];
  177. failedAttemptsLabel.textColor = [UIColor whiteColor];
  178. failedAttemptsLabel.font = [UIFont boldSystemFontOfSize:15];
  179. failedAttemptsLabel.shadowColor = [UIColor blackColor];
  180. failedAttemptsLabel.shadowOffset = CGSizeMake(0, -1);
  181. failedAttemptsLabel.textAlignment = NSTextAlignmentCenter;
  182. failedAttemptsLabel.hidden = YES;
  183. [contentView addSubview:failedAttemptsLabel];
  184. self.view = view;
  185. }
  186. - (void)viewDidLoad {
  187. [super viewDidLoad];
  188. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidCancel:)]) {
  189. if (_simple) {
  190. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
  191. } else {
  192. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
  193. }
  194. }
  195. if (_failedAttempts > 0) {
  196. [self showFailedAttempts];
  197. }
  198. }
  199. - (void)viewWillAppear:(BOOL)animated {
  200. [super viewWillAppear:animated];
  201. [self showScreenForPhase:0 animated:NO];
  202. [passcodeTextField becomeFirstResponder];
  203. }
  204. - (NSUInteger)supportedInterfaceOrientations {
  205. return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
  206. }
  207. - (void)cancel:(id)sender {
  208. [_delegate PAPasscodeViewControllerDidCancel:self];
  209. }
  210. #pragma mark - implementation helpers
  211. - (void)handleCompleteField {
  212. NSString *text = passcodeTextField.text;
  213. switch (_action) {
  214. case PasscodeActionSet:
  215. if (phase == 0) {
  216. _passcode = text;
  217. messageLabel.text = @"";
  218. [self showScreenForPhase:1 animated:YES];
  219. } else {
  220. if ([text isEqualToString:_passcode]) {
  221. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidSetPasscode:)]) {
  222. [_delegate PAPasscodeViewControllerDidSetPasscode:self];
  223. }
  224. } else {
  225. [self showScreenForPhase:0 animated:YES];
  226. messageLabel.text = NSLocalizedString(@"PASSCODES_DID_NOT_MATCH", nil);
  227. }
  228. }
  229. break;
  230. case PasscodeActionEnter:
  231. if ([text isEqualToString:_passcode]) {
  232. [self resetFailedAttempts];
  233. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidEnterPasscode:)]) {
  234. [_delegate PAPasscodeViewControllerDidEnterPasscode:self];
  235. }
  236. } else {
  237. if (_alternativePasscode && [text isEqualToString:_alternativePasscode]) {
  238. [self resetFailedAttempts];
  239. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidEnterAlternativePasscode:)]) {
  240. [_delegate PAPasscodeViewControllerDidEnterAlternativePasscode:self];
  241. }
  242. } else {
  243. [self handleFailedAttempt];
  244. [self showScreenForPhase:0 animated:NO];
  245. }
  246. }
  247. break;
  248. case PasscodeActionChange:
  249. if (phase == 0) {
  250. if ([text isEqualToString:_passcode]) {
  251. [self resetFailedAttempts];
  252. [self showScreenForPhase:1 animated:YES];
  253. } else {
  254. [self handleFailedAttempt];
  255. [self showScreenForPhase:0 animated:NO];
  256. }
  257. } else if (phase == 1) {
  258. _passcode = text;
  259. messageLabel.text = @"";
  260. [self showScreenForPhase:2 animated:YES];
  261. } else {
  262. if ([text isEqualToString:_passcode]) {
  263. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidChangePasscode:)]) {
  264. [_delegate PAPasscodeViewControllerDidChangePasscode:self];
  265. }
  266. } else {
  267. [self showScreenForPhase:1 animated:YES];
  268. messageLabel.text = NSLocalizedString(@"PASSCODES_DID_NOT_MATCH", nil);
  269. }
  270. }
  271. break;
  272. }
  273. }
  274. - (void)handleFailedAttempt {
  275. _failedAttempts++;
  276. [self showFailedAttempts];
  277. if ([_delegate respondsToSelector:@selector(PAPasscodeViewController:didFailToEnterPasscode:)]) {
  278. [_delegate PAPasscodeViewController:self didFailToEnterPasscode:_failedAttempts];
  279. }
  280. }
  281. - (void)resetFailedAttempts {
  282. messageLabel.hidden = NO;
  283. failedImageView.hidden = YES;
  284. failedAttemptsLabel.hidden = YES;
  285. _failedAttempts = 0;
  286. }
  287. - (void)showFailedAttempts {
  288. messageLabel.hidden = YES;
  289. failedImageView.hidden = NO;
  290. failedAttemptsLabel.hidden = NO;
  291. if (_failedAttempts == 1) {
  292. failedAttemptsLabel.text = NSLocalizedString(@"PASSCODE_FAILED_1", nil);
  293. } else {
  294. failedAttemptsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"PASSCODE_FAILED_FORMAT", nil), _failedAttempts];
  295. }
  296. [failedAttemptsLabel sizeToFit];
  297. CGFloat bgWidth = failedAttemptsLabel.bounds.size.width + FAILED_MARGIN*2;
  298. CGFloat x = floor((contentView.bounds.size.width-bgWidth)/2);
  299. CGFloat y = PROMPT_HEIGHT+DIGIT_HEIGHT+floor((MESSAGE_HEIGHT-FAILED_HEIGHT)/2);
  300. failedImageView.frame = CGRectMake(x, y, bgWidth, FAILED_HEIGHT);
  301. x = failedImageView.frame.origin.x+FAILED_MARGIN;
  302. y = failedImageView.frame.origin.y+floor((failedImageView.bounds.size.height-failedAttemptsLabel.frame.size.height)/2);
  303. failedAttemptsLabel.frame = CGRectMake(x, y, failedAttemptsLabel.bounds.size.width, failedAttemptsLabel.bounds.size.height);
  304. }
  305. - (void)passcodeChanged:(id)sender {
  306. NSString *text = passcodeTextField.text;
  307. if (_simple) {
  308. if ([text length] > 4) {
  309. text = [text substringToIndex:4];
  310. }
  311. for (int i=0;i<4;i++) {
  312. digitImageViews[i].hidden = i >= [text length];
  313. }
  314. if ([text length] == 4) {
  315. [self handleCompleteField];
  316. }
  317. } else {
  318. self.navigationItem.rightBarButtonItem.enabled = [text length] > 0;
  319. }
  320. }
  321. - (void)showScreenForPhase:(NSInteger)newPhase animated:(BOOL)animated {
  322. CGFloat dir = (newPhase > phase) ? 1 : -1;
  323. if (animated) {
  324. UIGraphicsBeginImageContext(self.view.bounds.size);
  325. [contentView.layer renderInContext:UIGraphicsGetCurrentContext()];
  326. UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
  327. UIGraphicsEndImageContext();
  328. snapshotImageView = [[UIImageView alloc] initWithImage:snapshot];
  329. snapshotImageView.frame = CGRectOffset(snapshotImageView.frame, -contentView.frame.size.width*dir, 0);
  330. [contentView addSubview:snapshotImageView];
  331. }
  332. phase = newPhase;
  333. passcodeTextField.text = @"";
  334. if (!_simple) {
  335. BOOL finalScreen = _action == PasscodeActionSet && phase == 1;
  336. finalScreen |= _action == PasscodeActionEnter && phase == 0;
  337. finalScreen |= _action == PasscodeActionChange && phase == 2;
  338. if (finalScreen) {
  339. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(handleCompleteField)];
  340. } else {
  341. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_NEXT", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(handleCompleteField)];
  342. }
  343. self.navigationItem.rightBarButtonItem.enabled = NO;
  344. }
  345. switch (_action) {
  346. case PasscodeActionSet:
  347. if (phase == 0) {
  348. promptLabel.text = _enterPrompt;
  349. } else {
  350. promptLabel.text = _confirmPrompt;
  351. }
  352. break;
  353. case PasscodeActionEnter:
  354. promptLabel.text = _enterPrompt;
  355. break;
  356. case PasscodeActionChange:
  357. if (phase == 0) {
  358. promptLabel.text = _changePrompt;
  359. } else if (phase == 1) {
  360. promptLabel.text = _enterPrompt;
  361. } else {
  362. promptLabel.text = _confirmPrompt;
  363. }
  364. break;
  365. }
  366. for (int i=0;i<4;i++) {
  367. digitImageViews[i].hidden = YES;
  368. }
  369. if (animated) {
  370. contentView.frame = CGRectOffset(contentView.frame, contentView.frame.size.width*dir, 0);
  371. [UIView animateWithDuration:SLIDE_DURATION animations:^() {
  372. contentView.frame = CGRectOffset(contentView.frame, -contentView.frame.size.width*dir, 0);
  373. } completion:^(BOOL finished) {
  374. [snapshotImageView removeFromSuperview];
  375. snapshotImageView = nil;
  376. }];
  377. }
  378. }
  379. @end