From 4d4b840787e7e6e5743c4b9010bb70ccddc42758 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 19 Feb 2019 13:59:49 -0500 Subject: [PATCH] Respond to CR. --- .../OnboardingPhoneNumberViewController.swift | 2 +- .../Registration/RegistrationViewController.m | 2 +- .../SelectRecipientViewController.m | 2 +- .../ViewControllers/ViewControllerUtils.h | 12 +-- .../ViewControllers/ViewControllerUtils.m | 78 +------------------ 5 files changed, 8 insertions(+), 88 deletions(-) diff --git a/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift b/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift index 724fb6871..bd521d2db 100644 --- a/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift +++ b/Signal/src/ViewControllers/Registration/OnboardingPhoneNumberViewController.swift @@ -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() 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 ed30d0e4c..f027db313 100644 --- a/SignalMessaging/ViewControllers/ViewControllerUtils.h +++ b/SignalMessaging/ViewControllers/ViewControllerUtils.h @@ -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 diff --git a/SignalMessaging/ViewControllers/ViewControllerUtils.m b/SignalMessaging/ViewControllers/ViewControllerUtils.m index c8023e942..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. // @@ -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