Last active
May 3, 2018 01:28
-
-
Save quesera2/3e9a103212c2b2391c55c074aa15b3aa to your computer and use it in GitHub Desktop.
RxSearchResultsUpdatingProxyの私的実装
This file contains hidden or 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 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