Merge branch 'charlesmchen/onboardingPhoneNumberFormatting'

This commit is contained in:
Matthew Chen 2019-02-19 14:00:06 -05:00
commit b74e2309f8
5 changed files with 20 additions and 26 deletions

View file

@ -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()

View file

@ -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
}

View file

@ -423,7 +423,7 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien
[ViewControllerUtils phoneNumberTextField:textField
shouldChangeCharactersInRange:range
replacementString:insertionText
countryCode:_callingCode];
callingCode:_callingCode];
[self updatePhoneNumberButtonEnabling];

View file

@ -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

View file

@ -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]];