Skip to content

Instantly share code, notes, and snippets.

@jimmythai
Created July 5, 2018 09:09
Show Gist options
  • Select an option

  • Save jimmythai/972bdfc48fdbe4024c33cd240086bf7b to your computer and use it in GitHub Desktop.

Select an option

Save jimmythai/972bdfc48fdbe4024c33cd240086bf7b to your computer and use it in GitHub Desktop.
import UIKit
import RxSwift
import RxCocoa
extension Reactive where Base: UISearchController {
var searchResultUpdater: DelegateProxy<UISearchController, UISearchResultsUpdating> {
return RxSearchResultUpdatingProxy.proxy(for: base)
}
var queryText: Observable<String> {
return Observable
.merge(Observable.just(""), RxSearchResultUpdatingProxy.proxy(for: base).didUpdateSearchResultsSubject.map { $0.searchBar.text ?? "" })
.distinctUntilChanged()
}
}
class RxSearchResultUpdatingProxy: DelegateProxy<UISearchController, UISearchResultsUpdating>, DelegateProxyType, UISearchResultsUpdating {
static func currentDelegate(for object: UISearchController) -> UISearchResultsUpdating? {
return object.searchResultsUpdater
}
static func setCurrentDelegate(_ delegate: UISearchResultsUpdating?, to object: UISearchController) {
object.searchResultsUpdater = delegate
}
static func registerKnownImplementations() {
register { RxSearchResultUpdatingProxy(searchController: $0) }
}
init(searchController: UISearchController) {
super.init(parentObject: searchController, delegateProxy: RxSearchResultUpdatingProxy.self)
}
let didUpdateSearchResultsSubject = PublishSubject<UISearchController>()
func updateSearchResults(for searchController: UISearchController) {
_forwardToDelegate?.updateSearchResults(for: searchController)
didUpdateSearchResultsSubject.onNext(searchController)
}
deinit {
didUpdateSearchResultsSubject.onCompleted()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment