Search in sources :

Example 1 with Func2

use of rx.functions.Func2 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 Func2

use of rx.functions.Func2 in project azure-sdk-for-java by Azure.

the class ServicePrincipalImpl method createResourceAsync.

@Override
public Observable<ServicePrincipal> createResourceAsync() {
    ActiveDirectoryApplication application = (ActiveDirectoryApplication) ((Object) super.createdModel(applicationCreatable.key()));
    createParameters.withAppId(application.applicationId());
    Observable<ServicePrincipal> sp = manager.inner().servicePrincipals().createAsync(createParameters).map(innerToFluentMap(this)).flatMap(new Func1<ServicePrincipal, Observable<ServicePrincipal>>() {

        @Override
        public Observable<ServicePrincipal> call(ServicePrincipal servicePrincipal) {
            return refreshCredentialsAsync();
        }
    });
    if (roles == null || roles.isEmpty()) {
        return sp;
    }
    return sp.flatMap(new Func1<ServicePrincipal, Observable<ServicePrincipal>>() {

        @Override
        public Observable<ServicePrincipal> call(final ServicePrincipal servicePrincipal) {
            return Observable.from(roles.entrySet()).flatMap(new Func1<Map.Entry<String, BuiltInRole>, Observable<?>>() {

                @Override
                public Observable<?> call(Map.Entry<String, BuiltInRole> role) {
                    return manager().roleAssignments().define(UUID.randomUUID().toString()).forServicePrincipal(servicePrincipal).withBuiltInRole(role.getValue()).withScope(role.getKey()).createAsync().retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {

                        @Override
                        public Observable<?> call(Observable<? extends Throwable> observable) {
                            return observable.zipWith(Observable.range(1, 30), new Func2<Throwable, Integer, Integer>() {

                                @Override
                                public Integer call(Throwable throwable, Integer integer) {
                                    if (throwable instanceof CloudException && ((CloudException) throwable).body().code().equalsIgnoreCase("PrincipalNotFound")) {
                                        return integer;
                                    } else {
                                        throw Exceptions.propagate(throwable);
                                    }
                                }
                            }).flatMap(new Func1<Integer, Observable<?>>() {

                                @Override
                                public Observable<?> call(Integer i) {
                                    return Observable.timer(i, TimeUnit.SECONDS);
                                }
                            });
                        }
                    });
                }
            }).last().map(new Func1<Object, ServicePrincipal>() {

                @Override
                public ServicePrincipal call(Object o) {
                    return servicePrincipal;
                }
            });
        }
    }).map(new Func1<ServicePrincipal, ServicePrincipal>() {

        @Override
        public ServicePrincipal call(ServicePrincipal servicePrincipal) {
            for (PasswordCredentialImpl<?> passwordCredential : passwordCredentials) {
                passwordCredential.exportAuthFile((ServicePrincipalImpl) servicePrincipal);
            }
            for (CertificateCredentialImpl<?> certificateCredential : certificateCredentials) {
                certificateCredential.exportAuthFile((ServicePrincipalImpl) servicePrincipal);
            }
            return servicePrincipal;
        }
    });
}
Also used : ServicePrincipal(com.microsoft.azure.management.graphrbac.ServicePrincipal) BuiltInRole(com.microsoft.azure.management.graphrbac.BuiltInRole) CloudException(com.microsoft.azure.CloudException) Observable(rx.Observable) ActiveDirectoryApplication(com.microsoft.azure.management.graphrbac.ActiveDirectoryApplication) Func1(rx.functions.Func1) HashMap(java.util.HashMap) Map(java.util.Map) Func2(rx.functions.Func2)

Example 3 with Func2

use of rx.functions.Func2 in project Hystrix by Netflix.

the class HystrixCommandAsyncDemo method observeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment.

public Observable<CreditCardAuthorizationResult> observeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment() {
    /* fetch user object with http cookies */
    try {
        Observable<UserAccount> user = new GetUserAccountCommand(new HttpCookie("mockKey", "mockValueFromHttpRequest")).observe();
        /* fetch the payment information (asynchronously) for the user so the credit card payment can proceed */
        Observable<PaymentInformation> paymentInformation = user.flatMap(new Func1<UserAccount, Observable<PaymentInformation>>() {

            @Override
            public Observable<PaymentInformation> call(UserAccount userAccount) {
                return new GetPaymentInformationCommand(userAccount).observe();
            }
        });
        /* fetch the order we're processing for the user */
        int orderIdFromRequestArgument = 13579;
        final Observable<Order> previouslySavedOrder = new GetOrderCommand(orderIdFromRequestArgument).observe();
        return Observable.zip(paymentInformation, previouslySavedOrder, new Func2<PaymentInformation, Order, Pair<PaymentInformation, Order>>() {

            @Override
            public Pair<PaymentInformation, Order> call(PaymentInformation paymentInformation, Order order) {
                return new Pair<PaymentInformation, Order>(paymentInformation, order);
            }
        }).flatMap(new Func1<Pair<PaymentInformation, Order>, Observable<CreditCardAuthorizationResult>>() {

            @Override
            public Observable<CreditCardAuthorizationResult> call(Pair<PaymentInformation, Order> pair) {
                return new CreditCardCommand(pair.b(), pair.a(), new BigDecimal(123.45)).observe();
            }
        });
    } catch (IllegalArgumentException ex) {
        return Observable.error(ex);
    }
}
Also used : Observable(rx.Observable) BigDecimal(java.math.BigDecimal) HttpCookie(java.net.HttpCookie) Func2(rx.functions.Func2)

Example 4 with Func2

use of rx.functions.Func2 in project realm-java by realm.

the class AnimationActivity method onResume.

@Override
protected void onResume() {
    super.onResume();
    // Load all persons and start inserting them with 1 sec. intervals.
    // All RealmObject access has to be done on the same thread `findAllAsync` was called on.
    // Warning: This example doesn't handle back pressure well.
    subscription = realm.where(Person.class).findAllAsync().asObservable().flatMap(new Func1<RealmResults<Person>, Observable<Person>>() {

        @Override
        public Observable<Person> call(RealmResults<Person> persons) {
            return Observable.from(persons);
        }
    }).zipWith(Observable.interval(1, TimeUnit.SECONDS), new Func2<Person, Long, Person>() {

        @Override
        public Person call(Person person, Long tick) {
            return person;
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Person>() {

        @Override
        public void call(Person person) {
            TextView personView = new TextView(AnimationActivity.this);
            personView.setText(person.getName());
            container.addView(personView);
        }
    });
}
Also used : Action1(rx.functions.Action1) TextView(android.widget.TextView) Person(io.realm.examples.rxjava.model.Person) Observable(rx.Observable) RealmResults(io.realm.RealmResults) Func2(rx.functions.Func2)

Example 5 with Func2

use of rx.functions.Func2 in project sqlbrite by square.

the class ItemsFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    String listId = String.valueOf(getListId());
    subscriptions = new CompositeSubscription();
    Observable<Integer> itemCount = //
    db.createQuery(TodoItem.TABLE, COUNT_QUERY, listId).map(new Func1<Query, Integer>() {

        @Override
        public Integer call(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getInt(0);
            } finally {
                cursor.close();
            }
        }
    });
    Observable<String> listName = db.createQuery(TodoList.TABLE, TITLE_QUERY, listId).map(new Func1<Query, String>() {

        @Override
        public String call(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getString(0);
            } finally {
                cursor.close();
            }
        }
    });
    subscriptions.add(Observable.combineLatest(listName, itemCount, new Func2<String, Integer, String>() {

        @Override
        public String call(String listName, Integer itemCount) {
            return listName + " (" + itemCount + ")";
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String title) {
            getActivity().setTitle(title);
        }
    }));
    subscriptions.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId).mapToList(TodoItem.MAPPER).observeOn(AndroidSchedulers.mainThread()).subscribe(adapter));
}
Also used : Action1(rx.functions.Action1) Query(com.squareup.sqlbrite.SqlBrite.Query) Cursor(android.database.Cursor) CompositeSubscription(rx.subscriptions.CompositeSubscription) Func2(rx.functions.Func2)

Aggregations

Func2 (rx.functions.Func2)6 Observable (rx.Observable)5 Action1 (rx.functions.Action1)3 Func1 (rx.functions.Func1)2 CompositeSubscription (rx.subscriptions.CompositeSubscription)2 Cursor (android.database.Cursor)1 Location (android.location.Location)1 TextView (android.widget.TextView)1 AutocompletePrediction (com.google.android.gms.location.places.AutocompletePrediction)1 AutocompletePredictionBuffer (com.google.android.gms.location.places.AutocompletePredictionBuffer)1 PlaceLikelihood (com.google.android.gms.location.places.PlaceLikelihood)1 PlaceLikelihoodBuffer (com.google.android.gms.location.places.PlaceLikelihoodBuffer)1 LatLng (com.google.android.gms.maps.model.LatLng)1 LatLngBounds (com.google.android.gms.maps.model.LatLngBounds)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 CloudException (com.microsoft.azure.CloudException)1 ActiveDirectoryApplication (com.microsoft.azure.management.graphrbac.ActiveDirectoryApplication)1 BuiltInRole (com.microsoft.azure.management.graphrbac.BuiltInRole)1 ServicePrincipal (com.microsoft.azure.management.graphrbac.ServicePrincipal)1