Add search scopes

Closes #328
This commit is contained in:
Shadowfacts 2023-01-22 11:41:38 -05:00
parent 155f4036f9
commit 0738683ee3
5 changed files with 59 additions and 11 deletions

View File

@ -64,8 +64,12 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
resultsController.exploreNavigationController = self.navigationController! resultsController.exploreNavigationController = self.navigationController!
searchController = UISearchController(searchResultsController: resultsController) searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController searchController.searchResultsUpdater = resultsController
if #available(iOS 16.0, *) {
searchController.scopeBarActivation = .onSearchActivation
}
searchController.searchBar.autocapitalizationType = .none searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.delegate = resultsController searchController.searchBar.delegate = resultsController
searchController.searchBar.scopeButtonTitles = SearchResultsViewController.Scope.allCases.map(\.title)
definesPresentationContext = true definesPresentationContext = true
navigationItem.searchController = searchController navigationItem.searchController = searchController

View File

@ -11,7 +11,7 @@ import UIKit
class HashtagSearchResultsViewController: SearchResultsViewController { class HashtagSearchResultsViewController: SearchResultsViewController {
init(mastodonController: MastodonController) { init(mastodonController: MastodonController) {
super.init(mastodonController: mastodonController, resultTypes: [.hashtags]) super.init(mastodonController: mastodonController, scope: .hashtags)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {

View File

@ -65,7 +65,7 @@ class EditListAccountsViewController: EnhancedTableViewController {
}) })
dataSource.editListAccountsController = self dataSource.editListAccountsController = self
searchResultsController = SearchResultsViewController(mastodonController: mastodonController, resultTypes: [.accounts]) searchResultsController = SearchResultsViewController(mastodonController: mastodonController, scope: .people)
searchResultsController.following = true searchResultsController.following = true
searchResultsController.delegate = self searchResultsController.delegate = self
searchController = UISearchController(searchResultsController: searchResultsController) searchController = UISearchController(searchResultsController: searchResultsController)

View File

@ -38,17 +38,17 @@ class SearchResultsViewController: EnhancedTableViewController {
private var activityIndicator: UIActivityIndicatorView! private var activityIndicator: UIActivityIndicatorView!
private var errorLabel: UILabel! private var errorLabel: UILabel!
/// Types of results to search for. `nil` means all results will be included. /// Types of results to search for.
var resultTypes: [SearchResultType]? = nil var scope: Scope
/// Whether to limit results to accounts the users is following. /// Whether to limit results to accounts the users is following.
var following: Bool? = nil var following: Bool? = nil
let searchSubject = PassthroughSubject<String?, Never>() let searchSubject = PassthroughSubject<String?, Never>()
var currentQuery: String? var currentQuery: String?
init(mastodonController: MastodonController, resultTypes: [SearchResultType]? = nil) { init(mastodonController: MastodonController, scope: Scope = .all) {
self.mastodonController = mastodonController self.mastodonController = mastodonController
self.resultTypes = resultTypes self.scope = scope
super.init(style: .grouped) super.init(style: .grouped)
@ -153,7 +153,7 @@ class SearchResultsViewController: EnhancedTableViewController {
activityIndicator.startAnimating() activityIndicator.startAnimating()
errorLabel.isHidden = true errorLabel.isHidden = true
let request = Client.search(query: query, types: resultTypes, resolve: true, limit: 10, following: following) let request = Client.search(query: query, types: scope.resultTypes, resolve: true, limit: 10, following: following)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
switch response { switch response {
case let .success(results, _): case let .success(results, _):
@ -178,17 +178,17 @@ class SearchResultsViewController: EnhancedTableViewController {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
let resultTypes = self.resultTypes let resultTypes = self.scope.resultTypes
if !results.accounts.isEmpty && (resultTypes == nil || resultTypes!.contains(.accounts)) { if !results.accounts.isEmpty && resultTypes.contains(.accounts) {
snapshot.appendSections([.accounts]) snapshot.appendSections([.accounts])
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts) snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
addAccounts(results.accounts) addAccounts(results.accounts)
} }
if !results.hashtags.isEmpty && (resultTypes == nil || resultTypes!.contains(.hashtags)) { if !results.hashtags.isEmpty && resultTypes.contains(.hashtags) {
snapshot.appendSections([.hashtags]) snapshot.appendSections([.hashtags])
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags) snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
} }
if !results.statuses.isEmpty && (resultTypes == nil || resultTypes!.contains(.statuses)) { if !results.statuses.isEmpty && resultTypes.contains(.statuses) {
snapshot.appendSections([.statuses]) snapshot.appendSections([.statuses])
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses) snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
addStatuses(results.statuses) addStatuses(results.statuses)
@ -252,6 +252,41 @@ class SearchResultsViewController: EnhancedTableViewController {
} }
extension SearchResultsViewController {
enum Scope: CaseIterable {
case all
case people
case hashtags
case posts
var title: String {
switch self {
case .all:
return "All"
case .people:
return "People"
case .hashtags:
return "Hashtags"
case .posts:
return "Posts"
}
}
var resultTypes: [SearchResultType] {
switch self {
case .all:
return [.accounts, .statuses, .hashtags]
case .people:
return [.accounts]
case .hashtags:
return [.hashtags]
case .posts:
return [.statuses]
}
}
}
}
extension SearchResultsViewController { extension SearchResultsViewController {
enum Section: CaseIterable { enum Section: CaseIterable {
case accounts case accounts
@ -311,6 +346,11 @@ extension SearchResultsViewController: UISearchBarDelegate {
// perform a search immedaitely when the search button is pressed // perform a search immedaitely when the search button is pressed
performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines)) performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines))
} }
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
self.scope = Scope.allCases[selectedScope]
performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines))
}
} }
extension SearchResultsViewController: TuskerNavigationDelegate { extension SearchResultsViewController: TuskerNavigationDelegate {

View File

@ -79,8 +79,12 @@ class SearchViewController: UIViewController {
resultsController.exploreNavigationController = self.navigationController resultsController.exploreNavigationController = self.navigationController
searchController = UISearchController(searchResultsController: resultsController) searchController = UISearchController(searchResultsController: resultsController)
searchController.obscuresBackgroundDuringPresentation = true searchController.obscuresBackgroundDuringPresentation = true
if #available(iOS 16.0, *) {
searchController.scopeBarActivation = .onSearchActivation
}
searchController.searchBar.autocapitalizationType = .none searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.delegate = resultsController searchController.searchBar.delegate = resultsController
searchController.searchBar.scopeButtonTitles = SearchResultsViewController.Scope.allCases.map(\.title)
searchController.hidesNavigationBarDuringPresentation = false searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true definesPresentationContext = true