mastodon-ios/Mastodon/Scene/Search/SearchViewController+Search...

92 lines
3.5 KiB
Swift
Raw Normal View History

//
// 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
import Foundation
2021-04-06 09:25:04 +02:00
import MastodonSDK
2021-04-07 13:49:33 +02:00
import OSLog
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))
view.addSubview(searchingTableView)
searchingTableView.constrain([
searchingTableView.frameLayoutGuide.topAnchor.constraint(equalTo: searchBar.bottomAnchor),
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-06 09:25:04 +02:00
searchingTableView.tableFooterView = UIView()
viewModel.isSearching
.receive(on: DispatchQueue.main)
2021-04-06 09:25:04 +02:00
.sink { [weak self] isSearching in
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()
}
}
// MARK: - UITableViewDelegate
extension SearchViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
66
}
2021-04-06 09:25:04 +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 }
viewModel.searchResultItemDidSelected(item: item, from: self)
2021-04-07 13:49:33 +02:00
}
}