use of com.github.pockethub.android.RequestCodes.GIST_VIEW in project PocketHub by pockethub.
the class GistsPagerFragment method randomGist.
private void randomGist() {
GistService service = ServiceGenerator.createService(getActivity(), GistService.class);
service.getPublicGists(1).flatMap(response -> {
Page<Gist> firstPage = response.body();
int randomPage = (int) (Math.random() * (firstPage.last() - 1));
randomPage = Math.max(1, randomPage);
return service.getPublicGists(randomPage);
}).flatMap(response -> {
Page<Gist> gistPage = response.body();
if (gistPage.items().isEmpty()) {
int randomPage = (int) (Math.random() * (gistPage.last() - 1));
randomPage = Math.max(1, randomPage);
return service.getPublicGists(randomPage);
}
return Single.just(response);
}).map(response -> {
Page<Gist> gistPage = response.body();
if (response.isSuccessful()) {
int size = gistPage.items().size();
if (size > 0) {
return store.addGist(gistPage.items().get(rand.nextInt(size)));
} else {
throw new IllegalArgumentException(getContext().getString(R.string.no_gists_found));
}
} else {
ToastUtils.show(getActivity(), R.string.error_gist_load);
return null;
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(RxProgress.bindToLifecycle(getActivity(), R.string.random_gist)).as(AutoDisposeUtils.bindToLifecycle(this)).subscribe(gist -> getActivity().startActivityForResult(GistsViewActivity.createIntent(gist), GIST_VIEW), e -> {
Log.d(TAG, "Exception opening random Gist", e);
ToastUtils.show((Activity) getContext(), e.getMessage());
});
}
Aggregations