Search in sources :

Example 6 with Func2

use of rx.functions.Func2 in project AnDevCon-RxPatterns by colintheshots.

the class Example7 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example7);
    mTextView = (TextView) findViewById(R.id.example7_textView);
    mTextView2 = (TextView) findViewById(R.id.example7_textView2);
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
    mWeatherInterface = new Retrofit.Builder().baseUrl("http://api.openweathermap.org").client(client).addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())).addConverterFactory(GsonConverterFactory.create(gson)).build().create(WeatherInterface.class);
    // Since we're returning a basic type from Observable.zip()
    // instead of an observable of a type, we can simply map to
    // the new type. In fact, the cast() operator is probably
    // good enough here.
    Observable.zip(mIntegerObservable1, mIntegerObservable2, new Func2<Integer, Integer, Integer>() {

        @Override
        public Integer call(Integer integer, Integer integer2) {
            return integer + integer2;
        }
    }).map(new Func1<Integer, String>() {

        @Override
        public String call(Integer integer) {
            return Integer.toString(integer);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            mTextView.setText(s);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    Observable.zip(mObservable1, mObservable2, new Func2<String, String, Observable<CityWeather>>() {

        @Override
        public Observable<CityWeather> call(String lat, String lon) {
            return mWeatherInterface.getWeather(lat, lon);
        }
    }).flatMap(new Func1<Observable<CityWeather>, Observable<CityWeather>>() {

        @Override
        public Observable<CityWeather> call(Observable<CityWeather> cityWeatherObservable) {
            return cityWeatherObservable;
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<CityWeather>() {

        @Override
        public void call(CityWeather cityWeather) {
            mTextView2.setText("City: " + cityWeather.name + "\nTemp: " + Float.toString(cityWeather.main.temp) + " Kelvins");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Observable(rx.Observable) Retrofit(retrofit2.Retrofit) Func1(rx.functions.Func1) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) 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