2021-07-14 14:28:41 +02:00
|
|
|
//
|
|
|
|
// SearchDetailViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-07-14 14:28:41 +02:00
|
|
|
|
2022-04-15 11:20:41 +02:00
|
|
|
final class CustomSearchController: UISearchController {
|
|
|
|
|
|
|
|
let customSearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
|
|
|
|
|
|
|
|
override var searchBar: UISearchBar { customSearchBar }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:28:26 +02:00
|
|
|
// Fake search bar not works on iPad with UISplitViewController
|
|
|
|
// check device and fallback to standard UISearchController
|
2023-09-15 17:45:22 +02:00
|
|
|
final class SearchDetailViewController: UIViewController, NeedsDependency {
|
2023-09-16 18:39:33 +02:00
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-07-15 09:49:30 +02:00
|
|
|
var observations = Set<NSKeyValueObservation>()
|
2023-09-18 21:17:39 +02:00
|
|
|
let searchResultOverviewCoordinator: SearchResultOverviewCoordinator
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
2023-09-16 18:39:33 +02:00
|
|
|
|
2021-09-27 09:28:26 +02:00
|
|
|
let isPhoneDevice: Bool = {
|
|
|
|
return UIDevice.current.userInterfaceIdiom == .phone
|
|
|
|
}()
|
2021-07-14 14:28:41 +02:00
|
|
|
|
|
|
|
var viewModel: SearchDetailViewModel!
|
|
|
|
|
2021-07-15 14:28:36 +02:00
|
|
|
let navigationBarVisualEffectBackgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemMaterial))
|
2021-07-15 09:49:30 +02:00
|
|
|
let navigationBarBackgroundView = UIView()
|
|
|
|
let navigationBar: UINavigationBar = {
|
|
|
|
let navigationItem = UINavigationItem()
|
|
|
|
let barAppearance = UINavigationBarAppearance()
|
|
|
|
barAppearance.configureWithTransparentBackground()
|
|
|
|
navigationItem.standardAppearance = barAppearance
|
|
|
|
navigationItem.compactAppearance = barAppearance
|
|
|
|
navigationItem.scrollEdgeAppearance = barAppearance
|
|
|
|
|
2021-07-15 14:28:36 +02:00
|
|
|
let navigationBar = UINavigationBar(
|
|
|
|
frame: CGRect(x: 0, y: 0, width: 300, height: 100)
|
|
|
|
)
|
2021-07-15 09:49:30 +02:00
|
|
|
navigationBar.setItems([navigationItem], animated: false)
|
|
|
|
return navigationBar
|
|
|
|
}()
|
2021-09-27 09:28:26 +02:00
|
|
|
|
2022-04-15 11:20:41 +02:00
|
|
|
let searchController: CustomSearchController = {
|
|
|
|
let searchController = CustomSearchController()
|
2021-09-27 09:28:26 +02:00
|
|
|
searchController.automaticallyShowsScopeBar = false
|
2022-11-14 16:36:50 +01:00
|
|
|
searchController.obscuresBackgroundDuringPresentation = false
|
2021-09-27 09:28:26 +02:00
|
|
|
return searchController
|
|
|
|
}()
|
|
|
|
private(set) lazy var searchBar: UISearchBar = {
|
|
|
|
let searchBar: UISearchBar
|
|
|
|
if isPhoneDevice {
|
|
|
|
searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
|
|
|
|
} else {
|
|
|
|
searchBar = searchController.searchBar
|
|
|
|
searchController.automaticallyShowsScopeBar = false
|
|
|
|
searchController.searchBar.setShowsScope(true, animated: false)
|
|
|
|
}
|
2021-07-14 14:28:41 +02:00
|
|
|
searchBar.placeholder = L10n.Scene.Search.SearchBar.placeholder
|
2021-07-15 14:28:36 +02:00
|
|
|
searchBar.sizeToFit()
|
2021-07-14 14:28:41 +02:00
|
|
|
return searchBar
|
|
|
|
}()
|
2021-07-15 14:28:36 +02:00
|
|
|
|
|
|
|
private(set) lazy var searchHistoryViewController: SearchHistoryViewController = {
|
|
|
|
let searchHistoryViewController = SearchHistoryViewController()
|
|
|
|
searchHistoryViewController.context = context
|
|
|
|
searchHistoryViewController.coordinator = coordinator
|
2022-10-09 14:07:57 +02:00
|
|
|
searchHistoryViewController.viewModel = SearchHistoryViewModel(context: context, authContext: viewModel.authContext)
|
2021-07-15 14:28:36 +02:00
|
|
|
return searchHistoryViewController
|
|
|
|
}()
|
2021-07-14 14:28:41 +02:00
|
|
|
|
2023-09-15 17:45:22 +02:00
|
|
|
private(set) lazy var searchResultsOverviewViewController: SearchResultsOverviewTableViewController = {
|
2023-09-18 21:17:39 +02:00
|
|
|
return searchResultOverviewCoordinator.overviewViewController
|
2023-09-15 17:45:22 +02:00
|
|
|
}()
|
2021-07-14 14:28:41 +02:00
|
|
|
|
2023-09-18 21:17:39 +02:00
|
|
|
//MARK: - init
|
|
|
|
|
|
|
|
init(appContext: AppContext, sceneCoordinator: SceneCoordinator, authContext: AuthContext) {
|
|
|
|
self.context = appContext
|
|
|
|
self.coordinator = sceneCoordinator
|
|
|
|
|
|
|
|
self.searchResultOverviewCoordinator = SearchResultOverviewCoordinator(appContext: appContext, authContext: authContext, sceneCoordinator: sceneCoordinator)
|
|
|
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
|
|
|
|
//MARK: - UIViewController
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
override func viewDidLoad() {
|
2023-09-18 21:17:39 +02:00
|
|
|
|
|
|
|
searchResultOverviewCoordinator.start()
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-07-15 09:49:30 +02:00
|
|
|
setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackgroundColor(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
setupSearchBar()
|
2021-09-27 09:28:26 +02:00
|
|
|
|
2021-07-15 14:28:36 +02:00
|
|
|
addChild(searchHistoryViewController)
|
|
|
|
searchHistoryViewController.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(searchHistoryViewController.view)
|
|
|
|
searchHistoryViewController.didMove(toParent: self)
|
2021-09-27 09:28:26 +02:00
|
|
|
if isPhoneDevice {
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
searchHistoryViewController.view.topAnchor.constraint(equalTo: navigationBarBackgroundView.bottomAnchor),
|
|
|
|
searchHistoryViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
searchHistoryViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
searchHistoryViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
])
|
|
|
|
} else {
|
2022-11-17 17:45:27 +01:00
|
|
|
searchHistoryViewController.view.pinToParent()
|
2021-09-27 09:28:26 +02:00
|
|
|
}
|
2021-07-15 14:28:36 +02:00
|
|
|
|
2023-09-15 17:45:22 +02:00
|
|
|
addChild(searchResultsOverviewViewController)
|
|
|
|
searchResultsOverviewViewController.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(searchResultsOverviewViewController.view)
|
|
|
|
searchResultsOverviewViewController.didMove(toParent: self)
|
|
|
|
if isPhoneDevice {
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
searchResultsOverviewViewController.view.topAnchor.constraint(equalTo: navigationBarBackgroundView.bottomAnchor),
|
|
|
|
searchResultsOverviewViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
searchResultsOverviewViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
searchResultsOverviewViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
searchResultsOverviewViewController.view.pinToParent()
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:28:36 +02:00
|
|
|
// bind search history display
|
|
|
|
viewModel.searchText
|
|
|
|
.removeDuplicates()
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] searchText in
|
|
|
|
guard let self = self else { return }
|
2023-09-15 17:45:22 +02:00
|
|
|
|
2021-07-15 14:28:36 +02:00
|
|
|
self.searchHistoryViewController.view.isHidden = !searchText.isEmpty
|
2023-09-15 17:45:22 +02:00
|
|
|
self.searchResultsOverviewViewController.view.isHidden = searchText.isEmpty
|
2021-07-15 14:28:36 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 09:49:30 +02:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2021-09-27 09:28:26 +02:00
|
|
|
if isPhoneDevice {
|
|
|
|
navigationController?.setNavigationBarHidden(true, animated: animated)
|
|
|
|
searchBar.setShowsScope(true, animated: false)
|
|
|
|
searchBar.setNeedsLayout()
|
|
|
|
searchBar.layoutIfNeeded()
|
|
|
|
} else {
|
|
|
|
// do nothing
|
|
|
|
}
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
super.viewWillDisappear(animated)
|
|
|
|
|
2021-09-27 09:28:26 +02:00
|
|
|
if isPhoneDevice {
|
|
|
|
if !isModal {
|
|
|
|
// prevent bar restore conflict with modal style issue
|
|
|
|
navigationController?.setNavigationBarHidden(false, animated: animated)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// do nothing
|
2021-07-15 14:38:32 +02:00
|
|
|
}
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
2021-09-27 09:28:26 +02:00
|
|
|
if isPhoneDevice {
|
|
|
|
searchBar.setShowsCancelButton(true, animated: animated)
|
2022-04-15 11:20:41 +02:00
|
|
|
UIView.performWithoutAnimation {
|
|
|
|
self.searchBar.becomeFirstResponder()
|
|
|
|
}
|
2021-09-27 09:28:26 +02:00
|
|
|
} else {
|
2022-04-15 11:20:41 +02:00
|
|
|
searchController.searchBar.setShowsCancelButton(true, animated: false)
|
|
|
|
searchController.searchBar.setShowsScope(true, animated: false)
|
|
|
|
UIView.performWithoutAnimation {
|
|
|
|
self.searchController.isActive = true
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.searchController.searchBar.becomeFirstResponder()
|
2021-11-03 08:29:25 +01:00
|
|
|
}
|
2021-09-27 09:28:26 +02:00
|
|
|
}
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchDetailViewController {
|
|
|
|
private func setupSearchBar() {
|
2021-09-27 09:28:26 +02:00
|
|
|
if isPhoneDevice {
|
|
|
|
navigationBar.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(navigationBar)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
navigationBar.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
|
|
|
|
navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
])
|
|
|
|
navigationBar.topItem?.titleView = searchBar
|
|
|
|
navigationBar.layer.observe(\.bounds, options: [.new]) { [weak self] navigationBar, _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.viewModel.navigationBarFrame.value = navigationBar.frame
|
|
|
|
}
|
|
|
|
.store(in: &observations)
|
|
|
|
|
|
|
|
navigationBarBackgroundView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.insertSubview(navigationBarBackgroundView, belowSubview: navigationBar)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
navigationBarBackgroundView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
navigationBarBackgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
navigationBarBackgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
navigationBarBackgroundView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
navigationBarVisualEffectBackgroundView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.insertSubview(navigationBarVisualEffectBackgroundView, belowSubview: navigationBarBackgroundView)
|
2022-11-17 17:45:27 +01:00
|
|
|
navigationBarVisualEffectBackgroundView.pinTo(to: navigationBarBackgroundView)
|
2021-09-27 09:28:26 +02:00
|
|
|
} else {
|
|
|
|
navigationItem.setHidesBackButton(true, animated: false)
|
|
|
|
navigationItem.titleView = nil
|
|
|
|
navigationItem.searchController = searchController
|
|
|
|
searchController.searchBar.sizeToFit()
|
|
|
|
}
|
2021-07-15 09:49:30 +02:00
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
searchBar.delegate = self
|
|
|
|
}
|
2021-07-15 09:49:30 +02:00
|
|
|
|
|
|
|
private func setupBackgroundColor(theme: Theme) {
|
|
|
|
navigationBarBackgroundView.backgroundColor = theme.navigationBarBackgroundColor
|
2023-06-02 09:52:12 +02:00
|
|
|
navigationBar.tintColor = Asset.Colors.Brand.blurple.color
|
2021-07-15 09:49:30 +02:00
|
|
|
}
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UISearchBarDelegate
|
|
|
|
extension SearchDetailViewController: UISearchBarDelegate {
|
|
|
|
|
|
|
|
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
|
2023-09-16 17:57:29 +02:00
|
|
|
let trimmedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
viewModel.searchText.value = trimmedSearchText
|
|
|
|
|
|
|
|
searchResultsOverviewViewController.showStandardSearch(for: trimmedSearchText)
|
|
|
|
searchResultsOverviewViewController.searchForSuggestions(for: trimmedSearchText)
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
|
2023-09-17 15:16:47 +02:00
|
|
|
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
2023-09-20 19:59:46 +02:00
|
|
|
guard let searchText = searchBar.text, searchText.isNotEmpty else { return }
|
|
|
|
|
2023-09-17 15:16:47 +02:00
|
|
|
searchBar.resignFirstResponder()
|
2023-09-20 19:59:46 +02:00
|
|
|
|
|
|
|
let searchResultViewModel = SearchResultViewModel(context: context, authContext: viewModel.authContext, searchScope: .all, searchText: searchText)
|
|
|
|
coordinator.present(scene: .searchResult(viewModel: searchResultViewModel), transition: .show)
|
2023-09-17 15:16:47 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 14:28:41 +02:00
|
|
|
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
|
2021-07-15 14:38:32 +02:00
|
|
|
// dismiss or pop
|
|
|
|
if isModal {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
} else {
|
|
|
|
navigationController?.popViewController(animated: false)
|
|
|
|
}
|
2021-07-14 14:28:41 +02:00
|
|
|
}
|
|
|
|
}
|