//для теста в onCreate activity..
val obs: Observable<Response> = RxEditText.fromView(et)
.debounce(300,TimeUnit.MILLISECONDS)
.distinctUntilChanged()
.switchMap { t -> presenter.test(t) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
obs.subscribe ({ t -> tv.text = t.toString() }, {tv.text = it.message})
//presenter.test
fun test(s: String): Observable<Response> {
return retrofit.create(NetworkInterface::class.java)
.query(ApiKey, s)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
//RxEditText
class RxEditText {
companion object {
fun fromView(editText: EditText): Observable<String> {
val subject: PublishSubject<String> = PublishSubject.create()
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
subject.onNext(s.toString())
}
})
return subject
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.