Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxSearchResultsUpdatingProxyの私的実装
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).didUpdateSearchResultSubject.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() { self.register { RxSearchResultUpdatingProxy(searchController: $0) } }
init(searchController: UISearchController) {
super.init(parentObject: searchController, delegateProxy: RxSearchResultUpdatingProxy.self)
}
let didUpdateSearchResultSubject = PublishSubject<UISearchController>()
func updateSearchResults(for searchController: UISearchController) {
_forwardToDelegate?.updateSearchResults?(for: searchController)
didUpdateSearchResultSubject.onNext(searchController)
}
deinit { didUpdateSearchResultSubject.onCompleted() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment