Respond to CR.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 54cd8cfa3c
commit dcb7eef3fc

@ -276,13 +276,14 @@ NSString *const kContactsTable_CellReuseIdentifier = @"kContactsTable_CellReuseI
__weak AddToBlockListViewController *weakSelf = self; __weak AddToBlockListViewController *weakSelf = self;
[BlockListUIUtils showBlockPhoneNumberActionSheet:[parsedPhoneNumber toE164] [BlockListUIUtils showBlockPhoneNumberActionSheet:[parsedPhoneNumber toE164]
displayName:[parsedPhoneNumber toE164]
fromViewController:self fromViewController:self
blockingManager:_blockingManager blockingManager:_blockingManager
contactsManager:_contactsManager
completionBlock:^(BOOL isBlocked) { completionBlock:^(BOOL isBlocked) {
if (isBlocked) { if (isBlocked) {
// Clear phone number text field is block succeeds. // Clear phone number text field is block succeeds.
weakSelf.phoneNumberTextField.text = nil; weakSelf.phoneNumberTextField.text = nil;
[weakSelf.navigationController popViewControllerAnimated:YES];
} }
}]; }];
} }
@ -409,11 +410,17 @@ NSString *const kContactsTable_CellReuseIdentifier = @"kContactsTable_CellReuseI
{ {
[tableView deselectRowAtIndexPath:indexPath animated:YES]; [tableView deselectRowAtIndexPath:indexPath animated:YES];
__weak AddToBlockListViewController *weakSelf = self;
Contact *contact = self.contacts[(NSUInteger)indexPath.item]; Contact *contact = self.contacts[(NSUInteger)indexPath.item];
[BlockListUIUtils showBlockContactActionSheet:contact [BlockListUIUtils showBlockContactActionSheet:contact
fromViewController:self fromViewController:self
blockingManager:_blockingManager blockingManager:_blockingManager
completionBlock:nil]; contactsManager:_contactsManager
completionBlock:^(BOOL isBlocked) {
if (isBlocked) {
[weakSelf.navigationController popViewControllerAnimated:YES];
}
}];
} }
#pragma mark - UIScrollViewDelegate #pragma mark - UIScrollViewDelegate

@ -6,6 +6,7 @@
@class Contact; @class Contact;
@class OWSBlockingManager; @class OWSBlockingManager;
@class OWSContactsManager;
typedef void (^BlockActionCompletionBlock)(BOOL isBlocked); typedef void (^BlockActionCompletionBlock)(BOOL isBlocked);
@ -16,18 +17,19 @@ typedef void (^BlockActionCompletionBlock)(BOOL isBlocked);
+ (void)showBlockContactActionSheet:(Contact *)contact + (void)showBlockContactActionSheet:(Contact *)contact
fromViewController:(UIViewController *)fromViewController fromViewController:(UIViewController *)fromViewController
blockingManager:(OWSBlockingManager *)blockingManager blockingManager:(OWSBlockingManager *)blockingManager
contactsManager:(OWSContactsManager *)contactsManager
completionBlock:(BlockActionCompletionBlock)completionBlock; completionBlock:(BlockActionCompletionBlock)completionBlock;
+ (void)showBlockPhoneNumberActionSheet:(NSString *)phoneNumber + (void)showBlockPhoneNumberActionSheet:(NSString *)phoneNumber
displayName:(NSString *)displayName
fromViewController:(UIViewController *)fromViewController fromViewController:(UIViewController *)fromViewController
blockingManager:(OWSBlockingManager *)blockingManager blockingManager:(OWSBlockingManager *)blockingManager
contactsManager:(OWSContactsManager *)contactsManager
completionBlock:(BlockActionCompletionBlock)completionBlock; completionBlock:(BlockActionCompletionBlock)completionBlock;
+ (void)showUnblockPhoneNumberActionSheet:(NSString *)phoneNumber + (void)showUnblockPhoneNumberActionSheet:(NSString *)phoneNumber
displayName:(NSString *)displayName
fromViewController:(UIViewController *)fromViewController fromViewController:(UIViewController *)fromViewController
blockingManager:(OWSBlockingManager *)blockingManager blockingManager:(OWSBlockingManager *)blockingManager
contactsManager:(OWSContactsManager *)contactsManager
completionBlock:(BlockActionCompletionBlock)completionBlock; completionBlock:(BlockActionCompletionBlock)completionBlock;
@end @end

@ -3,6 +3,7 @@
// //
#import "BlockListUIUtils.h" #import "BlockListUIUtils.h"
#import "OWSContactsManager.h"
#import "PhoneNumber.h" #import "PhoneNumber.h"
#import <SignalServiceKit/Contact.h> #import <SignalServiceKit/Contact.h>
#import <SignalServiceKit/OWSBlockingManager.h> #import <SignalServiceKit/OWSBlockingManager.h>
@ -14,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)showBlockContactActionSheet:(Contact *)contact + (void)showBlockContactActionSheet:(Contact *)contact
fromViewController:(UIViewController *)fromViewController fromViewController:(UIViewController *)fromViewController
blockingManager:(OWSBlockingManager *)blockingManager blockingManager:(OWSBlockingManager *)blockingManager
contactsManager:(OWSContactsManager *)contactsManager
completionBlock:(BlockActionCompletionBlock)completionBlock completionBlock:(BlockActionCompletionBlock)completionBlock
{ {
NSMutableArray<NSString *> *phoneNumbers = [NSMutableArray new]; NSMutableArray<NSString *> *phoneNumbers = [NSMutableArray new];
@ -29,19 +31,21 @@ NS_ASSUME_NONNULL_BEGIN
} }
return; return;
} }
NSString *displayName = [contactsManager displayNameForContact:contact];
[self showBlockPhoneNumbersActionSheet:phoneNumbers [self showBlockPhoneNumbersActionSheet:phoneNumbers
displayName:contact.fullName displayName:displayName
fromViewController:fromViewController fromViewController:fromViewController
blockingManager:blockingManager blockingManager:blockingManager
completionBlock:completionBlock]; completionBlock:completionBlock];
} }
+ (void)showBlockPhoneNumberActionSheet:(NSString *)phoneNumber + (void)showBlockPhoneNumberActionSheet:(NSString *)phoneNumber
displayName:(NSString *)displayName
fromViewController:(UIViewController *)fromViewController fromViewController:(UIViewController *)fromViewController
blockingManager:(OWSBlockingManager *)blockingManager blockingManager:(OWSBlockingManager *)blockingManager
contactsManager:(OWSContactsManager *)contactsManager
completionBlock:(BlockActionCompletionBlock)completionBlock completionBlock:(BlockActionCompletionBlock)completionBlock
{ {
NSString *displayName = [contactsManager displayNameForPhoneIdentifier:phoneNumber];
[self showBlockPhoneNumbersActionSheet:@[ phoneNumber ] [self showBlockPhoneNumbersActionSheet:@[ phoneNumber ]
displayName:displayName displayName:displayName
fromViewController:fromViewController fromViewController:fromViewController
@ -61,7 +65,8 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(blockingManager); OWSAssert(blockingManager);
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_BLOCK_TITLE_FORMAT", NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_BLOCK_TITLE_FORMAT",
@"A format for the 'block user' action sheet title."), @"A format for the 'block user' action sheet title. Embeds {{the "
@"blocked user's name or phone number}}."),
displayName]; displayName];
UIAlertController *actionSheetController = UIAlertController *actionSheetController =
@ -69,7 +74,7 @@ NS_ASSUME_NONNULL_BEGIN
UIAlertAction *unblockAction = [UIAlertAction UIAlertAction *unblockAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"BLOCK_LIST_BLOCK_BUTTON", @"Button label for the 'block' button") actionWithTitle:NSLocalizedString(@"BLOCK_LIST_BLOCK_BUTTON", @"Button label for the 'block' button")
style:UIAlertActionStyleDefault style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *_Nonnull action) { handler:^(UIAlertAction *_Nonnull action) {
[self blockPhoneNumbers:phoneNumbers [self blockPhoneNumbers:phoneNumbers
displayName:displayName displayName:displayName
@ -119,18 +124,20 @@ NS_ASSUME_NONNULL_BEGIN
} }
+ (void)showUnblockPhoneNumberActionSheet:(NSString *)phoneNumber + (void)showUnblockPhoneNumberActionSheet:(NSString *)phoneNumber
displayName:(NSString *)displayName
fromViewController:(UIViewController *)fromViewController fromViewController:(UIViewController *)fromViewController
blockingManager:(OWSBlockingManager *)blockingManager blockingManager:(OWSBlockingManager *)blockingManager
contactsManager:(OWSContactsManager *)contactsManager
completionBlock:(BlockActionCompletionBlock)completionBlock completionBlock:(BlockActionCompletionBlock)completionBlock
{ {
OWSAssert(phoneNumber.length > 0); OWSAssert(phoneNumber.length > 0);
OWSAssert(displayName.length > 0);
OWSAssert(fromViewController); OWSAssert(fromViewController);
OWSAssert(blockingManager); OWSAssert(blockingManager);
NSString *displayName = [contactsManager displayNameForPhoneIdentifier:phoneNumber];
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_TITLE_FORMAT", NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_TITLE_FORMAT",
@"A format for the 'unblock user' action sheet title."), @"A format for the 'unblock user' action sheet title. Embeds "
@"{{the blocked user's name or phone number}}."),
displayName]; displayName];
UIAlertController *actionSheetController = UIAlertController *actionSheetController =
@ -138,7 +145,7 @@ NS_ASSUME_NONNULL_BEGIN
UIAlertAction *unblockAction = [UIAlertAction UIAlertAction *unblockAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_BUTTON", @"Button label for the 'unblock' button") actionWithTitle:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_BUTTON", @"Button label for the 'unblock' button")
style:UIAlertActionStyleDefault style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *_Nonnull action) { handler:^(UIAlertAction *_Nonnull action) {
[BlockListUIUtils unblockPhoneNumber:phoneNumber [BlockListUIUtils unblockPhoneNumber:phoneNumber
displayName:displayName displayName:displayName
@ -179,8 +186,7 @@ NS_ASSUME_NONNULL_BEGIN
message:[NSString message:[NSString
stringWithFormat:NSLocalizedString(@"BLOCK_LIST_VIEW_UNBLOCKED_ALERT_MESSAGE_FORMAT", stringWithFormat:NSLocalizedString(@"BLOCK_LIST_VIEW_UNBLOCKED_ALERT_MESSAGE_FORMAT",
@"The message format of the 'user unblocked' " @"The message format of the 'user unblocked' "
@"alert. It is populated with the " @"alert. Embeds {{the blocked user's name or phone number}}."),
@"blocked phone number."),
displayName] displayName]
fromViewController:fromViewController]; fromViewController:fromViewController];
} }

@ -20,7 +20,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) OWSContactsManager *contactsManager; @property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic) NSArray<Contact *> *contacts; @property (nonatomic) NSArray<Contact *> *contacts;
@property (nonatomic) NSDictionary<NSString *, Contact *> *contactMap;
@end @end
@ -138,18 +137,7 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) {
- (NSString *)displayNameForIndexPath:(NSIndexPath *)indexPath - (NSString *)displayNameForIndexPath:(NSIndexPath *)indexPath
{ {
NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item]; NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item];
PhoneNumber *parsedPhoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumber]; NSString *displayName = [_contactsManager displayNameForPhoneIdentifier: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; return displayName;
} }
@ -168,11 +156,11 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) {
} }
case BlockListViewControllerSection_BlockList: { case BlockListViewControllerSection_BlockList: {
NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item]; NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item];
NSString *displayName = [self displayNameForIndexPath:indexPath];
[BlockListUIUtils showUnblockPhoneNumberActionSheet:phoneNumber [BlockListUIUtils showUnblockPhoneNumberActionSheet:phoneNumber
displayName:displayName
fromViewController:self fromViewController:self
blockingManager:_blockingManager blockingManager:_blockingManager
contactsManager:_contactsManager
completionBlock:nil]; completionBlock:nil];
break; break;
} }
@ -203,22 +191,6 @@ typedef NS_ENUM(NSInteger, BlockListViewControllerSection) {
}); });
} }
- (void)setContacts:(NSArray<Contact *> *)contacts
{
_contacts = contacts;
NSMutableDictionary<NSString *, Contact *> *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 #pragma mark - Logging
+ (NSString *)tag + (NSString *)tag

@ -437,9 +437,9 @@ static NSString *const OWSConversationSettingsTableViewControllerSegueShowGroupM
return; return;
} }
[BlockListUIUtils showBlockPhoneNumberActionSheet:self.thread.contactIdentifier [BlockListUIUtils showBlockPhoneNumberActionSheet:self.thread.contactIdentifier
displayName:self.thread.name
fromViewController:self fromViewController:self
blockingManager:_blockingManager blockingManager:_blockingManager
contactsManager:_contactsManager
completionBlock:^(BOOL isBlocked) { completionBlock:^(BOOL isBlocked) {
// Update switch state if user cancels action. // Update switch state if user cancels action.
blockUserSwitch.on = isBlocked; blockUserSwitch.on = isBlocked;
@ -450,9 +450,9 @@ static NSString *const OWSConversationSettingsTableViewControllerSegueShowGroupM
return; return;
} }
[BlockListUIUtils showUnblockPhoneNumberActionSheet:self.thread.contactIdentifier [BlockListUIUtils showUnblockPhoneNumberActionSheet:self.thread.contactIdentifier
displayName:self.thread.name
fromViewController:self fromViewController:self
blockingManager:_blockingManager blockingManager:_blockingManager
contactsManager:_contactsManager
completionBlock:^(BOOL isBlocked) { completionBlock:^(BOOL isBlocked) {
// Update switch state if user cancels action. // Update switch state if user cancels action.
blockUserSwitch.on = isBlocked; blockUserSwitch.on = isBlocked;

@ -39,6 +39,7 @@ extern NSString *const OWSContactsManagerSignalRecipientsDidChangeNotification;
- (void)doAfterEnvironmentInitSetup; - (void)doAfterEnvironmentInitSetup;
- (NSString *)displayNameForPhoneIdentifier:(nullable NSString *)identifier; - (NSString *)displayNameForPhoneIdentifier:(nullable NSString *)identifier;
- (NSString *)displayNameForContact:(Contact *)contact;
- (nullable UIImage *)imageForPhoneIdentifier:(nullable NSString *)identifier; - (nullable UIImage *)imageForPhoneIdentifier:(nullable NSString *)identifier;
- (NSAttributedString *)formattedFullNameForContact:(Contact *)contact font:(UIFont *)font; - (NSAttributedString *)formattedFullNameForContact:(Contact *)contact font:(UIFont *)font;

@ -20,12 +20,15 @@ NSString *const OWSContactsManagerSignalRecipientsDidChangeNotification =
@property TOCFuture *futureAddressBook; @property TOCFuture *futureAddressBook;
@property ObservableValueController *observableContactsController; @property ObservableValueController *observableContactsController;
@property TOCCancelTokenSource *life; @property TOCCancelTokenSource *life;
@property(atomic, copy) NSDictionary *latestContactsById; @property (atomic) NSDictionary *latestContactsById;
@property (atomic) NSDictionary<NSString *, Contact *> *contactMap;
@end @end
@implementation OWSContactsManager @implementation OWSContactsManager
@synthesize latestContactsById = _latestContactsById;
- (void)dealloc { - (void)dealloc {
[_life cancel]; [_life cancel];
} }
@ -46,6 +49,41 @@ NSString *const OWSContactsManagerSignalRecipientsDidChangeNotification =
return self; return self;
} }
- (NSDictionary *)latestContactsById
{
@synchronized(self)
{
return _latestContactsById;
}
}
- (void)setLatestContactsById:(NSDictionary *)latestContactsById
{
@synchronized(self)
{
_latestContactsById = [latestContactsById copy];
NSMutableDictionary<NSString *, Contact *> *contactMap = [NSMutableDictionary new];
for (Contact *contact in _latestContactsById.allValues) {
// The allContacts method seems to protect against non-contact instances
// in latestContactsById, so I've done the same here. I'm not sure if
// this is a real issue.
OWSAssert([contact isKindOfClass:[Contact class]]);
if (![contact isKindOfClass:[Contact class]]) {
continue;
}
for (PhoneNumber *phoneNumber in contact.parsedPhoneNumbers) {
NSString *phoneNumberE164 = phoneNumber.toE164;
if (phoneNumberE164.length > 0) {
contactMap[phoneNumberE164] = contact;
}
}
}
self.contactMap = contactMap;
}
}
- (void)doAfterEnvironmentInitSetup { - (void)doAfterEnvironmentInitSetup {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(9, 0)) { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(9, 0)) {
self.contactStore = [[CNContactStore alloc] init]; self.contactStore = [[CNContactStore alloc] init];
@ -419,6 +457,15 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
return displayName; return displayName;
} }
- (NSString *_Nonnull)displayNameForContact:(Contact *)contact
{
OWSAssert(contact);
NSString *displayName = (contact.fullName.length > 0) ? contact.fullName : self.unknownContactName;
return displayName;
}
- (NSAttributedString *_Nonnull)formattedFullNameForContact:(Contact *)contact font:(UIFont *_Nonnull)font - (NSAttributedString *_Nonnull)formattedFullNameForContact:(Contact *)contact font:(UIFont *_Nonnull)font
{ {
UIFont *boldFont = [UIFont ows_mediumFontWithSize:font.pointSize]; UIFont *boldFont = [UIFont ows_mediumFontWithSize:font.pointSize];
@ -473,14 +520,7 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
if (!identifier) { if (!identifier) {
return nil; return nil;
} }
for (Contact *contact in self.allContacts) { return self.contactMap[identifier];
for (PhoneNumber *phoneNumber in contact.parsedPhoneNumbers) {
if ([phoneNumber.toE164 isEqualToString:identifier]) {
return contact;
}
}
}
return nil;
} }
- (Contact *)getOrBuildContactForPhoneIdentifier:(NSString *)identifier - (Contact *)getOrBuildContactForPhoneIdentifier:(NSString *)identifier

Loading…
Cancel
Save