Skip to content

Instantly share code, notes, and snippets.

@aturan23
Last active August 13, 2018 04:12
Show Gist options
  • Save aturan23/83e53164791457458c6f2aabea80d50d to your computer and use it in GitHub Desktop.
Save aturan23/83e53164791457458c6f2aabea80d50d to your computer and use it in GitHub Desktop.
Eureka custom PushRow
import Eureka
open class _NetworkSearchSelectorViewController<Row: SelectableRowType, OptionsRow: OptionsProviderRow>: SelectorViewController<OptionsRow>, UISearchResultsUpdating where Row.Cell.Value: SearchItem {
let searchController = UISearchController(searchResultsController: nil)
var originalOptions = [ListCheckRow<Row.Cell.Value>]()
var currentOptions = [ListCheckRow<Row.Cell.Value>]()
open override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}
}
public func updateSearchResults(for searchController: UISearchController) {
guard let query = searchController.searchBar.text else { return }
print(query)
if query.isEmpty {
currentOptions = originalOptions
tableView.reloadData()
} else {
if query.count >= 3 {
ClientService.getClientsByRowSearch(searchText: query).done { (clients) in
self.currentOptions = []
clients.clients.forEach({ (client) in
self.currentOptions.append(self.getCheckRowBy(client: client))
})
DispatchQueue.main.async {
self.tableView.reloadData()
}
}.catch { (e) in
print(e)
}
} else {
currentOptions = originalOptions
tableView.reloadData()
}
}
}
func getCheckRowBy(client: Client) -> ListCheckRow<Row.Cell.Value> {
let newCheckRow = ListCheckRow<Row.Cell.Value>()
newCheckRow.baseCell.baseRow.title = client.clientName
newCheckRow.onCellSelection { (ch1, ch2) in
ch1.setup()
}
return newCheckRow
}
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()
tableView.deselectRow(at: indexPath, animated: true)
}
open override func setupForm(with options: [OptionsRow.OptionsProviderType.Option]) {
super.setupForm(with: options)
if let allRows = form.first?.map({ $0 }) as? [ListCheckRow<Row.Cell.Value>] {
originalOptions = allRows
currentOptions = originalOptions
}
tableView.reloadData()
}
}
open class NetworkSearchSelectorViewController<OptionsRow: OptionsProviderRow>: _NetworkSearchSelectorViewController<ListCheckRow<OptionsRow.OptionsProviderType.Option>, OptionsRow> where OptionsRow.OptionsProviderType.Option: SearchItem {
}
open class _ClientSearchPushRow<Cell: CellType> : SelectorRow<Cell> where Cell: BaseCell, Cell.Value : SearchItem {
public required init(tag: String?) {
super.init(tag: tag)
presentationMode = .show(controllerProvider: ControllerProvider.callback { return NetworkSearchSelectorViewController<SelectorRow<Cell>> { _ in } }, onDismiss: { vc in
let _ = vc.navigationController?.popViewController(animated: true) })
}
}
public final class ClientSearchPushRow<T: Equatable> : _ClientSearchPushRow<PushSelectorCell<T>>, RowType where T: SearchItem {
public required init(tag: String?) {
super.init(tag: tag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment