Search Hosts + search animations + reload table after filter manipulations

This commit is contained in:
relikd
2020-06-05 14:27:41 +02:00
parent 80829ad015
commit 23eab2310f
8 changed files with 205 additions and 73 deletions

View File

@@ -4,22 +4,11 @@ class TVCDomains: UITableViewController, UISearchBarDelegate, FilterPipelineDele
lazy var source = GroupedDomainDataSource(withDelegate: self, parent: nil)
private var searchActive: Bool = false
private var searchTerm: String?
private let searchBar: UISearchBar = {
let x = UISearchBar(frame: CGRect.init(x: 0, y: 0, width: 20, height: 10))
x.sizeToFit()
x.showsCancelButton = true
x.autocapitalizationType = .none
x.autocorrectionType = .no
return x
}()
@IBOutlet private var filterButton: UIBarButtonItem!
@IBOutlet private var filterButtonDetail: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
NotifyDateFilterChanged.observe(call: #selector(didChangeDateFilter), on: self)
didChangeDateFilter()
}
@@ -39,62 +28,10 @@ class TVCDomains: UITableViewController, UISearchBarDelegate, FilterPipelineDele
}
// MARK: - Table View Data Source
override func tableView(_ _: UITableView, numberOfRowsInSection _: Int) -> Int { source.numberOfRows }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DomainCell")!
let entry = source[indexPath.row]
cell.textLabel?.text = entry.domain
cell.detailTextLabel?.text = entry.detailCellText
cell.imageView?.image = entry.options?.tableRowImage()
return cell
}
func rowNeedsUpdate(_ row: Int) {
let entry = source[row]
let cell = tableView.cellForRow(at: IndexPath(row: row))
cell?.detailTextLabel?.text = entry.detailCellText
cell?.imageView?.image = entry.options?.tableRowImage()
}
// MARK: - Search
@IBAction private func searchButtonTapped(_ sender: UIBarButtonItem) {
setSearch(hidden: searchActive)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
setSearch(hidden: true)
}
private func setSearch(hidden: Bool) {
searchActive = !hidden
searchTerm = nil
searchBar.text = nil
tableView.tableHeaderView = hidden ? nil : searchBar
if searchActive {
source.pipeline.addFilter("search") {
$0.domain.lowercased().contains(self.searchTerm ?? "")
}
searchBar.becomeFirstResponder()
} else {
source.pipeline.removeFilter(withId: "search")
}
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(performSearch), object: nil)
perform(#selector(performSearch), with: nil, afterDelay: 0.2)
}
@objc private func performSearch() {
searchTerm = searchBar.text?.lowercased() ?? ""
source.pipeline.reloadFilter(withId: "search")
tableView.reloadData()
source.toggleSearch()
}
@@ -124,4 +61,25 @@ class TVCDomains: UITableViewController, UISearchBarDelegate, FilterPipelineDele
self.filterButton.image = UIImage(named: "filter-clear")
}
}
// MARK: - Table View Data Source
override func tableView(_ _: UITableView, numberOfRowsInSection _: Int) -> Int { source.numberOfRows }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DomainCell")!
let entry = source[indexPath.row]
cell.textLabel?.text = entry.domain
cell.detailTextLabel?.text = entry.detailCellText
cell.imageView?.image = entry.options?.tableRowImage()
return cell
}
func rowNeedsUpdate(_ row: Int) {
let entry = source[row]
let cell = tableView.cellForRow(at: IndexPath(row: row))
cell?.detailTextLabel?.text = entry.detailCellText
cell?.imageView?.image = entry.options?.tableRowImage()
}
}

View File

@@ -20,6 +20,12 @@ class TVCHosts: UITableViewController, FilterPipelineDelegate {
}
}
// MARK: - Search
@IBAction private func searchButtonTapped(_ sender: UIBarButtonItem) {
source.toggleSearch()
}
// MARK: - Table View Data Source
override func tableView(_ _: UITableView, numberOfRowsInSection _: Int) -> Int { source.numberOfRows }