Use search results controller

// FREEBIE
This commit is contained in:
Michael Kirk 2018-06-09 04:52:03 -06:00
parent 21788f5f93
commit 0f7dcccd59
2 changed files with 26 additions and 40 deletions

View File

@ -5,7 +5,7 @@
import Foundation
@objc
class ConversationSearchViewController: UITableViewController, UISearchBarDelegate {
class ConversationSearchViewController: UITableViewController {
var searchResultSet: SearchResultSet = SearchResultSet.empty
@ -125,31 +125,19 @@ class ConversationSearchViewController: UITableViewController, UISearchBarDelega
// MARK: UISearchBarDelegate
@objc
public var searchBar: UISearchBar?
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
guard let searchBar = self.searchBar else {
owsFail("\(logTag) searchBar was unexpectedly nil")
return
}
searchBar.resignFirstResponder()
}
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
public func updateSearchResults(searchText: String) {
guard searchText.stripped.count > 0 else {
self.searchResultSet = SearchResultSet.empty
self.view.isHidden = true
return
}
self.view.isHidden = false
// TODO: async?
// TODO: debounce?
self.uiDatabaseConnection.read { transaction in
self.searchResultSet = self.searcher.results(searchText: searchText, transaction: transaction)
}
// TODO: debounce
// TODO: more perfomant way to do this?
self.tableView.reloadData()
}
@ -242,7 +230,9 @@ class MessageSearchResultCell: UITableViewCell {
// Bold snippet text
do {
// FIXME - apply our font without clobbering bold.
// FIXME - The snippet marks up the matched search text with <b> tags.
// We can parse this into an attributed string, but it also takes on an undesirable font.
// We want to apply our own font without clobbering bold in the process - maybe by enumerating and inspecting the attributes? Or maybe we can pass in a base font?
let attributedSnippet = try NSMutableAttributedString(data: encodedString,
options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)

View File

@ -38,7 +38,7 @@ typedef NS_ENUM(NSInteger, HomeViewMode) {
NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversationsReuseIdentifier";
@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, UIViewControllerPreviewingDelegate>
@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, UIViewControllerPreviewingDelegate, UISearchResultsUpdating>
@property (nonatomic) UITableView *tableView;
@property (nonatomic) UILabel *emptyBoxLabel;
@ -56,8 +56,9 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
// Mark: Search
@property (nonatomic) UISearchBar *searchBar;
@property (nonatomic) ConversationSearchViewController *searchController;
//@property (nonatomic) UISearchBar *searchBar;
@property (nonatomic) UISearchController *searchController;
@property (nonatomic) ConversationSearchViewController *searchResultsController;
// Dependencies
@ -294,30 +295,18 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
&& (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) {
[self registerForPreviewingWithDelegate:self sourceView:self.tableView];
}
// 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 sizeToFit];
// Setting tableHeader calls numberOfSections, which must happen after updateMappings has been called at least once.
ConversationSearchViewController *searchController = [ConversationSearchViewController new];
ConversationSearchViewController *searchResultsController = [ConversationSearchViewController new];
self.searchResultsController = searchResultsController;
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController = searchController;
[self addChildViewController:searchController];
searchController.view.frame = self.view.frame;
[self.view addSubview:searchController.view];
// TODO - better/more flexible way to pin below search bar?
[searchController.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
searchBar.delegate = searchController;
searchController.searchBar = searchBar;
OWSAssert(self.tableView.tableHeaderView == nil);
self.tableView.tableHeaderView = self.searchBar;
searchController.searchResultsUpdater = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self updateBarButtonItems];
}
@ -871,6 +860,13 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
return YES;
}
#pragma mark - SearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
[self.searchResultsController updateSearchResultsWithSearchText:self.searchController.searchBar.text];
}
#pragma mark - HomeFeedTableViewCellDelegate
- (void)tableViewCellTappedDelete:(NSIndexPath *)indexPath