use of rx.Observer in project wh-app-android by WhiteHouse.
the class SearchFragment method submitQuery.
public void submitQuery(String query) {
showList(false);
showProgress(true);
if (mSearchSub != null) {
mSearchSub.unsubscribe();
}
if (!NetworkUtils.checkNetworkAvailable(getActivity())) {
showProgress(false);
Toast.makeText(getActivity(), R.string.no_network, Toast.LENGTH_SHORT).show();
return;
}
mSearchSub = AndroidObservable.bindFragment(this, SearchManager.get().search(query).first().map(searchResults -> {
List<SearchResult> list = new ArrayList<>();
SearchResult result;
if (searchResults.boosted_results() != null) {
for (BoostedSearchResult b : searchResults.boosted_results()) {
result = SearchResult.create(b.description(), b.title(), b.url());
list.add(result);
}
}
if (searchResults.results() != null) {
list.addAll(searchResults.results());
}
return list;
}).subscribeOn(Schedulers.newThread())).subscribe(new Observer<List<SearchResult>>() {
@Override
public void onCompleted() {
showProgress(false);
showList(true);
}
@Override
public void onError(Throwable e) {
Toast.makeText(getActivity(), "An error occurred while trying to search", Toast.LENGTH_SHORT).show();
Timber.w(e, "Error searching for query '%s'", query);
}
@Override
public void onNext(List<SearchResult> searchResults) {
getAdapter().clear();
if (searchResults != null) {
getAdapter().addAll(searchResults);
}
}
});
bindSubscription(mSearchSub);
if (getTracker() != null) {
getTracker().set("searchQuery", query);
}
}
use of rx.Observer in project scalecube by scalecube.
the class RemoteServiceInstance method listen.
@Override
public Observable<Message> listen(final Message request) {
final String cid = request.correlationId();
AtomicReference<Subscription> subscription = new AtomicReference<>();
Observable<Message> observer = transportObservable.doOnUnsubscribe(() -> {
Message unsubscribeRequest = Messages.asUnsubscribeRequest(cid);
LOGGER.info("sending remote unsubscribed event: {}", unsubscribeRequest);
subscription.get().unsubscribe();
sendRemote(unsubscribeRequest);
});
Subscription sub = observer.filter(message -> message.correlationId().equals(cid)).subscribe(onNext -> {
serviceResponses.onNext(onNext);
});
subscription.set(sub);
sendRemote(request);
return serviceResponses;
}
use of rx.Observer in project scalecube by scalecube.
the class DefaultStreamProcessor method listen.
@Override
public Observable<StreamMessage> listen() {
return Observable.create(emitter -> {
CompositeSubscription subscriptions = new CompositeSubscription();
emitter.setCancellation(subscriptions::clear);
// message logic: remote read => onMessage
subscriptions.add(channelContext.listenReadSuccess().map(Event::getMessageOrThrow).subscribe(message -> onMessage(message, emitter)));
// error logic: failed remote write => observer error
subscriptions.add(channelContext.listenWriteError().map(Event::getErrorOrThrow).subscribe(emitter::onError));
// connection logic: connection lost => observer error
subscriptions.add(eventStream.listenChannelContextClosed().filter(event -> event.getAddress().equals(channelContext.getAddress())).map(event -> new IOException("ChannelContext closed on address: " + event.getAddress())).subscribe(emitter::onError));
}, Emitter.BackpressureMode.BUFFER);
}
Aggregations