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
|
2023-02-07 02:26:42 +00:00
|
|
|
import WebURLFoundationExtras
|
2019-09-15 00:47:08 +00:00
|
|
|
|
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
class SearchResultsViewController: UIViewController, CollectionViewController {
|
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
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
var collectionView: UICollectionView! { view as? UICollectionView }
|
|
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
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
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2020-12-14 23:44:41 +00:00
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-09-15 00:47:08 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-05-22 15:22:01 +00:00
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
_ = searchSubject
|
|
|
|
.debounce(for: .milliseconds(500), scheduler: RunLoop.main)
|
|
|
|
.map { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
|
|
|
|
.filter { $0 != self.currentQuery }
|
|
|
|
.sink(receiveValue: performSearch(query:))
|
2021-05-22 15:22:01 +00:00
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
userActivity = UserActivityManager.searchActivity()
|
2023-01-16 22:47:56 +00:00
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
|
|
|
|
let sectionHeader = UICollectionView.SupplementaryRegistration<UICollectionViewListCell>(elementKind: UICollectionView.elementKindSectionHeader) { [unowned self] supplementaryView, elementKind, indexPath in
|
|
|
|
let section = self.dataSource.sectionIdentifier(for: indexPath.section)!
|
|
|
|
var config = UIListContentConfiguration.groupedHeader()
|
|
|
|
config.text = section.displayName
|
|
|
|
supplementaryView.contentConfiguration = config
|
|
|
|
}
|
|
|
|
let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, indexPath, itemIdentifier in
|
|
|
|
cell.indicator.startAnimating()
|
|
|
|
}
|
|
|
|
let accountCell = UICollectionView.CellRegistration<AccountCollectionViewCell, String> { cell, indexPath, itemIdentifier in
|
|
|
|
cell.delegate = self
|
|
|
|
cell.updateUI(accountID: itemIdentifier)
|
|
|
|
}
|
|
|
|
let hashtagCell = UICollectionView.CellRegistration<TrendingHashtagCollectionViewCell, Hashtag> { cell, indexPath, itemIdentifier in
|
|
|
|
cell.updateUI(hashtag: itemIdentifier)
|
|
|
|
}
|
|
|
|
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))
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
2023-02-03 04:02:11 +00:00
|
|
|
cell.configurationUpdateHandler = { cell, state in
|
|
|
|
var config = UIBackgroundConfiguration.listGroupedCell().updated(for: state)
|
|
|
|
if state.isHighlighted || state.isSelected {
|
|
|
|
config.backgroundColor = .appSelectedCellBackground
|
|
|
|
} else {
|
|
|
|
config.backgroundColor = .appGroupedCellBackground
|
|
|
|
}
|
|
|
|
cell.backgroundConfiguration = config
|
|
|
|
}
|
|
|
|
return cell
|
2023-02-07 02:26:42 +00:00
|
|
|
}
|
|
|
|
dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in
|
|
|
|
if elementKind == UICollectionView.elementKindSectionHeader {
|
|
|
|
return collectionView.dequeueConfiguredReusableSupplementary(using: sectionHeader, for: indexPath)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dataSource
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
2023-01-18 00:32:50 +00:00
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
clearSelectionOnAppear(animated: animated)
|
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
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
snapshot.appendSections([.loadingIndicator])
|
|
|
|
snapshot.appendItems([.loadingIndicator])
|
|
|
|
dataSource.apply(snapshot)
|
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 }
|
|
|
|
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.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.dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func showSearchError(_ error: Client.Error) {
|
|
|
|
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
dataSource.apply(snapshot)
|
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
let config = ToastConfiguration(from: error, with: "Error Searching", in: self) { [unowned self] toast in
|
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
self.performSearch(query: self.currentQuery)
|
|
|
|
}
|
|
|
|
showToast(configuration: config, animated: true)
|
2021-05-22 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
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-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 {
|
2023-02-07 02:26:42 +00:00
|
|
|
case loadingIndicator
|
2019-09-15 00:47:08 +00:00
|
|
|
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
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
var displayName: String? {
|
2019-09-15 00:47:08 +00:00
|
|
|
switch self {
|
2023-02-07 02:26:42 +00:00
|
|
|
case .loadingIndicator:
|
|
|
|
return nil
|
2019-09-15 00:47:08 +00:00
|
|
|
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 {
|
2023-02-07 02:26:42 +00:00
|
|
|
case loadingIndicator
|
2019-09-15 00:47:08 +00:00
|
|
|
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 {
|
2023-02-07 02:26:42 +00:00
|
|
|
case .loadingIndicator:
|
|
|
|
hasher.combine("loadingIndicator")
|
2020-06-30 02:21:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 02:26:42 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchResultsViewController: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
|
|
|
switch dataSource.itemIdentifier(for: indexPath) {
|
|
|
|
case .loadingIndicator:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
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())
|
2019-12-17 03:23:12 +00:00
|
|
|
}
|
2023-02-07 02:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath),
|
|
|
|
let cell = collectionView.cellForItem(at: indexPath) else {
|
2019-12-17 03:23:12 +00:00
|
|
|
return nil
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
2023-02-07 02:26:42 +00:00
|
|
|
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)]
|
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) {
|
2023-02-07 02:26:42 +00:00
|
|
|
let newQuery = 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)
|
|
|
|
}
|
2023-01-22 16:41:38 +00:00
|
|
|
}
|
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 {
|
|
|
|
}
|
|
|
|
|
2023-02-07 02:26:42 +00:00
|
|
|
extension SearchResultsViewController: StatusCollectionViewCellDelegate {
|
|
|
|
func statusCellNeedsReconfigure(_ cell: StatusCollectionViewCell, animated: Bool, completion: (() -> Void)?) {
|
|
|
|
if let indexPath = collectionView.indexPath(for: cell) {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.reconfigureItems([dataSource.itemIdentifier(for: indexPath)!])
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: animated, completion: completion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusCellShowFiltered(_ cell: StatusCollectionViewCell) {
|
|
|
|
// not yet supported
|
2019-09-15 00:47:08 +00:00
|
|
|
}
|
|
|
|
}
|