diff --git a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift index 38cb9f052..0ec65a0a1 100644 --- a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift +++ b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift @@ -7,7 +7,7 @@ import Foundation @objc class ConversationSearchViewController: UITableViewController { - var searchResults: ConversationSearchResults = ConversationSearchResults.empty + var searchResultSet: SearchResultSet = SearchResultSet.empty var uiDatabaseConnection: YapDatabaseConnection { // TODO do we want to respond to YapDBModified? Might be hard when there's lots of search results, for only marginal value @@ -44,18 +44,18 @@ class ConversationSearchViewController: UITableViewController { switch searchSection { case .conversations: - return searchResults.conversations.count + return searchResultSet.conversations.count case .contacts: - return searchResults.contacts.count + return searchResultSet.contacts.count case .messages: - return searchResults.messages.count + return searchResultSet.messages.count } } class ChatSearchResultCell: UITableViewCell { static let reuseIdentifier = "ChatSearchResultCell" - func configure(searchResult: ConversationSearchItem) { + func configure(searchResult: SearchResult) { self.textLabel!.text = searchResult.thread.name } } @@ -72,7 +72,7 @@ class ConversationSearchViewController: UITableViewController { return UITableViewCell() } - guard let searchResult = self.searchResults.conversations[safe: indexPath.row] else { + guard let searchResult = self.searchResultSet.conversations[safe: indexPath.row] else { return UITableViewCell() } cell.configure(searchResult: searchResult) @@ -98,19 +98,19 @@ class ConversationSearchViewController: UITableViewController { switch searchSection { case .conversations: - if searchResults.conversations.count > 0 { + if searchResultSet.conversations.count > 0 { return NSLocalizedString("SEARCH_SECTION_CONVERSATIONS", comment: "section header for search results that match existing conversations (either group or contact conversations)") } else { return nil } case .contacts: - if searchResults.contacts.count > 0 { + if searchResultSet.contacts.count > 0 { return NSLocalizedString("SEARCH_SECTION_CONTACTS", comment: "section header for search results that match a contact who doesn't have an existing conversation") } else { return nil } case .messages: - if searchResults.messages.count > 0 { + if searchResultSet.messages.count > 0 { return NSLocalizedString("SEARCH_SECTION_MESSAGES", comment: "section header for search results that match a message in a conversation") } else { return nil @@ -179,7 +179,7 @@ extension ConversationSearchViewController: UISearchBarDelegate { public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { guard searchText.stripped.count > 0 else { - self.searchResults = ConversationSearchResults.empty + self.searchResultSet = SearchResultSet.empty self.view.isHidden = true return } @@ -187,7 +187,7 @@ extension ConversationSearchViewController: UISearchBarDelegate { self.view.isHidden = false self.uiDatabaseConnection.read { transaction in - self.searchResults = self.searcher.results(searchText: searchText, transaction: transaction) + self.searchResultSet = self.searcher.results(searchText: searchText, transaction: transaction) } // TODO: more perfomant way to do... self.tableView.reloadData() diff --git a/Signal/test/util/SearcherTest.swift b/Signal/test/util/SearcherTest.swift index cdd9d8fe8..907cb79a9 100644 --- a/Signal/test/util/SearcherTest.swift +++ b/Signal/test/util/SearcherTest.swift @@ -22,7 +22,7 @@ class ConversationSearcherTest: XCTestCase { override func setUp() { super.setUp() - ConversationFullTextSearchFinder.syncRegisterDatabaseExtension(storage: OWSPrimaryStorage.shared()) + FullTextSearchFinder.syncRegisterDatabaseExtension(storage: OWSPrimaryStorage.shared()) } // Mark: Tests @@ -44,11 +44,11 @@ class ConversationSearcherTest: XCTestCase { } // No Match - let noMatch = results(searchText: "asdasdasd") + let noMatch = resultSet(searchText: "asdasdasd") XCTAssert(noMatch.conversations.isEmpty) // Partial Match - let bookMatch = results(searchText: "Book") + let bookMatch = resultSet(searchText: "Book") XCTAssert(bookMatch.conversations.count == 1) if let foundThread: ThreadViewModel = bookMatch.conversations.first?.thread { XCTAssertEqual(bookClubThread, foundThread) @@ -56,7 +56,7 @@ class ConversationSearcherTest: XCTestCase { XCTFail("no thread found") } - let snackMatch = results(searchText: "Snack") + let snackMatch = resultSet(searchText: "Snack") XCTAssert(snackMatch.conversations.count == 1) if let foundThread: ThreadViewModel = snackMatch.conversations.first?.thread { XCTAssertEqual(snackClubThread, foundThread) @@ -65,21 +65,21 @@ class ConversationSearcherTest: XCTestCase { } // Multiple Partial Matches - let multipleMatch = results(searchText: "Club") + let multipleMatch = resultSet(searchText: "Club") XCTAssert(multipleMatch.conversations.count == 2) XCTAssert(multipleMatch.conversations.map { $0.thread }.contains(bookClubThread)) XCTAssert(multipleMatch.conversations.map { $0.thread }.contains(snackClubThread)) // Match Name Exactly - let exactMatch = results(searchText: "Book Club") + let exactMatch = resultSet(searchText: "Book Club") XCTAssert(exactMatch.conversations.count == 1) XCTAssertEqual(bookClubThread, exactMatch.conversations.first!.thread) } // Mark: Helpers - private func results(searchText: String) -> ConversationSearchResults { - var results: ConversationSearchResults! + private func resultSet(searchText: String) -> SearchResultSet { + var results: SearchResultSet! self.dbConnection.read { transaction in results = self.searcher.results(searchText: searchText, transaction: transaction) } diff --git a/SignalMessaging/utils/ConversationSearcher.swift b/SignalMessaging/utils/ConversationSearcher.swift index f7ade633f..6bdc5a1b6 100644 --- a/SignalMessaging/utils/ConversationSearcher.swift +++ b/SignalMessaging/utils/ConversationSearcher.swift @@ -6,7 +6,7 @@ import Foundation import SignalServiceKit @objc -public class ConversationSearchItem: NSObject { +public class SearchResult: NSObject { @objc public let thread: ThreadViewModel @@ -15,19 +15,19 @@ public class ConversationSearchItem: NSObject { } } -public class ConversationSearchResults { - public let conversations: [ConversationSearchItem] - public let contacts: [ConversationSearchItem] - public let messages: [ConversationSearchItem] +public class SearchResultSet { + public let conversations: [SearchResult] + public let contacts: [SearchResult] + public let messages: [SearchResult] - public init(conversations: [ConversationSearchItem], contacts: [ConversationSearchItem], messages: [ConversationSearchItem]) { + public init(conversations: [SearchResult], contacts: [SearchResult], messages: [SearchResult]) { self.conversations = conversations self.contacts = contacts self.messages = messages } - public class var empty: ConversationSearchResults { - return ConversationSearchResults(conversations: [], contacts: [], messages: []) + public class var empty: SearchResultSet { + return SearchResultSet(conversations: [], contacts: [], messages: []) } } @@ -43,25 +43,25 @@ public class ConversationSearcher: NSObject { super.init() } - public func results(searchText: String, transaction: YapDatabaseReadTransaction) -> ConversationSearchResults { + public func results(searchText: String, transaction: YapDatabaseReadTransaction) -> SearchResultSet { // TODO limit results, prioritize conversations, then contacts, then messages. - var conversations: [ConversationSearchItem] = [] - var contacts: [ConversationSearchItem] = [] - var messages: [ConversationSearchItem] = [] + var conversations: [SearchResult] = [] + var contacts: [SearchResult] = [] + var messages: [SearchResult] = [] self.finder.enumerateObjects(searchText: searchText, transaction: transaction) { (match: Any) in if let thread = match as? TSThread { let threadViewModel = ThreadViewModel(thread: thread, transaction: transaction) - let searchItem = ConversationSearchItem(thread: threadViewModel) + let searchResult = SearchResult(thread: threadViewModel) - conversations.append(searchItem) + conversations.append(searchResult) } else { Logger.debug("\(self.logTag) in \(#function) unhandled item: \(match)") } } - return ConversationSearchResults(conversations: conversations, contacts: contacts, messages: messages) + return SearchResultSet(conversations: conversations, contacts: contacts, messages: messages) } @objc(filterThreads:withSearchText:)