Open search results.

This commit is contained in:
Matthew Chen 2018-06-11 12:15:46 -04:00
parent f118066b3f
commit 6b49bb5e19
2 changed files with 89 additions and 12 deletions

View File

@ -36,6 +36,36 @@ class ConversationSearchViewController: UITableViewController {
tableView.register(MessageSearchResultCell.self, forCellReuseIdentifier: MessageSearchResultCell.reuseIdentifier)
}
// MARK: UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
guard let searchSection = SearchSection(rawValue: indexPath.section) else {
owsFail("\(logTag) unknown section selected.")
return
}
var sectionResults: [SearchResult]
switch searchSection {
case .conversations:
sectionResults = searchResultSet.conversations
case .contacts:
sectionResults = searchResultSet.contacts
case .messages:
sectionResults = searchResultSet.messages
}
guard indexPath.row < sectionResults.count else {
owsFail("\(logTag) unknown row selected.")
return
}
let searchResult = sectionResults[indexPath.row]
let thread = searchResult.thread
SignalApp.shared().presentConversation(for: thread.threadRecord, action: .compose)
}
// MARK: UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

View File

@ -38,7 +38,10 @@ typedef NS_ENUM(NSInteger, HomeViewMode) {
NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversationsReuseIdentifier";
@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, UIViewControllerPreviewingDelegate, UISearchResultsUpdating>
@interface HomeViewController () <UITableViewDelegate,
UITableViewDataSource,
UIViewControllerPreviewingDelegate,
UISearchBarDelegate>
@property (nonatomic) UITableView *tableView;
@property (nonatomic) UILabel *emptyBoxLabel;
@ -56,7 +59,7 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
// Mark: Search
@property (nonatomic) UISearchController *searchController;
@property (nonatomic, readonly) UISearchBar *searchBar;
@property (nonatomic) ConversationSearchViewController *searchResultsController;
// Dependencies
@ -296,16 +299,29 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
}
// Search
UISearchBar *searchBar = [UISearchBar new];
_searchBar = searchBar;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchBar.placeholder = NSLocalizedString(@"HOME_VIEW_CONVERSATION_SEARCHBAR_PLACEHOLDER",
@"Placeholder text for search bar which filters conversations.");
searchBar.backgroundColor = [UIColor whiteColor];
searchBar.delegate = self;
[searchBar sizeToFit];
// Setting tableHeader calls numberOfSections, which must happen after updateMappings has been called at least once.
OWSAssert(self.tableView.tableHeaderView == nil);
self.tableView.tableHeaderView = self.searchBar;
ConversationSearchViewController *searchResultsController = [ConversationSearchViewController new];
self.searchResultsController = searchResultsController;
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController = searchController;
searchController.searchResultsUpdater = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self addChildViewController:searchResultsController];
[self.view addSubview:searchResultsController.view];
[searchResultsController.view autoPinWidthToSuperview];
[searchResultsController.view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:searchBar];
[searchResultsController.view autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.tableView];
searchResultsController.view.hidden = self;
[self updateBarButtonItems];
}
@ -859,11 +875,42 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
return YES;
}
#pragma mark - SearchResultsUpdating
#pragma mark - UISearchBarDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self.searchResultsController updateSearchResultsWithSearchText:self.searchController.searchBar.text];
[self updateSearchResultsVisibility];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[self updateSearchResultsVisibility];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self updateSearchResultsVisibility];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self updateSearchResultsVisibility];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
self.searchBar.text = nil;
[self updateSearchResultsVisibility];
}
- (void)updateSearchResultsVisibility
{
OWSAssertIsOnMainThread();
[self.searchResultsController updateSearchResultsWithSearchText:self.searchBar.text];
BOOL isSearching = self.searchBar.text.length > 0;
self.searchResultsController.view.hidden = !isSearching;
}
#pragma mark - HomeFeedTableViewCellDelegate