diff --git a/Signal/src/ViewControllers/AddToBlockListViewController.m b/Signal/src/ViewControllers/AddToBlockListViewController.m index 7a222bce2..917ba04aa 100644 --- a/Signal/src/ViewControllers/AddToBlockListViewController.m +++ b/Signal/src/ViewControllers/AddToBlockListViewController.m @@ -25,7 +25,6 @@ NSString *const kContactsTable_CellReuseIdentifier = @"kContactsTable_CellReuseI #pragma mark - -// TODO: Add a list of contacts to make it easier to block contacts. @interface AddToBlockListViewController () NS_ASSUME_NONNULL_BEGIN -// TODO: We should label phone numbers with contact names where possible. @interface BlockListViewController () @property (nonatomic, readonly) OWSBlockingManager *blockingManager; @property (nonatomic, readonly) NSArray *blockedPhoneNumbers; +@property (nonatomic, readonly) OWSContactsManager *contactsManager; +@property (nonatomic) NSArray *contacts; +@property (nonatomic) NSDictionary *contactMap; + @end #pragma mark - @@ -39,6 +45,8 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { _blockingManager = [OWSBlockingManager sharedManager]; _blockedPhoneNumbers = [_blockingManager blockedPhoneNumbers]; + _contactsManager = [Environment getCurrent].contactsManager; + self.contacts = self.contactsManager.signalContacts; self.title = NSLocalizedString(@"SETTINGS_BLOCK_LIST_TITLE", @"Label for the block list section of the settings view"); @@ -46,12 +54,23 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { [self addNotificationListeners]; } +- (void)viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + + [self refreshContacts]; +} + - (void)addNotificationListeners { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blockedPhoneNumbersDidChange:) name:kNSNotificationName_BlockedPhoneNumbersDidChange object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(signalRecipientsDidChange:) + name:OWSContactsManagerSignalRecipientsDidChangeNotification + object:nil]; } - (void)dealloc @@ -109,15 +128,8 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; break; case BlockListViewControllerSection_BlockList: { - NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger) indexPath.item]; - PhoneNumber *parsedPhoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumber]; - // Try to parse and present the phone number in E164. - // It should already be in E164, so this should always work. - // If an invalid or unparsable phone number is already in the block list, - // present it as-is. - cell.textLabel.text = (parsedPhoneNumber - ? parsedPhoneNumber.toE164 - : phoneNumber); + NSString *displayName = [self displayNameForIndexPath:indexPath]; + cell.textLabel.text = displayName; cell.textLabel.font = [UIFont ows_mediumFontWithSize:18.f]; cell.accessoryType = UITableViewCellAccessoryCheckmark; break; @@ -130,6 +142,24 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { return cell; } +- (NSString *)displayNameForIndexPath:(NSIndexPath *)indexPath +{ + NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item]; + PhoneNumber *parsedPhoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumber]; + + // Try to parse and present the phone number in E164. + // It should already be in E164, so this should always work. + // If an invalid or unparsable phone number is already in the block list, + // present it as-is. + NSString *displayName = (parsedPhoneNumber ? parsedPhoneNumber.toE164 : phoneNumber); + Contact *contact = self.contactMap[displayName]; + if (contact && [contact fullName].length > 0) { + displayName = [contact fullName]; + } + + return displayName; +} + - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; @@ -145,7 +175,8 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { } case BlockListViewControllerSection_BlockList: { NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item]; - [self showUnblockActionSheet:phoneNumber]; + NSString *displayName = [self displayNameForIndexPath:indexPath]; + [self showUnblockActionSheet:phoneNumber displayName:displayName]; break; } default: @@ -153,16 +184,14 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { } } -- (void)showUnblockActionSheet:(NSString *)phoneNumber +- (void)showUnblockActionSheet:(NSString *)phoneNumber displayName:(NSString *)displayName { OWSAssert(phoneNumber.length > 0); - - PhoneNumber *parsedPhoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumber]; - NSString *displayPhoneNumber = (parsedPhoneNumber ? parsedPhoneNumber.toE164 : phoneNumber); + OWSAssert(displayName.length > 0); NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_TITLE_FORMAT", @"A format for the 'unblock phone number' action sheet title."), - displayPhoneNumber]; + displayName]; UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet]; @@ -172,7 +201,7 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { actionWithTitle:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_BUTTON", @"Button label for the 'unblock' button") style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) { - [weakSelf unblockPhoneNumber:phoneNumber displayPhoneNumber:displayPhoneNumber]; + [weakSelf unblockPhoneNumber:phoneNumber displayName:displayName]; }]; [actionSheetController addAction:unblockAction]; @@ -184,7 +213,7 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { [self presentViewController:actionSheetController animated:YES completion:nil]; } -- (void)unblockPhoneNumber:(NSString *)phoneNumber displayPhoneNumber:(NSString *)displayPhoneNumber +- (void)unblockPhoneNumber:(NSString *)phoneNumber displayName:(NSString *)displayName { [_blockingManager removeBlockedPhoneNumber:phoneNumber]; @@ -196,7 +225,7 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { @"The message format of the 'phone number unblocked' " @"alert in the block view. It is populated with the " @"blocked phone number."), - displayPhoneNumber] + displayName] preferredStyle:UIAlertControllerStyleAlert]; [controller addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) @@ -214,6 +243,46 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) { [self.tableView reloadData]; } +- (void)signalRecipientsDidChange:(NSNotification *)notification +{ + [self updateContacts]; +} + +- (void)updateContacts +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.contacts = self.contactsManager.signalContacts; + [self.tableView reloadData]; + }); +} + +- (void)refreshContacts +{ + [[ContactsUpdater sharedUpdater] updateSignalContactIntersectionWithABContacts:self.contactsManager.allContacts + success:^{ + [self updateContacts]; + } + failure:^(NSError *error) { + DDLogError(@"%@ Error updating contacts", self.tag); + }]; +} + +- (void)setContacts:(NSArray *)contacts +{ + _contacts = contacts; + + NSMutableDictionary *contactMap = [NSMutableDictionary new]; + for (Contact *contact in contacts) { + for (PhoneNumber *phoneNumber in contact.parsedPhoneNumbers) { + NSString *phoneNumberE164 = phoneNumber.toE164; + if (phoneNumberE164.length > 0) { + contactMap[phoneNumberE164] = contact; + } + } + } + self.contactMap = contactMap; +} + #pragma mark - Logging + (NSString *)tag