2021-04-02 10:24:00 +02:00
|
|
|
//
|
|
|
|
// SearchViewController+Searching.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/2.
|
|
|
|
//
|
|
|
|
|
2021-04-07 13:49:33 +02:00
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-04-02 10:24:00 +02:00
|
|
|
import Foundation
|
2021-04-06 09:25:04 +02:00
|
|
|
import MastodonSDK
|
2021-04-07 13:49:33 +02:00
|
|
|
import OSLog
|
2021-04-02 10:24:00 +02:00
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension SearchViewController {
|
|
|
|
func setupSearchingTableView() {
|
|
|
|
searchingTableView.delegate = self
|
|
|
|
searchingTableView.register(SearchingTableViewCell.self, forCellReuseIdentifier: String(describing: SearchingTableViewCell.self))
|
2021-04-16 07:45:54 +02:00
|
|
|
searchingTableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
|
2021-04-02 10:24:00 +02:00
|
|
|
view.addSubview(searchingTableView)
|
|
|
|
searchingTableView.constrain([
|
2021-04-12 06:49:01 +02:00
|
|
|
searchingTableView.frameLayoutGuide.topAnchor.constraint(equalTo: searchBar.bottomAnchor),
|
2021-04-02 10:24:00 +02:00
|
|
|
searchingTableView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
|
|
|
|
searchingTableView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
|
|
|
|
searchingTableView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
|
2021-04-07 13:49:33 +02:00
|
|
|
searchingTableView.contentLayoutGuide.widthAnchor.constraint(equalTo: view.widthAnchor)
|
2021-04-02 10:24:00 +02:00
|
|
|
])
|
2021-04-06 09:25:04 +02:00
|
|
|
searchingTableView.tableFooterView = UIView()
|
2021-04-02 10:24:00 +02:00
|
|
|
viewModel.isSearching
|
|
|
|
.receive(on: DispatchQueue.main)
|
2021-04-06 09:25:04 +02:00
|
|
|
.sink { [weak self] isSearching in
|
2021-04-02 10:24:00 +02:00
|
|
|
self?.searchingTableView.isHidden = !isSearching
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-07 13:49:33 +02:00
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
viewModel.isSearching,
|
|
|
|
viewModel.searchText
|
|
|
|
)
|
|
|
|
.sink { [weak self] isSearching, text in
|
|
|
|
guard let self = self else { return }
|
|
|
|
if isSearching, text.isEmpty {
|
|
|
|
self.searchingTableView.tableHeaderView = self.searchHeader
|
|
|
|
} else {
|
|
|
|
self.searchingTableView.tableHeaderView = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupSearchHeader() {
|
|
|
|
searchHeader.addSubview(recentSearchesLabel)
|
|
|
|
recentSearchesLabel.constrain([
|
|
|
|
recentSearchesLabel.constraint(.leading, toView: searchHeader, constant: 16),
|
|
|
|
recentSearchesLabel.constraint(.centerY, toView: searchHeader)
|
|
|
|
])
|
|
|
|
|
|
|
|
searchHeader.addSubview(clearSearchHistoryButton)
|
|
|
|
recentSearchesLabel.constrain([
|
|
|
|
searchHeader.trailingAnchor.constraint(equalTo: clearSearchHistoryButton.trailingAnchor, constant: 16),
|
|
|
|
clearSearchHistoryButton.constraint(.centerY, toView: searchHeader)
|
|
|
|
])
|
|
|
|
|
|
|
|
clearSearchHistoryButton.addTarget(self, action: #selector(SearchViewController.clearAction(_:)), for: .touchUpInside)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchViewController {
|
|
|
|
@objc func clearAction(_ sender: UIButton) {
|
|
|
|
viewModel.deleteSearchHistory()
|
2021-04-02 10:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
|
|
|
|
extension SearchViewController: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
66
|
|
|
|
}
|
2021-04-06 09:25:04 +02:00
|
|
|
|
2021-04-02 10:24:00 +02:00
|
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
66
|
|
|
|
}
|
2021-04-06 09:25:04 +02:00
|
|
|
|
2021-04-07 13:49:33 +02:00
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
guard let diffableDataSource = viewModel.searchResultDiffableDataSource else { return }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
2021-04-08 06:31:48 +02:00
|
|
|
viewModel.searchResultItemDidSelected(item: item, from: self)
|
2021-04-07 13:49:33 +02:00
|
|
|
}
|
2021-04-02 10:24:00 +02:00
|
|
|
}
|