Created
September 7, 2017 13:27
-
-
Save noefroidevaux/d033e4fb4382f0dcfc877fa4e6ba95a2 to your computer and use it in GitHub Desktop.
SearchPushRow for Eureka 3.0 (Swift 3.1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add SearchItem protocol to the model (here Project) | |
extension Project: SearchItem { | |
func matchesSearchQuery(_ query: String) -> Bool { | |
return name.lowercased().contains(query.lowercased()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 did you upgrade to Eureka 4 ?
Updated to Eureka 4.0.1 and Swift 4
https://gist.github.com/gotelgest/cf309f6e2095ff22a20b09ba5c95be36
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have an update for this with Eureka 4.0.1? Getting a lot of errors: