From 54faff2db20966efb587b2f1027f6a3177b04f45 Mon Sep 17 00:00:00 2001
From: Matthew Chen <charlesmchen@gmail.com>
Date: Thu, 18 May 2017 09:40:56 -0400
Subject: [PATCH] Show alerts for missing or invalid phone numbers in
 registration view.

// FREEBIE
---
 .../RegistrationViewController.m              | 66 +++++++++++--------
 .../translations/en.lproj/Localizable.strings | 12 ++++
 2 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/Signal/src/ViewControllers/RegistrationViewController.m b/Signal/src/ViewControllers/RegistrationViewController.m
index 77d26514e..79b4f1ee7 100644
--- a/Signal/src/ViewControllers/RegistrationViewController.m
+++ b/Signal/src/ViewControllers/RegistrationViewController.m
@@ -119,44 +119,58 @@ static NSString *const kCodeSentSegue = @"codeSent";
 {
     DDLogInfo(@"called %s", __PRETTY_FUNCTION__);
 
-    NSString *alertTitleFormat = NSLocalizedString(@"EXISTING_USER_REGISTRATION_ALERT_TITLE",
-        @"during registration, embeds {{device type}}, e.g. \"iPhone\" or \"iPad\"");
-    NSString *alertTitle = [NSString stringWithFormat:alertTitleFormat, [UIDevice currentDevice].localizedModel];
-    NSString *alertBody = NSLocalizedString(@"EXISTING_USER_REGISTRATION_ALERT_BODY", @"during registration");
-    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
-                                                                             message:alertBody
-                                                                      preferredStyle:UIAlertControllerStyleAlert];
-
-    UIAlertAction *cancelAction =
-        [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:nil];
-    [alertController addAction:cancelAction];
-
-    [self presentViewController:alertController animated:YES completion:nil];
+    [OWSAlerts
+        showAlertWithTitle:
+            [NSString stringWithFormat:NSLocalizedString(@"EXISTING_USER_REGISTRATION_ALERT_TITLE",
+                                           @"during registration, embeds {{device type}}, e.g. \"iPhone\" or \"iPad\""),
+                      [UIDevice currentDevice].localizedModel]
+                   message:NSLocalizedString(@"EXISTING_USER_REGISTRATION_ALERT_BODY", @"during registration")];
 }
 
 - (IBAction)sendCodeAction:(id)sender {
-    NSString *phoneNumber = [NSString stringWithFormat:@"%@%@", _callingCode, _phoneNumberTextField.text];
+    NSString *phoneNumberText =
+        [_phoneNumberTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+    if (phoneNumberText.length < 1) {
+        [OWSAlerts
+            showAlertWithTitle:NSLocalizedString(@"REGISTRATION_VIEW_NO_PHONE_NUMBER_ALERT_TITLE",
+                                   @"Title of alert indicating that users needs to enter a phone number to register.")
+                       message:
+                           NSLocalizedString(@"REGISTRATION_VIEW_NO_PHONE_NUMBER_ALERT_MESSAGE",
+                               @"Message of alert indicating that users needs to enter a phone number to register.")];
+        return;
+    }
+    NSString *phoneNumber = [NSString stringWithFormat:@"%@%@", _callingCode, phoneNumberText];
     PhoneNumber *localNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumber];
+    NSString *parsedPhoneNumber = localNumber.toE164;
+    if (parsedPhoneNumber.length < 1) {
+        [OWSAlerts showAlertWithTitle:
+                       NSLocalizedString(@"REGISTRATION_VIEW_INVALID_PHONE_NUMBER_ALERT_TITLE",
+                           @"Title of alert indicating that users needs to enter a valid phone number to register.")
+                              message:NSLocalizedString(@"REGISTRATION_VIEW_INVALID_PHONE_NUMBER_ALERT_MESSAGE",
+                                          @"Message of alert indicating that users needs to enter a valid phone number "
+                                          @"to register.")];
+        return;
+    }
 
     [_sendCodeButton setEnabled:NO];
     [_spinnerView startAnimating];
     [_phoneNumberTextField resignFirstResponder];
 
-    [TSAccountManager registerWithPhoneNumber:localNumber.toE164
+    [TSAccountManager registerWithPhoneNumber:parsedPhoneNumber
         success:^{
-          [self performSegueWithIdentifier:@"codeSent" sender:self];
-          [_spinnerView stopAnimating];
+            [self performSegueWithIdentifier:@"codeSent" sender:self];
+            [_spinnerView stopAnimating];
         }
         failure:^(NSError *error) {
-          if (error.code == 400) {
-              [OWSAlerts showAlertWithTitle:NSLocalizedString(@"REGISTRATION_ERROR", nil)
-                                    message:NSLocalizedString(@"REGISTRATION_NON_VALID_NUMBER", nil)];
-          } else {
-              [OWSAlerts showAlertWithTitle:error.localizedDescription message:error.localizedRecoverySuggestion];
-          }
-
-          [_sendCodeButton setEnabled:YES];
-          [_spinnerView stopAnimating];
+            if (error.code == 400) {
+                [OWSAlerts showAlertWithTitle:NSLocalizedString(@"REGISTRATION_ERROR", nil)
+                                      message:NSLocalizedString(@"REGISTRATION_NON_VALID_NUMBER", nil)];
+            } else {
+                [OWSAlerts showAlertWithTitle:error.localizedDescription message:error.localizedRecoverySuggestion];
+            }
+
+            [_sendCodeButton setEnabled:YES];
+            [_spinnerView stopAnimating];
         }
         smsVerification:YES];
 }
diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings
index 5c40c61bf..b5887d7e9 100644
--- a/Signal/translations/en.lproj/Localizable.strings
+++ b/Signal/translations/en.lproj/Localizable.strings
@@ -997,6 +997,18 @@
 /* No comment provided by engineer. */
 "REGISTRATION_VERIFY_DEVICE" = "Activate This Device";
 
+/* Message of alert indicating that users needs to enter a valid phone number to register. */
+"REGISTRATION_VIEW_INVALID_PHONE_NUMBER_ALERT_MESSAGE" = "Please enter a valid phone number to register.";
+
+/* Title of alert indicating that users needs to enter a valid phone number to register. */
+"REGISTRATION_VIEW_INVALID_PHONE_NUMBER_ALERT_TITLE" = "Invalid Phone Number";
+
+/* Message of alert indicating that users needs to enter a phone number to register. */
+"REGISTRATION_VIEW_NO_PHONE_NUMBER_ALERT_MESSAGE" = "Please enter a phone number to register.";
+
+/* Title of alert indicating that users needs to enter a phone number to register. */
+"REGISTRATION_VIEW_NO_PHONE_NUMBER_ALERT_TITLE" = "No Phone Number";
+
 /* No comment provided by engineer. */
 "REJECT_CALL_BUTTON_TITLE" = "Reject";