From c7ed09ed91fe8f8ad36b8a2197ea16ef75cfd2e6 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 17 Aug 2018 14:25:34 -0400 Subject: [PATCH] Fix 'can't delete formatting in phone number' issue. --- .../ViewControllers/ViewControllerUtils.m | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/SignalMessaging/ViewControllers/ViewControllerUtils.m b/SignalMessaging/ViewControllers/ViewControllerUtils.m index 6215e4c3e..180e74f83 100644 --- a/SignalMessaging/ViewControllers/ViewControllerUtils.m +++ b/SignalMessaging/ViewControllers/ViewControllerUtils.m @@ -48,6 +48,20 @@ const NSUInteger kMax2FAPinLength = 16; NSString *right = [oldText substringFromIndex:range.location + range.length].digitsOnly; // 3. Determining the "center" substring: the contents of the new insertion text. NSString *center = insertionText.digitsOnly; + + // 3a. If user hits backspace, they should always delete a _digit_ to the + // left of the cursor, even if the text _immediately_ to the left of + // cursor is "formatting text" (e.g. whitespace, a hyphen or a + // parentheses). + bool isJustDeletion = insertionText.length == 0; + if (isJustDeletion) { + NSString *deletedText = [oldText substringWithRange:range]; + BOOL didDeleteFormatting = (deletedText.length == 1 && deletedText.digitsOnly.length < 1); + if (didDeleteFormatting && left.length > 0) { + left = [left substringToIndex:left.length - 1]; + } + } + // 4. Construct the "raw" new text by concatenating left, center and right. NSString *textAfterChange = [[left stringByAppendingString:center] stringByAppendingString:right]; // 4a. Ensure we don't exceed the maximum length for a e164 phone number, @@ -61,7 +75,6 @@ const NSUInteger kMax2FAPinLength = 16; } // 5. Construct the "formatted" new text by inserting a hyphen if necessary. // reformat the phone number, trying to keep the cursor beside the inserted or deleted digit - bool isJustDeletion = insertionText.length == 0; NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length); NSString *textAfterReformat = [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textAfterChange