PAPasscodeViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 44
  28. #define PROMPT_HEIGHT 74
  29. #define DIGIT_SPACING 10
  30. #define DIGIT_WIDTH 61
  31. #define DIGIT_HEIGHT 53
  32. #define MARKER_WIDTH 16
  33. #define MARKER_HEIGHT 16
  34. #define MARKER_X 22
  35. #define MARKER_Y 18
  36. #define MESSAGE_HEIGHT 74
  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. UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, NAVBAR_HEIGHT)];
  83. navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  84. navigationBar.items = @[self.navigationItem];
  85. [view addSubview:navigationBar];
  86. contentView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT, view.bounds.size.width, view.bounds.size.height-NAVBAR_HEIGHT)];
  87. contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  88. if (_backgroundView) {
  89. [contentView addSubview:_backgroundView];
  90. }
  91. contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
  92. [view addSubview:contentView];
  93. CGFloat panelWidth = DIGIT_WIDTH*4+DIGIT_SPACING*3;
  94. if (_simple) {
  95. UIView *digitPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, panelWidth, DIGIT_HEIGHT)];
  96. digitPanel.frame = CGRectOffset(digitPanel.frame, (contentView.bounds.size.width-digitPanel.bounds.size.width)/2, PROMPT_HEIGHT);
  97. digitPanel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  98. [contentView addSubview:digitPanel];
  99. UIImage *backgroundImage = [UIImage imageNamed:@"papasscode_background"];
  100. UIImage *markerImage = [UIImage imageNamed:@"papasscode_marker"];
  101. CGFloat xLeft = 0;
  102. for (int i=0;i<4;i++) {
  103. UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
  104. backgroundImageView.frame = CGRectOffset(backgroundImageView.frame, xLeft, 0);
  105. [digitPanel addSubview:backgroundImageView];
  106. digitImageViews[i] = [[UIImageView alloc] initWithImage:markerImage];
  107. digitImageViews[i].autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  108. digitImageViews[i].frame = CGRectOffset(digitImageViews[i].frame, backgroundImageView.frame.origin.x+MARKER_X, MARKER_Y);
  109. [digitPanel addSubview:digitImageViews[i]];
  110. xLeft += DIGIT_SPACING + backgroundImage.size.width;
  111. }
  112. passcodeTextField = [[UITextField alloc] initWithFrame:digitPanel.frame];
  113. passcodeTextField.hidden = YES;
  114. } else {
  115. UIView *passcodePanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, panelWidth, DIGIT_HEIGHT)];
  116. passcodePanel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  117. passcodePanel.frame = CGRectOffset(passcodePanel.frame, (contentView.bounds.size.width-passcodePanel.bounds.size.width)/2, PROMPT_HEIGHT);
  118. passcodePanel.frame = CGRectInset(passcodePanel.frame, TEXTFIELD_MARGIN, TEXTFIELD_MARGIN);
  119. passcodePanel.layer.borderColor = [UIColor colorWithRed:0.65 green:0.67 blue:0.70 alpha:1.0].CGColor;
  120. passcodePanel.layer.borderWidth = 1.0;
  121. passcodePanel.layer.cornerRadius = 5.0;
  122. passcodePanel.layer.shadowColor = [UIColor whiteColor].CGColor;
  123. passcodePanel.layer.shadowOffset = CGSizeMake(0, 1);
  124. passcodePanel.layer.shadowOpacity = 1.0;
  125. passcodePanel.layer.shadowRadius = 1.0;
  126. passcodePanel.backgroundColor = [UIColor whiteColor];
  127. [contentView addSubview:passcodePanel];
  128. passcodeTextField = [[UITextField alloc] initWithFrame:CGRectInset(passcodePanel.frame, 6, 6)];
  129. }
  130. passcodeTextField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  131. passcodeTextField.borderStyle = UITextBorderStyleNone;
  132. passcodeTextField.secureTextEntry = YES;
  133. passcodeTextField.textColor = [UIColor colorWithRed:0.23 green:0.33 blue:0.52 alpha:1.0];
  134. passcodeTextField.keyboardType = UIKeyboardTypeNumberPad;
  135. [passcodeTextField addTarget:self action:@selector(passcodeChanged:) forControlEvents:UIControlEventEditingChanged];
  136. [contentView addSubview:passcodeTextField];
  137. promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, contentView.bounds.size.width, PROMPT_HEIGHT)];
  138. promptLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  139. promptLabel.backgroundColor = [UIColor clearColor];
  140. promptLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
  141. promptLabel.font = [UIFont boldSystemFontOfSize:17];
  142. promptLabel.shadowColor = [UIColor whiteColor];
  143. promptLabel.shadowOffset = CGSizeMake(0, 1);
  144. promptLabel.textAlignment = UITextAlignmentCenter;
  145. promptLabel.numberOfLines = 0;
  146. [contentView addSubview:promptLabel];
  147. messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, PROMPT_HEIGHT+DIGIT_HEIGHT, contentView.bounds.size.width, MESSAGE_HEIGHT)];
  148. messageLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  149. messageLabel.backgroundColor = [UIColor clearColor];
  150. messageLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
  151. messageLabel.font = [UIFont systemFontOfSize:14];
  152. messageLabel.shadowColor = [UIColor whiteColor];
  153. messageLabel.shadowOffset = CGSizeMake(0, 1);
  154. messageLabel.textAlignment = UITextAlignmentCenter;
  155. messageLabel.numberOfLines = 0;
  156. messageLabel.text = _message;
  157. [contentView addSubview:messageLabel];
  158. UIImage *failedBg = [[UIImage imageNamed:@"papasscode_failed_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, FAILED_LCAP, 0, FAILED_RCAP)];
  159. failedImageView = [[UIImageView alloc] initWithImage:failedBg];
  160. failedImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  161. failedImageView.hidden = YES;
  162. [contentView addSubview:failedImageView];
  163. failedAttemptsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  164. failedAttemptsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  165. failedAttemptsLabel.backgroundColor = [UIColor clearColor];
  166. failedAttemptsLabel.textColor = [UIColor whiteColor];
  167. failedAttemptsLabel.font = [UIFont boldSystemFontOfSize:15];
  168. failedAttemptsLabel.shadowColor = [UIColor blackColor];
  169. failedAttemptsLabel.shadowOffset = CGSizeMake(0, -1);
  170. failedAttemptsLabel.textAlignment = UITextAlignmentCenter;
  171. failedAttemptsLabel.hidden = YES;
  172. [contentView addSubview:failedAttemptsLabel];
  173. self.view = view;
  174. }
  175. - (void)viewDidLoad {
  176. [super viewDidLoad];
  177. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidCancel:)]) {
  178. if (_simple) {
  179. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
  180. } else {
  181. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
  182. }
  183. }
  184. if (_failedAttempts > 0) {
  185. [self showFailedAttempts];
  186. }
  187. }
  188. - (void)viewWillAppear:(BOOL)animated {
  189. [super viewWillAppear:animated];
  190. [self showScreenForPhase:0 animated:NO];
  191. [passcodeTextField becomeFirstResponder];
  192. }
  193. - (NSUInteger)supportedInterfaceOrientations {
  194. return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
  195. }
  196. - (void)cancel:(id)sender {
  197. [_delegate PAPasscodeViewControllerDidCancel:self];
  198. }
  199. #pragma mark - implementation helpers
  200. - (void)handleCompleteField {
  201. NSString *text = passcodeTextField.text;
  202. switch (_action) {
  203. case PasscodeActionSet:
  204. if (phase == 0) {
  205. _passcode = text;
  206. messageLabel.text = @"";
  207. [self showScreenForPhase:1 animated:YES];
  208. } else {
  209. if ([text isEqualToString:_passcode]) {
  210. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidSetPasscode:)]) {
  211. [_delegate PAPasscodeViewControllerDidSetPasscode:self];
  212. }
  213. } else {
  214. [self showScreenForPhase:0 animated:YES];
  215. messageLabel.text = NSLocalizedString(@"PASSCODES_DID_NOT_MATCH", nil);
  216. }
  217. }
  218. break;
  219. case PasscodeActionEnter:
  220. if ([text isEqualToString:_passcode]) {
  221. [self resetFailedAttempts];
  222. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidEnterPasscode:)]) {
  223. [_delegate PAPasscodeViewControllerDidEnterPasscode:self];
  224. }
  225. } else {
  226. if (_alternativePasscode && [text isEqualToString:_alternativePasscode]) {
  227. [self resetFailedAttempts];
  228. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidEnterAlternativePasscode:)]) {
  229. [_delegate PAPasscodeViewControllerDidEnterAlternativePasscode:self];
  230. }
  231. } else {
  232. [self handleFailedAttempt];
  233. [self showScreenForPhase:0 animated:NO];
  234. }
  235. }
  236. break;
  237. case PasscodeActionChange:
  238. if (phase == 0) {
  239. if ([text isEqualToString:_passcode]) {
  240. [self resetFailedAttempts];
  241. [self showScreenForPhase:1 animated:YES];
  242. } else {
  243. [self handleFailedAttempt];
  244. [self showScreenForPhase:0 animated:NO];
  245. }
  246. } else if (phase == 1) {
  247. _passcode = text;
  248. messageLabel.text = @"";
  249. [self showScreenForPhase:2 animated:YES];
  250. } else {
  251. if ([text isEqualToString:_passcode]) {
  252. if ([_delegate respondsToSelector:@selector(PAPasscodeViewControllerDidChangePasscode:)]) {
  253. [_delegate PAPasscodeViewControllerDidChangePasscode:self];
  254. }
  255. } else {
  256. [self showScreenForPhase:1 animated:YES];
  257. messageLabel.text = NSLocalizedString(@"PASSCODES_DID_NOT_MATCH", nil);
  258. }
  259. }
  260. break;
  261. }
  262. }
  263. - (void)handleFailedAttempt {
  264. _failedAttempts++;
  265. [self showFailedAttempts];
  266. if ([_delegate respondsToSelector:@selector(PAPasscodeViewController:didFailToEnterPasscode:)]) {
  267. [_delegate PAPasscodeViewController:self didFailToEnterPasscode:_failedAttempts];
  268. }
  269. }
  270. - (void)resetFailedAttempts {
  271. messageLabel.hidden = NO;
  272. failedImageView.hidden = YES;
  273. failedAttemptsLabel.hidden = YES;
  274. _failedAttempts = 0;
  275. }
  276. - (void)showFailedAttempts {
  277. messageLabel.hidden = YES;
  278. failedImageView.hidden = NO;
  279. failedAttemptsLabel.hidden = NO;
  280. if (_failedAttempts == 1) {
  281. failedAttemptsLabel.text = NSLocalizedString(@"PASSCODE_FAILED_1", nil);
  282. } else {
  283. failedAttemptsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"PASSCODE_FAILED_FORMAT", nil), _failedAttempts];
  284. }
  285. [failedAttemptsLabel sizeToFit];
  286. CGFloat bgWidth = failedAttemptsLabel.bounds.size.width + FAILED_MARGIN*2;
  287. CGFloat x = floor((contentView.bounds.size.width-bgWidth)/2);
  288. CGFloat y = PROMPT_HEIGHT+DIGIT_HEIGHT+floor((MESSAGE_HEIGHT-FAILED_HEIGHT)/2);
  289. failedImageView.frame = CGRectMake(x, y, bgWidth, FAILED_HEIGHT);
  290. x = failedImageView.frame.origin.x+FAILED_MARGIN;
  291. y = failedImageView.frame.origin.y+floor((failedImageView.bounds.size.height-failedAttemptsLabel.frame.size.height)/2);
  292. failedAttemptsLabel.frame = CGRectMake(x, y, failedAttemptsLabel.bounds.size.width, failedAttemptsLabel.bounds.size.height);
  293. }
  294. - (void)passcodeChanged:(id)sender {
  295. NSString *text = passcodeTextField.text;
  296. if (_simple) {
  297. if ([text length] > 4) {
  298. text = [text substringToIndex:4];
  299. }
  300. for (int i=0;i<4;i++) {
  301. digitImageViews[i].hidden = i >= [text length];
  302. }
  303. if ([text length] == 4) {
  304. [self handleCompleteField];
  305. }
  306. } else {
  307. self.navigationItem.rightBarButtonItem.enabled = [text length] > 0;
  308. }
  309. }
  310. - (void)showScreenForPhase:(NSInteger)newPhase animated:(BOOL)animated {
  311. CGFloat dir = (newPhase > phase) ? 1 : -1;
  312. if (animated) {
  313. UIGraphicsBeginImageContext(self.view.bounds.size);
  314. [contentView.layer renderInContext:UIGraphicsGetCurrentContext()];
  315. UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
  316. UIGraphicsEndImageContext();
  317. snapshotImageView = [[UIImageView alloc] initWithImage:snapshot];
  318. snapshotImageView.frame = CGRectOffset(snapshotImageView.frame, -contentView.frame.size.width*dir, 0);
  319. [contentView addSubview:snapshotImageView];
  320. }
  321. phase = newPhase;
  322. passcodeTextField.text = @"";
  323. if (!_simple) {
  324. BOOL finalScreen = _action == PasscodeActionSet && phase == 1;
  325. finalScreen |= _action == PasscodeActionEnter && phase == 0;
  326. finalScreen |= _action == PasscodeActionChange && phase == 2;
  327. if (finalScreen) {
  328. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(handleCompleteField)];
  329. } else {
  330. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_NEXT", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(handleCompleteField)];
  331. }
  332. self.navigationItem.rightBarButtonItem.enabled = NO;
  333. }
  334. switch (_action) {
  335. case PasscodeActionSet:
  336. if (phase == 0) {
  337. promptLabel.text = _enterPrompt;
  338. } else {
  339. promptLabel.text = _confirmPrompt;
  340. }
  341. break;
  342. case PasscodeActionEnter:
  343. promptLabel.text = _enterPrompt;
  344. break;
  345. case PasscodeActionChange:
  346. if (phase == 0) {
  347. promptLabel.text = _changePrompt;
  348. } else if (phase == 1) {
  349. promptLabel.text = _enterPrompt;
  350. } else {
  351. promptLabel.text = _confirmPrompt;
  352. }
  353. break;
  354. }
  355. for (int i=0;i<4;i++) {
  356. digitImageViews[i].hidden = YES;
  357. }
  358. if (animated) {
  359. contentView.frame = CGRectOffset(contentView.frame, contentView.frame.size.width*dir, 0);
  360. [UIView animateWithDuration:SLIDE_DURATION animations:^() {
  361. contentView.frame = CGRectOffset(contentView.frame, -contentView.frame.size.width*dir, 0);
  362. } completion:^(BOOL finished) {
  363. [snapshotImageView removeFromSuperview];
  364. snapshotImageView = nil;
  365. }];
  366. }
  367. }
  368. @end