Search in sources :

Example 11 with Observer

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);
    }
}
Also used : BoostedSearchResult(gov.whitehouse.data.model.BoostedSearchResult) Observer(rx.Observer) ArrayList(java.util.ArrayList) BoostedSearchResult(gov.whitehouse.data.model.BoostedSearchResult) SearchResult(gov.whitehouse.data.model.SearchResult) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with Observer

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;
}
Also used : Logger(org.slf4j.Logger) Address(io.scalecube.transport.Address) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Observer(rx.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subject(rx.subjects.Subject) Observable(rx.Observable) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) Message(io.scalecube.transport.Message) Subscription(rx.Subscription) PublishSubject(rx.subjects.PublishSubject) Collections(java.util.Collections) Message(io.scalecube.transport.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(rx.Subscription)

Example 13 with Observer

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);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Emitter(rx.Emitter) Observer(rx.Observer) Observable(rx.Observable) CompositeSubscription(rx.subscriptions.CompositeSubscription) IOException(java.io.IOException)

Aggregations

Observer (rx.Observer)13 Observable (rx.Observable)7 Nullable (android.support.annotation.Nullable)3 TextUtils (android.text.TextUtils)3 Log (android.util.Log)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Subscription (rx.Subscription)3 Activity (android.app.Activity)2 Bundle (android.os.Bundle)2 AppBarLayout (android.support.design.widget.AppBarLayout)2 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)2 TabLayout (android.support.design.widget.TabLayout)2 PagerAdapter (android.support.v4.view.PagerAdapter)2 ViewPager (android.support.v4.view.ViewPager)2 NestedScrollView (android.support.v4.widget.NestedScrollView)2 Toolbar (android.support.v7.widget.Toolbar)2 Html (android.text.Html)2 TypedValue (android.util.TypedValue)2 LayoutInflater (android.view.LayoutInflater)2