Respond to CR.

pull/2/head
Matthew Chen 7 years ago
parent ef5cd5344e
commit 4d4b840787

@ -371,7 +371,7 @@ public class OnboardingPhoneNumberViewController: OnboardingBaseViewController {
extension OnboardingPhoneNumberViewController: UITextFieldDelegate {
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
ViewControllerUtils.phoneNumber(textField, shouldChangeCharactersIn: range, replacementString: string, countryCode: countryCode, prefix: callingCode)
ViewControllerUtils.phoneNumber(textField, shouldChangeCharactersIn: range, replacementString: string, callingCode: callingCode)
isPhoneNumberInvalid = false
updateValidationWarnings()

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

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

@ -17,18 +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;
// If non-null, the prefix should represent the calling code
// prefix for the number, e.g. +1.
+ (void)phoneNumberTextField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)insertionText
countryCode:(NSString *)countryCode
prefix:(nullable NSString *)prefix;
callingCode:(NSString *)callingCode;
+ (void)ows2FAPINTextField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range

@ -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.
//
@ -92,79 +79,18 @@ const NSUInteger kMax2FAPinLength = 16;
NSString *textToFormat = textAfterChange;
NSString *formattedText = [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textToFormat
withSpecifiedCountryCodeString:countryCode];
withSpecifiedCountryCodeString:callingCode];
NSUInteger cursorPositionAfterReformat = [PhoneNumberUtil translateCursorPosition:cursorPositionAfterChange
from:textToFormat
to:formattedText
stickingRightward:isJustDeletion];
// PhoneNumber's formatting logic requires a calling code.
//
// If we want to edit the phone number separately from the calling code
// (e.g. in the new onboarding views), we need to temporarily prepend the
// calling code during formatting, then remove it afterward. This is
// non-trivial since the calling code itself can be affected by the
// formatting. Additionally, we need to ensure that this prepend/remove
// doesn't affect the cursor position.
BOOL hasPrefix = prefix.length > 0;
if (hasPrefix) {
// Prepend the prefix.
NSString *textToFormatWithPrefix = [prefix stringByAppendingString:textAfterChange];
// Format with the prefix.
NSString *formattedTextWithPrefix =
[PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textToFormatWithPrefix
withSpecifiedCountryCodeString:countryCode];
// Determine the new cursor position with the prefix.
NSUInteger cursorPositionWithPrefix = [PhoneNumberUtil translateCursorPosition:cursorPositionAfterChange
from:textToFormat
to:formattedTextWithPrefix
stickingRightward:isJustDeletion];
// Try to determine how much of the formatted text is derived
// from the prefix.
NSString *_Nullable formattedPrefix =
[self findFormattedPrefixForPrefix:prefix formattedText:formattedTextWithPrefix];
if (formattedPrefix && cursorPositionWithPrefix >= formattedPrefix.length) {
// Remove the prefix from the formatted text.
formattedText = [formattedTextWithPrefix substringFromIndex:formattedPrefix.length];
// Adjust the cursor position accordingly.
cursorPositionAfterReformat = cursorPositionWithPrefix - formattedPrefix.length;
}
}
textField.text = formattedText;
UITextPosition *pos =
[textField positionFromPosition:textField.beginningOfDocument offset:(NSInteger)cursorPositionAfterReformat];
[textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]];
}
+ (nullable NSString *)findFormattedPrefixForPrefix:(NSString *)prefix formattedText:(NSString *)formattedText
{
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:@"+0123456789"] invertedSet];
NSString *filteredPrefix =
[[prefix componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
NSString *filteredText =
[[formattedText componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
if (filteredPrefix.length < 1 || filteredText.length < 1 || ![filteredText hasPrefix:filteredPrefix]) {
OWSFailDebug(@"Invalid prefix: '%@' for formatted text: '%@'", prefix, formattedText);
return nil;
}
NSString *filteredTextWithoutPrefix = [filteredText substringFromIndex:filteredPrefix.length];
// To find the "formatted prefix", try to find the shortest "tail" of formattedText
// which after being filtered is equivalent to the "filtered text" - "filter prefix".
// The "formatted prefix" is the "head" that corresponds to that "tail".
for (NSUInteger substringLength = 1; substringLength < formattedText.length - 1; substringLength++) {
NSUInteger pivot = formattedText.length - substringLength;
NSString *head = [formattedText substringToIndex:pivot];
NSString *tail = [formattedText substringFromIndex:pivot];
NSString *filteredTail =
[[tail componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
if ([filteredTail isEqualToString:filteredTextWithoutPrefix]) {
return head;
}
}
return nil;
}
+ (void)ows2FAPINTextField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)insertionText

Loading…
Cancel
Save