2021-02-23 09:45:00 +01:00
|
|
|
//
|
|
|
|
// SearchViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
2021-03-31 08:28:40 +02:00
|
|
|
// Created by sxiaojian on 2021/3/31.
|
2021-02-23 09:45:00 +01:00
|
|
|
//
|
|
|
|
|
2021-03-31 08:28:40 +02:00
|
|
|
import Combine
|
2021-04-06 09:25:04 +02:00
|
|
|
import GameplayKit
|
2021-04-01 14:54:57 +02:00
|
|
|
import MastodonSDK
|
2021-04-02 06:10:12 +02:00
|
|
|
import UIKit
|
2021-02-23 09:45:00 +01:00
|
|
|
|
|
|
|
final class SearchViewController: UIViewController, NeedsDependency {
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
2021-03-31 08:28:40 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-04-08 06:31:48 +02:00
|
|
|
private(set) lazy var viewModel = SearchViewModel(context: context, coordinator: coordinator)
|
2021-03-31 08:28:40 +02:00
|
|
|
|
2021-03-31 09:06:46 +02:00
|
|
|
let searchBar: UISearchBar = {
|
|
|
|
let searchBar = UISearchBar()
|
2021-03-31 13:29:54 +02:00
|
|
|
searchBar.placeholder = L10n.Scene.Search.Searchbar.placeholder
|
2021-04-06 10:42:45 +02:00
|
|
|
searchBar.tintColor = Asset.Colors.brandBlue.color
|
2021-03-31 13:29:54 +02:00
|
|
|
searchBar.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
let micImage = UIImage(systemName: "mic.fill")
|
|
|
|
searchBar.setImage(micImage, for: .bookmark, state: .normal)
|
|
|
|
searchBar.showsBookmarkButton = true
|
2021-04-02 10:24:00 +02:00
|
|
|
searchBar.showsScopeBar = false
|
2021-04-06 09:25:04 +02:00
|
|
|
searchBar.scopeButtonTitles = [L10n.Scene.Search.Searching.Segment.all, L10n.Scene.Search.Searching.Segment.people, L10n.Scene.Search.Searching.Segment.hashtags]
|
2021-04-07 15:42:43 +02:00
|
|
|
searchBar.barTintColor = Asset.Colors.Background.navigationBar.color
|
2021-03-31 09:06:46 +02:00
|
|
|
return searchBar
|
|
|
|
}()
|
2021-03-31 13:29:54 +02:00
|
|
|
|
2021-04-02 10:24:00 +02:00
|
|
|
// recommend
|
2021-04-01 14:54:57 +02:00
|
|
|
let scrollView: UIScrollView = {
|
|
|
|
let scrollView = UIScrollView()
|
|
|
|
scrollView.showsVerticalScrollIndicator = false
|
|
|
|
scrollView.alwaysBounceVertical = true
|
|
|
|
scrollView.clipsToBounds = false
|
|
|
|
return scrollView
|
|
|
|
}()
|
|
|
|
|
|
|
|
let stackView: UIStackView = {
|
|
|
|
let stackView = UIStackView()
|
|
|
|
stackView.axis = .vertical
|
|
|
|
stackView.distribution = .fill
|
|
|
|
stackView.spacing = 0
|
|
|
|
stackView.layoutMargins = UIEdgeInsets(top: 0, left: 0, bottom: 68, right: 0)
|
|
|
|
stackView.isLayoutMarginsRelativeArrangement = true
|
|
|
|
return stackView
|
|
|
|
}()
|
|
|
|
|
2021-04-07 15:01:32 +02:00
|
|
|
let hashtagCollectionView: UICollectionView = {
|
2021-04-01 14:54:57 +02:00
|
|
|
let flowLayout = UICollectionViewFlowLayout()
|
|
|
|
flowLayout.scrollDirection = .horizontal
|
|
|
|
let view = ControlContainableCollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
|
|
|
view.backgroundColor = .clear
|
|
|
|
view.showsHorizontalScrollIndicator = false
|
|
|
|
view.showsVerticalScrollIndicator = false
|
|
|
|
view.layer.masksToBounds = false
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
let accountsCollectionView: UICollectionView = {
|
2021-03-31 13:29:54 +02:00
|
|
|
let flowLayout = UICollectionViewFlowLayout()
|
|
|
|
flowLayout.scrollDirection = .horizontal
|
|
|
|
let view = ControlContainableCollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
|
|
|
view.backgroundColor = .clear
|
|
|
|
view.showsHorizontalScrollIndicator = false
|
|
|
|
view.showsVerticalScrollIndicator = false
|
|
|
|
view.layer.masksToBounds = false
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return view
|
|
|
|
}()
|
2021-04-02 10:24:00 +02:00
|
|
|
|
|
|
|
// searching
|
|
|
|
let searchingTableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
2021-04-07 15:42:43 +02:00
|
|
|
tableView.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
2021-04-02 10:24:00 +02:00
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .singleLine
|
2021-04-07 15:42:43 +02:00
|
|
|
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
|
2021-04-02 10:24:00 +02:00
|
|
|
return tableView
|
|
|
|
}()
|
2021-04-07 13:49:33 +02:00
|
|
|
|
|
|
|
lazy var searchHeader: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
|
|
|
view.frame = CGRect(origin: .zero, size: CGSize(width: searchingTableView.frame.width, height: 56))
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
let recentSearchesLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
|
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
|
|
|
label.text = L10n.Scene.Search.Searching.recentSearch
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let clearSearchHistoryButton: UIButton = {
|
|
|
|
let button = UIButton(type: .custom)
|
2021-04-07 15:42:43 +02:00
|
|
|
button.setTitleColor(Asset.Colors.brandBlue.color, for: .normal)
|
2021-04-07 13:49:33 +02:00
|
|
|
button.setTitle(L10n.Scene.Search.Searching.clear, for: .normal)
|
|
|
|
return button
|
|
|
|
}()
|
2021-02-23 09:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchViewController {
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-04-07 13:49:33 +02:00
|
|
|
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
2021-04-07 15:42:43 +02:00
|
|
|
let barAppearance = UINavigationBarAppearance()
|
|
|
|
barAppearance.configureWithTransparentBackground()
|
|
|
|
barAppearance.backgroundColor = Asset.Colors.Background.navigationBar.color
|
|
|
|
navigationItem.standardAppearance = barAppearance
|
|
|
|
navigationItem.compactAppearance = barAppearance
|
|
|
|
navigationItem.scrollEdgeAppearance = barAppearance
|
2021-03-31 13:29:54 +02:00
|
|
|
searchBar.delegate = self
|
|
|
|
navigationItem.titleView = searchBar
|
|
|
|
navigationItem.hidesBackButton = true
|
2021-04-01 14:54:57 +02:00
|
|
|
setupScrollView()
|
|
|
|
setupHashTagCollectionView()
|
|
|
|
setupAccountsCollectionView()
|
2021-04-02 10:24:00 +02:00
|
|
|
setupSearchingTableView()
|
2021-04-06 09:25:04 +02:00
|
|
|
setupDataSource()
|
2021-04-07 13:49:33 +02:00
|
|
|
setupSearchHeader()
|
2021-04-01 14:54:57 +02:00
|
|
|
}
|
2021-04-02 06:10:12 +02:00
|
|
|
|
2021-04-01 14:54:57 +02:00
|
|
|
func setupScrollView() {
|
|
|
|
view.addSubview(scrollView)
|
|
|
|
scrollView.constrain([
|
|
|
|
scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
|
|
|
|
scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
|
|
|
|
scrollView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
|
|
|
|
scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
|
2021-04-02 06:10:12 +02:00
|
|
|
scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: view.widthAnchor),
|
2021-04-01 14:54:57 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
scrollView.addSubview(stackView)
|
|
|
|
stackView.constrain([
|
|
|
|
stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
|
|
|
|
stackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
|
|
|
|
stackView.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
|
|
|
|
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor),
|
|
|
|
])
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
2021-04-06 09:25:04 +02:00
|
|
|
|
|
|
|
func setupDataSource() {
|
2021-04-07 15:01:32 +02:00
|
|
|
viewModel.hashtagDiffableDataSource = RecommendHashTagSection.collectionViewDiffableDataSource(for: hashtagCollectionView)
|
2021-04-06 09:25:04 +02:00
|
|
|
viewModel.accountDiffableDataSource = RecommendAccountSection.collectionViewDiffableDataSource(for: accountsCollectionView)
|
2021-04-07 13:49:33 +02:00
|
|
|
viewModel.searchResultDiffableDataSource = SearchResultSection.tableViewDiffableDataSource(for: searchingTableView, dependency: self)
|
2021-04-06 09:25:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchViewController: UIScrollViewDelegate {
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
|
if scrollView == searchingTableView {
|
|
|
|
handleScrollViewDidScroll(scrollView)
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
2021-03-31 09:06:46 +02:00
|
|
|
|
2021-03-31 13:29:54 +02:00
|
|
|
extension SearchViewController: UISearchBarDelegate {
|
|
|
|
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
|
|
|
|
searchBar.setShowsCancelButton(true, animated: true)
|
2021-04-02 10:24:00 +02:00
|
|
|
searchBar.showsScopeBar = true
|
|
|
|
viewModel.isSearching.value = true
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
|
|
|
|
searchBar.setShowsCancelButton(false, animated: true)
|
2021-04-02 10:24:00 +02:00
|
|
|
searchBar.showsScopeBar = false
|
|
|
|
viewModel.isSearching.value = true
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
|
|
|
|
searchBar.setShowsCancelButton(false, animated: true)
|
2021-04-02 10:24:00 +02:00
|
|
|
searchBar.showsScopeBar = false
|
2021-03-31 13:29:54 +02:00
|
|
|
searchBar.text = ""
|
|
|
|
searchBar.resignFirstResponder()
|
2021-04-02 10:24:00 +02:00
|
|
|
viewModel.isSearching.value = false
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
|
|
|
|
viewModel.searchText.send(searchText)
|
2021-02-23 09:45:00 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:24:00 +02:00
|
|
|
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
|
|
|
|
switch selectedScope {
|
|
|
|
case 0:
|
2021-04-07 15:42:43 +02:00
|
|
|
viewModel.searchScope.value = Mastodon.API.Search.SearchType.default
|
2021-04-02 10:24:00 +02:00
|
|
|
case 1:
|
2021-04-07 15:42:43 +02:00
|
|
|
viewModel.searchScope.value = Mastodon.API.Search.SearchType.accounts
|
2021-04-02 10:24:00 +02:00
|
|
|
case 2:
|
2021-04-07 15:42:43 +02:00
|
|
|
viewModel.searchScope.value = Mastodon.API.Search.SearchType.hashtags
|
2021-04-02 10:24:00 +02:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 09:25:04 +02:00
|
|
|
|
2021-04-02 06:10:12 +02:00
|
|
|
func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {}
|
2021-02-23 09:45:00 +01:00
|
|
|
}
|
2021-04-01 14:54:57 +02:00
|
|
|
|
2021-04-06 09:25:04 +02:00
|
|
|
extension SearchViewController: LoadMoreConfigurableTableViewContainer {
|
|
|
|
typealias BottomLoaderTableViewCell = SearchBottomLoader
|
|
|
|
typealias LoadingState = SearchViewModel.LoadOldestState.Loading
|
|
|
|
var loadMoreConfigurableTableView: UITableView { searchingTableView }
|
|
|
|
var loadMoreConfigurableStateMachine: GKStateMachine { viewModel.loadoldestStateMachine }
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:54:57 +02:00
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SearchViewController_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
UIViewControllerPreview {
|
|
|
|
let viewController = SearchViewController()
|
|
|
|
return viewController
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 800))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|