From 37877b9083206611e13e87dc6912b2ba00899c67 Mon Sep 17 00:00:00 2001 From: giomfo Date: Wed, 6 Apr 2016 18:33:26 +0200 Subject: [PATCH] BugFix: Include address book emails in search result #196 --- .../RoomParticipantsViewController.m | 185 +++++++++++------- 1 file changed, 116 insertions(+), 69 deletions(-) diff --git a/Vector/ViewController/RoomParticipantsViewController.m b/Vector/ViewController/RoomParticipantsViewController.m index 28888b3ca..e1374ec2a 100644 --- a/Vector/ViewController/RoomParticipantsViewController.m +++ b/Vector/ViewController/RoomParticipantsViewController.m @@ -557,9 +557,13 @@ { participantCell = [[ContactTableViewCell alloc] init]; } - - // Reset the accessory view - participantCell.accessoryView = nil; + else + { + // Restore default values + participantCell.accessoryView = nil; + participantCell.contentView.alpha = 1; + participantCell.userInteractionEnabled = YES; + } participantCell.mxRoom = self.mxRoom; @@ -656,7 +660,26 @@ // in order to make it more understandable for the end user if (indexPath.section == searchResultSection) { - participantCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plus_icon"]]; + if (indexPath.row == 0) + { + // This is the text entered by the user + NSString *searchText = contact.displayName; + + // Check whether this input is a valid email or a Matrix user ID before adding the plus icon. + if (![MXTools isEmailAddress:searchText] && ([searchText characterAtIndex:0] != '@' || [searchText containsString:@":"] == NO)) + { + participantCell.contentView.alpha = 0.5; + participantCell.userInteractionEnabled = NO; + } + else + { + participantCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plus_icon"]]; + } + } + else + { + participantCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plus_icon"]]; + } } cell = participantCell; @@ -705,69 +728,46 @@ NSString *participantId = contact.displayName; // Is it an email or a Matrix user ID? - BOOL isEmailAddress = [MXTools isEmailAddress:participantId]; - - // Sanity check the input - if (!isEmailAddress && - ([participantId characterAtIndex:0] != '@' || [participantId containsString:@":"] == NO)) + if ([MXTools isEmailAddress:participantId]) { - __weak typeof(self) weakSelf = self; - currentAlert = [[MXKAlert alloc] initWithTitle:NSLocalizedStringFromTable(@"room_participants_invite_malformed_id_title", @"Vector", nil) - message:NSLocalizedStringFromTable(@"room_participants_invite_malformed_id", @"Vector", nil) - style:MXKAlertStyleAlert]; - - currentAlert.cancelButtonIndex = [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"] - style:MXKAlertActionStyleCancel - handler:^(MXKAlert *alert) { - - __strong __typeof(weakSelf)strongSelf = weakSelf; - strongSelf->currentAlert = nil; - - }]; - [currentAlert showInViewController:self]; + [self addPendingActionMask]; + [self.mxRoom inviteUserByEmail:participantId success:^{ + + [self removePendingActionMask]; + + // Refresh display by leaving search session + [self searchBarCancelButtonClicked:addParticipantsSearchBarCell.mxkSearchBar]; + + } failure:^(NSError *error) { + + [self removePendingActionMask]; + + NSLog(@"[RoomParticipantsVC] Invite be email %@ failed", participantId); + // Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; } else { - if (isEmailAddress) - { - [self addPendingActionMask]; - [self.mxRoom inviteUserByEmail:participantId success:^{ - - [self removePendingActionMask]; - - // Refresh display by leaving search session - [self searchBarCancelButtonClicked:addParticipantsSearchBarCell.mxkSearchBar]; - - } failure:^(NSError *error) { - - [self removePendingActionMask]; - - NSLog(@"[RoomParticipantsVC] Invite be email %@ failed", participantId); - // Alert user - [[AppDelegate theDelegate] showErrorAsAlert:error]; - }]; - } - else - { - [self addPendingActionMask]; - [self.mxRoom inviteUser:participantId success:^{ - - [self removePendingActionMask]; - - // Refresh display by leaving search session - [self searchBarCancelButtonClicked:addParticipantsSearchBarCell.mxkSearchBar]; - - } failure:^(NSError *error) { - - [self removePendingActionMask]; - - NSLog(@"[RoomParticipantsVC] Invite %@ failed", participantId); - // Alert user - [[AppDelegate theDelegate] showErrorAsAlert:error]; - }]; - } + [self addPendingActionMask]; + [self.mxRoom inviteUser:participantId success:^{ + + [self removePendingActionMask]; + + // Refresh display by leaving search session + [self searchBarCancelButtonClicked:addParticipantsSearchBarCell.mxkSearchBar]; + + } failure:^(NSError *error) { + + [self removePendingActionMask]; + + NSLog(@"[RoomParticipantsVC] Invite %@ failed", participantId); + // Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; } } + // TODO handle here the case where self.mxRoom is undefined. } else if (row < filteredParticipants.count) { @@ -809,6 +809,37 @@ [self searchBarCancelButtonClicked:addParticipantsSearchBarCell.mxkSearchBar]; } } + else + { + // This is a local email contact + NSString *emailAddress = contact.displayName; + + // Invite this user if a room is defined + if (self.mxRoom) + { + // Sanity check + if ([MXTools isEmailAddress:emailAddress]) + { + [self addPendingActionMask]; + [self.mxRoom inviteUserByEmail:emailAddress success:^{ + + [self removePendingActionMask]; + + // Refresh display by leaving search session + [self searchBarCancelButtonClicked:addParticipantsSearchBarCell.mxkSearchBar]; + + } failure:^(NSError *error) { + + [self removePendingActionMask]; + + NSLog(@"[RoomParticipantsVC] Invite be email %@ failed", emailAddress); + // Alert user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; + } + } + // TODO handle here the case where self.mxRoom is undefined. + } } } else if (indexPath.section == participantsSection) @@ -1018,7 +1049,7 @@ - (void)refreshSearchBarItemsColor:(UISearchBar *)searchBar { - // caret color + // bar tint color searchBar.barTintColor = searchBar.tintColor = kVectorColorGreen; searchBar.tintColor = kVectorColorGreen; @@ -1092,19 +1123,29 @@ { NSInteger previousFilteredCount = filteredParticipants.count; - NSMutableArray *constacts; + NSMutableArray *contacts; if (addParticipantsSearchText.length && [searchText hasPrefix:addParticipantsSearchText]) { - constacts = filteredParticipants; + contacts = filteredParticipants; } else { // Retrieve all known matrix users NSArray *matrixContacts = [NSMutableArray arrayWithArray:[MXKContactManager sharedManager].matrixContacts]; - constacts = [NSMutableArray arrayWithCapacity:matrixContacts.count]; - // Split contacts with several ids, and remove the current participants. + // Retrieve all known email addresses from local contacts + NSArray *localEmailContacts = [MXKContactManager sharedManager].localEmailContacts; + + contacts = [NSMutableArray arrayWithCapacity:(matrixContacts.count + localEmailContacts.count)]; + + // Add first email contacts + if (localEmailContacts.count) + { + [contacts addObjectsFromArray:localEmailContacts]; + } + + // Matrix ids: split contacts with several ids, and remove the current participants. for (MXKContact* contact in matrixContacts) { NSArray *identifiers = contact.matrixIdentifiers; @@ -1118,7 +1159,7 @@ { Contact *splitContact = [[Contact alloc] initMatrixContactWithDisplayName:contact.displayName andMatrixID:userId]; splitContact.mxMember = [self.mxRoom.state memberWithUserId:userId]; - [constacts addObject:splitContact]; + [contacts addObject:splitContact]; } } } @@ -1130,7 +1171,7 @@ { if (![userId isEqualToString:userMatrixId]) { - [constacts addObject:contact]; + [contacts addObject:contact]; } } } @@ -1151,7 +1192,7 @@ [indexArray addObject:[NSIndexPath indexPathForRow:index++ inSection:0]]; } - for (MXKContact* contact in constacts) + for (MXKContact* contact in contacts) { if ([contact matchedWithPatterns:@[addParticipantsSearchText]]) { @@ -1172,6 +1213,12 @@ self.isAddParticipantSearchBarEditing = YES; searchBar.showsCancelButton = YES; + if (![MXKAppSettings standardAppSettings].syncLocalContacts) + { + // Allow local contacts sync in order to add address book emails in search result + [MXKAppSettings standardAppSettings].syncLocalContacts = YES; + } + [self refreshSearchBarItemsColor:searchBar]; return YES;