How to split a stream correctly into 4 Actions using RxAndroid?

173 Views Asked by At

I trying to get into reactive programing for the first time. I'm using the ReactiveLocation library for Android, And I have 4 textviews that need to be populated with location data when a new location is received.

So this is the code that will get me a location on each location update:

    locationProvider = new ReactiveLocationProvider(getApplicationContext());
    lastKnownLocationObservable = locationProvider.getLastKnownLocation();

    final LocationRequest locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setNumUpdates(150)
            .setInterval(100);
    locationUpdatesObservable = locationProvider
            .checkLocationSettings(
                    new LocationSettingsRequest.Builder()
                            .addLocationRequest(locationRequest)
                            .setAlwaysShow(true)  //Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
                            .build()
            )
            .doOnNext(new Action1<LocationSettingsResult>() {
                @Override
                public void call(LocationSettingsResult locationSettingsResult) {
                    Status status = locationSettingsResult.getStatus();
                    if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                        try {
                            status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException th) {
                            Log.e("MainActivity", "Error opening settings activity.", th);
                        }
                    }
                }
            })
            .flatMap(new Func1<LocationSettingsResult, Observable<Location>>() {
                @Override
                public Observable<Location> call(LocationSettingsResult locationSettingsResult) {
                    return locationProvider.getUpdatedLocation(locationRequest);
                }
            });

I also have this Func that will split the location into an array of 4 strings:

public class LocationToStringsArrayListFunc implements Func1<Location, ArrayList<String>> {
    @Override
    public ArrayList<String> call(Location location) {
        ArrayList<String> locationDetails = null;
        if (location != null) {
            locationDetails = new ArrayList<>();
            locationDetails.add(String.valueOf(location.getLatitude()));
            locationDetails.add(String.valueOf(location.getLongitude()));
            locationDetails.add(String.valueOf(location.getTime()));
            locationDetails.add(String.valueOf(location.getSpeed()));
        }
        return locationDetails;
    }
}

Finaly I have this Action to pupulate the TextView with text:

public class DisplayTextOnViewAction implements Action1<String> {
   private final TextView target;

    public DisplayTextOnViewAction(TextView target) {
        this.target = target;
    }

    @Override
    public void call(String s) {
        target.setText(s);
    }
}

What I want now is to split this stream each time to 4 views, I know that for one I will do the following:

 mUpdatableLocationSubscription = locationUpdatesObservable 
            .map(new LocationToStringsArrayListFunc())
            .map(new Func1<ArrayList<String>, String>() {

                @Override
                public String call(ArrayList<String> stringArrayList) {
                    return stringArrayList.get(0);
                }
            })
            .subscribe(new DisplayTextOnViewAction(tvLatitudeValue), new ErrorHandler());

How to populate the other 3 TextView's: LongitudeValue, tvLastUpdateValue, tvSpeedValue using the same stream?

1

There are 1 best solutions below

0
On BEST ANSWER

.share() operator is what you should be looking at.

With this "helper" class :

public class ListItemFunc extends Func1<ArrayList<String>, String>{

    private final int index;

    public ListItemFunc(int index) {
        this.index = index;
    }

    @Override
    public String call(ArrayList<String> strings) {
        return strings.get(index);
    }
}

Your code can look like this:

Observable<ArrayList<String>> sharedObservable =
        locationUpdatesObservable.map(new LocationToStringsArrayListFunc()).share();

latitudeSubscription = sharedObservable
        .map(new ListItemFunc(0))
        .subscribe(new DisplayTextOnViewAction(tvLatitudeValue), new ErrorHandler());

longitudeSubscription = sharedObservable
        .map(new ListItemFunc(1))
        .subscribe(new DisplayTextOnViewAction(tvLongitudeValue), new ErrorHandler());

lastUpdateSubscription = sharedObservable
        .map(new ListItemFunc(2))
        .subscribe(new DisplayTextOnViewAction(tvLastUpdateValue), new ErrorHandler());

speedSubscription = sharedObservable
        .map(new ListItemFunc(3))
        .subscribe(new DisplayTextOnViewAction(tvSpeedValue), new ErrorHandler());