From 922d48904f4fc4f5360099b23ad15a55b62dcded Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 31 Mar 2017 11:57:58 -0400 Subject: [PATCH] Refine BlockListViewController and AddToBlockListViewController. // FREEBIE --- .../AddToBlockListViewController.m | 42 +++++++++---------- .../CountryCodeViewController.h | 1 + .../CountryCodeViewController.m | 6 ++- .../RegistrationViewController.m | 10 ++--- .../src/ViewControllers/ViewControllerUtils.m | 2 +- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Signal/src/ViewControllers/AddToBlockListViewController.m b/Signal/src/ViewControllers/AddToBlockListViewController.m index b754ed455..68eeb5408 100644 --- a/Signal/src/ViewControllers/AddToBlockListViewController.m +++ b/Signal/src/ViewControllers/AddToBlockListViewController.m @@ -31,8 +31,7 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList @property (nonatomic) UIButton *blockButton; -@property (nonatomic) NSString *lastCallingCode; -@property (nonatomic) NSString *lastCountryCode; +@property (nonatomic) NSString *callingCode; @end @@ -194,9 +193,8 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList - (void)updateCountryWithName:(NSString *)countryName callingCode:(NSString *)callingCode countryCode:(NSString *)countryCode { - - _lastCallingCode = callingCode; - _lastCountryCode = countryCode; + + _callingCode = callingCode; NSString *title = [NSString stringWithFormat:@"%@ (%@)", callingCode, @@ -206,9 +204,9 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList [_countryCodeButton layoutSubviews]; } -- (void)setLastCallingCode:(NSString *)lastCallingCode +- (void)setcallingCode:(NSString *)callingCode { - _lastCallingCode = lastCallingCode; + _callingCode = callingCode; [self updateBlockButtonEnabling]; } @@ -216,11 +214,13 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList #pragma mark - Actions - (void)showCountryCodeView:(id)sender { - CountryCodeViewController *countryCodeController = [CountryCodeViewController new]; + CountryCodeViewController *countryCodeController = [[UIStoryboard storyboardWithName:@"Registration" bundle:NULL] + instantiateViewControllerWithIdentifier:@"CountryCodeViewController"]; countryCodeController.delegate = self; - [self presentViewController:countryCodeController - animated:YES - completion:[UIUtil modalCompletionBlock]]; + countryCodeController.shouldDismissWithoutSegue = YES; + UINavigationController *navigationController = + [[UINavigationController alloc] initWithRootViewController:countryCodeController]; + [self presentViewController:navigationController animated:YES completion:[UIUtil modalCompletionBlock]]; } - (void)blockButtonPressed:(id)sender @@ -230,14 +230,13 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList - (void)tryToBlockPhoneNumber { - if ([self hasValidPhoneNumber]) { + if (![self hasValidPhoneNumber]) { return; } - NSString *possiblePhoneNumber = - [self.lastCallingCode stringByAppendingString:_phoneNumberTextField.text.digitsOnly]; + NSString *possiblePhoneNumber = [self.callingCode stringByAppendingString:_phoneNumberTextField.text.digitsOnly]; PhoneNumber *parsedPhoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:possiblePhoneNumber]; - OWSAssert(parsedPhoneNumber && parsedPhoneNumber.isValid); + OWSAssert(parsedPhoneNumber); [_blockingManager addBlockedPhoneNumber:[parsedPhoneNumber toE164]]; @@ -268,13 +267,15 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList // TODO: We could also do this in registration view. - (BOOL)hasValidPhoneNumber { - if (!self.lastCallingCode) { + if (!self.callingCode) { return NO; } - NSString *possiblePhoneNumber = - [self.lastCallingCode stringByAppendingString:_phoneNumberTextField.text.digitsOnly]; + NSString *possiblePhoneNumber = [self.callingCode stringByAppendingString:_phoneNumberTextField.text.digitsOnly]; PhoneNumber *parsedPhoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:possiblePhoneNumber]; - return parsedPhoneNumber && parsedPhoneNumber.isValid; + // It'd be nice to use [PhoneNumber isValid] but it always returns false for some countries + // (like afghanistan) and there doesn't seem to be a good way to determine beforehand + // which countries it can validate for without forking libPhoneNumber. + return parsedPhoneNumber && parsedPhoneNumber.toE164.length > 1; } - (void)updateBlockButtonEnabling @@ -309,11 +310,10 @@ NSString * const kAddToBlockListViewControllerCellIdentifier = @"kAddToBlockList shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)insertionText { - [ViewControllerUtils phoneNumberTextField:textField shouldChangeCharactersInRange:range replacementString:insertionText - countryCode:_lastCountryCode]; + countryCode:_callingCode]; [self updateBlockButtonEnabling]; diff --git a/Signal/src/ViewControllers/CountryCodeViewController.h b/Signal/src/ViewControllers/CountryCodeViewController.h index d5ffa372f..ef13036ff 100644 --- a/Signal/src/ViewControllers/CountryCodeViewController.h +++ b/Signal/src/ViewControllers/CountryCodeViewController.h @@ -24,5 +24,6 @@ @property (nonatomic) NSString *countryCodeSelected; @property (nonatomic) NSString *callingCodeSelected; @property (nonatomic) NSString *countryNameSelected; +@property (nonatomic) BOOL shouldDismissWithoutSegue; @end diff --git a/Signal/src/ViewControllers/CountryCodeViewController.m b/Signal/src/ViewControllers/CountryCodeViewController.m index 3f0b014c2..990ba384b 100644 --- a/Signal/src/ViewControllers/CountryCodeViewController.m +++ b/Signal/src/ViewControllers/CountryCodeViewController.m @@ -61,7 +61,11 @@ static NSString *const kUnwindToCountryCodeWasSelectedSegue = @"UnwindToCountryC countryName:_countryNameSelected callingCode:_callingCodeSelected]; [self.searchBar resignFirstResponder]; - [self performSegueWithIdentifier:kUnwindToCountryCodeWasSelectedSegue sender:self]; + if (self.shouldDismissWithoutSegue) { + [self dismissViewControllerAnimated:YES completion:nil]; + } else { + [self performSegueWithIdentifier:kUnwindToCountryCodeWasSelectedSegue sender:self]; + } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { diff --git a/Signal/src/ViewControllers/RegistrationViewController.m b/Signal/src/ViewControllers/RegistrationViewController.m index 75301426a..e5d90cb7e 100644 --- a/Signal/src/ViewControllers/RegistrationViewController.m +++ b/Signal/src/ViewControllers/RegistrationViewController.m @@ -17,8 +17,7 @@ static NSString *const kCodeSentSegue = @"codeSent"; @interface RegistrationViewController () -@property (nonatomic) NSString *lastCallingCode; -@property (nonatomic) NSString *lastCountryCode; +@property (nonatomic) NSString *callingCode; @end @@ -77,8 +76,7 @@ static NSString *const kCodeSentSegue = @"codeSent"; callingCode:(NSString *)callingCode countryCode:(NSString *)countryCode { - _lastCallingCode = callingCode; - _lastCountryCode = countryCode; + _callingCode = callingCode; NSString *title = [NSString stringWithFormat:@"%@ (%@)", callingCode, @@ -133,7 +131,7 @@ static NSString *const kCodeSentSegue = @"codeSent"; } - (IBAction)sendCodeAction:(id)sender { - NSString *phoneNumber = [NSString stringWithFormat:@"%@%@", _lastCallingCode, _phoneNumberTextField.text]; + NSString *phoneNumber = [NSString stringWithFormat:@"%@%@", _callingCode, _phoneNumberTextField.text]; PhoneNumber *localNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumber]; [_sendCodeButton setEnabled:NO]; @@ -196,7 +194,7 @@ static NSString *const kCodeSentSegue = @"codeSent"; [ViewControllerUtils phoneNumberTextField:textField shouldChangeCharactersInRange:range replacementString:insertionText - countryCode:_lastCountryCode]; + countryCode:_callingCode]; return NO; // inform our caller that we took care of performing the change } diff --git a/Signal/src/ViewControllers/ViewControllerUtils.m b/Signal/src/ViewControllers/ViewControllerUtils.m index 271e625ce..9f973c267 100644 --- a/Signal/src/ViewControllers/ViewControllerUtils.m +++ b/Signal/src/ViewControllers/ViewControllerUtils.m @@ -54,7 +54,7 @@ NS_ASSUME_NONNULL_BEGIN // 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 = left.length + center.length; + NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length); NSString *textAfterReformat = [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textAfterChange withSpecifiedCountryCodeString:countryCode];