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));
}
}));
}
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();
}
}));
}
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();
}
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);
}
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);
}
Aggregations