Last active
December 25, 2018 18:58
-
-
Save vaderdan/f568e33460894ae7dd2312db6ab456ad to your computer and use it in GitHub Desktop.
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
let textFull = BehaviorRelay<String?>(value: nil) | |
let textFirst = BehaviorRelay<String?>(value: nil) | |
let textSecond = BehaviorRelay<String?>(value: nil) | |
typealias ItemType = (current: String, previous: String) | |
Observable.combineLatest(textFirst.map({ $0 ?? "" }).currentAndPrevious(), textSecond.map({ $0 ?? "" }).currentAndPrevious(), textFull.map({ $0 ?? "" }).currentAndPrevious()) | |
.filter({ (first: ItemType, second: ItemType, full: ItemType) -> Bool in | |
return "\(first.current) \(second.current)" != full.current && "\(first.current)" != full.current | |
}) | |
.subscribe(onNext: { (first: ItemType, second: ItemType, full: ItemType) in | |
if first.current != first.previous || second.current != second.previous { | |
textFull.accept("\(first.current) \(second.current)") | |
} | |
else if (full.current != full.previous) { | |
let items = full.current.components(separatedBy: " ") | |
let firstName = items.count > 0 ? items[0] : "" | |
let lastName = items.count > 1 ? items[1] : "" | |
if firstName != first.current { | |
textFirst.accept(firstName) | |
} else if lastName != second.current { | |
textSecond.accept(lastName) | |
} | |
} | |
}) | |
.disposed(by: disposeBag) | |
(textFieldFirst.rx.text <-> textFirst).disposed(by: disposeBag) | |
(textFieldSecond.rx.text <-> textSecond).disposed(by: disposeBag) | |
(textFieldFull.rx.text <-> textFull).disposed(by: disposeBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment