2019-09-15 00:47:08 +00:00
|
|
|
//
|
2019-12-17 05:22:25 +00:00
|
|
|
// SearchResultsViewController.swift
|
2019-09-15 00:47:08 +00:00
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/14/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
fileprivate let accountCell = "accountCell"
|
|
|
|
fileprivate let statusCell = "statusCell"
|
2019-09-15 01:24:43 +00:00
|
|
|
fileprivate let hashtagCell = "hashtagCell"
|
2019-09-15 00:47:08 +00:00
|
|
|
|
2021-05-22 17:42:53 +00:00
|
|
|
protocol SearchResultsViewControllerDelegate: AnyObject {
|
2019-12-18 03:56:53 +00:00
|
|
|
func selectedSearchResult(account accountID: String)
|
|
|
|
func selectedSearchResult(hashtag: Hashtag)
|
|
|
|
func selectedSearchResult(status statusID: String)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchResultsViewControllerDelegate {
|
|
|
|
func selectedSearchResult(account accountID: String) {}
|
|
|
|
func selectedSearchResult(hashtag: Hashtag) {}
|
|
|
|
func selectedSearchResult(status statusID: String) {}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
class SearchResultsViewController: EnhancedTableViewController {
|
2019-09-15 00:47:08 +00:00
|
|
|
|
2020-06-24 20:40:45 +00:00
|
|
|
weak var mastodonController: MastodonController!
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
weak var exploreNavigationController: UINavigationController?
|
2019-12-18 03:56:53 +00:00
|
|
|
weak var delegate: SearchResultsViewControllerDelegate?
|
2019-09-15 00:47:08 +00:00
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
var dataSource: UITableViewDiffableDataSource<Section, Item>!
|
|
|
|
|
2020-10-16 23:14:29 +00:00
|
|
|
private var activityIndicator: UIActivityIndicatorView!
|
2021-05-22 15:22:01 +00:00
|
|
|
private var errorLabel: UILabel!
|
2019-09-15 00:47:08 +00:00
|
|
|
|
2023-01-22 16:41:38 +00:00
|
|
|
/// Types of results to search for.
|
|
|
|
var scope: Scope
|
2022-11-28 02:44:17 +00:00
|
|
|
/// Whether to limit results to accounts the users is following.
|
|
|
|
var following: Bool? = nil
|
2019-12-18 03:56:53 +00:00
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
let searchSubject = PassthroughSubject<String?, Never>()
|
|
|
|
var currentQuery: String?
|
|
|
|
|
2023-01-22 16:41:38 +00:00
|
|
|
init(mastodonController: MastodonController, scope: Scope = .all) {
|
2020-01-05 20:25:07 +00:00
|
|
|
self.mastodonController = mastodonController
|
2023-01-22 16:41:38 +00:00
|
|
|
self.scope = scope
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
super.init(style: .grouped)
|
|
|
|
|
2020-12-14 23:44:41 +00:00
|
|
|
dragEnabled = true
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
title = NSLocalizedString("Search", comment: "search screen title")
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-05-22 15:22:01 +00:00
|
|
|
|
|
|
|
errorLabel = UILabel()
|
|
|
|
errorLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
errorLabel.font = .preferredFont(forTextStyle: .callout)
|
|
|
|
errorLabel.textColor = .secondaryLabel
|
|
|
|
errorLabel.numberOfLines = 0
|
|
|
|
errorLabel.textAlignment = .center
|
|
|
|
errorLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
|
|
|
tableView.addSubview(errorLabel)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
errorLabel.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor),
|
|
|
|
errorLabel.centerXAnchor.constraint(equalTo: tableView.centerXAnchor),
|
|
|
|
errorLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: tableView.leadingAnchor, multiplier: 1),
|
|
|
|
tableView.trailingAnchor.constraint(equalToSystemSpacingAfter: errorLabel.trailingAnchor, multiplier: 1),
|
|
|
|
])
|
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
tableView.register(UINib(nibName: "AccountTableViewCell", bundle: .main), forCellReuseIdentifier: accountCell)
|
2019-11-19 17:08:11 +00:00
|
|
|
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell)
|
2019-09-15 01:24:43 +00:00
|
|
|
tableView.register(UINib(nibName: "HashtagTableViewCell", bundle: .main), forCellReuseIdentifier: hashtagCell)
|
2019-09-15 00:47:08 +00:00
|
|
|
|
2023-01-16 22:47:56 +00:00
|
|
|
tableView.allowsFocus = true
|
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
|
|
|
|
switch item {
|
|
|
|
case let .account(id):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: accountCell, for: indexPath) as! AccountTableViewCell
|
|
|
|
cell.delegate = self
|
2020-01-06 00:54:28 +00:00
|
|
|
cell.updateUI(accountID: id)
|
2019-09-15 00:47:08 +00:00
|
|
|
return cell
|
2019-09-15 01:24:43 +00:00
|
|
|
case let .hashtag(tag):
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: hashtagCell, for: indexPath) as! HashtagTableViewCell
|
|
|
|
cell.delegate = self
|
2020-01-06 00:54:28 +00:00
|
|
|
cell.updateUI(hashtag: tag)
|
2019-09-15 01:24:43 +00:00
|
|
|
return cell
|
2019-11-28 23:36:58 +00:00
|
|
|
case let .status(id, state):
|
2019-11-19 17:08:11 +00:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: statusCell, for: indexPath) as! TimelineStatusTableViewCell
|
2019-09-15 00:47:08 +00:00
|
|
|
cell.delegate = self
|
2020-01-06 00:54:28 +00:00
|
|
|
cell.updateUI(statusID: id, state: state)
|
2019-09-15 00:47:08 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
activityIndicator = UIActivityIndicatorView(style: .large)
|
|
|
|
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
activityIndicator.isHidden = true
|
|
|
|
view.addSubview(activityIndicator)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
|
|
|
activityIndicator.topAnchor.constraint(equalTo: view.topAnchor, constant: 8)
|
|
|
|
])
|
|
|
|
|
|
|
|
_ = searchSubject
|
|
|
|
.debounce(for: .milliseconds(500), scheduler: RunLoop.main)
|
|
|
|
.map { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
|
|
|
|
.filter { $0 != self.currentQuery }
|
|
|
|
.sink(receiveValue: performSearch(query:))
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2019-09-16 01:20:50 +00:00
|
|
|
userActivity = UserActivityManager.searchActivity()
|
2023-01-18 00:32:50 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil)
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
override func targetViewController(forAction action: Selector, sender: Any?) -> UIViewController? {
|
|
|
|
// if we're showing a view controller, we need to go up to the explore VC's nav controller
|
|
|
|
// the UISearchController that is our parent is not part of the normal VC hierarchy and itself doesn't have a parent
|
|
|
|
if action == #selector(UIViewController.show(_:sender:)),
|
|
|
|
let exploreNavController = exploreNavigationController {
|
|
|
|
return exploreNavController
|
|
|
|
}
|
|
|
|
return super.targetViewController(forAction: action, sender: sender)
|
|
|
|
}
|
|
|
|
|
2020-06-30 02:21:03 +00:00
|
|
|
func loadResults(from source: SearchResultsViewController) {
|
|
|
|
currentQuery = source.currentQuery
|
|
|
|
if let sourceDataSource = source.dataSource {
|
|
|
|
dataSource.apply(sourceDataSource.snapshot())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
func performSearch(query: String?) {
|
|
|
|
guard let query = query, !query.isEmpty else {
|
|
|
|
self.dataSource.apply(NSDiffableDataSourceSnapshot<Section, Item>())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.currentQuery = query
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2020-05-10 19:47:50 +00:00
|
|
|
activityIndicator.isHidden = false
|
|
|
|
activityIndicator.startAnimating()
|
2021-05-22 15:22:01 +00:00
|
|
|
errorLabel.isHidden = true
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2023-01-22 16:41:38 +00:00
|
|
|
let request = Client.search(query: query, types: scope.resultTypes, resolve: true, limit: 10, following: following)
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2021-05-22 15:22:01 +00:00
|
|
|
switch response {
|
|
|
|
case let .success(results, _):
|
|
|
|
guard self.currentQuery == query else { return }
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.activityIndicator.isHidden = true
|
|
|
|
self.activityIndicator.stopAnimating()
|
2020-05-10 19:47:50 +00:00
|
|
|
}
|
2021-05-22 15:22:01 +00:00
|
|
|
self.showSearchResults(results)
|
|
|
|
case let .failure(error):
|
2020-05-10 19:47:50 +00:00
|
|
|
DispatchQueue.main.async {
|
2021-05-22 15:22:01 +00:00
|
|
|
self.activityIndicator.isHidden = true
|
|
|
|
self.activityIndicator.stopAnimating()
|
|
|
|
|
|
|
|
self.showSearchError(error)
|
2020-05-10 19:47:50 +00:00
|
|
|
}
|
2021-05-22 15:22:01 +00:00
|
|
|
}
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-18 03:56:53 +00:00
|
|
|
|
2021-05-22 15:22:01 +00:00
|
|
|
private func showSearchResults(_ results: SearchResults) {
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
|
|
|
|
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
|
2023-01-22 16:41:38 +00:00
|
|
|
let resultTypes = self.scope.resultTypes
|
|
|
|
if !results.accounts.isEmpty && resultTypes.contains(.accounts) {
|
2021-05-22 15:22:01 +00:00
|
|
|
snapshot.appendSections([.accounts])
|
|
|
|
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
|
|
|
|
addAccounts(results.accounts)
|
|
|
|
}
|
2023-01-22 16:41:38 +00:00
|
|
|
if !results.hashtags.isEmpty && resultTypes.contains(.hashtags) {
|
2021-05-22 15:22:01 +00:00
|
|
|
snapshot.appendSections([.hashtags])
|
|
|
|
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
|
|
|
|
}
|
2023-01-22 16:41:38 +00:00
|
|
|
if !results.statuses.isEmpty && resultTypes.contains(.statuses) {
|
2021-05-22 15:22:01 +00:00
|
|
|
snapshot.appendSections([.statuses])
|
|
|
|
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
|
|
|
|
addStatuses(results.statuses)
|
|
|
|
}
|
|
|
|
}, completion: {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.errorLabel.isHidden = true
|
|
|
|
self.dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func showSearchError(_ error: Client.Error) {
|
|
|
|
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
dataSource.apply(snapshot)
|
|
|
|
|
|
|
|
errorLabel.isHidden = false
|
|
|
|
errorLabel.text = error.localizedDescription
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:32:50 +00:00
|
|
|
@objc private func handleStatusDeleted(_ notification: Foundation.Notification) {
|
|
|
|
guard let userInfo = notification.userInfo,
|
|
|
|
let accountID = mastodonController.accountInfo?.id,
|
|
|
|
userInfo["accountID"] as? String == accountID,
|
|
|
|
let statusIDs = userInfo["statusIDs"] as? [String] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var snapshot = self.dataSource.snapshot()
|
|
|
|
let toDelete = statusIDs
|
|
|
|
.map { id in
|
|
|
|
Item.status(id, .unknown)
|
|
|
|
}
|
|
|
|
.filter { item in
|
|
|
|
snapshot.itemIdentifiers.contains(item)
|
|
|
|
}
|
|
|
|
if !toDelete.isEmpty {
|
|
|
|
snapshot.deleteItems(toDelete)
|
|
|
|
self.dataSource.apply(snapshot, animatingDifferences: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-18 03:56:53 +00:00
|
|
|
// MARK: - Table view delegate
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
if let delegate = delegate {
|
|
|
|
switch dataSource.itemIdentifier(for: indexPath) {
|
|
|
|
case nil:
|
|
|
|
return
|
|
|
|
case let .account(id):
|
|
|
|
delegate.selectedSearchResult(account: id)
|
|
|
|
case let .hashtag(hashtag):
|
|
|
|
delegate.selectedSearchResult(hashtag: hashtag)
|
|
|
|
case let .status(id, _):
|
|
|
|
delegate.selectedSearchResult(status: id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
super.tableView(tableView, didSelectRowAt: indexPath)
|
|
|
|
}
|
|
|
|
}
|
2019-09-15 00:47:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-22 16:41:38 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
extension SearchResultsViewController {
|
2019-09-15 00:47:08 +00:00
|
|
|
enum Section: CaseIterable {
|
|
|
|
case accounts
|
2019-09-15 01:24:43 +00:00
|
|
|
case hashtags
|
2019-09-15 00:47:08 +00:00
|
|
|
case statuses
|
2019-09-15 01:24:43 +00:00
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
var displayName: String {
|
|
|
|
switch self {
|
|
|
|
case .accounts:
|
|
|
|
return NSLocalizedString("People", comment: "accounts search results section")
|
2019-09-15 01:24:43 +00:00
|
|
|
case .hashtags:
|
|
|
|
return NSLocalizedString("Hashtags", comment: "hashtag search results section")
|
2019-09-15 00:47:08 +00:00
|
|
|
case .statuses:
|
|
|
|
return NSLocalizedString("Posts", comment: "statuses search results section")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enum Item: Hashable {
|
|
|
|
case account(String)
|
2019-09-15 01:24:43 +00:00
|
|
|
case hashtag(Hashtag)
|
2022-12-03 23:21:49 +00:00
|
|
|
case status(String, CollapseState)
|
2020-06-30 02:21:03 +00:00
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
switch self {
|
|
|
|
case let .account(id):
|
|
|
|
hasher.combine("account")
|
|
|
|
hasher.combine(id)
|
|
|
|
case let .hashtag(hashtag):
|
|
|
|
hasher.combine("hashtag")
|
|
|
|
hasher.combine(hashtag.url)
|
|
|
|
case let .status(id, _):
|
|
|
|
hasher.combine("status")
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
|
|
|
}
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataSource: UITableViewDiffableDataSource<Section, Item> {
|
2019-12-17 03:23:12 +00:00
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection sectionIndex: Int) -> String? {
|
|
|
|
let currentSnapshot = snapshot()
|
|
|
|
for section in Section.allCases where currentSnapshot.indexOfSection(section) == sectionIndex {
|
|
|
|
return section.displayName
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
extension SearchResultsViewController: UISearchResultsUpdating {
|
2019-09-15 00:47:08 +00:00
|
|
|
func updateSearchResults(for searchController: UISearchController) {
|
|
|
|
searchSubject.send(searchController.searchBar.text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
extension SearchResultsViewController: UISearchBarDelegate {
|
2019-09-15 00:47:08 +00:00
|
|
|
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
|
|
|
|
// perform a search immedaitely when the search button is pressed
|
|
|
|
performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines))
|
|
|
|
}
|
2023-01-22 16:41:38 +00:00
|
|
|
|
|
|
|
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
|
|
|
|
self.scope = Scope.allCases[selectedScope]
|
|
|
|
performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines))
|
|
|
|
}
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 03:04:56 +00:00
|
|
|
extension SearchResultsViewController: TuskerNavigationDelegate {
|
2022-10-31 20:27:13 +00:00
|
|
|
var apiController: MastodonController! { mastodonController }
|
2022-05-02 03:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchResultsViewController: ToastableViewController {
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchResultsViewController: MenuActionProvider {
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
extension SearchResultsViewController: StatusTableViewCellDelegate {
|
2019-11-28 23:36:58 +00:00
|
|
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
|
2019-09-15 00:47:08 +00:00
|
|
|
tableView.beginUpdates()
|
|
|
|
tableView.endUpdates()
|
|
|
|
}
|
|
|
|
}
|