Skip to content

Instantly share code, notes, and snippets.

@vaderdan
Last active December 25, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaderdan/f568e33460894ae7dd2312db6ab456ad to your computer and use it in GitHub Desktop.
Save vaderdan/f568e33460894ae7dd2312db6ab456ad to your computer and use it in GitHub Desktop.
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