Skip to content

Commit 4923ce6

Browse files
authored
RxJava support (#304)
RxJava support
1 parent 5e9ca8a commit 4923ce6

24 files changed

Lines changed: 1134 additions & 116 deletions

File tree

mapbox/app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ dependencies {
3535
// The Test App depends in Mapbox Android UI, which includes all modules
3636
compile project(':libandroid-ui')
3737

38+
// Rx extensions
39+
compile project(':libjava-services-rx')
40+
3841
// Android Support libraries
3942
compile 'com.android.support:appcompat-v7:25.1.0'
4043
compile 'com.android.support:design:25.1.0'

mapbox/app/src/main/java/com/mapbox/services/android/testapp/directions/DirectionsV5Activity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.mapbox.services.api.directions.v5.MapboxDirections;
2424
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
2525
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
26+
import com.mapbox.services.api.rx.directions.v5.MapboxDirectionsRx;
2627
import com.mapbox.services.commons.geojson.LineString;
2728
import com.mapbox.services.commons.models.Position;
2829

@@ -33,6 +34,9 @@
3334
import retrofit2.Call;
3435
import retrofit2.Callback;
3536
import retrofit2.Response;
37+
import rx.android.schedulers.AndroidSchedulers;
38+
import rx.functions.Action1;
39+
import rx.schedulers.Schedulers;
3640

3741
public class DirectionsV5Activity extends AppCompatActivity {
3842

@@ -109,6 +113,25 @@ private void getRoute(Position origin, Position destination) throws ServicesExce
109113
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
110114
.build();
111115

116+
MapboxDirectionsRx clientRx = new MapboxDirectionsRx.Builder()
117+
.setAccessToken(Utils.getMapboxAccessToken(this))
118+
.setCoordinates(positions)
119+
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
120+
.setSteps(true)
121+
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
122+
.build();
123+
clientRx.getObservable()
124+
.subscribeOn(Schedulers.newThread())
125+
.observeOn(AndroidSchedulers.mainThread())
126+
.subscribe(new Action1<DirectionsResponse>() {
127+
@Override
128+
public void call(DirectionsResponse response) {
129+
DirectionsRoute currentRoute = response.getRoutes().get(0);
130+
Log.d(LOG_TAG, "Response code: " + response.getCode());
131+
Log.d(LOG_TAG, "Distance: " + currentRoute.getDistance());
132+
}
133+
});
134+
112135
client.enqueueCall(new Callback<DirectionsResponse>() {
113136
@Override
114137
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {

mapbox/dependencies.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ext {
1212
gson: 'com.google.code.gson:gson:2.8.0',
1313
retrofit2Main: 'com.squareup.retrofit2:retrofit:2.1.0',
1414
retrofit2Gson: 'com.squareup.retrofit2:converter-gson:2.1.0',
15+
retrofit2Rx: 'com.squareup.retrofit2:adapter-rxjava:2.1.0',
1516
okhttp3Logging: 'com.squareup.okhttp3:logging-interceptor:3.5.0',
1617
okhttp3Mockwebserver: 'com.squareup.okhttp3:mockwebserver:3.5.0',
1718
junit: 'junit:junit:4.12',

mapbox/libjava-services-rx/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ targetCompatibility = "1.7"
66
dependencies {
77
// Rx support depends on non-Rx services
88
compile project(':libjava-services')
9+
10+
// Rx extensions
11+
compile rootProject.ext.dep.retrofit2Rx
12+
13+
// Testing
14+
testCompile rootProject.ext.dep.okhttp3Mockwebserver
15+
testCompile rootProject.ext.dep.junit
16+
testCompile rootProject.ext.dep.hamcrestJunit
917
}
1018

1119
apply from: 'gradle-javadoc.gradle'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.mapbox.services.api.rx.directions.v5;
2+
3+
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
4+
5+
import retrofit2.http.GET;
6+
import retrofit2.http.Header;
7+
import retrofit2.http.Path;
8+
import retrofit2.http.Query;
9+
import rx.Observable;
10+
11+
/**
12+
* Interface that defines the directions service (v5).
13+
*
14+
* @since 2.0.0
15+
*/
16+
public interface DirectionsServiceRx {
17+
18+
/**
19+
* Observable-based interface
20+
*
21+
* @param userAgent The user.
22+
* @param user The user.
23+
* @param profile The profile directions should use.
24+
* @param coordinates The coordinates the route should follow.
25+
* @param accessToken Mapbox access token.
26+
* @param alternatives Define whether you want to recieve more then one route.
27+
* @param geometries Route geometry.
28+
* @param overview Route full, simplified, etc.
29+
* @param radiuses start at the most efficient point within the radius.
30+
* @param steps Define if you'd like the route steps.
31+
* @param continueStraight Define whether the route should continue straight even if the route
32+
* will be slower.
33+
* @return A retrofit Observable object
34+
* @since 2.0.0
35+
*/
36+
@GET("directions/v5/{user}/{profile}/{coordinates}")
37+
Observable<DirectionsResponse> getObservable(
38+
@Header("User-Agent") String userAgent,
39+
@Path("user") String user,
40+
@Path("profile") String profile,
41+
@Path("coordinates") String coordinates,
42+
@Query("access_token") String accessToken,
43+
@Query("alternatives") Boolean alternatives,
44+
@Query("geometries") String geometries,
45+
@Query("overview") String overview,
46+
@Query("radiuses") String radiuses,
47+
@Query("steps") Boolean steps,
48+
@Query("continue_straight") Boolean continueStraight
49+
);
50+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.mapbox.services.api.rx.directions.v5;
2+
3+
import com.mapbox.services.api.ServicesException;
4+
import com.mapbox.services.api.directions.v5.MapboxDirections;
5+
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
6+
7+
import retrofit2.Retrofit;
8+
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
9+
import retrofit2.converter.gson.GsonConverterFactory;
10+
import rx.Observable;
11+
12+
/**
13+
* The Directions API allows the calculation of routes between coordinates. The fastest route
14+
* is returned with geometries, and turn-by-turn instructions. The Mapbox Directions API supports
15+
* routing for driving cars, riding bicycles and walking.
16+
*
17+
* This class has support for Rx Observables.
18+
*
19+
* @since 2.0.0
20+
*/
21+
public class MapboxDirectionsRx extends MapboxDirections {
22+
23+
private DirectionsServiceRx serviceRx = null;
24+
private Observable<DirectionsResponse> observable = null;
25+
26+
public MapboxDirectionsRx(Builder builder) {
27+
super(builder);
28+
}
29+
30+
private DirectionsServiceRx getServiceRx() {
31+
// No need to recreate it
32+
if (serviceRx != null) {
33+
return serviceRx;
34+
}
35+
36+
// Retrofit instance
37+
Retrofit retrofit = new Retrofit.Builder()
38+
.client(getOkHttpClient())
39+
.baseUrl(builder.getBaseUrl())
40+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
41+
.addConverterFactory(GsonConverterFactory.create())
42+
.build();
43+
44+
// Directions service
45+
serviceRx = retrofit.create(DirectionsServiceRx.class);
46+
return serviceRx;
47+
}
48+
49+
public Observable<DirectionsResponse> getObservable() {
50+
// No need to recreate it
51+
if (observable != null) {
52+
return observable;
53+
}
54+
55+
observable = getServiceRx().getObservable(
56+
getHeaderUserAgent(builder.getClientAppName()),
57+
builder.getUser(),
58+
builder.getProfile(),
59+
builder.getCoordinates(),
60+
builder.getAccessToken(),
61+
builder.isAlternatives(),
62+
builder.getGeometries(),
63+
builder.getOverview(),
64+
builder.getRadiuses(),
65+
builder.isSteps(),
66+
builder.isContinueStraight());
67+
68+
// Done
69+
return observable;
70+
}
71+
72+
public static class Builder extends MapboxDirections.Builder<Builder> {
73+
@Override
74+
public MapboxDirectionsRx build() throws ServicesException {
75+
super.build();
76+
return new MapboxDirectionsRx(this);
77+
}
78+
}
79+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mapbox.services.api.rx.distance.v1;
2+
3+
import com.mapbox.services.api.distance.v1.models.DistanceResponse;
4+
import com.mapbox.services.commons.geojson.MultiPoint;
5+
6+
import okhttp3.RequestBody;
7+
import retrofit2.http.Body;
8+
import retrofit2.http.Header;
9+
import retrofit2.http.POST;
10+
import retrofit2.http.Path;
11+
import retrofit2.http.Query;
12+
import rx.Observable;
13+
14+
/**
15+
* Interface that defines the distance service.
16+
*
17+
* @since 2.0.0
18+
*/
19+
public interface DistanceServiceRx {
20+
/**
21+
* Observable-based interface
22+
*
23+
* @param userAgent The User.
24+
* @param user The user.
25+
* @param profile Directions profile id.
26+
* @param accessToken Mapbox access token.
27+
* @param coordinates converted to a {@link MultiPoint#toJson()}.
28+
* @return The {@link DistanceResponse} in a Observable wrapper
29+
* @since 2.0.0
30+
*/
31+
@POST("distances/v1/{user}/{profile}")
32+
Observable<DistanceResponse> getObservable(
33+
@Header("User-Agent") String userAgent,
34+
@Path("user") String user,
35+
@Path("profile") String profile,
36+
@Query("access_token") String accessToken,
37+
@Body RequestBody coordinates);
38+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.mapbox.services.api.rx.distance.v1;
2+
3+
import com.mapbox.services.api.ServicesException;
4+
import com.mapbox.services.api.distance.v1.MapboxDistance;
5+
import com.mapbox.services.api.distance.v1.models.DistanceResponse;
6+
7+
import retrofit2.Retrofit;
8+
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
9+
import retrofit2.converter.gson.GsonConverterFactory;
10+
import rx.Observable;
11+
12+
/**
13+
* Note this API is still in preview.
14+
* <p>
15+
* The Mapbox Distance API returns all travel times between many points. For example, given 3
16+
* locations A, B, C, the Distance API will return a matrix of travel times in seconds between each
17+
* location. This API allows you to build tools that efficiently check the reachability of
18+
* coordinates from each other, filter points by travel time, or run algorithms for solving
19+
* optimization problems.
20+
* </p>
21+
* <p>
22+
* Limits placed on this API include a maximum 100 coordinate pairs per request and a maximum 60
23+
* requests per minute.
24+
* </p>
25+
* This class has support for Rx Observables.
26+
*
27+
* @see <a href="https://www.mapbox.com/api-documentation/?language=Java#distance">Mapbox Distance API documentation</a>
28+
* @since 2.0.0
29+
*/
30+
public class MapboxDistanceRx extends MapboxDistance {
31+
32+
private DistanceServiceRx serviceRx = null;
33+
private Observable<DistanceResponse> observable = null;
34+
35+
public MapboxDistanceRx(Builder builder) {
36+
super(builder);
37+
}
38+
39+
private DistanceServiceRx getServiceRx() {
40+
// No need to recreate it
41+
if (serviceRx != null) {
42+
return serviceRx;
43+
}
44+
45+
// Retrofit instance
46+
Retrofit retrofit = new Retrofit.Builder()
47+
.client(getOkHttpClient())
48+
.baseUrl(builder.getBaseUrl())
49+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
50+
.addConverterFactory(GsonConverterFactory.create(getGson()))
51+
.build();
52+
53+
// Distance service
54+
serviceRx = retrofit.create(DistanceServiceRx.class);
55+
return serviceRx;
56+
}
57+
58+
public Observable<DistanceResponse> getObservable() {
59+
// No need to recreate it
60+
if (observable != null) {
61+
return observable;
62+
}
63+
64+
observable = getServiceRx().getObservable(
65+
getHeaderUserAgent(builder.getClientAppName()),
66+
builder.getUser(),
67+
builder.getProfile(),
68+
builder.getAccessToken(),
69+
builder.getCoordinates()
70+
);
71+
72+
// Done
73+
return observable;
74+
}
75+
76+
public static class Builder extends MapboxDistance.Builder<Builder> {
77+
@Override
78+
public MapboxDistanceRx build() throws ServicesException {
79+
super.build();
80+
return new MapboxDistanceRx(this);
81+
}
82+
}
83+
84+
}

0 commit comments

Comments
 (0)