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!
searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController
if #available(iOS 16.0, *) {
searchController.scopeBarActivation = .onSearchActivation
}
searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.delegate = resultsController
searchController.searchBar.scopeButtonTitles = SearchResultsViewController.Scope.allCases.map(\.title)
definesPresentationContext = true
navigationItem.searchController = searchController

View File

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

View File

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

View File

@ -38,17 +38,17 @@ class SearchResultsViewController: EnhancedTableViewController {
private var activityIndicator: UIActivityIndicatorView!
private var errorLabel: UILabel!
/// Types of results to search for. `nil` means all results will be included.
var resultTypes: [SearchResultType]? = nil
/// Types of results to search for.
var scope: Scope
/// Whether to limit results to accounts the users is following.
var following: Bool? = nil
let searchSubject = PassthroughSubject<String?, Never>()
var currentQuery: String?
init(mastodonController: MastodonController, resultTypes: [SearchResultType]? = nil) {
init(mastodonController: MastodonController, scope: Scope = .all) {
self.mastodonController = mastodonController
self.resultTypes = resultTypes
self.scope = scope
super.init(style: .grouped)
@ -153,7 +153,7 @@ class SearchResultsViewController: EnhancedTableViewController {
activityIndicator.startAnimating()
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
switch response {
case let .success(results, _):
@ -178,17 +178,17 @@ class SearchResultsViewController: EnhancedTableViewController {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
let resultTypes = self.resultTypes
if !results.accounts.isEmpty && (resultTypes == nil || resultTypes!.contains(.accounts)) {
let resultTypes = self.scope.resultTypes
if !results.accounts.isEmpty && resultTypes.contains(.accounts) {
snapshot.appendSections([.accounts])
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
addAccounts(results.accounts)
}
if !results.hashtags.isEmpty && (resultTypes == nil || resultTypes!.contains(.hashtags)) {
if !results.hashtags.isEmpty && resultTypes.contains(.hashtags) {
snapshot.appendSections([.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.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .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 {
enum Section: CaseIterable {
case accounts
@ -311,6 +346,11 @@ extension SearchResultsViewController: UISearchBarDelegate {
// perform a search immedaitely when the search button is pressed
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 {

View File

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