Tusker/Tusker/Screens/Search/SearchResultsViewController...

225 lines
8.7 KiB
Swift
Raw Normal View History

2019-09-15 00:47:08 +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
2019-12-18 03:56:53 +00:00
protocol SearchResultsViewControllerDelegate: class {
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) {}
}
class SearchResultsViewController: EnhancedTableViewController {
2019-09-15 00:47:08 +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
var dataSource: UITableViewDiffableDataSource<Section, Item>!
2019-09-15 00:47:08 +00:00
var activityIndicator: UIActivityIndicatorView!
2019-12-18 03:56:53 +00:00
var onlySections: [Section] = Section.allCases
2019-09-15 00:47:08 +00:00
let searchSubject = PassthroughSubject<String?, Never>()
var currentQuery: String?
init() {
super.init(style: .grouped)
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()
tableView.register(UINib(nibName: "AccountTableViewCell", bundle: .main), forCellReuseIdentifier: accountCell)
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
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.updateUI(accountID: id)
cell.delegate = self
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.updateUI(hashtag: tag)
cell.delegate = self
return cell
case let .status(id, state):
let cell = tableView.dequeueReusableCell(withIdentifier: statusCell, for: indexPath) as! TimelineStatusTableViewCell
cell.updateUI(statusID: id, state: state)
2019-09-15 00:47:08 +00:00
cell.delegate = self
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-09-16 01:20:50 +00:00
userActivity = UserActivityManager.searchActivity()
2019-09-15 00:47:08 +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)
}
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-09-15 00:47:08 +00:00
if self.dataSource.snapshot().numberOfItems == 0 {
activityIndicator.isHidden = false
activityIndicator.startAnimating()
}
2019-09-15 00:47:08 +00:00
let request = MastodonController.client.search(query: query, resolve: true, limit: 10)
MastodonController.client.run(request) { (response) in
guard case let .success(results, _) = response else { fatalError() }
2019-09-15 00:47:08 +00:00
DispatchQueue.main.async {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
}
2019-09-15 00:47:08 +00:00
guard self.currentQuery == query else { return }
2019-09-15 00:47:08 +00:00
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
2019-12-18 03:56:53 +00:00
if self.onlySections.contains(.accounts) && !results.accounts.isEmpty {
2019-09-15 00:47:08 +00:00
snapshot.appendSections([.accounts])
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
2019-09-15 00:47:08 +00:00
MastodonCache.addAll(accounts: results.accounts)
}
2019-12-18 03:56:53 +00:00
if self.onlySections.contains(.hashtags) && !results.hashtags.isEmpty {
2019-09-15 01:24:43 +00:00
snapshot.appendSections([.hashtags])
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
2019-09-15 01:24:43 +00:00
}
2019-12-18 03:56:53 +00:00
if self.onlySections.contains(.statuses) && !results.statuses.isEmpty {
2019-09-15 00:47:08 +00:00
snapshot.appendSections([.statuses])
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
2019-09-15 00:47:08 +00:00
MastodonCache.addAll(statuses: results.statuses)
MastodonCache.addAll(accounts: results.statuses.map { $0.account })
}
self.dataSource.apply(snapshot)
}
}
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
}
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)
case status(String, StatusState)
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
}
}
}
extension SearchResultsViewController: UISearchResultsUpdating {
2019-09-15 00:47:08 +00:00
func updateSearchResults(for searchController: UISearchController) {
searchSubject.send(searchController.searchBar.text)
}
}
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))
}
}
extension SearchResultsViewController: StatusTableViewCellDelegate {
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
2019-09-15 00:47:08 +00:00
tableView.beginUpdates()
tableView.endUpdates()
}
}