-
-
Save Bresiu/08355047f3979c67b6bf to your computer and use it in GitHub Desktop.
as
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
/** | |
* Port from {@link https://github.com/ReactiveX/RxSwift} | |
*/ | |
RxTextView.textChanges(mSearchbar.getEditTextSearch()) | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
//delay 500ms | |
//debounce and throttle will use different thread after | |
.throttleWithTimeout(300, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) | |
.distinct() | |
.filter(new Func1() { | |
@UiThread @Override public Boolean call(CharSequence charSequence) { | |
//void unnecessary request | |
return charSequence.length() != 0; | |
} | |
}) | |
.map(new Func1() { | |
@UiThread @Override public String call(CharSequence charSequence) { | |
//fit network api doc require | |
return charSequence + "*"; | |
} | |
}) | |
.doOnNext(new Action1() { | |
@UiThread @Override public void call(CharSequence charSequence) { | |
progressBar.setVisibility(View.VISIBLE); | |
arrayList.clear(); | |
adapter.notifyDataSetChanged(); | |
} | |
}) | |
.observeOn(Schedulers.io()) | |
.switchMap(new Func1>>() { | |
@WorkerThread @Override public Observable> call(String s) { | |
return repo.getTags(20, s); | |
} | |
}) | |
.retry(new Func2() { | |
//fix InterruptedIOException bugs on Retrofit | |
// when stop old search | |
@WorkerThread @Override public Boolean call(Integer integer, Throwable throwable) { | |
return throwable instanceof InterruptedIOException; | |
} | |
}) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Subscriber>() { | |
@Override public void onCompleted() { | |
} | |
@Override public void onError(Throwable e) { | |
progressBar.setVisibility(View.INVISIBLE); | |
e.printStackTrace(); | |
Toast.makeText(SearchActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override public void onNext(List tags) { | |
progressBar.setVisibility(View.INVISIBLE); | |
arrayList.clear(); | |
arrayList.addAll(tags); | |
adapter.notifyDataSetChanged(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment