Search in sources :

Example 1 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription 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 CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project Android-ReactiveLocation by mcharmas.

the class PlacesResultActivity method onLocationPermissionGranted.

@Override
protected void onLocationPermissionGranted() {
    compositeSubscription = new CompositeSubscription();
    compositeSubscription.add(reactiveLocationProvider.getPlaceById(placeId).subscribe(new Action1<PlaceBuffer>() {

        @Override
        public void call(PlaceBuffer buffer) {
            Place place = buffer.get(0);
            if (place != null) {
                placeNameView.setText(place.getName());
                placeLocationView.setText(place.getLatLng().latitude + ", " + place.getLatLng().longitude);
                placeAddressView.setText(place.getAddress());
            }
            buffer.release();
        }
    }));
}
Also used : Action1(rx.functions.Action1) CompositeSubscription(rx.subscriptions.CompositeSubscription) PlaceBuffer(com.google.android.gms.location.places.PlaceBuffer) Place(com.google.android.gms.location.places.Place)

Example 3 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project realm-rxjava-example by kboyarshinov.

the class MainActivity method onStart.

@Override
protected void onStart() {
    super.onStart();
    compositeSubscription = new CompositeSubscription();
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription)

Example 4 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project realm-rxjava-example by kboyarshinov.

the class MainActivity method requestWithZip.

private void requestWithZip() {
    if (compositeSubscription == null) {
        return;
    }
    Observable<Issue> issues = dataService.issues();
    Subscription subscription = Observable.zip(issues.take(10), issues.takeLast(10), new Func2<Issue, Issue, Pair<Issue, Issue>>() {

        @Override
        public Pair<Issue, Issue> call(Issue issue, Issue issue2) {
            return new Pair<>(issue, issue2);
        }
    }).toList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Pair<Issue, Issue>>>() {

        @Override
        public void call(List<Pair<Issue, Issue>> pairs) {
            Log.d(TAG, "List of issue pairs " + pairs.size());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.e(TAG, "Error requesting issue pairs", throwable);
        }
    });
    compositeSubscription.add(subscription);
}
Also used : Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Pair(android.support.v4.util.Pair)

Example 5 with CompositeSubscription

use of rx.subscriptions.CompositeSubscription in project realm-rxjava-example by kboyarshinov.

the class MainActivity method requestWithFlatMap.

private void requestWithFlatMap() {
    if (compositeSubscription == null) {
        return;
    }
    Subscription subscription = dataService.findUser("kboyarshinov").flatMap(new Func1<User, Observable<List<Issue>>>() {

        @Override
        public Observable<List<Issue>> call(User user) {
            return dataService.issuesListByUser(user);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Issue>>() {

        @Override
        public void call(List<Issue> issues) {
            Log.d(TAG, "Issues by user received with size " + issues.size());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.e(TAG, "Request issues by user error", throwable);
        }
    });
    compositeSubscription.add(subscription);
}
Also used : User(com.kboyarshinov.realmrxjavaexample.model.User) Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Func1(rx.functions.Func1)

Aggregations

CompositeSubscription (rx.subscriptions.CompositeSubscription)49 ArrayList (java.util.ArrayList)9 View (android.view.View)7 Subscription (rx.Subscription)7 Context (android.content.Context)6 Bundle (android.os.Bundle)6 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)6 Intent (android.content.Intent)5 AppCompatActivity (android.support.v7.app.AppCompatActivity)5 RecyclerView (android.support.v7.widget.RecyclerView)5 ViewGroup (android.view.ViewGroup)5 List (java.util.List)5 Schedulers (rx.schedulers.Schedulers)5 MenuItem (android.view.MenuItem)4 Issue (com.kboyarshinov.realmrxjavaexample.model.Issue)4 ActionBar (android.support.v7.app.ActionBar)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 Menu (android.view.Menu)3 TextView (android.widget.TextView)3 TwitterStringUtils (com.github.moko256.twicalico.text.TwitterStringUtils)3