2016-05-12 6 views
1

私は初めて反応性の高いプログラミングに慣れようとしています。私はReactiveLocationライブラリをAndroid用に使用しています。新しい場所を受け取ったときに位置情報を入力する必要がある4つのテキストビューがあります。RxAndroid:ストリームを4つに正しく分割する方法:

だからこれは私に、各位置更新の場所を取得するコードです:

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

私も4つの文字列の配列に場所を分割します。こののFuncを持っている:

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私はテキストでのTextViewをpupulateするには、このアクションを持っている:私は今、欲しい

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

がにあります4つのビューに、このストリームを毎回分割し、私は1つのために、私は次の操作を実行することを知っている:

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

私の質問は IS:同じストリームを使用してLongitudeValue、tvLastUpdateValue、tvSpeedValue:他の3のTextViewのをpopluateする方法?

ありがとうございます。

答えて

4

.share()オペレータはあなたが目にするべきものです。この "ヘルパー" クラスで

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

あなたのコードは次のようになります。あなたの助けを

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()); 
+0

おかげで、テストされ、うまくいきました。 –

関連する問題