PAPasscodeViewController.m 19 KB

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