New Chat screens: implement participants selections.

This commit is contained in:
giomfo
2015-08-14 16:40:03 +02:00
parent f260ad9d5f
commit 44fc29a4cb
10 changed files with 331 additions and 26 deletions
@@ -27,13 +27,15 @@
UIButton *createButton;
MXHTTPOperation *roomCreationRequest;
// Add participant
NSInteger addParticipantSection;
UISearchBar *participantsSearchBar;
// Add participants
NSInteger addParticipantsSection;
UISearchBar *addParticipantsSearchBar;
NSString *addParticipantsSearchText;
NSMutableArray *filteredParticipants;
// Participants
NSInteger participantsSection;
NSMutableDictionary *participantsByIds;
}
@end
@@ -65,8 +67,9 @@
- (void)destroy
{
createButton = nil;
participantsSearchBar = nil;
addParticipantsSearchBar = nil;
filteredParticipants = nil;
participantsByIds = nil;
[super destroy];
}
@@ -153,10 +156,10 @@
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger count = 0;
createButtonSection = addParticipantSection = participantsSection = -1;
createButtonSection = addParticipantsSection = participantsSection = -1;
createButtonSection = count++;
addParticipantSection = count++;
addParticipantsSection = count++;
participantsSection = count++;
return count;
@@ -169,13 +172,17 @@
{
count = 1;
}
else if (section == addParticipantSection)
else if (section == addParticipantsSection)
{
count = 1 + filteredParticipants.count;
}
else if (section == participantsSection)
{
count = _roomCreationInputs.roomParticipants.count;
if (_roomCreationInputs.mxSession)
{
count++;
}
}
return count;
@@ -183,7 +190,7 @@
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == addParticipantSection)
if (section == addParticipantsSection)
{
return NSLocalizedStringFromTable(@"room_creation_add_participants", @"Vector", nil);
}
@@ -216,14 +223,123 @@
cell = createButtonCell;
}
else if (indexPath.section == addParticipantSection)
else if (indexPath.section == addParticipantsSection)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"roomPictureCell"];
cell.textLabel.text = @"todo";
if (indexPath.row == 0)
{
MXKTableViewCellWithSearchBar *addParticipantsSearchCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCellWithSearchBar defaultReuseIdentifier]];
if (!addParticipantsSearchCell)
{
addParticipantsSearchCell = [[MXKTableViewCellWithSearchBar alloc] init];
}
addParticipantsSearchBar = addParticipantsSearchCell.mxkSearchBar;
addParticipantsSearchBar.backgroundColor = [UIColor clearColor];
addParticipantsSearchBar.text = addParticipantsSearchText;
addParticipantsSearchBar.returnKeyType = UIReturnKeyDone;
addParticipantsSearchBar.delegate = self;
if (addParticipantsSearchText.length)
{
[addParticipantsSearchBar becomeFirstResponder];
}
cell = addParticipantsSearchCell;
}
else
{
NSInteger index = indexPath.row - 1;
if (index < filteredParticipants.count)
{
MXKContactTableCell* filteredParticipantCell = [tableView dequeueReusableCellWithIdentifier:[MXKContactTableCell defaultReuseIdentifier]];
if (!filteredParticipantCell)
{
filteredParticipantCell = [[MXKContactTableCell alloc] init];
}
[filteredParticipantCell render:filteredParticipants[index]];
// Trick, the 'add' icon is displayed here by using the matrixUserIconview which is visible for matrix contact only.
filteredParticipantCell.matrixUserIconViewHeightConstraint.constant = 30;
filteredParticipantCell.matrixUserIconView.image = [UIImage imageNamed:@"add"];
filteredParticipantCell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell = filteredParticipantCell;
}
}
}
else if (indexPath.section == participantsSection)
{
//TODO
MXKContactTableCell *participantCell = [tableView dequeueReusableCellWithIdentifier:[MXKContactTableCell defaultReuseIdentifier]];
if (!participantCell)
{
participantCell = [[MXKContactTableCell alloc] init];
}
if (_roomCreationInputs.mxSession && indexPath.row == 0)
{
MXKContact *contact = [[MXKContact alloc] initMatrixContactWithDisplayName:NSLocalizedStringFromTable(@"you", @"Vector", nil) andMatrixID:_roomCreationInputs.mxSession.myUser.userId];
[participantCell render:contact];
// Trick, remove matrixUserIconview image to hide it.
participantCell.matrixUserIconView.image = nil;
participantCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
NSInteger index = indexPath.row;
NSArray *participants = _roomCreationInputs.roomParticipants;
if (_roomCreationInputs.mxSession)
{
index --;
}
if (index < participants.count)
{
NSString *userId = participants[index];
MXKContact *contact = [participantsByIds objectForKey:userId];
// Note: contact may be nil here if the participant has not been added into _roomCreationInputs by self.
if (!contact)
{
// Create this missing contact
// Look for the correpsonding MXUser
NSArray *sessions = self.mxSessions;
MXUser *mxUser;
for (MXSession *session in sessions)
{
mxUser = [session userWithUserId:userId];
if (mxUser)
{
contact = [[MXKContact alloc] initMatrixContactWithDisplayName:((mxUser.displayname.length > 0) ? mxUser.displayname : userId) andMatrixID:userId];
break;
}
}
if (contact)
{
if (!participantsByIds)
{
participantsByIds = [NSMutableDictionary dictionary];
}
[participantsByIds setObject:contact forKey:userId];
}
}
if (contact)
{
[participantCell render:contact];
}
// Trick, the 'remove' icon is displayed here by using the matrixUserIconview which is visible for matrix contact only.
participantCell.matrixUserIconViewHeightConstraint.constant = 30;
participantCell.matrixUserIconView.image = [UIImage imageNamed:@"remove"];
participantCell.selectionStyle = UITableViewCellSelectionStyleDefault;
}
}
cell = participantCell;
}
return cell;
@@ -246,6 +362,17 @@
return 30;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]])
{
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
tableViewHeaderFooterView.textLabel.text = [tableViewHeaderFooterView.textLabel.text capitalizedString];
tableViewHeaderFooterView.textLabel.font = [UIFont boldSystemFontOfSize:17];
tableViewHeaderFooterView.textLabel.textColor = [UIColor blackColor];
}
}
//- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
// return 1;
@@ -253,9 +380,162 @@
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// TODO
if (indexPath.section == addParticipantsSection && indexPath.row)
{
NSInteger index = indexPath.row - 1;
if (index < filteredParticipants.count)
{
MXKContact *contact = filteredParticipants[index];
NSArray *identifiers = contact.matrixIdentifiers;
if (identifiers.count)
{
[_roomCreationInputs addParticipant:identifiers.firstObject];
// Handle a mapping contact by userId for selected participants
if (!participantsByIds)
{
participantsByIds = [NSMutableDictionary dictionary];
}
[participantsByIds setObject:contact forKey:identifiers.firstObject];
}
[filteredParticipants removeObjectAtIndex:index];
// Refresh display
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange (addParticipantsSection, 2)];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}
}
else if (indexPath.section == participantsSection)
{
NSInteger index = indexPath.row;
if (_roomCreationInputs.mxSession)
{
index --;
}
NSArray *participants = _roomCreationInputs.roomParticipants;
if (index < participants.count)
{
NSInteger firstSectionToRefresh = participantsSection;
[_roomCreationInputs removeParticipant:participants[index]];
// Check whether this removed participant must be added to the search result if any
MXKContact *contact = [participantsByIds objectForKey:participants[index]];
if (contact && addParticipantsSearchText.length && [contact matchedWithPatterns:@[addParticipantsSearchText]])
{
[filteredParticipants addObject:contact];
firstSectionToRefresh = addParticipantsSection;
}
[participantsByIds removeObjectForKey:participants[index]];
// Refresh display
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange (firstSectionToRefresh, 2)];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - UISearchBar delegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSInteger previousFilteredCount = filteredParticipants.count;
NSMutableArray *mxUsers;
if (addParticipantsSearchText.length && [searchText hasPrefix:addParticipantsSearchText])
{
mxUsers = filteredParticipants;
}
else
{
// Retrieve all known matrix users
NSArray *matrixContacts = [NSMutableArray arrayWithArray:[MXKContactManager sharedManager].matrixContacts];
mxUsers = [NSMutableArray arrayWithCapacity:matrixContacts.count];
// Split contacts with several ids, and remove the current participants.
NSArray *participants = _roomCreationInputs.roomParticipants;
for (MXKContact* contact in matrixContacts)
{
NSArray *identifiers = contact.matrixIdentifiers;
if (identifiers.count > 1)
{
for (NSString *userId in identifiers)
{
if (!participants || [participants indexOfObject:userId] == NSNotFound)
{
if (![userId isEqualToString:_roomCreationInputs.mxSession.myUser.userId])
{
MXKContact *splitContact = [[MXKContact alloc] initMatrixContactWithDisplayName:contact.displayName andMatrixID:userId];
[mxUsers addObject:splitContact];
}
}
}
}
else if (identifiers.count)
{
NSString *userId = identifiers.firstObject;
if (!participants || [participants indexOfObject:userId] == NSNotFound)
{
if (![userId isEqualToString:_roomCreationInputs.mxSession.myUser.userId])
{
[mxUsers addObject:contact];
}
}
}
}
}
addParticipantsSearchText = searchText;
filteredParticipants = [NSMutableArray array];
for (MXKContact* contact in mxUsers)
{
if ([contact matchedWithPatterns:@[addParticipantsSearchText]])
{
[filteredParticipants addObject:contact];
}
}
if (previousFilteredCount || filteredParticipants.count)
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange (addParticipantsSection, 1)];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = YES;
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
// "Done" key has been pressed
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// Leave search
[searchBar resignFirstResponder];
addParticipantsSearchBar = nil;
addParticipantsSearchText = nil;
filteredParticipants = nil;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange (addParticipantsSection, 1)];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}
@end