diff --git a/Classes/ChatConversationCreateTableView.h b/Classes/ChatConversationCreateTableView.h index e4e97087b..5de6fc8d4 100644 --- a/Classes/ChatConversationCreateTableView.h +++ b/Classes/ChatConversationCreateTableView.h @@ -9,10 +9,12 @@ #import @interface ChatConversationCreateTableView : UITableViewController -@property(weak, nonatomic) IBOutlet UISearchBar *searchBar; @property(nonatomic) Boolean allFilter; @property(nonatomic) Boolean notFirstTime; @property(nonatomic, strong) NSMutableArray *contactsGroup; +@property(nonatomic) LinphoneMagicSearch *magicSearch; + +@property(weak, nonatomic) IBOutlet UISearchBar *searchBar; @property (weak, nonatomic) IBOutlet UICollectionView *collectionView; @property (weak, nonatomic) IBOutlet UIIconButton *controllerNextButton; @property (weak, nonatomic) IBOutlet UIView *waitView; diff --git a/Classes/ChatConversationCreateTableView.m b/Classes/ChatConversationCreateTableView.m index c3f756717..294e40ae1 100644 --- a/Classes/ChatConversationCreateTableView.m +++ b/Classes/ChatConversationCreateTableView.m @@ -23,15 +23,6 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - _allContacts = [[NSMutableDictionary alloc] initWithDictionary:LinphoneManager.instance.fastAddressBook.addressBookMap]; - _sortedAddresses = [[LinphoneManager.instance.fastAddressBook.addressBookMap allKeys] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { - Contact* first = [_allContacts objectForKey:a]; - Contact* second = [_allContacts objectForKey:b]; - if([[first.firstName lowercaseString] compare:[second.firstName lowercaseString]] == NSOrderedSame) - return [[first.lastName lowercaseString] compare:[second.lastName lowercaseString]]; - else - return [[first.firstName lowercaseString] compare:[second.firstName lowercaseString]]; - }]; int y = _contactsGroup.count > 0 ? _collectionView.frame.origin.y + _collectionView.frame.size.height @@ -47,7 +38,7 @@ } completion:nil]; - _addresses = [[NSMutableArray alloc] initWithCapacity:_sortedAddresses.count]; + _addresses = [[NSMutableArray alloc] initWithCapacity:LinphoneManager.instance.fastAddressBook.addressBookMap.allKeys.count]; if(_notFirstTime) { for(NSString *addr in _contactsGroup) { [_collectionView registerClass:UIChatCreateCollectionViewCell.class forCellWithReuseIdentifier:addr]; @@ -59,10 +50,13 @@ [_searchBar setText:@""]; [self searchBar:_searchBar textDidChange:_searchBar.text]; self.tableView.accessibilityIdentifier = @"Suggested addresses"; + _magicSearch = linphone_core_create_magic_search(LC); } - (void) viewWillDisappear:(BOOL)animated { _notFirstTime = FALSE; + linphone_magic_search_unref(_magicSearch); + _magicSearch = NULL; } - (void) loadData { @@ -72,34 +66,33 @@ - (void)reloadDataWithFilter:(NSString *)filter { [_addresses removeAllObjects]; - for (NSString* key in _sortedAddresses) { - Contact *contact = [LinphoneManager.instance.fastAddressBook.addressBookMap objectForKey:key]; + if (!_magicSearch) + return; + + bctbx_list_t *results = linphone_magic_search_get_contact_list_from_filter(_magicSearch, filter.UTF8String, ""); + + while (results) { + LinphoneSearchResult *result = results->data; + const LinphoneAddress* addr = linphone_search_result_get_address(result) ?: linphone_friend_get_address(linphone_search_result_get_friend(result)); + char *uri = linphone_address_as_string_uri_only(addr); + NSString *address = [NSString stringWithUTF8String:uri]; + ms_free(uri); + + Contact *contact = [LinphoneManager.instance.fastAddressBook.addressBookMap objectForKey:address]; NSString *name = [FastAddressBook displayNameForContact:contact]; Boolean linphoneContact = [FastAddressBook contactHasValidSipDomain:contact] - || (contact.friend && linphone_presence_model_get_basic_status(linphone_friend_get_presence_model(contact.friend)) == LinphonePresenceBasicStatusOpen); + || (contact.friend && linphone_presence_model_get_basic_status(linphone_friend_get_presence_model(contact.friend)) == LinphonePresenceBasicStatusOpen); BOOL add = _allFilter || linphoneContact; if (((filter.length == 0) - || ([name.lowercaseString containsSubstring:filter.lowercaseString]) - || ([key.lowercaseString containsSubstring:filter.lowercaseString])) + || ([name.lowercaseString containsSubstring:filter.lowercaseString]) + || ([address.lowercaseString containsSubstring:filter.lowercaseString])) && add) - [_addresses addObject:key]; + [_addresses addObject:address]; + + results = results->next; } - // also add current entry, if not listed - NSString *nsuri = filter.lowercaseString; - LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:nsuri]; - if (addr) { - char *uri = linphone_address_as_string(addr); - nsuri = [NSString stringWithUTF8String:uri]; - ms_free(uri); - linphone_address_destroy(addr); - } - - if (nsuri.length > 0 && ![_addresses containsObject:nsuri]) - [_addresses addObject:nsuri]; - - [self.tableView reloadData]; } @@ -220,8 +213,12 @@ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { searchBar.showsCancelButton = (searchText.length > 0); [self reloadDataWithFilter:searchText]; - if ([searchText isEqualToString:@""]) + if ([searchText isEqualToString:@""]) { + if (_magicSearch) + linphone_magic_search_reset_search_cache(_magicSearch); + [_searchBar resignFirstResponder]; + } } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {