Skip to content

Instantly share code, notes, and snippets.

@noefroidevaux
Created September 7, 2017 13:27
Show Gist options
  • Save noefroidevaux/d033e4fb4382f0dcfc877fa4e6ba95a2 to your computer and use it in GitHub Desktop.
Save noefroidevaux/d033e4fb4382f0dcfc877fa4e6ba95a2 to your computer and use it in GitHub Desktop.
SearchPushRow for Eureka 3.0 (Swift 3.1)
// Add SearchItem protocol to the model (here Project)
extension Project: SearchItem {
func matchesSearchQuery(_ query: String) -> Bool {
return name.lowercased().contains(query.lowercased())
}
}
import Eureka
// swiftlint:disable type_name
open class _SearchSelectorViewController<T: Equatable, Row: SelectableRowType>: _SelectorViewController<Row>, UISearchResultsUpdating where Row: BaseRow, Row: TypedRowType, Row.Cell.Value == T, T: SearchItem {
let searchController = UISearchController(searchResultsController: nil)
var originalOptions = [ListCheckRow<T>]()
var currentOptions = [ListCheckRow<T>]()
open override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
if let allRows = form.first?.map({ $0 }) as? [ListCheckRow<T>] {
originalOptions = allRows
currentOptions = originalOptions
}
tableView.tableHeaderView = searchController.searchBar
}
public func updateSearchResults(for searchController: UISearchController) {
guard let query = searchController.searchBar.text else { return }
if query.isEmpty {
currentOptions = originalOptions
} else {
currentOptions = originalOptions.filter { $0.selectableValue?.matchesSearchQuery(query) ?? false }
}
tableView.reloadData()
}
open override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentOptions.count
}
open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let option = currentOptions[indexPath.row]
option.updateCell()
return option.baseCell
}
open override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return nil
}
open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentOptions[indexPath.row].didSelect()
}
}
// swiftlint:enable type_name
open class SearchSelectorViewController<T: Equatable>: _SearchSelectorViewController<T, ListCheckRow<T>> where T: SearchItem {}
// swiftlint:disable type_name
open class _SearchPushRow<Cell: CellType> : SelectorRow<Cell, SearchSelectorViewController<Cell.Value>> where Cell: BaseCell, Cell.Value: SearchItem {
public required init(tag: String?) {
super.init(tag: tag)
presentationMode = .show(controllerProvider: ControllerProvider.callback { return SearchSelectorViewController<Cell.Value> { _ in } }, onDismiss: { vc in
_ = vc.navigationController?.popViewController(animated: true) })
}
}
// swiftlint:enable type_name
public final class SearchPushRow<T: Equatable> : _SearchPushRow<PushSelectorCell<T>>, RowType where T: SearchItem {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public protocol SearchItem {
func matchesSearchQuery(_ query: String) -> Bool
}
@noefroidevaux
Copy link
Author

Simply add the SearchPushRow.swift file to your project, add SearchItem protocol to your model and use SearchPushRow instead of PushRow in your code.

@guillianbalisi
Copy link

Do you have an update for this with Eureka 4.0.1? Getting a lot of errors:

screen shot 2017-10-24 at 4 22 59 pm

@allaire
Copy link

allaire commented Nov 14, 2017

@noefroidevaux did you upgrade to Eureka 4 ?

@gotelgest
Copy link

gotelgest commented Nov 23, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment