Rewrite search results VC using UICollectionView

This commit is contained in:
Shadowfacts 2023-02-06 21:26:42 -05:00
parent 6e2f6bb8e9
commit 87bc1f5f75
3 changed files with 264 additions and 111 deletions

View File

@ -101,12 +101,23 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
// UISearchController exists outside of the normal VC hierarchy,
// so we manually propagate this down to the results controller
// so that it can deselect on appear
if searchController.isActive {
resultsController.viewWillAppear(animated)
}
clearSelectionOnAppear(animated: animated) clearSelectionOnAppear(animated: animated)
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) super.viewDidAppear(animated)
if searchController.isActive {
resultsController.viewDidAppear(animated)
}
// this is a workaround for the issue that setting isActive on a search controller that is not visible // this is a workaround for the issue that setting isActive on a search controller that is not visible
// does not cause it to automatically become active once it becomes visible // does not cause it to automatically become active once it becomes visible
// see FB7814561 // see FB7814561

View File

@ -98,7 +98,7 @@ class TrendsViewController: UIViewController, CollectionViewController {
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> { private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
let sectionHeaderCell = UICollectionView.SupplementaryRegistration<UICollectionViewListCell>(elementKind: UICollectionView.elementKindSectionHeader) { [unowned self] (headerView, collectionView, indexPath) in let sectionHeaderCell = UICollectionView.SupplementaryRegistration<UICollectionViewListCell>(elementKind: UICollectionView.elementKindSectionHeader) { [unowned self] (headerView, collectionView, indexPath) in
let section = self.dataSource.snapshot().sectionIdentifiers[indexPath.section] let section = self.dataSource.sectionIdentifier(for: indexPath.section)!
var config = UIListContentConfiguration.groupedHeader() var config = UIListContentConfiguration.groupedHeader()
config.text = section.title config.text = section.title
headerView.contentConfiguration = config headerView.contentConfiguration = config

View File

@ -9,6 +9,7 @@
import UIKit import UIKit
import Combine import Combine
import Pachyderm import Pachyderm
import WebURLFoundationExtras
fileprivate let accountCell = "accountCell" fileprivate let accountCell = "accountCell"
fileprivate let statusCell = "statusCell" fileprivate let statusCell = "statusCell"
@ -26,17 +27,15 @@ extension SearchResultsViewControllerDelegate {
func selectedSearchResult(status statusID: String) {} func selectedSearchResult(status statusID: String) {}
} }
class SearchResultsViewController: EnhancedTableViewController { class SearchResultsViewController: UIViewController, CollectionViewController {
weak var mastodonController: MastodonController! weak var mastodonController: MastodonController!
weak var exploreNavigationController: UINavigationController? weak var exploreNavigationController: UINavigationController?
weak var delegate: SearchResultsViewControllerDelegate? weak var delegate: SearchResultsViewControllerDelegate?
var dataSource: UITableViewDiffableDataSource<Section, Item>! var collectionView: UICollectionView! { view as? UICollectionView }
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
private var activityIndicator: UIActivityIndicatorView!
private var errorLabel: UILabel!
/// Types of results to search for. /// Types of results to search for.
var scope: Scope var scope: Scope
@ -50,9 +49,7 @@ class SearchResultsViewController: EnhancedTableViewController {
self.mastodonController = mastodonController self.mastodonController = mastodonController
self.scope = scope self.scope = scope
super.init(style: .grouped) super.init(nibName: nil, bundle: nil)
dragEnabled = true
title = NSLocalizedString("Search", comment: "search screen title") title = NSLocalizedString("Search", comment: "search screen title")
} }
@ -61,49 +58,88 @@ class SearchResultsViewController: EnhancedTableViewController {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
override func loadView() {
let layout = UICollectionViewCompositionalLayout { [unowned self] sectionIndex, environment in
var config = UICollectionLayoutListConfiguration(appearance: .grouped)
config.backgroundColor = .appGroupedBackground
config.headerMode = .supplementary
switch self.dataSource.sectionIdentifier(for: sectionIndex) {
case .loadingIndicator:
config.showsSeparators = false
config.headerMode = .none
case .statuses:
config.leadingSwipeActionsConfigurationProvider = { [unowned self] in
(self.collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.leadingSwipeActions()
}
config.trailingSwipeActionsConfigurationProvider = { [unowned self] in
(self.collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.trailingSwipeActions()
}
config.separatorConfiguration.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
config.separatorConfiguration.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
default:
break
}
let section = NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment)
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
section.contentInsetsReference = .readableContent
}
return section
}
view = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dragDelegate = self
collectionView.allowsFocus = true
collectionView.backgroundColor = .appGroupedBackground
dataSource = createDataSource()
}
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
errorLabel = UILabel() _ = searchSubject
errorLabel.translatesAutoresizingMaskIntoConstraints = false .debounce(for: .milliseconds(500), scheduler: RunLoop.main)
errorLabel.font = .preferredFont(forTextStyle: .callout) .map { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
errorLabel.textColor = .secondaryLabel .filter { $0 != self.currentQuery }
errorLabel.numberOfLines = 0 .sink(receiveValue: performSearch(query:))
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),
])
tableView.register(UINib(nibName: "AccountTableViewCell", bundle: .main), forCellReuseIdentifier: accountCell) userActivity = UserActivityManager.searchActivity()
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell)
tableView.register(UINib(nibName: "HashtagTableViewCell", bundle: .main), forCellReuseIdentifier: hashtagCell)
tableView.allowsFocus = true NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil)
tableView.backgroundColor = .appGroupedBackground }
dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
let cell: UITableViewCell let sectionHeader = UICollectionView.SupplementaryRegistration<UICollectionViewListCell>(elementKind: UICollectionView.elementKindSectionHeader) { [unowned self] supplementaryView, elementKind, indexPath in
switch item { let section = self.dataSource.sectionIdentifier(for: indexPath.section)!
case let .account(id): var config = UIListContentConfiguration.groupedHeader()
let accountCell = tableView.dequeueReusableCell(withIdentifier: accountCell, for: indexPath) as! AccountTableViewCell config.text = section.displayName
accountCell.delegate = self supplementaryView.contentConfiguration = config
accountCell.updateUI(accountID: id) }
cell = accountCell let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, indexPath, itemIdentifier in
case let .hashtag(tag): cell.indicator.startAnimating()
let hashtagCell = tableView.dequeueReusableCell(withIdentifier: hashtagCell, for: indexPath) as! HashtagTableViewCell }
hashtagCell.delegate = self let accountCell = UICollectionView.CellRegistration<AccountCollectionViewCell, String> { cell, indexPath, itemIdentifier in
hashtagCell.updateUI(hashtag: tag) cell.delegate = self
cell = hashtagCell cell.updateUI(accountID: itemIdentifier)
case let .status(id, state): }
let statusCell = tableView.dequeueReusableCell(withIdentifier: statusCell, for: indexPath) as! TimelineStatusTableViewCell let hashtagCell = UICollectionView.CellRegistration<TrendingHashtagCollectionViewCell, Hashtag> { cell, indexPath, itemIdentifier in
statusCell.delegate = self cell.updateUI(hashtag: itemIdentifier)
statusCell.updateUI(statusID: id, state: state) }
cell = statusCell let statusCell = UICollectionView.CellRegistration<TimelineStatusCollectionViewCell, (String, CollapseState)> { [unowned self] cell, indexPath, itemIdentifier in
cell.delegate = self
cell.updateUI(statusID: itemIdentifier.0, state: itemIdentifier.1, filterResult: .allow, precomputedContent: nil)
}
let dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in
let cell: UICollectionViewCell
switch itemIdentifier {
case .loadingIndicator:
return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ())
case .account(let accountID):
cell = collectionView.dequeueConfiguredReusableCell(using: accountCell, for: indexPath, item: accountID)
case .hashtag(let hashtag):
cell = collectionView.dequeueConfiguredReusableCell(using: hashtagCell, for: indexPath, item: hashtag)
case .status(let id, let state):
cell = collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state))
} }
cell.configurationUpdateHandler = { cell, state in cell.configurationUpdateHandler = { cell, state in
var config = UIBackgroundConfiguration.listGroupedCell().updated(for: state) var config = UIBackgroundConfiguration.listGroupedCell().updated(for: state)
@ -115,26 +151,21 @@ class SearchResultsViewController: EnhancedTableViewController {
cell.backgroundConfiguration = config cell.backgroundConfiguration = config
} }
return cell return cell
}) }
dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in
if elementKind == UICollectionView.elementKindSectionHeader {
return collectionView.dequeueConfiguredReusableSupplementary(using: sectionHeader, for: indexPath)
} else {
return nil
}
}
return dataSource
}
activityIndicator = UIActivityIndicatorView(style: .large) override func viewWillAppear(_ animated: Bool) {
activityIndicator.translatesAutoresizingMaskIntoConstraints = false super.viewWillAppear(animated)
activityIndicator.isHidden = true
view.addSubview(activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
activityIndicator.topAnchor.constraint(equalTo: view.topAnchor, constant: 8)
])
_ = searchSubject clearSelectionOnAppear(animated: animated)
.debounce(for: .milliseconds(500), scheduler: RunLoop.main)
.map { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { $0 != self.currentQuery }
.sink(receiveValue: performSearch(query:))
userActivity = UserActivityManager.searchActivity()
NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil)
} }
override func targetViewController(forAction action: Selector, sender: Any?) -> UIViewController? { override func targetViewController(forAction action: Selector, sender: Any?) -> UIViewController? {
@ -161,25 +192,19 @@ class SearchResultsViewController: EnhancedTableViewController {
} }
self.currentQuery = query self.currentQuery = query
activityIndicator.isHidden = false var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
activityIndicator.startAnimating() snapshot.appendSections([.loadingIndicator])
errorLabel.isHidden = true snapshot.appendItems([.loadingIndicator])
dataSource.apply(snapshot)
let request = Client.search(query: query, types: scope.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, _):
guard self.currentQuery == query else { return } guard self.currentQuery == query else { return }
DispatchQueue.main.async {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
}
self.showSearchResults(results) self.showSearchResults(results)
case let .failure(error): case let .failure(error):
DispatchQueue.main.async { DispatchQueue.main.async {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
self.showSearchError(error) self.showSearchError(error)
} }
} }
@ -207,7 +232,6 @@ class SearchResultsViewController: EnhancedTableViewController {
} }
}, completion: { }, completion: {
DispatchQueue.main.async { DispatchQueue.main.async {
self.errorLabel.isHidden = true
self.dataSource.apply(snapshot) self.dataSource.apply(snapshot)
} }
}) })
@ -217,8 +241,11 @@ class SearchResultsViewController: EnhancedTableViewController {
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>() let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
dataSource.apply(snapshot) dataSource.apply(snapshot)
errorLabel.isHidden = false let config = ToastConfiguration(from: error, with: "Error Searching", in: self) { [unowned self] toast in
errorLabel.text = error.localizedDescription toast.dismissToast(animated: true)
self.performSearch(query: self.currentQuery)
}
showToast(configuration: config, animated: true)
} }
@objc private func handleStatusDeleted(_ notification: Foundation.Notification) { @objc private func handleStatusDeleted(_ notification: Foundation.Notification) {
@ -242,26 +269,6 @@ class SearchResultsViewController: EnhancedTableViewController {
} }
} }
// 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 { extension SearchResultsViewController {
@ -301,12 +308,15 @@ extension SearchResultsViewController {
extension SearchResultsViewController { extension SearchResultsViewController {
enum Section: CaseIterable { enum Section: CaseIterable {
case loadingIndicator
case accounts case accounts
case hashtags case hashtags
case statuses case statuses
var displayName: String { var displayName: String? {
switch self { switch self {
case .loadingIndicator:
return nil
case .accounts: case .accounts:
return NSLocalizedString("People", comment: "accounts search results section") return NSLocalizedString("People", comment: "accounts search results section")
case .hashtags: case .hashtags:
@ -317,12 +327,15 @@ extension SearchResultsViewController {
} }
} }
enum Item: Hashable { enum Item: Hashable {
case loadingIndicator
case account(String) case account(String)
case hashtag(Hashtag) case hashtag(Hashtag)
case status(String, CollapseState) case status(String, CollapseState)
func hash(into hasher: inout Hasher) { func hash(into hasher: inout Hasher) {
switch self { switch self {
case .loadingIndicator:
hasher.combine("loadingIndicator")
case let .account(id): case let .account(id):
hasher.combine("account") hasher.combine("account")
hasher.combine(id) hasher.combine(id)
@ -334,16 +347,121 @@ extension SearchResultsViewController {
hasher.combine(id) hasher.combine(id)
} }
} }
static func ==(lhs: Item, rhs: Item) -> Bool {
switch (lhs, rhs) {
case (.loadingIndicator, .loadingIndicator):
return true
case (.account(let a), .account(let b)):
return a == b
case (.hashtag(let a), .hashtag(let b)):
return a.name == b.name
case (.status(let a, _), .status(let b, _)):
return a == b
default:
return false
}
}
}
} }
class DataSource: UITableViewDiffableDataSource<Section, Item> { extension SearchResultsViewController: UICollectionViewDelegate {
override func tableView(_ tableView: UITableView, titleForHeaderInSection sectionIndex: Int) -> String? { func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
let currentSnapshot = snapshot() switch dataSource.itemIdentifier(for: indexPath) {
for section in Section.allCases where currentSnapshot.indexOfSection(section) == sectionIndex { case .loadingIndicator:
return section.displayName return false
default:
return true
} }
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch dataSource.itemIdentifier(for: indexPath) {
case nil, .loadingIndicator:
return
case let .account(id):
if let delegate {
delegate.selectedSearchResult(account: id)
} else {
selected(account: id)
}
case let .hashtag(hashtag):
if let delegate {
delegate.selectedSearchResult(hashtag: hashtag)
} else {
selected(tag: hashtag)
}
case let .status(id, state):
if let delegate {
delegate.selectedSearchResult(status: id)
} else {
selected(status: id, state: state.copy())
}
}
}
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
guard let item = dataSource.itemIdentifier(for: indexPath),
let cell = collectionView.cellForItem(at: indexPath) else {
return nil return nil
} }
switch item {
case .loadingIndicator:
return nil
case .account(let id):
return UIContextMenuConfiguration {
ProfileViewController(accountID: id, mastodonController: self.mastodonController)
} actionProvider: { _ in
UIMenu(children: self.actionsForProfile(accountID: id, source: .view(cell)))
}
case .hashtag(let tag):
return UIContextMenuConfiguration {
HashtagTimelineViewController(for: tag, mastodonController: self.mastodonController)
} actionProvider: { _ in
UIMenu(children: self.actionsForHashtag(tag, source: .view(cell)))
}
case .status(_, _):
return (cell as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration()
}
}
func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: self)
}
}
extension SearchResultsViewController: UICollectionViewDragDelegate {
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
guard let accountInfo = mastodonController.accountInfo,
let item = dataSource.itemIdentifier(for: indexPath) else {
return []
}
let url: URL
let activity: NSUserActivity
switch item {
case .loadingIndicator:
return []
case .account(let id):
guard let account = mastodonController.persistentContainer.account(for: id) else {
return []
}
url = account.url
activity = UserActivityManager.showProfileActivity(id: id, accountID: accountInfo.id)
case .hashtag(let tag):
url = URL(tag.url)!
activity = UserActivityManager.showTimelineActivity(timeline: .tag(hashtag: tag.name), accountID: accountInfo.id)!
case .status(let id, _):
guard let status = mastodonController.persistentContainer.status(for: id),
status.url != nil else {
return []
}
url = status.url!
activity = UserActivityManager.showConversationActivity(mainStatusID: id, accountID: accountInfo.id)
}
activity.displaysAuxiliaryScene = true
let provider = NSItemProvider(object: url as NSURL)
provider.registerObject(activity, visibility: .all)
return [UIDragItem(itemProvider: provider)]
} }
} }
@ -360,8 +478,25 @@ extension SearchResultsViewController: UISearchBarDelegate {
} }
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
self.scope = Scope.allCases[selectedScope] let newQuery = searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines)
performSearch(query: searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines)) let newScope = Scope.allCases[selectedScope]
if self.scope == .all && currentQuery == newQuery {
self.scope = newScope
var snapshot = dataSource.snapshot()
if snapshot.sectionIdentifiers.contains(.accounts) && scope != .people {
snapshot.deleteSections([.accounts])
}
if snapshot.sectionIdentifiers.contains(.hashtags) && scope != .hashtags {
snapshot.deleteSections([.hashtags])
}
if snapshot.sectionIdentifiers.contains(.statuses) && scope != .posts {
snapshot.deleteSections([.statuses])
}
dataSource.apply(snapshot)
} else {
self.scope = newScope
performSearch(query: newQuery)
}
} }
} }
@ -375,9 +510,16 @@ extension SearchResultsViewController: ToastableViewController {
extension SearchResultsViewController: MenuActionProvider { extension SearchResultsViewController: MenuActionProvider {
} }
extension SearchResultsViewController: StatusTableViewCellDelegate { extension SearchResultsViewController: StatusCollectionViewCellDelegate {
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) { func statusCellNeedsReconfigure(_ cell: StatusCollectionViewCell, animated: Bool, completion: (() -> Void)?) {
tableView.beginUpdates() if let indexPath = collectionView.indexPath(for: cell) {
tableView.endUpdates() var snapshot = dataSource.snapshot()
snapshot.reconfigureItems([dataSource.itemIdentifier(for: indexPath)!])
dataSource.apply(snapshot, animatingDifferences: animated, completion: completion)
}
}
func statusCellShowFiltered(_ cell: StatusCollectionViewCell) {
// not yet supported
} }
} }