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

271 lines
11 KiB
Swift

//
// SearchResultsViewController.swift
// 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"
fileprivate let hashtagCell = "hashtagCell"
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 {
weak var mastodonController: MastodonController!
weak var exploreNavigationController: UINavigationController?
weak var delegate: SearchResultsViewControllerDelegate?
var dataSource: UITableViewDiffableDataSource<Section, Item>!
var activityIndicator: UIActivityIndicatorView!
var onlySections: [Section] = Section.allCases
let searchSubject = PassthroughSubject<String?, Never>()
var currentQuery: String?
init(mastodonController: MastodonController) {
self.mastodonController = mastodonController
super.init(style: .grouped)
title = NSLocalizedString("Search", comment: "search screen title")
}
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)
tableView.register(UINib(nibName: "HashtagTableViewCell", bundle: .main), forCellReuseIdentifier: hashtagCell)
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
cell.updateUI(accountID: id)
return cell
case let .hashtag(tag):
let cell = tableView.dequeueReusableCell(withIdentifier: hashtagCell, for: indexPath) as! HashtagTableViewCell
cell.delegate = self
cell.updateUI(hashtag: tag)
return cell
case let .status(id, state):
let cell = tableView.dequeueReusableCell(withIdentifier: statusCell, for: indexPath) as! TimelineStatusTableViewCell
cell.delegate = self
cell.updateUI(statusID: id, state: state)
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:))
userActivity = UserActivityManager.searchActivity()
}
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)
}
func loadResults(from source: SearchResultsViewController) {
currentQuery = source.currentQuery
if let sourceDataSource = source.dataSource {
dataSource.apply(sourceDataSource.snapshot())
}
// todo: check if the search needs to be performed before searching
// performSearch(query: currentQuery)
}
func performSearch(query: String?) {
guard let query = query, !query.isEmpty else {
self.dataSource.apply(NSDiffableDataSourceSnapshot<Section, Item>())
return
}
self.currentQuery = query
activityIndicator.isHidden = false
activityIndicator.startAnimating()
let request = Client.search(query: query, resolve: true, limit: 10)
mastodonController.run(request) { (response) in
guard case let .success(results, _) = response else { fatalError() }
DispatchQueue.main.async {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
}
guard self.currentQuery == query else { return }
let oldSnapshot = self.dataSource.snapshot()
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
if oldSnapshot.indexOfSection(.accounts) != nil {
oldSnapshot.itemIdentifiers(inSection: .accounts).forEach { (item) in
guard case let .account(id) = item else { return }
self.mastodonController.persistentContainer.account(for: id, in: context)?.decrementReferenceCount()
}
}
if oldSnapshot.indexOfSection(.statuses) != nil {
oldSnapshot.itemIdentifiers(inSection: .statuses).forEach { (item) in
guard case let .status(id, _) = item else { return }
self.mastodonController.persistentContainer.status(for: id, in: context)?.decrementReferenceCount()
}
}
if self.onlySections.contains(.accounts) && !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 {
snapshot.appendSections([.hashtags])
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
}
if self.onlySections.contains(.statuses) && !results.statuses.isEmpty {
snapshot.appendSections([.statuses])
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
addStatuses(results.statuses)
}
}, completion: {
DispatchQueue.main.async {
self.dataSource.apply(snapshot)
}
})
}
}
// 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)
}
}
}
extension SearchResultsViewController {
enum Section: CaseIterable {
case accounts
case hashtags
case statuses
var displayName: String {
switch self {
case .accounts:
return NSLocalizedString("People", comment: "accounts search results section")
case .hashtags:
return NSLocalizedString("Hashtags", comment: "hashtag search results section")
case .statuses:
return NSLocalizedString("Posts", comment: "statuses search results section")
}
}
}
enum Item: Hashable {
case account(String)
case hashtag(Hashtag)
case status(String, StatusState)
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)
}
}
}
class DataSource: UITableViewDiffableDataSource<Section, Item> {
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
}
}
}
extension SearchResultsViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
searchSubject.send(searchController.searchBar.text)
}
}
extension SearchResultsViewController: UISearchBarDelegate {
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
// perform a search immedaitely when the search button is pressed
performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines))
}
}
extension SearchResultsViewController: StatusTableViewCellDelegate {
var apiController: MastodonController { mastodonController }
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
tableView.beginUpdates()
tableView.endUpdates()
}
}