Skip to content

Commit

Permalink
Prepare #904: Improve the people invite screens
Browse files Browse the repository at this point in the history
Handle 'Start chat' and 'Add new member' screens. (TODO update people search screen)

- Display by default all the local contacts with at least one contact method (email, phone number...) in alphabetic order, mixing Matrix enabled and non-Matrix enabled users.
- The search result lists only the contacts who have the search pattern as prefix in their display name, their matrix identifiers and/or their contact methods (emails, phones).

- The search result is displayed in 2 sections: “Local Contacts" and “Known Contacts”.

- Local contacts with several contact methods are split in several contacts.
  • Loading branch information
giomfo committed Jan 9, 2017
1 parent 3788c5e commit 2d0995c
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 258 deletions.
7 changes: 4 additions & 3 deletions Vector/Assets/en.lproj/Vector.strings
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
"directory_searching_title" = "Searching directory...";
"directory_search_fail" = "Failed to fetch data";

// Contacts
"contacts_address_book_section" = "LOCAL CONTACTS";
"contacts_matrix_users_section" = "KNOWN CONTACTS";

// Chat participants
"room_participants_title" = "Participants";
"room_participants_add_participant" = "Add participant";
Expand All @@ -133,9 +137,6 @@
"room_participants_invite_another_user" = "Search / invite by name, email, id";
"room_participants_invite_malformed_id_title" = "Invite Error";
"room_participants_invite_malformed_id" = "Malformed ID. Should be an email address or a Matrix ID like '@localpart:domain'";

"room_participants_address_book_section" = "CONTACT BOOK";
"room_participants_matrix_users_section" = "ROOM CONTACTS";
"room_participants_invited_section" = "INVITED";

"room_participants_online" = "Online";
Expand Down
158 changes: 50 additions & 108 deletions Vector/ViewController/RoomParticipantsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -817,43 +817,43 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
if (currentSearchText.length)
{
invitableSectionSearchInput = count++;

if (invitableAddressBookContacts.count)
{
invitableSectionAddressBookContacts = count++;
}

if (invitableMatrixContacts.count)
{
invitableSectionMatrixContacts = count++;
}
}

if (invitableAddressBookContacts.count)
{
invitableSectionAddressBookContacts = count++;
}

if (invitableMatrixContacts.count)
{
invitableSectionMatrixContacts = count++;
}
else if (!currentSearchText.length && self.mxRoom)
else
{
// Display by default all the contacts who share a private room with the current user
invitableMatrixContacts = [NSMutableArray arrayWithArray:[[MXKContactManager sharedManager] privateMatrixContacts:self.mxRoom.mxSession]];
// Display by default the full address book ordered alphabetically, mixing Matrix enabled and non-Matrix enabled users.
invitableAddressBookContacts = [NSMutableArray arrayWithArray:[MXKContactManager sharedManager].localContactsSplitByContactMethod];

// Remove the current participants
for (NSUInteger index = 0; index < invitableMatrixContacts.count;)
for (NSUInteger index = 0; index < invitableAddressBookContacts.count;)
{
MXKContact* contact = invitableMatrixContacts[index];
if ([contactsById objectForKey:contact.matrixIdentifiers.firstObject] != nil)
{
[invitableMatrixContacts removeObject:contact];
}
else
MXKContact* contact = invitableAddressBookContacts[index];

NSArray *identifiers = contact.matrixIdentifiers;
if (identifiers.count)
{
// Next
index ++;
if ([contactsById objectForKey:identifiers.firstObject])
{
[invitableAddressBookContacts removeObjectAtIndex:index];
continue;
}
}

index++;
}

if (invitableMatrixContacts.count)
if (invitableAddressBookContacts.count)
{
// Sort alphabetically this list of contacts
[self sortAlphabeticallyInvitableContacts:invitableMatrixContacts];

invitableSectionMatrixContacts = count++;
invitableSectionAddressBookContacts = count++;
}
}

Expand Down Expand Up @@ -1142,11 +1142,11 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger
}
else if (section == invitableSectionAddressBookContacts)
{
headerLabel.text = NSLocalizedStringFromTable(@"room_participants_address_book_section", @"Vector", nil);
headerLabel.text = NSLocalizedStringFromTable(@"contacts_address_book_section", @"Vector", nil);
}
else if (section == invitableSectionMatrixContacts)
{
headerLabel.text = NSLocalizedStringFromTable(@"room_participants_matrix_users_section", @"Vector", nil);
headerLabel.text = NSLocalizedStringFromTable(@"contacts_matrix_users_section", @"Vector", nil);
}

[sectionHeader addSubview:headerLabel];
Expand Down Expand Up @@ -1655,7 +1655,25 @@ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
if (!currentSearchText.length || [searchText hasPrefix:currentSearchText] == NO)
{
// Retrieve all the local contacts with emails
invitableAddressBookContacts = [NSMutableArray arrayWithArray:[MXKContactManager sharedManager].localContactsWithMethods];
invitableAddressBookContacts = [NSMutableArray arrayWithArray:[MXKContactManager sharedManager].localContactsSplitByContactMethod];

// Remove the current participants
for (index = 0; index < invitableAddressBookContacts.count;)
{
MXKContact* contact = invitableAddressBookContacts[index];

NSArray *identifiers = contact.matrixIdentifiers;
if (identifiers.count)
{
if ([contactsById objectForKey:identifiers.firstObject])
{
[invitableAddressBookContacts removeObjectAtIndex:index];
continue;
}
}

index++;
}

// Retrieve all known matrix users
NSArray *allMatrixContacts = [MXKContactManager sharedManager].matrixContacts;
Expand Down Expand Up @@ -1748,8 +1766,8 @@ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText


// Sort the refreshed lists of the invitable contacts
[self sortAlphabeticallyInvitableContacts:invitableAddressBookContacts];
[self sortInvitableMatrixContacts];
[[MXKContactManager sharedManager] sortAlphabeticallyContacts:invitableAddressBookContacts];
[[MXKContactManager sharedManager] sortContactsByLastActiveInformation:invitableMatrixContacts];

// Update filtered participants list
for (index = 0; index < filteredActualParticipants.count;)
Expand Down Expand Up @@ -1848,80 +1866,4 @@ - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
[searchBar resignFirstResponder];
}

#pragma mark -

- (void)sortAlphabeticallyInvitableContacts:(NSMutableArray<MXKContact*> *)invitableContacts
{
NSComparator comparator = ^NSComparisonResult(MXKContact *contactA, MXKContact *contactB) {

return [contactA.sortingDisplayName compare:contactB.sortingDisplayName options:NSCaseInsensitiveSearch];
};

// Sort invitable contacts list
[invitableContacts sortUsingComparator:comparator];
}

- (void)sortInvitableMatrixContacts
{
// Sort invitable contacts by last active, with "active now" first.
// ...and then alphabetically.
NSComparator comparator = ^NSComparisonResult(MXKContact *contactA, MXKContact *contactB) {

MXUser *userA;
MXUser *userB;

if (contactA.matrixIdentifiers.count)
{
userA = [self.mxRoom.mxSession userWithUserId:contactA.matrixIdentifiers.firstObject];
}
if (contactB.matrixIdentifiers.count)
{
userB = [self.mxRoom.mxSession userWithUserId:contactB.matrixIdentifiers.firstObject];
}

if (userA.currentlyActive && userB.currentlyActive)
{
// Then order by name
if (contactA.sortingDisplayName.length && contactB.sortingDisplayName.length)
{
return [contactA.sortingDisplayName compare:contactB.sortingDisplayName options:NSCaseInsensitiveSearch];
}
else if (contactA.sortingDisplayName.length)
{
return NSOrderedAscending;
}
else if (contactB.sortingDisplayName.length)
{
return NSOrderedDescending;
}
return [contactA.displayName compare:contactB.displayName options:NSCaseInsensitiveSearch];
}

if (userA.currentlyActive && !userB.currentlyActive)
{
return NSOrderedAscending;
}
if (!userA.currentlyActive && userB.currentlyActive)
{
return NSOrderedDescending;
}

// Finally, compare the lastActiveAgo
NSUInteger lastActiveAgoA = userA.lastActiveAgo;
NSUInteger lastActiveAgoB = userB.lastActiveAgo;

if (lastActiveAgoA == lastActiveAgoB)
{
return NSOrderedSame;
}
else
{
return ((lastActiveAgoA > lastActiveAgoB) ? NSOrderedDescending : NSOrderedAscending);
}
};

// Sort invitable contacts list
[invitableMatrixContacts sortUsingComparator:comparator];
}

@end
Loading

0 comments on commit 2d0995c

Please sign in to comment.