From e0acb0f04aa67bc4457a07af12c89d35268027a9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 16 Oct 2020 19:14:29 -0400 Subject: [PATCH] Don't search for unnecessary data --- .../Explore/AddSavedHashtagViewController.swift | 9 ++++++++- .../Lists/EditListAccountsViewController.swift | 3 +-- .../Search/SearchResultsViewController.swift | 16 +++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Tusker/Screens/Explore/AddSavedHashtagViewController.swift b/Tusker/Screens/Explore/AddSavedHashtagViewController.swift index 65585f6728..be1b031eff 100644 --- a/Tusker/Screens/Explore/AddSavedHashtagViewController.swift +++ b/Tusker/Screens/Explore/AddSavedHashtagViewController.swift @@ -13,11 +13,18 @@ class AddSavedHashtagViewController: SearchResultsViewController { var searchController: UISearchController! + init(mastodonController: MastodonController) { + super.init(mastodonController: mastodonController, resultTypes: [.hashtags]) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + override func viewDidLoad() { super.viewDidLoad() delegate = self - onlySections = [.hashtags] searchController = UISearchController(searchResultsController: nil) searchController.obscuresBackgroundDuringPresentation = false diff --git a/Tusker/Screens/Lists/EditListAccountsViewController.swift b/Tusker/Screens/Lists/EditListAccountsViewController.swift index 6857aee78a..aa411892e1 100644 --- a/Tusker/Screens/Lists/EditListAccountsViewController.swift +++ b/Tusker/Screens/Lists/EditListAccountsViewController.swift @@ -52,9 +52,8 @@ class EditListAccountsViewController: EnhancedTableViewController { }) dataSource.editListAccountsController = self - searchResultsController = SearchResultsViewController(mastodonController: mastodonController) + searchResultsController = SearchResultsViewController(mastodonController: mastodonController, resultTypes: [.accounts]) searchResultsController.delegate = self - searchResultsController.onlySections = [.accounts] searchController = UISearchController(searchResultsController: searchResultsController) searchController.hidesNavigationBarDuringPresentation = false searchController.searchResultsUpdater = searchResultsController diff --git a/Tusker/Screens/Search/SearchResultsViewController.swift b/Tusker/Screens/Search/SearchResultsViewController.swift index 6919ad88d6..8d3ba36f62 100644 --- a/Tusker/Screens/Search/SearchResultsViewController.swift +++ b/Tusker/Screens/Search/SearchResultsViewController.swift @@ -35,15 +35,17 @@ class SearchResultsViewController: EnhancedTableViewController { var dataSource: UITableViewDiffableDataSource! - var activityIndicator: UIActivityIndicatorView! + private var activityIndicator: UIActivityIndicatorView! - var onlySections: [Section] = Section.allCases + /// Types of results to search for. `nil` means all results will be included. + var resultTypes: [SearchResultType]? = nil let searchSubject = PassthroughSubject() var currentQuery: String? - init(mastodonController: MastodonController) { + init(mastodonController: MastodonController, resultTypes: [SearchResultType]? = nil) { self.mastodonController = mastodonController + self.resultTypes = resultTypes super.init(style: .grouped) @@ -128,7 +130,7 @@ class SearchResultsViewController: EnhancedTableViewController { activityIndicator.isHidden = false activityIndicator.startAnimating() - let request = Client.search(query: query, resolve: true, limit: 10) + let request = Client.search(query: query, types: resultTypes, resolve: true, limit: 10) mastodonController.run(request) { (response) in guard case let .success(results, _) = response else { fatalError() } @@ -157,16 +159,16 @@ class SearchResultsViewController: EnhancedTableViewController { } } - if self.onlySections.contains(.accounts) && !results.accounts.isEmpty { + if !results.accounts.isEmpty { snapshot.appendSections([.accounts]) snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts) addAccounts(results.accounts) } - if self.onlySections.contains(.hashtags) && !results.hashtags.isEmpty { + if !results.hashtags.isEmpty { snapshot.appendSections([.hashtags]) snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags) } - if self.onlySections.contains(.statuses) && !results.statuses.isEmpty { + if !results.statuses.isEmpty { snapshot.appendSections([.statuses]) snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses) addStatuses(results.statuses)