Skip to content

Instantly share code, notes, and snippets.

@Orange168
Created April 10, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orange168/2f751594f95f9b05c129cd44f5b10739 to your computer and use it in GitHub Desktop.
Save Orange168/2f751594f95f9b05c129cd44f5b10739 to your computer and use it in GitHub Desktop.
RetrofitFragment Rx demo
public class GithubService {
private GithubService() { }
public static GithubApi createGithubService(final String githubToken) {
Retrofit.Builder builder = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://api.github.com");
if (!TextUtils.isEmpty(githubToken)) {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newReq = request.newBuilder()
.addHeader("Authorization", format("token %s", githubToken))
.build();
return chain.proceed(newReq);
}
}).build();
builder.client(client);
}
return builder.build().create(GithubApi.class);
}
}
@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
public void onListContributorsWithFullUserInfoClicked() {
_adapter.clear();
_subscriptions.add(_githubService.contributors(_username.getText().toString(), _repo.getText().toString())
.flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override
public Observable<Contributor> call(List<Contributor> contributors) {
return Observable.from(contributors);
}
})
.flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() {
@Override
public Observable<Pair<User, Contributor>> call(Contributor contributor) {
Observable<User> _userObservable = _githubService.user(contributor.login)
.filter(new Func1<User, Boolean>() {
@Override
public Boolean call(User user) {
return !isEmpty(user.name) && !isEmpty(user.email);
}
});
return Observable.zip(_userObservable,
Observable.just(contributor),
new Func2<User, Contributor, Pair<User, Contributor>>() {
@Override
public Pair<User, Contributor> call(User user, Contributor contributor) {
return new Pair<>(user, contributor);
}
});
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Pair<User, Contributor>>() {
@Override
public void onCompleted() {
Timber.d("Retrofit call 2 completed ");
}
@Override
public void onError(Throwable e) {
Timber.e(e, "error while getting the list of contributors along with full " + "names");
}
@Override
public void onNext(Pair<User, Contributor> pair) {
User user = pair.first;
Contributor contributor = pair.second;
_adapter.add(format("%s(%s) has made %d contributions to %s",
user.name,
user.email,
contributor.contributions,
_repo.getText().toString()));
_adapter.notifyDataSetChanged();
Timber.d("%s(%s) has made %d contributions to %s",
user.name,
user.email,
contributor.contributions,
_repo.getText().toString());
}
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment