EnterCodeViewController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "EnterCodeViewController.h"
  21. #import "MyIdentityStore.h"
  22. #import "ServerAPIConnector.h"
  23. #import "MBProgressHUD.h"
  24. #import "PhoneNumberNormalizer.h"
  25. #import "BundleUtil.h"
  26. #import "UIImage+ColoredImage.h"
  27. #import "ThreemaFramework/ThreemaFramework-swift.h"
  28. #define kCallWaitTime 600
  29. @interface EnterCodeViewController ()
  30. @end
  31. @implementation EnterCodeViewController {
  32. NSTimer *updateTimer;
  33. BOOL callEnabled;
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. [Colors updateKeyboardAppearanceFor:self.codeTextField];
  38. UIImage *image = nil;
  39. switch ([Colors getTheme]) {
  40. case ColorThemeDark:
  41. case ColorThemeDarkWork:
  42. image = [BundleUtil imageNamed:@"Phone"];
  43. _phoneImageView.image = [image imageWithTint:[Colors fontNormal]];
  44. break;
  45. case ColorThemeLight:
  46. case ColorThemeLightWork:
  47. case ColorThemeUndefined:
  48. break;
  49. }
  50. }
  51. - (void)viewWillAppear:(BOOL)animated {
  52. [super viewWillAppear:animated];
  53. updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateView) userInfo:nil repeats:YES];
  54. [self updateView];
  55. [self.codeTextField becomeFirstResponder];
  56. }
  57. - (void)viewWillDisappear:(BOOL)animated {
  58. [updateTimer invalidate];
  59. updateTimer = nil;
  60. [super viewWillDisappear:animated];
  61. }
  62. - (void)updateView {
  63. if ([MyIdentityStore sharedMyIdentityStore].linkMobileNoStartDate != nil) {
  64. int secondsSinceStart = -[[MyIdentityStore sharedMyIdentityStore].linkMobileNoStartDate timeIntervalSinceNow];
  65. int waitRemainingSecs = kCallWaitTime - secondsSinceStart;
  66. if (waitRemainingSecs < 0)
  67. waitRemainingSecs = 0;
  68. int mins = waitRemainingSecs / 60;
  69. int secs = waitRemainingSecs - mins * 60;
  70. self.callMeCell.detailTextLabel.text = [NSString stringWithFormat:@"%02d:%02d", mins, secs];
  71. if (waitRemainingSecs == 0) {
  72. self.callMeCell.detailTextLabel.hidden = YES;
  73. self.callMeCell.textLabel.enabled = YES;
  74. self.callMeCell.imageView.alpha = 1.0;
  75. self.callMeCell.selectionStyle = UITableViewCellSelectionStyleBlue;
  76. callEnabled = YES;
  77. } else {
  78. self.callMeCell.detailTextLabel.hidden = NO;
  79. self.callMeCell.textLabel.enabled = NO;
  80. self.callMeCell.imageView.alpha = 0.5;
  81. self.callMeCell.selectionStyle = UITableViewCellSelectionStyleNone;
  82. callEnabled = NO;
  83. }
  84. } else {
  85. self.callMeCell.detailTextLabel.hidden = YES;
  86. self.callMeCell.textLabel.enabled = YES;
  87. self.callMeCell.imageView.alpha = 1.0;
  88. self.callMeCell.selectionStyle = UITableViewCellSelectionStyleBlue;
  89. callEnabled = YES;
  90. }
  91. }
  92. - (BOOL)shouldAutorotate {
  93. return YES;
  94. }
  95. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  96. if (SYSTEM_IS_IPAD) {
  97. return UIInterfaceOrientationMaskAll;
  98. }
  99. return UIInterfaceOrientationMaskAllButUpsideDown;
  100. }
  101. - (IBAction)cancelAction:(id)sender {
  102. [self dismissViewControllerAnimated:YES completion:nil];
  103. }
  104. - (IBAction)doneAction:(id)sender {
  105. if (self.codeTextField.text.length == 0)
  106. return;
  107. self.navigationItem.rightBarButtonItem.enabled = NO;
  108. [self.codeTextField resignFirstResponder];
  109. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  110. ServerAPIConnector *conn = [[ServerAPIConnector alloc] init];
  111. [conn linkMobileNoWithStore:[MyIdentityStore sharedMyIdentityStore] code:self.codeTextField.text onCompletion:^(BOOL linked) {
  112. [MBProgressHUD hideHUDForView:self.view animated:YES];
  113. [self dismissViewControllerAnimated:YES completion:nil];
  114. } onError:^(NSError *error) {
  115. [MBProgressHUD hideHUDForView:self.view animated:YES];
  116. self.navigationItem.rightBarButtonItem.enabled = YES;
  117. [UIAlertTemplate showAlertWithOwner:self title:error.localizedDescription message:error.localizedFailureReason actionOk:nil];
  118. }];
  119. }
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  121. if (indexPath.section == 1 && indexPath.row == 0) {
  122. /* call me */
  123. if (!callEnabled) {
  124. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  125. return;
  126. }
  127. [self.codeTextField resignFirstResponder];
  128. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"call_me_title", nil) message:NSLocalizedString(@"call_me_message", nil) preferredStyle:UIAlertControllerStyleAlert];
  129. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  130. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  131. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  132. ServerAPIConnector *connector = [[ServerAPIConnector alloc] init];
  133. [connector linkMobileNoRequestCallWithStore:[MyIdentityStore sharedMyIdentityStore] onCompletion:^{
  134. [MBProgressHUD hideHUDForView:self.view animated:YES];
  135. } onError:^(NSError *error) {
  136. [MBProgressHUD hideHUDForView:self.view animated:YES];
  137. UIAlertController *alert = [UIAlertController alertControllerWithTitle:error.localizedDescription message:error.localizedFailureReason preferredStyle:UIAlertControllerStyleAlert];
  138. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:nil]];
  139. [self presentViewController:alert animated:YES completion:nil];
  140. }];
  141. }]];
  142. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  143. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  144. }]];
  145. if (SYSTEM_IS_IPAD == YES) {
  146. UIPopoverPresentationController *popOver = alert.popoverPresentationController;
  147. alert.popoverPresentationController.sourceView = _callMeCell;
  148. alert.popoverPresentationController.sourceRect = _callMeCell.bounds;
  149. alert.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
  150. }
  151. [self presentViewController:alert animated:YES completion:nil];
  152. } else if (indexPath.section == 1 && indexPath.row == 1) {
  153. /* abort verification */
  154. [self.codeTextField resignFirstResponder];
  155. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"abort_verification_message", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  156. [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"abort_verification", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
  157. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  158. [MyIdentityStore sharedMyIdentityStore].linkMobileNoPending = NO;
  159. [MyIdentityStore sharedMyIdentityStore].linkMobileNoStartDate = nil;
  160. [MyIdentityStore sharedMyIdentityStore].linkMobileNoVerificationId = nil;
  161. [MyIdentityStore sharedMyIdentityStore].linkedMobileNo = nil;
  162. [self dismissViewControllerAnimated:YES completion:nil];
  163. }]];
  164. [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  165. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  166. }]];
  167. if (SYSTEM_IS_IPAD == YES) {
  168. UIPopoverPresentationController *popOver = actionSheet.popoverPresentationController;
  169. actionSheet.popoverPresentationController.sourceView = _abortCell;
  170. actionSheet.popoverPresentationController.sourceRect = _abortCell.bounds;
  171. actionSheet.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
  172. }
  173. [self presentViewController:actionSheet animated:YES completion:nil];
  174. }
  175. }
  176. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  177. if (section == 0) {
  178. if ([MyIdentityStore sharedMyIdentityStore].linkedMobileNo == nil)
  179. return nil;
  180. /* get linked number, format it and return it */
  181. PhoneNumberNormalizer *normalizer = [PhoneNumberNormalizer sharedInstance];
  182. NSString *intlMobileNo = [NSString stringWithFormat:@"+%@", [MyIdentityStore sharedMyIdentityStore].linkedMobileNo];
  183. NSString *prettyMobileNo = nil;
  184. [normalizer phoneNumberToE164:intlMobileNo withDefaultRegion:[PhoneNumberNormalizer userRegion] prettyFormat:&prettyMobileNo];
  185. return [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"number", nil), prettyMobileNo];
  186. }
  187. return nil;
  188. }
  189. @end