diff --git a/matrixConsole/ViewController/ContactsViewController.m b/matrixConsole/ViewController/ContactsViewController.m index 36bcb4bd0..b3b6b8188 100644 --- a/matrixConsole/ViewController/ContactsViewController.m +++ b/matrixConsole/ViewController/ContactsViewController.m @@ -35,7 +35,8 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Please, visit the website http://matrix.org to have more information."; -@interface ContactsViewController () { +@interface ContactsViewController () +{ // YES -> only matrix users // NO -> display local contacts BOOL displayMatrixUsers; @@ -66,7 +67,8 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl @implementation ContactsViewController -- (void)viewDidLoad { +- (void)viewDidLoad +{ [super viewDidLoad]; // get the system collation titles @@ -75,7 +77,7 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl // global init displayMatrixUsers = (0 == self.contactsControls.selectedSegmentIndex); matrixUserByMatrixID = [[NSMutableDictionary alloc] init]; - + // add the search icon on the right // need to add more buttons ? UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search:)]; @@ -87,16 +89,19 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl self.rageShakeManager = [RageShakeManager sharedManager]; } -- (void)viewWillDisappear:(BOOL)animated { +- (void)viewWillDisappear:(BOOL)animated +{ [super viewWillDisappear:animated]; // Leave potential search session - if (contactsSearchBar) { + if (contactsSearchBar) + { [self searchBarCancelButtonClicked:contactsSearchBar]; } } -- (void)scrollToTop { +- (void)scrollToTop +{ // stop any scrolling effect [UIView setAnimationsEnabled:NO]; // before scrolling to the tableview top @@ -106,10 +111,12 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl // should be called when resetting the application // the contact manager warn there is a contacts list update -// but the Matrix SDK handler has no more userID -> so assume there is a reset -- (void)reset { +// but the Matrix SDK handler has no more userID -> so assume there is a reset +- (void)reset +{ // Leave potential search session - if (contactsSearchBar) { + if (contactsSearchBar) + { [self searchBarCancelButtonClicked:contactsSearchBar]; } @@ -123,13 +130,18 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl [self.tableView reloadData]; } -- (void)refreshMatrixUsers { - if (displayMatrixUsers) { - if (contactsSearchBar) { +- (void)refreshMatrixUsers +{ + if (displayMatrixUsers) + { + if (contactsSearchBar) + { [self updateSectionedMatrixContacts]; latestSearchedPattern = nil; [self searchBar:contactsSearchBar textDidChange:contactsSearchBar.text]; - } else { + } + else + { [self.tableView reloadData]; } } @@ -137,7 +149,8 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl #pragma mark - overridden MXKTableViewController methods -- (void)onMatrixSessionChange { +- (void)onMatrixSessionChange +{ [super onMatrixSessionChange]; @@ -146,26 +159,33 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl #pragma mark - UITableView delegate -- (void)updateSectionedLocalContacts { +- (void)updateSectionedLocalContacts +{ [self stopActivityIndicator]; ContactManager* sharedManager = [ContactManager sharedManager]; - if (!localContacts) { + if (!localContacts) + { localContacts = sharedManager.contacts; } - if (!sectionedLocalContacts) { + if (!sectionedLocalContacts) + { sectionedLocalContacts = [sharedManager getSectionedContacts:sharedManager.contacts]; } } -- (void)updateSectionedMatrixContacts { +- (void)updateSectionedMatrixContacts +{ // Check whether mainSession is available - if (!self.mainSession) { + if (!self.mainSession) + { [self startActivityIndicator]; sectionedMatrixContacts = nil; - } else { + } + else + { [self stopActivityIndicator]; NSArray* usersIDs = [self oneToOneRoomMemberIDs]; @@ -176,21 +196,26 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl // list the contacts IDs // avoid delete and create the same ones // it could save thumbnail downloads - for(NSString* userID in usersIDs) { + for(NSString* userID in usersIDs) + { // MXUser* user = [self.mainSession userWithUserId:userID]; // sanity check - if (user) { + if (user) + { // managed UserID [knownUserIDs removeObject:userID]; MXCContact* contact = [matrixUserByMatrixID objectForKey:userID]; // already defined - if (contact) { + if (contact) + { contact.displayName = (user.displayname.length > 0) ? user.displayname : user.userId; - } else { + } + else + { contact = [[MXCContact alloc] initWithDisplayName:((user.displayname.length > 0) ? user.displayname : user.userId) matrixID:user.userId]; [matrixUserByMatrixID setValue:contact forKey:userID]; } @@ -198,7 +223,8 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } // some userIDs don't exist anymore - for (NSString* userID in knownUserIDs) { + for (NSString* userID in knownUserIDs) + { [matrixUserByMatrixID removeObjectForKey:userID]; } @@ -206,52 +232,67 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } } -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ // search in progress - if (contactsSearchBar) { + if (contactsSearchBar) + { return sectionedFilteredContacts.sectionedContacts.count; } - else if (displayMatrixUsers) { + else if (displayMatrixUsers) + { [self updateSectionedMatrixContacts]; return sectionedMatrixContacts.sectionedContacts.count; - } else { + } + else + { [self updateSectionedLocalContacts]; return sectionedLocalContacts.sectionedContacts.count; } } -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ SectionedContacts* sectionedContacts = contactsSearchBar ? sectionedFilteredContacts : (displayMatrixUsers ? sectionedMatrixContacts : sectionedLocalContacts); return [[sectionedContacts.sectionedContacts objectAtIndex:section] count]; } -- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ return 50; } -- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { - if (contactsSearchBar) { +- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section +{ + if (contactsSearchBar) + { // Hide section titles during search session return nil; } SectionedContacts* sectionedContacts = contactsSearchBar ? sectionedFilteredContacts : (displayMatrixUsers ? sectionedMatrixContacts : sectionedLocalContacts); - if (sectionedContacts.sectionTitles.count <= section) { + if (sectionedContacts.sectionTitles.count <= section) + { return nil; } - else { + else + { return (NSString*)[sectionedContacts.sectionTitles objectAtIndex:section]; } } -- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)aTableView { +- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)aTableView +{ // do not display the collation during a search - if (contactsSearchBar) { + if (contactsSearchBar) + { return nil; - } else { + } + else + { [self.tableView setSectionIndexColor:[AppDelegate theDelegate].masterTabBarController.tabBar.tintColor]; [self.tableView setSectionIndexBackgroundColor:[UIColor clearColor]]; @@ -259,34 +300,41 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } } -- (NSInteger)tableView:(UITableView *)aTableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { +- (NSInteger)tableView:(UITableView *)aTableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index +{ SectionedContacts* sectionedContacts = contactsSearchBar ? sectionedFilteredContacts : (displayMatrixUsers ? sectionedMatrixContacts : sectionedLocalContacts); NSUInteger section = [sectionedContacts.sectionTitles indexOfObject:title]; // undefined title -> jump to the first valid non empty section - if (NSNotFound == section) { + if (NSNotFound == section) + { NSUInteger systemCollationIndex = [collationTitles indexOfObject:title]; // find in the system collation - if (NSNotFound != systemCollationIndex) { + if (NSNotFound != systemCollationIndex) + { systemCollationIndex--; - while ((systemCollationIndex == 0) && (NSNotFound == section)) { + while ((systemCollationIndex == 0) && (NSNotFound == section)) + { NSString* systemTitle = [collationTitles objectAtIndex:systemCollationIndex]; section = [sectionedContacts.sectionTitles indexOfObject:systemTitle]; systemCollationIndex--; } } } - + return section; } -- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section +{ // In case of search, the section titles are hidden and the search bar is displayed in first section header. - if (contactsSearchBar) { - if (section == 0) { + if (contactsSearchBar) + { + if (section == 0) + { return contactsSearchBar.frame.size.height; } return 0; @@ -297,23 +345,28 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl return 22; } -- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { - if (contactsSearchBar && section == 0) { +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section +{ + if (contactsSearchBar && section == 0) + { return contactsSearchBar; } return nil; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ ContactTableCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath]; SectionedContacts* sectionedContacts = contactsSearchBar ? sectionedFilteredContacts : (displayMatrixUsers ? sectionedMatrixContacts : sectionedLocalContacts); MXCContact* contact = nil; - if (indexPath.section < sectionedContacts.sectionedContacts.count) { + if (indexPath.section < sectionedContacts.sectionedContacts.count) + { NSArray *thisSection = [sectionedContacts.sectionedContacts objectAtIndex:indexPath.section]; - if (indexPath.row < thisSection.count) { + if (indexPath.row < thisSection.count) + { contact = [thisSection objectAtIndex:indexPath.row]; } } @@ -324,16 +377,19 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl // check if it is already defined // gesture in storyboard does not seem to work properly // it always triggers a tap event on the first cell - for (UIGestureRecognizer* gesture in cell.thumbnailView.gestureRecognizers) { + for (UIGestureRecognizer* gesture in cell.thumbnailView.gestureRecognizers) + { - if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) { + if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) + { tapGesture = (UITapGestureRecognizer*)gesture; break; } } - + // add it if it is not yet defined - if (!tapGesture) { + if (!tapGesture) + { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onContactThumbnailTap:)]; [tap setNumberOfTouchesRequired:1]; [tap setNumberOfTapsRequired:1]; @@ -342,59 +398,72 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } cell.contact = contact; - + return cell; } -- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; SectionedContacts* sectionedContacts = contactsSearchBar ? sectionedFilteredContacts : (displayMatrixUsers ? sectionedMatrixContacts : sectionedLocalContacts); MXCContact* contact = nil; - if (indexPath.section < sectionedContacts.sectionedContacts.count) { + if (indexPath.section < sectionedContacts.sectionedContacts.count) + { NSArray *thisSection = [sectionedContacts.sectionedContacts objectAtIndex:indexPath.section]; - if (indexPath.row < thisSection.count) { + if (indexPath.row < thisSection.count) + { contact = [thisSection objectAtIndex:indexPath.row]; } } __weak typeof(self) weakSelf = self; NSArray* matrixIDs = contact.matrixIdentifiers; - + // matrix user ? - if (matrixIDs.count) { + if (matrixIDs.count) + { // display only if the mainSession is available in matrix SDK handler - if (self.mainSession) { + if (self.mainSession) + { // only 1 matrix ID - if (matrixIDs.count == 1) { + if (matrixIDs.count == 1) + { NSString* matrixID = [matrixIDs objectAtIndex:0]; - + self.startChatMenu = [[MXKAlert alloc] initWithTitle:[NSString stringWithFormat:@"Chat with %@", matrixID] message:nil style:MXKAlertStyleAlert]; - [self.startChatMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.startChatMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; }]; - [self.startChatMenu addActionWithTitle:@"OK" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.startChatMenu addActionWithTitle:@"OK" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; [[AppDelegate theDelegate] startPrivateOneToOneRoomWithUserId:matrixID]; }]; - } else { + } + else + { self.startChatMenu = [[MXKAlert alloc] initWithTitle:[NSString stringWithFormat:@"Chat with "] message:nil style:MXKAlertStyleActionSheet]; - for(NSString* matrixID in matrixIDs) { - [self.startChatMenu addActionWithTitle:matrixID style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + for(NSString* matrixID in matrixIDs) + { + [self.startChatMenu addActionWithTitle:matrixID style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; [[AppDelegate theDelegate] startPrivateOneToOneRoomWithUserId:matrixID]; }]; } - [self.startChatMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.startChatMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; }]; @@ -404,29 +473,36 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl [self.startChatMenu showInViewController:self]; } - } else { + } + else + { // invite to use matrix - if (([MFMessageComposeViewController canSendText] ? contact.emailAddresses.count : 0) + (contact.phoneNumbers.count > 0)) { - + if (([MFMessageComposeViewController canSendText] ? contact.emailAddresses.count : 0) + (contact.phoneNumbers.count > 0)) + { + self.startChatMenu = [[MXKAlert alloc] initWithTitle:[NSString stringWithFormat:@"Invite this user to use matrix with"] message:nil style:MXKAlertStyleActionSheet]; // check if the target can send SMSes - if ([MFMessageComposeViewController canSendText]) { + if ([MFMessageComposeViewController canSendText]) + { // list phonenumbers - for(MXCPhoneNumber* phonenumber in contact.phoneNumbers) { + for(MXCPhoneNumber* phonenumber in contact.phoneNumbers) + { - [self.startChatMenu addActionWithTitle:phonenumber.textNumber style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.startChatMenu addActionWithTitle:phonenumber.textNumber style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; // launch SMS composer MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc] init]; if (messageComposer) + { messageComposer.messageComposeDelegate = weakSelf; messageComposer.body =kInvitationMessage; messageComposer.recipients = [NSArray arrayWithObject:phonenumber.textNumber]; - + dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf presentViewController:messageComposer animated:YES completion:nil]; }); @@ -436,9 +512,11 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } // list emails - for(MXCEmail* email in contact.emailAddresses) { + for(MXCEmail* email in contact.emailAddresses) + { - [self.startChatMenu addActionWithTitle:email.emailAddress style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.startChatMenu addActionWithTitle:email.emailAddress style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; dispatch_async(dispatch_get_main_queue(), ^{ @@ -451,7 +529,8 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl }]; } - [self.startChatMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.startChatMenu addActionWithTitle:@"Cancel" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.startChatMenu = nil; }]; @@ -464,59 +543,79 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl #pragma mark - Actions -- (void)onContactsRefresh:(NSNotification *)notif { +- (void)onContactsRefresh:(NSNotification *)notif +{ localContacts = nil; sectionedLocalContacts = nil; // there is an user id - if (self.mainSession && self.mainSession.myUser.userId) { + if (self.mainSession && self.mainSession.myUser.userId) + { [self updateSectionedLocalContacts]; // - if (!displayMatrixUsers) { - if (contactsSearchBar) { + if (!displayMatrixUsers) + { + if (contactsSearchBar) + { latestSearchedPattern = nil; [self searchBar:contactsSearchBar textDidChange:contactsSearchBar.text]; - } else { + } + else + { [self.tableView reloadData]; } } - } else { + } + else + { // the client could have been logged out [self reset]; } } -- (IBAction)onSegmentValueChange:(id)sender { - if (sender == self.contactsControls) { +- (IBAction)onSegmentValueChange:(id)sender +{ + if (sender == self.contactsControls) + { displayMatrixUsers = (0 == self.contactsControls.selectedSegmentIndex); - if (contactsSearchBar) { - if (displayMatrixUsers) { + if (contactsSearchBar) + { + if (displayMatrixUsers) + { [self updateSectionedMatrixContacts]; - } else { + } + else + { [self updateSectionedLocalContacts]; } latestSearchedPattern = nil; [self searchBar:contactsSearchBar textDidChange:contactsSearchBar.text]; - } else { + } + else + { [self.tableView reloadData]; } - - if (!displayMatrixUsers) { + + if (!displayMatrixUsers) + { MXKAppSettings* appSettings = [MXKAppSettings standardAppSettings]; - if (!appSettings.syncLocalContacts) { + if (!appSettings.syncLocalContacts) + { __weak typeof(self) weakSelf = self; self.allowContactSyncAlert = [[MXKAlert alloc] initWithTitle:@"Allow local contacts synchronization ?" message:nil style:MXKAlertStyleAlert]; - [self.allowContactSyncAlert addActionWithTitle:@"No" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + [self.allowContactSyncAlert addActionWithTitle:@"No" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { weakSelf.allowContactSyncAlert = nil; }]; - [self.allowContactSyncAlert addActionWithTitle:@"Yes" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { - weakSelf.allowContactSyncAlert = nil; + [self.allowContactSyncAlert addActionWithTitle:@"Yes" style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) + { + weakSelf.allowContactSyncAlert = nil; dispatch_async(dispatch_get_main_queue(), ^{ appSettings.syncLocalContacts = YES; [weakSelf.tableView reloadData]; @@ -529,21 +628,26 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } } -- (IBAction)onContactThumbnailTap:(id)sender { - if ([sender isKindOfClass:[UITapGestureRecognizer class]]) { +- (IBAction)onContactThumbnailTap:(id)sender +{ + if ([sender isKindOfClass:[UITapGestureRecognizer class]]) + { UIView* tappedView = ((UITapGestureRecognizer*)sender).view; // search the parentce cell - while (tappedView && ![tappedView isKindOfClass:[ContactTableCell class]]) { + while (tappedView && ![tappedView isKindOfClass:[ContactTableCell class]]) + { tappedView = tappedView.superview; } // find it ? - if ([tappedView isKindOfClass:[ContactTableCell class]]) { + if ([tappedView isKindOfClass:[ContactTableCell class]]) + { MXCContact* contact = ((ContactTableCell*)tappedView).contact; // open detailled sheet if there - if (contact.matrixIdentifiers.count > 0) { + if (contact.matrixIdentifiers.count > 0) + { selectedContact = ((ContactTableCell*)tappedView).contact; [self performSegueWithIdentifier:@"showContactDetails" sender:self]; } @@ -554,8 +658,10 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl #pragma mark - Segues -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - if ([segue.identifier isEqualToString:@"showContactDetails"]) { +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +{ + if ([segue.identifier isEqualToString:@"showContactDetails"]) + { ContactDetailsViewController *contactDetailsViewController = segue.destinationViewController; contactDetailsViewController.contact = selectedContact; selectedContact = nil; @@ -564,18 +670,22 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl #pragma mark MFMessageComposeViewControllerDelegate -- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { +- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result +{ [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark Search management -- (void)search:(id)sender { - if (!contactsSearchBar) { +- (void)search:(id)sender +{ + if (!contactsSearchBar) + { SectionedContacts* sectionedContacts = displayMatrixUsers ? sectionedMatrixContacts : sectionedLocalContacts; // Check whether there are data in which search - if (sectionedContacts.sectionedContacts.count > 0) { + if (sectionedContacts.sectionedContacts.count > 0) + { // Create search bar contactsSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; contactsSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; @@ -589,40 +699,48 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl latestSearchedPattern = @""; filteredContacts = [(displayMatrixUsers ? [matrixUserByMatrixID allValues] : localContacts) mutableCopy]; sectionedFilteredContacts = [[ContactManager sharedManager] getSectionedContacts:filteredContacts]; - + [self.tableView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ [contactsSearchBar becomeFirstResponder]; }); } - } else { + } + else + { [self searchBarCancelButtonClicked:contactsSearchBar]; } } #pragma mark - UISearchBarDelegate -- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { +- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar +{ searchBarShouldEndEditing = NO; return YES; } -- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { +- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar +{ return searchBarShouldEndEditing; } -- (NSArray*)patternsFromText:(NSString*)text { +- (NSArray*)patternsFromText:(NSString*)text +{ NSArray* items = [text componentsSeparatedByString:@" "]; - if (items.count <= 1) { + if (items.count <= 1) + { return items; } NSMutableArray* patterns = [[NSMutableArray alloc] init]; - for (NSString* item in items) { - if (item.length > 0) { + for (NSString* item in items) + { + if (item.length > 0) + { [patterns addObject:item]; } } @@ -630,25 +748,32 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl return patterns; } -- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { - if ((contactsSearchBar == searchBar) && (![latestSearchedPattern isEqualToString:searchText])) { +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText +{ + if ((contactsSearchBar == searchBar) && (![latestSearchedPattern isEqualToString:searchText])) + { latestSearchedPattern = searchText; // contacts NSArray* contacts = displayMatrixUsers ? [matrixUserByMatrixID allValues] : localContacts; // Update filtered list - if (searchText.length && contacts.count) { + if (searchText.length && contacts.count) + { filteredContacts = [[NSMutableArray alloc] init]; NSArray* patterns = [self patternsFromText:searchText]; - for(MXCContact* contact in contacts) { - if ([contact matchedWithPatterns:patterns]) { + for(MXCContact* contact in contacts) + { + if ([contact matchedWithPatterns:patterns]) + { [filteredContacts addObject:contact]; } } - } else { + } + else + { filteredContacts = [contacts mutableCopy]; } @@ -660,16 +785,20 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl } } -- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { - if (contactsSearchBar == searchBar) { +- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar +{ + if (contactsSearchBar == searchBar) + { // "Done" key has been pressed searchBarShouldEndEditing = YES; [contactsSearchBar resignFirstResponder]; } } -- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { - if (contactsSearchBar == searchBar) { +- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar +{ + if (contactsSearchBar == searchBar) + { // Leave search searchBarShouldEndEditing = YES; [contactsSearchBar resignFirstResponder]; @@ -686,22 +815,29 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl #pragma mark - Matrix session handling // return a MatrixIDs list of 1:1 room members -- (NSArray*)oneToOneRoomMemberIDs { +- (NSArray*)oneToOneRoomMemberIDs +{ NSMutableArray* matrixIDs = [[NSMutableArray alloc] init]; - if (self.mainSession) { - for (MXRoom *mxRoom in self.mainSession.rooms) { + if (self.mainSession) + { + for (MXRoom *mxRoom in self.mainSession.rooms) + { NSArray* membersList = [mxRoom.state members]; // keep only 1:1 chat - if ([mxRoom.state members].count <= 2) { + if ([mxRoom.state members].count <= 2) + { - for (MXRoomMember* member in membersList) { + for (MXRoomMember* member in membersList) + { // not myself - if (![member.userId isEqualToString:self.mainSession.myUser.userId]) { - if ([matrixIDs indexOfObject:member.userId] == NSNotFound) { + if (![member.userId isEqualToString:self.mainSession.myUser.userId]) + { + if ([matrixIDs indexOfObject:member.userId] == NSNotFound) + { [matrixIDs addObject:member.userId]; } }