|
| 1 | +package com.oreilly.rxjava.ch8; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.util.Log; |
| 5 | +import android.view.View; |
| 6 | +import android.widget.*; |
| 7 | +import com.oreilly.rxjava.ch8.rxandroid.AndroidSchedulers; |
| 8 | +import com.oreilly.rxjava.ch8.rxbinding.RxTextView; |
| 9 | +import com.oreilly.rxjava.ch8.rxbinding.RxView; |
| 10 | +import com.oreilly.rxjava.ch8.rxbinding.TextViewAfterTextChangeEvent; |
| 11 | +import org.apache.commons.lang3.tuple.Pair; |
| 12 | +import org.junit.Ignore; |
| 13 | +import org.junit.Test; |
| 14 | +import rx.Observable; |
| 15 | +import rx.functions.Action1; |
| 16 | +import rx.functions.Func1; |
| 17 | +import rx.functions.Func2; |
| 18 | +import rx.schedulers.Schedulers; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import static android.content.ContentValues.TAG; |
| 24 | + |
| 25 | +@Ignore |
| 26 | +public class Android extends Activity { |
| 27 | + |
| 28 | + private final MeetupApi meetup = new ApiFactory().meetup(); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void sample_9() throws Exception { |
| 32 | + Button button = null; //... |
| 33 | + button.setOnClickListener(new View.OnClickListener() { |
| 34 | + @Override |
| 35 | + public void onClick(View view) { |
| 36 | + meetup |
| 37 | + .listCities(52.229841, 21.011736) |
| 38 | + .concatMapIterable(extractCities()) |
| 39 | + .map(toCityName()) |
| 40 | + .toList() |
| 41 | + .subscribeOn(Schedulers.io()) |
| 42 | + .observeOn(AndroidSchedulers.mainThread()) |
| 43 | + .subscribe( |
| 44 | + putOnListView(), |
| 45 | + displayError()); |
| 46 | + } |
| 47 | + |
| 48 | + //... |
| 49 | + |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + //Cities::getResults |
| 54 | + Func1<Cities, Iterable<City>> extractCities() { |
| 55 | + return new Func1<Cities, Iterable<City>>() { |
| 56 | + @Override |
| 57 | + public Iterable<City> call(Cities cities) { |
| 58 | + return cities.getResults(); |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + //City::getCity |
| 64 | + Func1<City, String> toCityName() { |
| 65 | + return new Func1<City, String>() { |
| 66 | + @Override |
| 67 | + public String call(City city) { |
| 68 | + return city.getCity(); |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + //cities -> listView.setAdapter(...) |
| 74 | + Action1<List<String>> putOnListView() { |
| 75 | + ListView listView = null; //... |
| 76 | + return new Action1<List<String>>() { |
| 77 | + @Override |
| 78 | + public void call(List<String> cities) { |
| 79 | + listView.setAdapter(new ArrayAdapter(Android.this, -1 /*R.layout.list*/, cities)); |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + //throwable -> {...} |
| 85 | + Action1<Throwable> displayError() { |
| 86 | + return new Action1<Throwable>() { |
| 87 | + @Override |
| 88 | + public void call(Throwable throwable) { |
| 89 | + Log.e(TAG, "Error", throwable); |
| 90 | + Toast.makeText(Android.this, "Unable to load cities", Toast.LENGTH_SHORT).show(); |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void sample_98() throws Exception { |
| 97 | + Button button = new Button(null); |
| 98 | + RxView |
| 99 | + .clicks(button) |
| 100 | + .flatMap(c -> meetup.listCities(52.229841, 21.011736)) |
| 101 | + .delay(2, TimeUnit.SECONDS) |
| 102 | + .concatMapIterable(extractCities()) |
| 103 | + .map(toCityName()) |
| 104 | + .toList() |
| 105 | + .subscribeOn(Schedulers.io()) |
| 106 | + .observeOn(AndroidSchedulers.mainThread()) |
| 107 | + .subscribe( |
| 108 | + putOnListView(), |
| 109 | + displayError()); |
| 110 | + } |
| 111 | + |
| 112 | + Func1<Void, Observable<Cities>> listCities(final double lat, final double lon) { |
| 113 | + return new Func1<Void, Observable<Cities>>() { |
| 114 | + @Override |
| 115 | + public Observable<Cities> call(Void aVoid) { |
| 116 | + return meetup.listCities(lat, lon); |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void sample_121() throws Exception { |
| 123 | + EditText latText = null;//... |
| 124 | + EditText lonText = null;//... |
| 125 | + |
| 126 | + Observable<Double> latChanges = RxTextView |
| 127 | + .afterTextChangeEvents(latText) |
| 128 | + .flatMap(toDouble()); |
| 129 | + Observable<Double> lonChanges = RxTextView |
| 130 | + .afterTextChangeEvents(lonText) |
| 131 | + .flatMap(toDouble()); |
| 132 | + |
| 133 | + Observable<Cities> cities = Observable |
| 134 | + .combineLatest(latChanges, lonChanges, toPair()) |
| 135 | + .debounce(1, TimeUnit.SECONDS) |
| 136 | + .flatMap(listCitiesNear()); |
| 137 | + } |
| 138 | + |
| 139 | + Func1<TextViewAfterTextChangeEvent, Observable<Double>> toDouble() { |
| 140 | + return new Func1<TextViewAfterTextChangeEvent, Observable<Double>>() { |
| 141 | + @Override |
| 142 | + public Observable<Double> call(TextViewAfterTextChangeEvent e) { |
| 143 | + String s = e.editable().toString(); |
| 144 | + try { |
| 145 | + return Observable.just(Double.parseDouble(s)); |
| 146 | + } catch (NumberFormatException ex) { |
| 147 | + return Observable.empty(); |
| 148 | + } |
| 149 | + } |
| 150 | + }; |
| 151 | + } |
| 152 | + |
| 153 | + //return Pair::new |
| 154 | + Func2<Double, Double, Pair<Double, Double>> toPair() { |
| 155 | + return new Func2<Double, Double, Pair<Double, Double>>() { |
| 156 | + @Override |
| 157 | + public Pair<Double, Double> call(Double lat, Double lon) { |
| 158 | + return Pair.of(lat, lon); |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + //return latLon -> meetup.listCities(latLon.first, latLon.second) |
| 164 | + Func1<Pair<Double, Double>, Observable<Cities>> listCitiesNear() { |
| 165 | + return new Func1<Pair<Double, Double>, Observable<Cities>>() { |
| 166 | + @Override |
| 167 | + public Observable<Cities> call(Pair<Double, Double> latLon) { |
| 168 | + return meetup.listCities(latLon.getLeft(), latLon.getRight()); |
| 169 | + } |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments