BackupPasswordViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "BackupPasswordViewController.h"
  21. #import "BackupPasswordVerifyViewController.h"
  22. #import "UIDefines.h"
  23. @interface BackupPasswordViewController ()
  24. @end
  25. @implementation BackupPasswordViewController
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. [Colors updateKeyboardAppearanceFor:self.passwordField];
  30. self.tableView.rowHeight = 85.0;
  31. self.tableView.estimatedRowHeight = 85.0;
  32. }
  33. - (void)viewWillAppear:(BOOL)animated {
  34. [super viewWillAppear:animated];
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
  36. if (self.passwordField.text.length < kMinimumPasswordLength) {
  37. self.navigationItem.rightBarButtonItem.enabled = NO;
  38. } else {
  39. self.navigationItem.rightBarButtonItem.enabled = YES;
  40. }
  41. self.passwordField.delegate = self;
  42. [self.passwordField becomeFirstResponder];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated {
  45. [super viewWillDisappear:animated];
  46. self.passwordField.delegate = nil;
  47. [[NSNotificationCenter defaultCenter] removeObserver:UIApplicationDidEnterBackgroundNotification];
  48. }
  49. - (void)setPasswordTitle:(NSString *)passwordTitle {
  50. if (passwordTitle && [passwordTitle length] > 0) {
  51. _titleNavigationItem.title = passwordTitle;
  52. }
  53. }
  54. -(NSString *)passwordTitle {
  55. return _titleNavigationItem.title;
  56. }
  57. - (BOOL)shouldAutorotate {
  58. return YES;
  59. }
  60. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  61. if (SYSTEM_IS_IPAD) {
  62. return UIInterfaceOrientationMaskAll;
  63. }
  64. return UIInterfaceOrientationMaskAllButUpsideDown;
  65. }
  66. - (IBAction)nextAction:(id)sender {
  67. if (self.passwordField.text.length < kMinimumPasswordLength) {
  68. [UIAlertTemplate showAlertWithOwner:self title:NSLocalizedString(@"password_too_short_title", nil) message:NSLocalizedString(@"password_too_short_message", nil) actionOk:nil];
  69. return;
  70. } else {
  71. [self performSegueWithIdentifier:@"VerifyPassword" sender:self];
  72. }
  73. }
  74. - (IBAction)cancelAction:(id)sender {
  75. [self dismissViewControllerAnimated:YES completion:nil];
  76. }
  77. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  78. if ([segue.identifier isEqualToString:@"VerifyPassword"]) {
  79. BackupPasswordVerifyViewController *verifyVc = (BackupPasswordVerifyViewController*)segue.destinationViewController;
  80. verifyVc.chosenPassword = self.passwordField.text;
  81. }
  82. }
  83. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  84. NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
  85. if (newText.length < kMinimumPasswordLength) {
  86. self.navigationItem.rightBarButtonItem.enabled = NO;
  87. } else {
  88. self.navigationItem.rightBarButtonItem.enabled = YES;
  89. }
  90. return YES;
  91. }
  92. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  93. if (textField.text.length < kMinimumPasswordLength) {
  94. return NO;
  95. }
  96. return YES;
  97. }
  98. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  99. [self nextAction:textField];
  100. return NO;
  101. }
  102. #pragma mark - UITableViewDataSource
  103. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  104. return _passwordAdditionalText;
  105. }
  106. #pragma mark - Notifications
  107. - (void)applicationWillResignActiveNotification:(NSNotification*)note {
  108. [self dismissViewControllerAnimated:NO completion:nil];
  109. }
  110. @end