Merge branch 'charlesmchen/onboardingPhoneNumberFormatting'

pull/2/head
Matthew Chen 7 years ago
commit b74e2309f8

@ -103,7 +103,7 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController {
let validationWarningRow = UIView() let validationWarningRow = UIView()
validationWarningRow.addSubview(validationWarningLabel) validationWarningRow.addSubview(validationWarningLabel)
validationWarningLabel.autoPinHeightToSuperview() validationWarningLabel.autoPinHeightToSuperview()
validationWarningLabel.autoPinEdge(toSuperviewEdge: .leading) validationWarningLabel.autoPinEdge(toSuperviewEdge: .trailing)
let nextButton = self.button(title: NSLocalizedString("BUTTON_NEXT", let nextButton = self.button(title: NSLocalizedString("BUTTON_NEXT",
comment: "Label for the 'next' button."), comment: "Label for the 'next' button."),
@ -216,6 +216,9 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController {
phoneNumberTextField.isEnabled = false phoneNumberTextField.isEnabled = false
updateViewState() updateViewState()
// Trigger the formatting logic with a no-op edit.
_ = textField(phoneNumberTextField, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: "")
} }
// MARK: - // MARK: -
@ -250,6 +253,9 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController {
} }
updateViewState() updateViewState()
// Trigger the formatting logic with a no-op edit.
_ = textField(phoneNumberTextField, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: "")
} }
private func updateViewState() { private func updateViewState() {
@ -365,8 +371,7 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController {
extension OnboardingPhoneNumberViewController: UITextFieldDelegate { extension OnboardingPhoneNumberViewController: UITextFieldDelegate {
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 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, callingCode: callingCode)
ViewControllerUtils.phoneNumber(textField, shouldChangeCharactersIn: range, replacementString: string, countryCode: countryCode)
isPhoneNumberInvalid = false isPhoneNumberInvalid = false
updateValidationWarnings() updateValidationWarnings()

@ -552,7 +552,7 @@ NSString *const kKeychainKey_LastRegisteredPhoneNumber = @"kKeychainKey_LastRegi
[ViewControllerUtils phoneNumberTextField:textField [ViewControllerUtils phoneNumberTextField:textField
shouldChangeCharactersInRange:range shouldChangeCharactersInRange:range
replacementString:insertionText replacementString:insertionText
countryCode:_callingCode]; callingCode:_callingCode];
return NO; // inform our caller that we took care of performing the change return NO; // inform our caller that we took care of performing the change
} }

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

@ -17,10 +17,12 @@ extern NSString *const TappedStatusBarNotification;
// This convenience function can be used to reformat the contents of // This convenience function can be used to reformat the contents of
// a phone number text field as the user modifies its text by typing, // a phone number text field as the user modifies its text by typing,
// pasting, etc. // pasting, etc.
//
// "callingCode" should be of the form: "+1".
+ (void)phoneNumberTextField:(UITextField *)textField + (void)phoneNumberTextField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)insertionText replacementString:(NSString *)insertionText
countryCode:(NSString *)countryCode; callingCode:(NSString *)callingCode;
+ (void)ows2FAPINTextField:(UITextField *)textField + (void)ows2FAPINTextField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range shouldChangeCharactersInRange:(NSRange)range

@ -21,20 +21,7 @@ const NSUInteger kMax2FAPinLength = 16;
+ (void)phoneNumberTextField:(UITextField *)textField + (void)phoneNumberTextField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)insertionText replacementString:(NSString *)insertionText
countryCode:(NSString *)countryCode callingCode:(NSString *)callingCode
{
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
{ {
// Phone numbers takes many forms. // 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 // reformat the phone number, trying to keep the cursor beside the inserted or deleted digit
NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length); NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length);
NSString *textAfterReformat = NSString *textToFormat = textAfterChange;
[PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textAfterChange NSString *formattedText = [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textToFormat
withSpecifiedCountryCodeString:countryCode]; withSpecifiedCountryCodeString:callingCode];
NSUInteger cursorPositionAfterReformat = [PhoneNumberUtil translateCursorPosition:cursorPositionAfterChange NSUInteger cursorPositionAfterReformat = [PhoneNumberUtil translateCursorPosition:cursorPositionAfterChange
from:textAfterChange from:textToFormat
to:textAfterReformat to:formattedText
stickingRightward:isJustDeletion]; stickingRightward:isJustDeletion];
textField.text = textAfterReformat; textField.text = formattedText;
UITextPosition *pos = UITextPosition *pos =
[textField positionFromPosition:textField.beginningOfDocument offset:(NSInteger)cursorPositionAfterReformat]; [textField positionFromPosition:textField.beginningOfDocument offset:(NSInteger)cursorPositionAfterReformat];
[textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]]; [textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]];

Loading…
Cancel
Save