diff --git a/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift b/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift index 7f9d8a7a7..bd521d2db 100644 --- a/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift +++ b/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift @@ -103,7 +103,7 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController { let validationWarningRow = UIView() validationWarningRow.addSubview(validationWarningLabel) validationWarningLabel.autoPinHeightToSuperview() - validationWarningLabel.autoPinEdge(toSuperviewEdge: .leading) + validationWarningLabel.autoPinEdge(toSuperviewEdge: .trailing) let nextButton = self.button(title: NSLocalizedString("BUTTON_NEXT", comment: "Label for the 'next' button."), @@ -216,6 +216,9 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController { phoneNumberTextField.isEnabled = false updateViewState() + + // Trigger the formatting logic with a no-op edit. + _ = textField(phoneNumberTextField, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: "") } // MARK: - @@ -250,6 +253,9 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController { } updateViewState() + + // Trigger the formatting logic with a no-op edit. + _ = textField(phoneNumberTextField, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: "") } private func updateViewState() { @@ -365,8 +371,7 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController { extension OnboardingPhoneNumberViewController: UITextFieldDelegate { public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { - // TODO: Fix auto-format of phone numbers. - ViewControllerUtils.phoneNumber(textField, shouldChangeCharactersIn: range, replacementString: string, countryCode: countryCode) + ViewControllerUtils.phoneNumber(textField, shouldChangeCharactersIn: range, replacementString: string, callingCode: callingCode) isPhoneNumberInvalid = false updateValidationWarnings() diff --git a/Signal/src/ViewControllers/Registration/RegistrationViewController.m b/Signal/src/ViewControllers/Registration/RegistrationViewController.m index 823d7385f..2a2a0d914 100644 --- a/Signal/src/ViewControllers/Registration/RegistrationViewController.m +++ b/Signal/src/ViewControllers/Registration/RegistrationViewController.m @@ -552,7 +552,7 @@ NSString *const kKeychainKey_LastRegisteredPhoneNumber = @"kKeychainKey_LastRegi [ViewControllerUtils phoneNumberTextField:textField shouldChangeCharactersInRange:range replacementString:insertionText - countryCode:_callingCode]; + callingCode:_callingCode]; return NO; // inform our caller that we took care of performing the change } diff --git a/SignalMessaging/ViewControllers/SelectRecipientViewController.m b/SignalMessaging/ViewControllers/SelectRecipientViewController.m index 01c06b2ce..006bd0c9c 100644 --- a/SignalMessaging/ViewControllers/SelectRecipientViewController.m +++ b/SignalMessaging/ViewControllers/SelectRecipientViewController.m @@ -423,7 +423,7 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien [ViewControllerUtils phoneNumberTextField:textField shouldChangeCharactersInRange:range replacementString:insertionText - countryCode:_callingCode]; + callingCode:_callingCode]; [self updatePhoneNumberButtonEnabling]; diff --git a/SignalMessaging/ViewControllers/ViewControllerUtils.h b/SignalMessaging/ViewControllers/ViewControllerUtils.h index 38b26561d..f027db313 100644 --- a/SignalMessaging/ViewControllers/ViewControllerUtils.h +++ b/SignalMessaging/ViewControllers/ViewControllerUtils.h @@ -17,10 +17,12 @@ extern NSString *const TappedStatusBarNotification; // This convenience function can be used to reformat the contents of // a phone number text field as the user modifies its text by typing, // pasting, etc. +// +// "callingCode" should be of the form: "+1". + (void)phoneNumberTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)insertionText - countryCode:(NSString *)countryCode; + callingCode:(NSString *)callingCode; + (void)ows2FAPINTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range diff --git a/SignalMessaging/ViewControllers/ViewControllerUtils.m b/SignalMessaging/ViewControllers/ViewControllerUtils.m index 441e3e797..96896f08a 100644 --- a/SignalMessaging/ViewControllers/ViewControllerUtils.m +++ b/SignalMessaging/ViewControllers/ViewControllerUtils.m @@ -21,20 +21,7 @@ const NSUInteger kMax2FAPinLength = 16; + (void)phoneNumberTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)insertionText - countryCode:(NSString *)countryCode -{ - return [self phoneNumberTextField:textField - shouldChangeCharactersInRange:range - replacementString:insertionText - countryCode:countryCode - prefix:nil]; -} - -+ (void)phoneNumberTextField:(UITextField *)textField - shouldChangeCharactersInRange:(NSRange)range - replacementString:(NSString *)insertionText - countryCode:(NSString *)countryCode - prefix:(nullable NSString *)prefix + callingCode:(NSString *)callingCode { // Phone numbers takes many forms. // @@ -90,15 +77,15 @@ const NSUInteger kMax2FAPinLength = 16; // reformat the phone number, trying to keep the cursor beside the inserted or deleted digit NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length); - NSString *textAfterReformat = - [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textAfterChange - withSpecifiedCountryCodeString:countryCode]; + NSString *textToFormat = textAfterChange; + NSString *formattedText = [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textToFormat + withSpecifiedCountryCodeString:callingCode]; NSUInteger cursorPositionAfterReformat = [PhoneNumberUtil translateCursorPosition:cursorPositionAfterChange - from:textAfterChange - to:textAfterReformat + from:textToFormat + to:formattedText stickingRightward:isJustDeletion]; - textField.text = textAfterReformat; + textField.text = formattedText; UITextPosition *pos = [textField positionFromPosition:textField.beginningOfDocument offset:(NSInteger)cursorPositionAfterReformat]; [textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]];