Skip to content

Instantly share code, notes, and snippets.

@miroslavign
Created January 17, 2017 07:59
Show Gist options
  • Save miroslavign/945152346288d4dc62ccddb1743ff6ae to your computer and use it in GitHub Desktop.
Save miroslavign/945152346288d4dc62ccddb1743ff6ae to your computer and use it in GitHub Desktop.
private void setUpSearchQuerySubscription2(){
Subscription searchQuerySubscription = searchQueryChangeObservable
.doOnNext(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
searchProgressBar.setVisibility(View.INVISIBLE);
}
})
.debounce(400, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.filter(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
if(TextUtils.isEmpty(charSequence)){
searchProgressBar.setVisibility(View.INVISIBLE);
searchMoviesAdapter.clear();
moviesLinearLayout.setVisibility(View.GONE);
searchTelevisionShowsAdapter.clear();
televisionShowsLinearLayout.setVisibility(View.GONE);
searchPersonsAdapter.clear();
personsLinearLayout.setVisibility(View.GONE);
emptyLinearLayout.setVisibility(View.GONE);
} else {
searchProgressBar.setVisibility(View.VISIBLE);
}
return !TextUtils.isEmpty(charSequence);
}
})
.observeOn(Schedulers.io())
.map(new Func1<CharSequence, String>() {
@Override
public String call(CharSequence charSequence) {
return charSequence.toString();
}
})
.switchMap(new Func1<String, Observable<FullSearchResponse>>() {
@Override
public Observable<FullSearchResponse> call(String q) {
query = q;
return Observable.combineLatest(movieHubService.searchMovies(query, 1),
movieHubService.searchTelevisionShows(query, 1),
movieHubService.searchPeople(query, 1),
new Func3<MoviesEnvelope, TelevisionShowsEnvelope, PeopleEnvelope, FullSearchResponse>() {
@Override
public FullSearchResponse call(MoviesEnvelope moviesEnvelope, TelevisionShowsEnvelope televisionShowsEnvelope, PeopleEnvelope peopleEnvelope) {
return new FullSearchResponse(moviesEnvelope, televisionShowsEnvelope, peopleEnvelope);
}
});
}
})
.observeOn(AndroidSchedulers.mainThread()) // UI Thread
.subscribe(new Subscriber<FullSearchResponse>() {
@Override
public void onCompleted() {
Timber.d("onCompleted()");
}
@Override
public void onError(Throwable t) {
searchProgressBar.setVisibility(View.INVISIBLE);
t.printStackTrace();
if (NetworkUtility.isKnownException(t)) {
Snackbar snackbar = Snackbar.make(ButterKnife.findById(getActivity(), R.id.main_content),
TrestleUtility.getFormattedText("Network connection is unavailable.", font, 16),
Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.grey_200));
TextView textView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(ContextCompat.getColor(getContext(), R.color.secondary_text_dark));
textView.setTypeface(font);
snackbar.show();
}
}
@Override
public void onNext(FullSearchResponse fullSearchResponse) {
searchProgressBar.setVisibility(View.INVISIBLE);
if (fullSearchResponse != null) {
setUpMovies(fullSearchResponse.getMoviesEnvelope());
setUpTelevisionShows(fullSearchResponse.getTelevisionShowsEnvelope());
setUpPeople(fullSearchResponse.getPeopleEnvelope());
if(fullSearchResponse.hasResults()){
emptyLinearLayout.setVisibility(View.GONE);
} else {
emptyTextView.setText(String.format("No results found for \"%s\"", query));
emptyLinearLayout.setVisibility(View.VISIBLE);
}
}
}
});
compositeSubscription.add(searchQuerySubscription);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment