Search in sources :

Example 1 with Observable

use of rx.Observable in project Android-ReactiveLocation by mcharmas.

the class PlacesActivity method onLocationPermissionGranted.

@Override
protected void onLocationPermissionGranted() {
    compositeSubscription = new CompositeSubscription();
    compositeSubscription.add(reactiveLocationProvider.getCurrentPlace(null).subscribe(new Action1<PlaceLikelihoodBuffer>() {

        @Override
        public void call(PlaceLikelihoodBuffer buffer) {
            PlaceLikelihood likelihood = buffer.get(0);
            if (likelihood != null) {
                currentPlaceView.setText(likelihood.getPlace().getName());
            }
            buffer.release();
        }
    }));
    Observable<String> queryObservable = RxTextView.textChanges(queryView).map(new Func1<CharSequence, String>() {

        @Override
        public String call(CharSequence charSequence) {
            return charSequence.toString();
        }
    }).debounce(1, TimeUnit.SECONDS).filter(new Func1<String, Boolean>() {

        @Override
        public Boolean call(String s) {
            return !TextUtils.isEmpty(s);
        }
    });
    Observable<Location> lastKnownLocationObservable = reactiveLocationProvider.getLastKnownLocation();
    Observable<AutocompletePredictionBuffer> suggestionsObservable = Observable.combineLatest(queryObservable, lastKnownLocationObservable, new Func2<String, Location, QueryWithCurrentLocation>() {

        @Override
        public QueryWithCurrentLocation call(String query, Location currentLocation) {
            return new QueryWithCurrentLocation(query, currentLocation);
        }
    }).flatMap(new Func1<QueryWithCurrentLocation, Observable<AutocompletePredictionBuffer>>() {

        @Override
        public Observable<AutocompletePredictionBuffer> call(QueryWithCurrentLocation q) {
            if (q.location == null)
                return Observable.empty();
            double latitude = q.location.getLatitude();
            double longitude = q.location.getLongitude();
            LatLngBounds bounds = new LatLngBounds(new LatLng(latitude - 0.05, longitude - 0.05), new LatLng(latitude + 0.05, longitude + 0.05));
            return reactiveLocationProvider.getPlaceAutocompletePredictions(q.query, bounds, null);
        }
    });
    compositeSubscription.add(suggestionsObservable.subscribe(new Action1<AutocompletePredictionBuffer>() {

        @Override
        public void call(AutocompletePredictionBuffer buffer) {
            List<AutocompleteInfo> infos = new ArrayList<>();
            for (AutocompletePrediction prediction : buffer) {
                infos.add(new AutocompleteInfo(prediction.getFullText(null).toString(), prediction.getPlaceId()));
            }
            buffer.release();
            placeSuggestionsList.setAdapter(new ArrayAdapter<>(PlacesActivity.this, android.R.layout.simple_list_item_1, infos));
        }
    }));
}
Also used : PlaceLikelihoodBuffer(com.google.android.gms.location.places.PlaceLikelihoodBuffer) AutocompletePredictionBuffer(com.google.android.gms.location.places.AutocompletePredictionBuffer) ArrayList(java.util.ArrayList) LatLng(com.google.android.gms.maps.model.LatLng) Func2(rx.functions.Func2) Action1(rx.functions.Action1) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Observable(rx.Observable) CompositeSubscription(rx.subscriptions.CompositeSubscription) AutocompletePrediction(com.google.android.gms.location.places.AutocompletePrediction) PlaceLikelihood(com.google.android.gms.location.places.PlaceLikelihood) Location(android.location.Location)

Example 2 with Observable

use of rx.Observable in project jersey by jersey.

the class ObservableAgentResource method observable.

@GET
public void observable(@Suspended final AsyncResponse async) {
    final long time = System.nanoTime();
    final Queue<String> errors = new ConcurrentLinkedQueue<>();
    Observable.just(new AgentResponse()).zipWith(visited(errors), (response, visited) -> {
        response.setVisited(visited);
        return response;
    }).zipWith(recommended(errors), (response, recommendations) -> {
        response.setRecommended(recommendations);
        return response;
    }).observeOn(Schedulers.io()).subscribe(response -> {
        response.setProcessingTime((System.nanoTime() - time) / 1000000);
        async.resume(response);
    }, async::resume);
}
Also used : Produces(javax.ws.rs.Produces) RxObservableInvokerProvider(org.glassfish.jersey.client.rx.rxjava.RxObservableInvokerProvider) GET(javax.ws.rs.GET) AsyncResponse(javax.ws.rs.container.AsyncResponse) RxObservableInvoker(org.glassfish.jersey.client.rx.rxjava.RxObservableInvoker) Path(javax.ws.rs.Path) Singleton(javax.inject.Singleton) Suspended(javax.ws.rs.container.Suspended) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) Uri(org.glassfish.jersey.server.Uri) Destination(org.glassfish.jersey.examples.rx.domain.Destination) Observable(rx.Observable) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) List(java.util.List) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) Schedulers(rx.schedulers.Schedulers) Queue(java.util.Queue) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) GET(javax.ws.rs.GET)

Example 3 with Observable

use of rx.Observable in project android-oss by kickstarter.

the class ApiExceptionFactory method badRequestException.

@NonNull
public static ApiException badRequestException() {
    final ErrorEnvelope envelope = ErrorEnvelope.builder().errorMessages(Collections.singletonList("bad request")).httpCode(400).build();
    final ResponseBody body = ResponseBody.create(null, "");
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(400, body);
    return new ApiException(envelope, response);
}
Also used : ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(android.support.annotation.NonNull)

Example 4 with Observable

use of rx.Observable in project android-oss by kickstarter.

the class ApiExceptionFactory method tfaRequired.

@NonNull
public static ApiException tfaRequired() {
    final ErrorEnvelope envelope = ErrorEnvelope.builder().ksrCode(ErrorEnvelope.TFA_REQUIRED).httpCode(403).errorMessages(Collections.singletonList("Two-factor authentication required.")).build();
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(envelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(envelope.httpCode(), body);
    return new ApiException(envelope, response);
}
Also used : ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(android.support.annotation.NonNull)

Example 5 with Observable

use of rx.Observable in project android-oss by kickstarter.

the class ApiExceptionFactory method tfaFailed.

@NonNull
public static ApiException tfaFailed() {
    final ErrorEnvelope envelope = ErrorEnvelope.builder().ksrCode(ErrorEnvelope.TFA_FAILED).httpCode(400).build();
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(envelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(envelope.httpCode(), body);
    return new ApiException(envelope, response);
}
Also used : ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(android.support.annotation.NonNull)

Aggregations

Observable (rx.Observable)311 ResponseBody (okhttp3.ResponseBody)119 Response (retrofit2.Response)111 ServiceResponse (com.microsoft.rest.ServiceResponse)108 Test (org.junit.Test)59 ArrayList (java.util.ArrayList)57 List (java.util.List)55 IOException (java.io.IOException)46 Subscription (rx.Subscription)34 Func1 (rx.functions.Func1)31 Map (java.util.Map)26 ByteBuf (io.netty.buffer.ByteBuf)24 TimeUnit (java.util.concurrent.TimeUnit)23 Schedulers (rx.schedulers.Schedulers)22 Collections (java.util.Collections)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 File (java.io.File)17 HashMap (java.util.HashMap)16 Subscriber (rx.Subscriber)15