Created
June 20, 2017 21:57
-
-
Save richy486/847459eb20595c25e3f93c31bca45a6a to your computer and use it in GitHub Desktop.
Rx filter for text fields, doesn't really work with other observers
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
// | |
// UITextField+Rx+Extensions.swift | |
// ddxp-kiosk | |
// | |
// Created by Richard Adem on 6/20/17. | |
// Copyright © 2017 Richard Adem. All rights reserved. | |
// | |
import RxSwift | |
import RxCocoa | |
extension Reactive where Base: UITextField { | |
func filterInput(withMapFilter mapFilter: @escaping (String?) -> String?) -> Disposable { | |
let textValue = Variable<String?>("") | |
let bindToUIDisposable = textValue.asObservable() | |
.map(mapFilter) | |
.bind(to: self.text) | |
let bindToVariable = self.text | |
.subscribe(onNext: { str in | |
textValue.value = str | |
}, onCompleted: { | |
bindToUIDisposable.dispose() | |
}) | |
return Disposables.create(bindToUIDisposable, bindToVariable) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment