Merge branch 'charlesmchen/nonUSNonContactSearch'

pull/1/head
Matthew Chen 8 years ago
commit 04a112aac9

@ -136,7 +136,7 @@ CHECKOUT OPTIONS:
:commit: a3c843cc8a423c5924c663490978f81dba34d04e :commit: a3c843cc8a423c5924c663490978f81dba34d04e
:git: https://github.com/WhisperSystems/SignalProtocolKit.git :git: https://github.com/WhisperSystems/SignalProtocolKit.git
SignalServiceKit: SignalServiceKit:
:commit: ca81e139bbe968dbaf41445ea4c92d45f57f6f5f :commit: 312f398dddb5eac902ae5a7739f0ae814caa7912
:git: https://github.com/WhisperSystems/SignalServiceKit.git :git: https://github.com/WhisperSystems/SignalServiceKit.git
SocketRocket: SocketRocket:
:commit: 877ac7438be3ad0b45ef5ca3969574e4b97112bf :commit: 877ac7438be3ad0b45ef5ca3969574e4b97112bf
@ -169,4 +169,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: 7088298933b189805c955880c8d1be86da3078af PODFILE CHECKSUM: 7088298933b189805c955880c8d1be86da3078af
COCOAPODS: 1.2.0 COCOAPODS: 1.1.1

@ -22,8 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
MFMessageComposeViewControllerDelegate> MFMessageComposeViewControllerDelegate>
@property (nonatomic) IBOutlet UITableViewCell *inviteCell; @property (nonatomic) IBOutlet UITableViewCell *inviteCell;
@property (nonatomic) UITableViewCell *conversationForNonContactCell;
@property (nonatomic) UITableViewCell *inviteViaSMSCell;
@property (nonatomic) IBOutlet OWSNoSignalContactsView *noSignalContactsView; @property (nonatomic) IBOutlet OWSNoSignalContactsView *noSignalContactsView;
@property (nonatomic) UISearchController *searchController; @property (nonatomic) UISearchController *searchController;
@ -31,14 +29,16 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) UIBarButtonItem *addGroup; @property (nonatomic) UIBarButtonItem *addGroup;
@property (nonatomic) UIView *loadingBackgroundView; @property (nonatomic) UIView *loadingBackgroundView;
@property (nonatomic) NSString *currentSearchTerm; @property (nonatomic, copy) NSArray<Contact *> *contacts;
@property (copy) NSArray<Contact *> *contacts; @property (nonatomic, copy) NSArray<Contact *> *searchResults;
@property (copy) NSArray<Contact *> *searchResults;
@property (nonatomic, readonly) OWSContactsManager *contactsManager; @property (nonatomic, readonly) OWSContactsManager *contactsManager;
// This property should be set IFF the current search text can // A list of possible phone numbers parsed from the search text as
// be parsed as a phone number. If set, it contains a E164 value. // E164 values.
@property (nonatomic) NSString *searchPhoneNumber; @property (nonatomic) NSArray<NSString *> *searchPhoneNumbers;
// A list of possible phone numbers parsed from the search text
// that correspond to known accounts as E164 values.
@property (nonatomic) NSArray<NSString *> *searchPhoneNumberWithAccounts;
// This dictionary is used to cache the set of phone numbers // This dictionary is used to cache the set of phone numbers
// which are known to correspond to Signal accounts. // which are known to correspond to Signal accounts.
@property (nonatomic, nonnull, readonly) NSMutableSet *phoneNumberAccountSet; @property (nonatomic, nonnull, readonly) NSMutableSet *phoneNumberAccountSet;
@ -63,7 +63,6 @@ typedef NS_ENUM(NSInteger, AdvancedSettingsTableViewControllerSection) {
MessageComposeTableViewControllerSection_Count // meta section MessageComposeTableViewControllerSection_Count // meta section
}; };
NSString *const MessageComposeTableViewControllerCellInvite = @"ContactTableInviteCell";
NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableViewCell"; NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableViewCell";
@implementation MessageComposeTableViewController @implementation MessageComposeTableViewController
@ -132,9 +131,6 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
self.searchController.searchBar.backgroundColor = [UIColor whiteColor]; self.searchController.searchBar.backgroundColor = [UIColor whiteColor];
self.inviteCell.textLabel.text = NSLocalizedString( self.inviteCell.textLabel.text = NSLocalizedString(
@"INVITE_FRIENDS_CONTACT_TABLE_BUTTON", @"Text for button at the top of the contact picker"); @"INVITE_FRIENDS_CONTACT_TABLE_BUTTON", @"Text for button at the top of the contact picker");
self.conversationForNonContactCell = [UITableViewCell new];
self.inviteViaSMSCell = [UITableViewCell new];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self createLoadingAndBackgroundViews]; [self createLoadingAndBackgroundViews];
@ -275,8 +271,6 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
self.inviteCell.hidden = YES; self.inviteCell.hidden = YES;
self.conversationForNonContactCell.hidden = YES;
self.inviteViaSMSCell.hidden = YES;
self.searchController.searchBar.hidden = YES; self.searchController.searchBar.hidden = YES;
self.tableView.backgroundView = self.noSignalContactsView; self.tableView.backgroundView = self.noSignalContactsView;
self.tableView.backgroundView.opaque = YES; self.tableView.backgroundView.opaque = YES;
@ -288,8 +282,10 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
self.searchController.searchBar.hidden = NO; self.searchController.searchBar.hidden = NO;
self.tableView.backgroundView = nil; self.tableView.backgroundView = nil;
self.inviteCell.hidden = NO; self.inviteCell.hidden = NO;
self.conversationForNonContactCell.hidden = NO; }
self.inviteViaSMSCell.hidden = NO;
for (UITableViewCell *cell in self.tableView.visibleCells) {
cell.hidden = show;
} }
} }
@ -353,34 +349,47 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
OWSContactsSearcher *contactsSearcher = [[OWSContactsSearcher alloc] initWithContacts: self.contacts]; OWSContactsSearcher *contactsSearcher = [[OWSContactsSearcher alloc] initWithContacts: self.contacts];
self.searchResults = [contactsSearcher filterWithString:searchText]; self.searchResults = [contactsSearcher filterWithString:searchText];
NSString *formattedNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:searchText].toE164; NSMutableArray<NSString *> *searchPhoneNumbers = [NSMutableArray new];
for (PhoneNumber *phoneNumber in [PhoneNumber tryParsePhoneNumbersFromsUserSpecifiedText:searchText
clientPhoneNumber:[TSStorageManager localNumber]]) {
[searchPhoneNumbers addObject:phoneNumber.toE164];
}
// text to a non-signal number if we have no results and a valid phone # // text to a non-signal number if we have no results and a valid phone #
if (self.searchResults.count == 0 && searchText.length > 8 && formattedNumber) { if (self.searchResults.count == 0 && searchText.length > 8 && searchPhoneNumbers.count > 0) {
self.currentSearchTerm = formattedNumber; self.searchPhoneNumbers = searchPhoneNumbers;
self.searchPhoneNumber = formattedNumber;
// Kick off account lookup if necessary. // Kick off account lookup if necessary.
[self checkIsNonContactPhoneNumberSignalUser:formattedNumber]; [self checkForAccountsForPhoneNumbers:searchPhoneNumbers];
} else { } else {
_searchPhoneNumber = nil; _searchPhoneNumbers = nil;
} }
} }
- (void)checkIsNonContactPhoneNumberSignalUser:(NSString *)phoneNumber - (void)checkForAccountsForPhoneNumbers:(NSArray *)phoneNumbers
{ {
if ([self.phoneNumberAccountSet containsObject:phoneNumber]) { NSMutableArray<NSString *> *unknownPhoneNumbers = [NSMutableArray new];
for (NSString *phoneNumber in phoneNumbers) {
if (![self.phoneNumberAccountSet containsObject:phoneNumber]) {
[unknownPhoneNumbers addObject:phoneNumber];
}
}
if ([unknownPhoneNumbers count] < 1) {
return; return;
} }
__weak MessageComposeTableViewController *weakSelf = self; __weak MessageComposeTableViewController *weakSelf = self;
[[ContactsUpdater sharedUpdater] lookupIdentifier:phoneNumber [[ContactsUpdater sharedUpdater] lookupIdentifiers:unknownPhoneNumbers
success:^(SignalRecipient *recipient) { success:^(NSArray<SignalRecipient *> *recipients) {
MessageComposeTableViewController *strongSelf = weakSelf; MessageComposeTableViewController *strongSelf = weakSelf;
if (!strongSelf) { if (!strongSelf) {
return; return;
} }
if (![strongSelf.phoneNumberAccountSet containsObject:phoneNumber]) { NSUInteger oldCount = strongSelf.phoneNumberAccountSet.count;
for (SignalRecipient *recipient in recipients) {
NSString *phoneNumber = recipient.uniqueId;
[strongSelf.phoneNumberAccountSet addObject:phoneNumber]; [strongSelf.phoneNumberAccountSet addObject:phoneNumber];
[strongSelf.tableView reloadData]; }
if (oldCount != strongSelf.phoneNumberAccountSet.count) {
[strongSelf ensureSearchPhoneNumberWithAccounts];
} }
} }
failure:^(NSError *error) { failure:^(NSError *error) {
@ -388,24 +397,48 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
}]; }];
} }
- (void)setSearchPhoneNumber:(NSString *)searchPhoneNumber { - (void)setSearchPhoneNumbers:(NSArray<NSString *> *)searchPhoneNumbers {
if ([_searchPhoneNumber isEqualToString:searchPhoneNumber]) { if ([_searchPhoneNumbers isEqual:searchPhoneNumbers]) {
return; return;
} }
_searchPhoneNumbers = searchPhoneNumbers;
[self ensureSearchPhoneNumberWithAccounts];
[self.tableView reloadData];
}
- (void)ensureSearchPhoneNumberWithAccounts {
NSMutableArray<NSString *> *searchPhoneNumberWithAccounts = [NSMutableArray new];
for (NSString *phoneNumber in self.searchPhoneNumbers) {
if ([self.phoneNumberAccountSet containsObject:phoneNumber] &&
![searchPhoneNumberWithAccounts containsObject:phoneNumber]) {
[searchPhoneNumberWithAccounts addObject:phoneNumber];
}
}
self.searchPhoneNumberWithAccounts = searchPhoneNumberWithAccounts;
}
_searchPhoneNumber = searchPhoneNumber; - (void)setSearchPhoneNumberWithAccounts:(NSArray<NSString *> *)searchPhoneNumberWithAccounts {
if ([_searchPhoneNumberWithAccounts isEqual:searchPhoneNumberWithAccounts]) {
return;
}
_searchPhoneNumberWithAccounts = searchPhoneNumberWithAccounts;
[self.tableView reloadData]; [self.tableView reloadData];
} }
#pragma mark - Send Normal Text to Unknown Contact #pragma mark - Send Normal Text to Unknown Contact
- (void)sendText { - (void)sendTextToPhoneNumber:(NSString *)phoneNumber {
OWSAssert([phoneNumber length] > 0);
NSString *confirmMessage = NSLocalizedString(@"SEND_SMS_CONFIRM_TITLE", @""); NSString *confirmMessage = NSLocalizedString(@"SEND_SMS_CONFIRM_TITLE", @"");
if ([self.currentSearchTerm length] > 0) { if ([phoneNumber length] > 0) {
confirmMessage = NSLocalizedString(@"SEND_SMS_INVITE_TITLE", @""); confirmMessage = [[NSLocalizedString(@"SEND_SMS_INVITE_TITLE", @"")
confirmMessage = [confirmMessage stringByAppendingString:self.currentSearchTerm]; stringByAppendingString:phoneNumber]
confirmMessage = [confirmMessage stringByAppendingString:NSLocalizedString(@"QUESTIONMARK_PUNCTUATION", @"")]; stringByAppendingString:NSLocalizedString(@"QUESTIONMARK_PUNCTUATION", @"")];
} }
UIAlertController *alertController = UIAlertController *alertController =
@ -429,8 +462,7 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self; picker.messageComposeDelegate = self;
picker.recipients = picker.recipients = @[phoneNumber,];
[self.currentSearchTerm length] > 0 ? [NSArray arrayWithObject:self.currentSearchTerm] : nil;
picker.body = [NSLocalizedString(@"SMS_INVITE_BODY", @"") picker.body = [NSLocalizedString(@"SMS_INVITE_BODY", @"")
stringByAppendingString: stringByAppendingString:
@" https://itunes.apple.com/us/app/signal-private-messenger/id874139669?mt=8"]; @" https://itunes.apple.com/us/app/signal-private-messenger/id874139669?mt=8"];
@ -512,10 +544,10 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
BOOL showInviteViaSMS = NO; BOOL showInviteViaSMS = NO;
BOOL showInviteFlow = NO; BOOL showInviteFlow = NO;
BOOL hasPhoneNumber = self.searchPhoneNumber.length > 0; BOOL hasPhoneNumber = self.searchPhoneNumbers.count > 0;
BOOL isKnownSignalUser = hasPhoneNumber && [self.phoneNumberAccountSet containsObject:self.searchPhoneNumber]; BOOL hasKnownSignalUser = self.searchPhoneNumberWithAccounts.count > 0;
BOOL isInviteFlowSupported = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(9, 0); BOOL isInviteFlowSupported = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(9, 0);
if (hasPhoneNumber && isKnownSignalUser) { if (hasKnownSignalUser) {
showNonContactConversation = YES; showNonContactConversation = YES;
} else if (hasPhoneNumber) { } else if (hasPhoneNumber) {
showInviteViaSMS = YES; showInviteViaSMS = YES;
@ -524,9 +556,9 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
} }
if (section == MessageComposeTableViewControllerSectionInviteNonContactConversation) { if (section == MessageComposeTableViewControllerSectionInviteNonContactConversation) {
return showNonContactConversation ? 1 : 0; return showNonContactConversation ? (NSInteger) self.searchPhoneNumberWithAccounts.count : 0;
} else if (section == MessageComposeTableViewControllerSectionInviteViaSMS) { } else if (section == MessageComposeTableViewControllerSectionInviteViaSMS) {
return showInviteViaSMS ? 1 : 0; return showInviteViaSMS ? (NSInteger) self.searchPhoneNumbers.count : 0;
} else if (section == MessageComposeTableViewControllerSectionInviteFlow) { } else if (section == MessageComposeTableViewControllerSectionInviteFlow) {
return showInviteFlow ? 1 : 0; return showInviteFlow ? 1 : 0;
} else { } else {
@ -543,22 +575,36 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ {
if (indexPath.section == MessageComposeTableViewControllerSectionInviteNonContactConversation) { if (indexPath.section == MessageComposeTableViewControllerSectionInviteNonContactConversation) {
self.conversationForNonContactCell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"NEW_CONVERSATION_FOR_NON_CONTACT_FORMAT", if (indexPath.row < 0 ||
indexPath.row >= (NSInteger) self.searchPhoneNumberWithAccounts.count) {
OWSAssert(0);
}
NSString *phoneNumber = self.searchPhoneNumberWithAccounts[(NSUInteger) indexPath.row];
UITableViewCell *conversationForNonContactCell = [UITableViewCell new];
conversationForNonContactCell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"NEW_CONVERSATION_FOR_NON_CONTACT_FORMAT",
@"Text for button to start a new conversation with a non-contact"), @"Text for button to start a new conversation with a non-contact"),
self.searchPhoneNumber]; phoneNumber];
return self.conversationForNonContactCell; return conversationForNonContactCell;
} else if (indexPath.section == MessageComposeTableViewControllerSectionInviteViaSMS) { } else if (indexPath.section == MessageComposeTableViewControllerSectionInviteViaSMS) {
self.inviteViaSMSCell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"SEND_INVITE_VIA_SMS_BUTTON_FORMAT", if (indexPath.row < 0 ||
indexPath.row >= (NSInteger) self.searchPhoneNumbers.count) {
OWSAssert(0);
}
NSString *phoneNumber = self.searchPhoneNumbers[(NSUInteger) indexPath.row];
UITableViewCell *inviteViaSMSCell = [UITableViewCell new];
inviteViaSMSCell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"SEND_INVITE_VIA_SMS_BUTTON_FORMAT",
@"Text for button to send a Signal invite via SMS. %@ is placeholder for the receipient's phone number."), @"Text for button to send a Signal invite via SMS. %@ is placeholder for the receipient's phone number."),
self.searchPhoneNumber]; phoneNumber];
return self.inviteViaSMSCell; return inviteViaSMSCell;
} else if (indexPath.section == MessageComposeTableViewControllerSectionInviteFlow) { } else if (indexPath.section == MessageComposeTableViewControllerSectionInviteFlow) {
self.inviteCell.hidden = NO;
return self.inviteCell; return self.inviteCell;
} else { } else {
OWSAssert(indexPath.section == MessageComposeTableViewControllerSectionContacts) OWSAssert(indexPath.section == MessageComposeTableViewControllerSectionContacts)
ContactTableViewCell *cell = (ContactTableViewCell *)[tableView ContactTableViewCell *cell = (ContactTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:MessageComposeTableViewControllerCellContact]; dequeueReusableCellWithIdentifier:MessageComposeTableViewControllerCellContact];
cell.hidden = NO;
[cell configureWithContact:[self contactForIndexPath:indexPath] contactsManager:self.contactsManager]; [cell configureWithContact:[self contactForIndexPath:indexPath] contactsManager:self.contactsManager];
@ -571,16 +617,26 @@ NSString *const MessageComposeTableViewControllerCellContact = @"ContactTableVie
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == MessageComposeTableViewControllerSectionInviteNonContactConversation) { if (indexPath.section == MessageComposeTableViewControllerSectionInviteNonContactConversation) {
OWSAssert(self.searchPhoneNumber.length > 0); if (indexPath.row < 0 ||
indexPath.row >= (NSInteger) self.searchPhoneNumberWithAccounts.count) {
OWSAssert(0);
}
NSString *phoneNumber = self.searchPhoneNumberWithAccounts[(NSUInteger) indexPath.row];
OWSAssert(phoneNumber.length > 0);
if (self.searchPhoneNumber.length > 0) { if (phoneNumber.length > 0) {
[self dismissViewControllerAnimated:YES [self dismissViewControllerAnimated:YES
completion:^() { completion:^() {
[Environment messageIdentifier:self.searchPhoneNumber withCompose:YES]; [Environment messageIdentifier:phoneNumber withCompose:YES];
}]; }];
} }
} else if (indexPath.section == MessageComposeTableViewControllerSectionInviteViaSMS) { } else if (indexPath.section == MessageComposeTableViewControllerSectionInviteViaSMS) {
[self sendText]; if (indexPath.row < 0 ||
indexPath.row >= (NSInteger) self.searchPhoneNumbers.count) {
OWSAssert(0);
}
NSString *phoneNumber = self.searchPhoneNumbers[(NSUInteger) indexPath.row];
[self sendTextToPhoneNumber:phoneNumber];
} else if (indexPath.section == MessageComposeTableViewControllerSectionInviteFlow) { } else if (indexPath.section == MessageComposeTableViewControllerSectionInviteFlow) {
void (^showInvite)() = ^{ void (^showInvite)() = ^{
OWSInviteFlow *inviteFlow = OWSInviteFlow *inviteFlow =

Loading…
Cancel
Save