TOPasscodeSettingsViewController.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. //
  2. // TOPasscodeSettingsViewController.m
  3. //
  4. // Copyright 2017 Timothy Oliver. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #import "TOPasscodeSettingsViewController.h"
  23. #import "TOPasscodeInputField.h"
  24. #import "TOPasscodeSettingsKeypadView.h"
  25. #import "TOPasscodeSettingsWarningLabel.h"
  26. const CGFloat kTOPasscodeSettingsLabelInputSpacing = 15.0f;
  27. const CGFloat kTOPasscodeSettingsOptionsButtonOffset = 15.0f;
  28. const CGFloat kTOPasscodeKeypadMaxSizeRatio = 0.40f;
  29. const CGFloat kTOPasscodeKeypadMinHeight = 165.0f;
  30. const CGFloat kTOPasscodeKeypadMaxHeight = 330.0f;
  31. @interface TOPasscodeSettingsViewController ()
  32. @property (nonatomic, copy) NSString *potentialPasscode;
  33. /* Layout Calculations */
  34. @property (nonatomic, assign) CGFloat verticalMidPoint;
  35. @property (nonatomic, assign) CGRect keyboardFrame;
  36. @property (nonatomic, readonly) CGRect contentOverlapFrame; // Either the keypad or the system keyboard
  37. /* Views */
  38. @property (nonatomic, strong) UIView *containerView;
  39. @property (nonatomic, strong) UILabel *titleLabel;
  40. @property (nonatomic, strong) UILabel *errorLabel;
  41. @property (nonatomic, strong) UIButton *optionsButton;
  42. @property (nonatomic, strong) TOPasscodeInputField *inputField;
  43. @property (nonatomic, strong) TOPasscodeSettingsKeypadView *keypadView;
  44. @property (nonatomic, strong) TOPasscodeSettingsWarningLabel *warningLabel;
  45. /* Bar Items */
  46. @property (nonatomic, strong) UIBarButtonItem *nextBarButtonItem;
  47. @property (nonatomic, strong) UIBarButtonItem *doneBarButtonItem;
  48. @end
  49. @implementation TOPasscodeSettingsViewController
  50. #pragma mark - Object Creation -
  51. - (instancetype)initWithStyle:(TOPasscodeSettingsViewStyle)style
  52. {
  53. if (self = [self initWithNibName:nil bundle:nil]) {
  54. _style = style;
  55. [self setUp];
  56. }
  57. return self;
  58. }
  59. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  60. {
  61. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  62. [self setUp];
  63. }
  64. return self;
  65. }
  66. - (void)setUp
  67. {
  68. _failedPasscodeAttemptCount = 0;
  69. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  70. }
  71. - (void)dealloc
  72. {
  73. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
  74. }
  75. #pragma mark - View Set-up -
  76. - (void)viewDidLoad {
  77. [super viewDidLoad];
  78. __weak typeof(self) weakSelf = self;
  79. self.title = NSLocalizedString(@"Enter Passcode", @"");
  80. // Create container view
  81. self.containerView = [[UIView alloc] initWithFrame:CGRectZero];
  82. self.containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin
  83. | UIViewAutoresizingFlexibleBottomMargin;
  84. [self.view addSubview:self.containerView];
  85. // Create title label
  86. self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  87. self.titleLabel.font = [UIFont systemFontOfSize:17.0f];
  88. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  89. self.titleLabel.textColor = [UIColor blackColor];
  90. self.titleLabel.text = @"Enter your passcode";
  91. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  92. [self.titleLabel sizeToFit];
  93. [self.containerView addSubview:self.titleLabel];
  94. // Create number view
  95. self.inputField = [[TOPasscodeInputField alloc] init];
  96. self.inputField.tintColor = [UIColor blackColor];
  97. self.inputField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  98. self.inputField.passcodeCompletedHandler = ^(NSString *passcode) { [weakSelf inputViewDidCompletePasscode:passcode]; };
  99. [self.inputField sizeToFit];
  100. [self.containerView addSubview:self.inputField];
  101. // Create keypad view
  102. self.keypadView = [[TOPasscodeSettingsKeypadView alloc] initWithFrame:CGRectZero];
  103. self.keypadView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  104. [self.view addSubview:self.keypadView];
  105. // Create warning label view
  106. self.warningLabel = [[TOPasscodeSettingsWarningLabel alloc] initWithFrame:CGRectZero];
  107. self.warningLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  108. self.warningLabel.hidden = YES;
  109. [self.warningLabel sizeToFit];
  110. [self.containerView addSubview:self.warningLabel];
  111. // Create error label view
  112. self.errorLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  113. self.errorLabel.text = NSLocalizedString(@"Passcodes didn't match. Try again.", @"");
  114. self.errorLabel.textAlignment = NSTextAlignmentCenter;
  115. self.errorLabel.font = [UIFont systemFontOfSize:15.0f];
  116. self.errorLabel.numberOfLines = 0;
  117. self.errorLabel.hidden = YES;
  118. [self.errorLabel sizeToFit];
  119. [self.containerView addSubview:self.errorLabel];
  120. // Create Options button
  121. self.optionsButton = [UIButton buttonWithType:UIButtonTypeSystem];
  122. [self.optionsButton setTitle:NSLocalizedString(@"Passcode Options", @"") forState:UIControlStateNormal];
  123. self.optionsButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
  124. [self.optionsButton sizeToFit];
  125. self.optionsButton.hidden = true;
  126. [self.optionsButton addTarget:self action:@selector(optionsCodeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  127. [self.view addSubview:self.optionsButton];
  128. // Add callbacks for the keypad view
  129. self.keypadView.numberButtonTappedHandler = ^(NSInteger number) {
  130. NSString *numberString = [NSString stringWithFormat:@"%ld", (long)number];
  131. [weakSelf.inputField appendPasscodeCharacters:numberString animated:NO];
  132. };
  133. self.keypadView.deleteButtonTappedHandler = ^{ [weakSelf.inputField deletePasscodeCharactersOfCount:1 animated:NO]; };
  134. // Set height of the container view (This will never change)
  135. CGRect frame = self.containerView.frame;
  136. frame.size.width = self.view.bounds.size.width;
  137. frame.size.height = CGRectGetHeight(self.titleLabel.frame) + CGRectGetHeight(self.inputField.frame)
  138. + CGRectGetHeight(self.warningLabel.frame) + (kTOPasscodeSettingsLabelInputSpacing * 2.0f);
  139. self.containerView.frame = CGRectIntegral(frame);
  140. //Work out the vertical offset of the container view assuming the warning label doesn't count
  141. self.verticalMidPoint = CGRectGetHeight(self.titleLabel.frame) + CGRectGetHeight(self.inputField.frame)
  142. + kTOPasscodeSettingsLabelInputSpacing;
  143. self.verticalMidPoint *= 0.5f;
  144. // Bar button items
  145. self.nextBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Next", @"") style:UIBarButtonItemStylePlain target:self action:@selector(nextButtonTapped:)];
  146. self.doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped:)];
  147. // Apply light/dark mode
  148. [self applyThemeForStyle:self.style];
  149. }
  150. - (void)viewWillAppear:(BOOL)animated
  151. {
  152. [super viewWillAppear:animated];
  153. self.state = self.requireCurrentPasscode ? TOPasscodeSettingsViewStateEnterCurrentPasscode : TOPasscodeSettingsViewStateEnterNewPasscode;
  154. [self updateContentForState:self.state type:self.passcodeType animated:NO];
  155. }
  156. #pragma mark - View Update -
  157. - (void)updateContentForState:(TOPasscodeSettingsViewState)state type:(TOPasscodeType)type animated:(BOOL)animated
  158. {
  159. BOOL variableSizePasscode = (type >= TOPasscodeTypeCustomNumeric);
  160. // Update the visibility of the options button
  161. //self.optionsButton.hidden = !(state == TOPasscodeSettingsViewStateEnterNewPasscode);
  162. // Clear the input view
  163. self.inputField.passcode = nil;
  164. // Disable the input view
  165. self.inputField.enabled = NO;
  166. //Update the warning label
  167. [self updateWarningLabelForState:state];
  168. // Change the input view if needed
  169. if (!variableSizePasscode) {
  170. self.inputField.style = TOPasscodeInputFieldStyleFixed;
  171. self.inputField.fixedInputView.length = (self.passcodeType == TOPasscodeTypeSixDigits) ? 6 : 4;
  172. }
  173. else {
  174. self.inputField.style = TOPasscodeInputFieldStyleVariable;
  175. }
  176. // Update text depending on state
  177. switch (state) {
  178. case TOPasscodeSettingsViewStateEnterCurrentPasscode:
  179. self.titleLabel.text = NSLocalizedString(@"Enter your passcode", @"");
  180. self.navigationItem.rightBarButtonItem = variableSizePasscode ? self.nextBarButtonItem : nil;
  181. if (@available(iOS 9.0, *)) {
  182. self.inputField.returnKeyType = UIReturnKeyContinue;
  183. }
  184. else {
  185. self.inputField.returnKeyType = UIReturnKeyNext;
  186. }
  187. break;
  188. case TOPasscodeSettingsViewStateEnterNewPasscode:
  189. self.titleLabel.text = NSLocalizedString(@"Enter a new passcode", @"");
  190. self.navigationItem.rightBarButtonItem = variableSizePasscode ? self.nextBarButtonItem : nil;
  191. if (@available(iOS 9.0, *)) {
  192. self.inputField.returnKeyType = UIReturnKeyContinue;
  193. }
  194. else {
  195. self.inputField.returnKeyType = UIReturnKeyNext;
  196. }
  197. break;
  198. case TOPasscodeSettingsViewStateConfirmNewPasscode:
  199. self.titleLabel.text = NSLocalizedString(@"Confirm new passcode", @"");
  200. self.navigationItem.rightBarButtonItem = variableSizePasscode ? self.doneBarButtonItem : nil;
  201. self.inputField.returnKeyType = UIReturnKeyDone;
  202. break;
  203. }
  204. CGRect frame = CGRectZero;
  205. // Reload the 'Done' button
  206. [self.inputField reloadInputViews];
  207. // Resize text label to fit new text
  208. [self.titleLabel sizeToFit];
  209. frame = self.titleLabel.frame;
  210. frame.origin.x = (CGRectGetWidth(self.containerView.frame) - CGRectGetWidth(frame)) * 0.5f;
  211. self.titleLabel.frame = CGRectIntegral(frame);
  212. // Resize passcode view
  213. [self.inputField sizeToFit];
  214. frame = self.inputField.frame;
  215. frame.origin.x = (CGRectGetWidth(self.containerView.frame) - CGRectGetWidth(frame)) * 0.5f;
  216. self.inputField.frame = CGRectIntegral(frame);
  217. // If we're the alphanumeric type, present the keyboard
  218. if (type == TOPasscodeTypeCustomAlphanumeric) {
  219. self.inputField.enabled = YES;
  220. [self.inputField becomeFirstResponder];
  221. }
  222. else {
  223. if (self.inputField.isFirstResponder) {
  224. [self.inputField resignFirstResponder];
  225. }
  226. }
  227. // If not animated, force a blanket re-layout
  228. if (!animated) {
  229. [self viewDidLayoutSubviews];
  230. return;
  231. }
  232. // If animated, perform the animation
  233. [UIView animateWithDuration:0.3f animations:^{
  234. [self viewDidLayoutSubviews];
  235. }];
  236. }
  237. - (void)updateWarningLabelForState:(TOPasscodeSettingsViewState)state
  238. {
  239. BOOL confirmingPasscode = state == TOPasscodeSettingsViewStateEnterCurrentPasscode;
  240. // Update the warning label
  241. self.warningLabel.hidden = !(confirmingPasscode && self.failedPasscodeAttemptCount > 0);
  242. self.warningLabel.numberOfWarnings = self.failedPasscodeAttemptCount;
  243. CGRect frame = self.warningLabel.frame;
  244. frame.origin.x = (CGRectGetWidth(self.view.frame) - frame.size.width) * 0.5f;
  245. self.warningLabel.frame = frame;
  246. }
  247. - (void)transitionToState:(TOPasscodeSettingsViewState)state animated:(BOOL)animated
  248. {
  249. // Preserve the current view state
  250. UIView *snapshot = nil;
  251. BOOL reverseDirection = state < self.state;
  252. // If animated, take a snapshot of the current container view
  253. if (animated) {
  254. snapshot = [self.containerView snapshotViewAfterScreenUpdates:NO];
  255. snapshot.frame = self.containerView.frame;
  256. [self.view addSubview:snapshot];
  257. }
  258. self.errorLabel.hidden = YES;
  259. // Update the layout for the new state
  260. self.state = state;
  261. // Cancel out now if we're not animating
  262. if (!animated) {
  263. return;
  264. }
  265. // Place the live container off screen to the right
  266. CGFloat multiplier = reverseDirection ? -1.0f : 1.0f;
  267. self.containerView.frame = CGRectOffset(self.containerView.frame, self.view.frame.size.width * multiplier, 0.0f);
  268. // Update the options button alpha depending on transition state
  269. //self.optionsButton.hidden = NO;
  270. self.optionsButton.alpha = (state == TOPasscodeSettingsViewStateEnterNewPasscode) ? 0.0f : 1.0f;
  271. // Perform an animation where the snapshot slides off, and the new container slides in
  272. id animationBlock = ^{
  273. snapshot.frame = CGRectOffset(snapshot.frame, -self.view.frame.size.width * multiplier, 0.0f);
  274. self.containerView.frame = CGRectOffset(self.containerView.frame, -self.view.frame.size.width * multiplier, 0.0f);
  275. self.optionsButton.alpha = (state == TOPasscodeSettingsViewStateEnterNewPasscode) ? 1.0f : 0.0f;
  276. };
  277. // Clean up by removing the snapshot view
  278. id completionBlock = ^(BOOL complete) {
  279. [snapshot removeFromSuperview];
  280. };
  281. // Perform the animation
  282. [UIView animateWithDuration:0.4f
  283. delay:0.0f
  284. usingSpringWithDamping:1.0f
  285. initialSpringVelocity:0.7f
  286. options:0
  287. animations:animationBlock
  288. completion:completionBlock];
  289. }
  290. - (void)viewDidLayoutSubviews
  291. {
  292. [super viewDidLayoutSubviews];
  293. CGSize viewSize = self.view.bounds.size;
  294. // Layout the keypad view
  295. CGRect frame = self.keypadView.frame;
  296. frame.size.height = viewSize.height * kTOPasscodeKeypadMaxSizeRatio;
  297. frame.size.height = MAX(frame.size.height, kTOPasscodeKeypadMinHeight);
  298. frame.size.height = MIN(frame.size.height, kTOPasscodeKeypadMaxHeight);
  299. frame.size.width = viewSize.width;
  300. frame.origin.y = viewSize.height;
  301. if (self.passcodeType != TOPasscodeTypeCustomAlphanumeric) {
  302. frame.origin.y -= frame.size.height;
  303. }
  304. self.keypadView.frame = CGRectIntegral(frame);
  305. BOOL horizontalLayout = frame.size.height < kTOPasscodeKeypadMinHeight + FLT_EPSILON;
  306. BOOL animated = ([self.view.layer animationForKey:@"bounds.size"] != nil);
  307. [self.keypadView setButtonLabelHorizontalLayout:horizontalLayout animated:animated];
  308. CGFloat topContentHeight = self.topLayoutGuide.length;
  309. // Layout the container view
  310. frame = self.containerView.frame;
  311. frame.origin.y = (((viewSize.height - (topContentHeight + self.contentOverlapFrame.size.height))) * 0.5f) - self.verticalMidPoint;
  312. frame.origin.y += topContentHeight;
  313. self.containerView.frame = CGRectIntegral(frame);
  314. // Layout the passcode options button
  315. frame = self.optionsButton.frame;
  316. frame.origin.y = CGRectGetMinY(self.contentOverlapFrame) - kTOPasscodeSettingsOptionsButtonOffset - CGRectGetHeight(frame);
  317. frame.origin.x = (CGRectGetWidth(self.view.frame) - CGRectGetWidth(frame)) * 0.5f;
  318. self.optionsButton.frame = frame;
  319. // Set frame of title label
  320. frame = self.titleLabel.frame;
  321. frame.origin.x = (CGRectGetWidth(self.view.frame) - CGRectGetWidth(frame)) * 0.5f;
  322. self.titleLabel.frame = CGRectIntegral(frame);
  323. // Set frame of number pad
  324. frame = self.inputField.frame;
  325. frame.origin.x = (CGRectGetWidth(self.view.frame) - CGRectGetWidth(frame)) * 0.5f;
  326. frame.origin.y = (CGRectGetHeight(self.titleLabel.frame) + kTOPasscodeSettingsLabelInputSpacing);
  327. self.inputField.frame = CGRectIntegral(frame);
  328. // Set the frame for the warning view
  329. frame = self.warningLabel.frame;
  330. frame.origin.x = (CGRectGetWidth(self.view.frame) - CGRectGetWidth(frame)) * 0.5f;
  331. frame.origin.y = CGRectGetMaxY(self.inputField.frame) + kTOPasscodeSettingsLabelInputSpacing;
  332. self.warningLabel.frame = CGRectIntegral(frame);
  333. // Set the frame of the error view
  334. frame = self.errorLabel.frame;
  335. frame.size = [self.errorLabel sizeThatFits:CGSizeMake(300.0f, CGFLOAT_MAX)];
  336. frame.origin.y = CGRectGetMaxY(self.inputField.frame) + kTOPasscodeSettingsLabelInputSpacing;
  337. frame.origin.x = (CGRectGetWidth(self.containerView.frame) - CGRectGetWidth(frame)) * 0.5f;
  338. self.errorLabel.frame = CGRectIntegral(frame);
  339. }
  340. - (void)applyThemeForStyle:(TOPasscodeSettingsViewStyle)style
  341. {
  342. BOOL isDark = (style == TOPasscodeSettingsViewStyleDark);
  343. // Set background color
  344. UIColor *backgroundColor;
  345. if (isDark) {
  346. backgroundColor = [UIColor colorWithWhite:0.15f alpha:1.0f];
  347. }
  348. else {
  349. backgroundColor = [UIColor colorWithRed:235.0f/255.0f green:235.0f/255.0f blue:241.0f/255.0f alpha:1.0f];
  350. }
  351. self.view.backgroundColor = backgroundColor;
  352. // Set the style of the keypad view
  353. self.keypadView.style = style;
  354. // Set the color for the input content
  355. UIColor *inputColor = isDark ? [UIColor whiteColor] : [UIColor blackColor];
  356. // Set the label style
  357. self.titleLabel.textColor = inputColor;
  358. // Set the number input tint
  359. self.inputField.tintColor = inputColor;
  360. // Set the tint color of the incorrect warning label
  361. UIColor *warningColor = nil;
  362. if (isDark) {
  363. warningColor = [UIColor colorWithRed:214.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0f];
  364. }
  365. else {
  366. warningColor = [UIColor colorWithRed:214.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0f];
  367. }
  368. }
  369. #pragma mark - Data Management -
  370. - (void)inputViewDidCompletePasscode:(NSString *)passcode
  371. {
  372. switch (self.state) {
  373. case TOPasscodeSettingsViewStateEnterCurrentPasscode:
  374. [self validateCurrentPasscodeAttemptWithPasscode:passcode];
  375. break;
  376. case TOPasscodeSettingsViewStateEnterNewPasscode:
  377. [self didReceiveNewPasscode:passcode];
  378. break;
  379. case TOPasscodeSettingsViewStateConfirmNewPasscode:
  380. [self confirmNewPasscode:passcode];
  381. break;
  382. }
  383. }
  384. - (void)validateCurrentPasscodeAttemptWithPasscode:(NSString *)passcode
  385. {
  386. if (![self.delegate respondsToSelector:@selector(passcodeSettingsViewController:didAttemptCurrentPasscode:)]) {
  387. return;
  388. }
  389. BOOL correct = [self.delegate passcodeSettingsViewController:self didAttemptCurrentPasscode:passcode];
  390. if (!correct) {
  391. [self.inputField resetPasscodeAnimated:YES playImpact:YES];
  392. self.failedPasscodeAttemptCount++;
  393. }
  394. else {
  395. [self transitionToState:TOPasscodeSettingsViewStateEnterNewPasscode animated:YES];
  396. }
  397. }
  398. - (void)didReceiveNewPasscode:(NSString *)passcode
  399. {
  400. self.potentialPasscode = passcode;
  401. [self transitionToState:TOPasscodeSettingsViewStateConfirmNewPasscode animated:YES];
  402. }
  403. - (void)confirmNewPasscode:(NSString *)passcode
  404. {
  405. if (![passcode isEqualToString:self.potentialPasscode]) {
  406. [self transitionToState:TOPasscodeSettingsViewStateEnterNewPasscode animated:YES];
  407. self.errorLabel.hidden = NO;
  408. return;
  409. }
  410. if (![self.delegate respondsToSelector:@selector(passcodeSettingsViewController:didChangeToNewPasscode:ofType:)]) {
  411. return;
  412. }
  413. [self.delegate passcodeSettingsViewController:self didChangeToNewPasscode:self.potentialPasscode ofType:self.passcodeType];
  414. }
  415. #pragma mark - System Keyboard Handling -
  416. - (void)keyboardWillChangeFrame:(NSNotification *)notification
  417. {
  418. self.keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  419. [self viewDidLayoutSubviews];
  420. }
  421. - (CGRect)contentOverlapFrame
  422. {
  423. if (self.passcodeType < TOPasscodeTypeCustomAlphanumeric) {
  424. return self.keypadView.frame;
  425. }
  426. // Work out where our view is in relation to the screen
  427. UIWindow *window = self.view.window;
  428. CGRect viewFrame = [self.view.superview convertRect:self.view.frame toView:window];
  429. CGFloat overlap = CGRectGetMaxY(viewFrame) - CGRectGetMinY(self.keyboardFrame);
  430. CGRect overlapFrame = self.keyboardFrame;
  431. overlapFrame.origin.y = MIN(viewFrame.size.height - overlap, viewFrame.size.height);
  432. overlapFrame.size.height = MAX(overlap, 0.0f);
  433. return overlapFrame;
  434. }
  435. #pragma mark - Button Callbacks -
  436. - (void)optionsCodeButtonTapped:(id)sender
  437. {
  438. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  439. UIAlertActionStyle style = UIAlertActionStyleDefault;
  440. __weak typeof(self) weakSelf = self;
  441. NSArray *types = @[@(TOPasscodeTypeFourDigits),
  442. @(TOPasscodeTypeSixDigits),
  443. @(TOPasscodeTypeCustomNumeric),
  444. @(TOPasscodeTypeCustomAlphanumeric)
  445. ];
  446. NSArray *titles = @[NSLocalizedString(@"4-Digit Numeric Code", @""),
  447. NSLocalizedString(@"6-Digit Numeric Code", @""),
  448. NSLocalizedString(@"Custom Numeric Code", @""),
  449. NSLocalizedString(@"Custom Alphanumeric Code", @"")];
  450. // Add all the buttons
  451. for (NSInteger i = 0; i < types.count; i++) {
  452. TOPasscodeType type = [types[i] integerValue];
  453. if (type == self.passcodeType) { continue; }
  454. id handler = ^(UIAlertAction *action) {
  455. [weakSelf setPasscodeType:type];
  456. };
  457. [alertController addAction:[UIAlertAction actionWithTitle:titles[i] style:style handler:handler]];
  458. }
  459. // Cancel button
  460. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"") style:UIAlertActionStyleCancel handler:nil]];
  461. alertController.modalPresentationStyle = UIModalPresentationPopover;
  462. alertController.popoverPresentationController.sourceView = self.optionsButton;
  463. alertController.popoverPresentationController.sourceRect = self.optionsButton.bounds;
  464. alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown | UIPopoverArrowDirectionUp;
  465. [self presentViewController:alertController animated:YES completion:nil];
  466. }
  467. - (void)nextButtonTapped:(id)sender
  468. {
  469. [self inputViewDidCompletePasscode:self.inputField.passcode];
  470. }
  471. - (void)doneButtonTapped:(id)sender
  472. {
  473. [self inputViewDidCompletePasscode:self.inputField.passcode];
  474. }
  475. #pragma mark - Accessors -
  476. - (void)setPasscodeType:(TOPasscodeType)passcodeType
  477. {
  478. [self setPasscodeType:passcodeType animated:NO];
  479. }
  480. - (void)setPasscodeType:(TOPasscodeType)passcodeType animated:(BOOL)animated
  481. {
  482. if (_passcodeType == passcodeType) { return; }
  483. _passcodeType = passcodeType;
  484. [self updateContentForState:self.state type:_passcodeType animated:animated];
  485. }
  486. - (void)setState:(TOPasscodeSettingsViewState)state
  487. {
  488. if (_state == state) { return; }
  489. _state = state;
  490. [self updateContentForState:_state type:self.passcodeType animated:NO];
  491. }
  492. - (void)setFailedPasscodeAttemptCount:(NSInteger)failedPasscodeAttemptCount
  493. {
  494. if (_failedPasscodeAttemptCount == failedPasscodeAttemptCount) { return; }
  495. _failedPasscodeAttemptCount = failedPasscodeAttemptCount;
  496. [self updateWarningLabelForState:self.state];
  497. }
  498. @end