Skip to content

Commit 5ea090a

Browse files
committed
Chapters 8 and 9
1 parent 6c55533 commit 5ea090a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2482
-2
lines changed

build.gradle

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ repositories {
1111
dependencies {
1212
testCompile 'io.reactivex:rxjava:1.2.2'
1313
testCompile 'io.reactivex:rxnetty-http:0.5.2-rc.4'
14+
testCompile 'com.netflix.hystrix:hystrix-core:1.5.8'
15+
testCompile 'com.netflix.hystrix:hystrix-metrics-event-stream:1.5.8'
1416

1517
testCompile 'com.google.guava:guava:18.0'
1618
testCompile 'org.apache.commons:commons-collections4:4.1'
@@ -28,15 +30,26 @@ dependencies {
2830
testCompile 'org.springframework:spring-jdbc:4.3.4.RELEASE'
2931

3032
testCompile 'com.google.android:android:4.1.1.4'
33+
// testCompile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
34+
3135
testCompile 'org.postgresql:postgresql:9.4-1206-jdbc42'
3236
testCompile 'org.twitter4j:twitter4j-stream:4.0.5'
3337
testCompile 'com.ning:async-http-client:1.9.40'
3438

3539
testCompile 'javax.jms:jms-api:1.1-rev-1'
3640
testCompile 'org.apache.activemq:activemq-client:5.14.1'
41+
testCompile 'org.eclipse.jetty:jetty-servlet:9.4.0.RC2'
42+
43+
testCompile 'com.couchbase.client:java-client:2.3.5'
44+
testCompile 'org.mongodb:mongodb-driver-rx:1.2.0'
45+
46+
testCompile 'org.apache.camel:camel-rx:2.17.3'
47+
testCompile 'org.apache.camel:camel-kafka:2.17.3'
48+
testCompile 'org.apache.activemq:activemq-camel:5.14.0'
49+
testCompile 'org.apache.activemq:activemq-client:5.14.0'
3750

38-
// testCompile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
39-
// testCompile 'com.squareup.retrofit2:converter-jackson:2.0.1'
51+
testCompile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
52+
testCompile 'com.squareup.retrofit2:converter-jackson:2.0.1'
4053

4154
testCompile 'junit:junit:4.12'
4255
testCompile 'org.assertj:assertj-core:3.5.2'
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.oreilly.rxjava.ch8;
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
6+
import retrofit2.Retrofit;
7+
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
8+
import retrofit2.converter.jackson.JacksonConverterFactory;
9+
10+
class ApiFactory {
11+
12+
GeoNames geoNames() {
13+
return new Retrofit.Builder()
14+
.baseUrl("http://api.geonames.org")
15+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
16+
.addConverterFactory(JacksonConverterFactory.create(objectMapper()))
17+
.build()
18+
.create(GeoNames.class);
19+
}
20+
21+
MeetupApi meetup() {
22+
Retrofit retrofit = new Retrofit.Builder()
23+
.baseUrl("https://api.meetup.com/")
24+
.addCallAdapterFactory(
25+
RxJavaCallAdapterFactory.create())
26+
.addConverterFactory(
27+
JacksonConverterFactory.create(objectMapper()))
28+
.build();
29+
return retrofit.create(MeetupApi.class);
30+
}
31+
32+
private ObjectMapper objectMapper() {
33+
ObjectMapper objectMapper = new ObjectMapper();
34+
objectMapper.setPropertyNamingStrategy(
35+
PropertyNamingStrategy.SNAKE_CASE);
36+
objectMapper.configure(
37+
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
38+
return objectMapper;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)