LinkMobileNoViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "LinkMobileNoViewController.h"
  21. #import "MyIdentityStore.h"
  22. #import "ServerAPIConnector.h"
  23. #import "MBProgressHUD.h"
  24. #import "PhoneNumberNormalizer.h"
  25. #import "MDMSetup.h"
  26. @interface LinkMobileNoViewController ()
  27. @end
  28. @implementation LinkMobileNoViewController {
  29. NSString *mobileNo;
  30. NSString *prevMobileNo;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. PhoneNumberNormalizer *normalizer = [PhoneNumberNormalizer sharedInstance];
  35. NSString *examplePhone = [normalizer examplePhoneNumberForRegion:[PhoneNumberNormalizer userRegion]];
  36. if (examplePhone != nil)
  37. self.mobileNoTextField.placeholder = examplePhone;
  38. [Colors updateKeyboardAppearanceFor:self.mobileNoTextField];
  39. self.tableView.rowHeight = 85.0;
  40. self.tableView.estimatedRowHeight = 85.0;
  41. }
  42. - (void)viewWillAppear:(BOOL)animated {
  43. [super viewWillAppear:animated];
  44. if ([MyIdentityStore sharedMyIdentityStore].linkedMobileNo != nil) {
  45. prevMobileNo = [NSString stringWithFormat:@"+%@", [MyIdentityStore sharedMyIdentityStore].linkedMobileNo];
  46. } else {
  47. prevMobileNo = @"";
  48. }
  49. _mobileNoTextField.text = prevMobileNo;
  50. MDMSetup *mdmSetup = [[MDMSetup alloc] initWithSetup:NO];
  51. if ([mdmSetup readonlyProfile]) {
  52. _mobileNoTextField.enabled = NO;
  53. } else {
  54. [_mobileNoTextField becomeFirstResponder];
  55. }
  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)cancelAction:(id)sender {
  67. [self dismissViewControllerAnimated:YES completion:nil];
  68. }
  69. - (IBAction)saveAction:(id)sender {
  70. if ([self.mobileNoTextField.text isEqualToString:prevMobileNo]) {
  71. /* no change - nothing to do */
  72. [self dismissViewControllerAnimated:YES completion:nil];
  73. return;
  74. }
  75. /* normalize phone number */
  76. if (self.mobileNoTextField.text.length > 0) {
  77. PhoneNumberNormalizer *normalizer = [PhoneNumberNormalizer sharedInstance];
  78. NSString *prettyMobileNo;
  79. mobileNo = [normalizer phoneNumberToE164:self.mobileNoTextField.text withDefaultRegion:[PhoneNumberNormalizer userRegion] prettyFormat:&prettyMobileNo];
  80. if (mobileNo == nil) {
  81. [UIAlertTemplate showAlertWithOwner:self title:NSLocalizedString(@"bad_phone_number_format_title", nil) message:NSLocalizedString(@"bad_phone_number_format_message", nil) actionOk:nil];
  82. return;
  83. }
  84. /* ask user whether our normalization is correct */
  85. UIAlertController *confirmAlert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"confirm_phone_number_title", nil) message:[NSString stringWithFormat:NSLocalizedString(@"confirm_phone_number_x", nil), prettyMobileNo] preferredStyle:UIAlertControllerStyleAlert];
  86. [confirmAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  87. [self doLinkMobileNo];
  88. }]];
  89. [confirmAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:nil]];
  90. [self presentViewController:confirmAlert animated:YES completion:nil];
  91. } else {
  92. /* unlink */
  93. mobileNo = @"";
  94. [self doLinkMobileNo];
  95. }
  96. }
  97. - (void)doLinkMobileNo {
  98. self.navigationItem.rightBarButtonItem.enabled = NO;
  99. [self.mobileNoTextField resignFirstResponder];
  100. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  101. ServerAPIConnector *conn = [[ServerAPIConnector alloc] init];
  102. [conn linkMobileNoWithStore:[MyIdentityStore sharedMyIdentityStore] mobileNo:mobileNo onCompletion:^(BOOL linked) {
  103. [MBProgressHUD hideHUDForView:self.view animated:YES];
  104. [self dismissViewControllerAnimated:YES completion:nil];
  105. } onError:^(NSError *error) {
  106. [MBProgressHUD hideHUDForView:self.view animated:YES];
  107. self.navigationItem.rightBarButtonItem.enabled = YES;
  108. [UIAlertTemplate showAlertWithOwner:self title:error.localizedDescription message:error.localizedFailureReason actionOk:nil];
  109. }];
  110. }
  111. #pragma mark - UITableViewDelegate
  112. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  113. return self.tableView.rowHeight;
  114. }
  115. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  116. return self.tableView.rowHeight;
  117. }
  118. @end