From 6b49bb5e194e65c859f3e45fccc6c8f486261df7 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 11 Jun 2018 12:15:46 -0400 Subject: [PATCH] Open search results. --- .../ConversationSearchViewController.swift | 30 ++++++++ .../HomeView/HomeViewController.m | 71 +++++++++++++++---- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift index 0e6f14efc..5d6d27f8e 100644 --- a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift +++ b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift @@ -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 { diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index d39ebf4cb..07c52a4d5 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -38,7 +38,10 @@ typedef NS_ENUM(NSInteger, HomeViewMode) { NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversationsReuseIdentifier"; -@interface HomeViewController () +@interface HomeViewController () @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)searchBarTextDidBeginEditing:(UISearchBar *)searchBar +{ + [self updateSearchResultsVisibility]; +} + +- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar +{ + [self updateSearchResultsVisibility]; +} -- (void)updateSearchResultsForSearchController:(UISearchController *)searchController +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { - [self.searchResultsController updateSearchResultsWithSearchText:self.searchController.searchBar.text]; + [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