use of com.meisolsson.githubsdk.service.gists.GistService 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());
});
}
use of com.meisolsson.githubsdk.service.gists.GistService in project gh4a by slapperwan.
the class GistActivity method loadStarredState.
private void loadStarredState(boolean force) {
GistService service = ServiceFactory.get(GistService.class, force);
service.checkIfGistIsStarred(mGistId).map(ApiHelpers::mapToBooleanOrThrowOnFailure).compose(makeLoaderSingle(ID_LOADER_STARRED, force)).subscribe(result -> {
mIsStarred = result;
supportInvalidateOptionsMenu();
}, this::handleLoadFailure);
}
use of com.meisolsson.githubsdk.service.gists.GistService in project gh4a by slapperwan.
the class GistActivity method updateStarringState.
private void updateStarringState() {
GistService service = ServiceFactory.get(GistService.class, false);
Single<Response<Void>> responseSingle = mIsStarred ? service.unstarGist(mGistId) : service.starGist(mGistId);
responseSingle.map(ApiHelpers::mapToBooleanOrThrowOnFailure).compose(RxUtils::doInBackground).subscribe(result -> {
mIsStarred = !mIsStarred;
supportInvalidateOptionsMenu();
}, error -> {
handleActionFailure("Updating gist starring state failed", error);
supportInvalidateOptionsMenu();
});
}
use of com.meisolsson.githubsdk.service.gists.GistService in project gh4a by slapperwan.
the class GistActivity method loadGist.
private void loadGist(boolean force) {
GistService service = ServiceFactory.get(GistService.class, force);
service.getGist(mGistId).map(ApiHelpers::throwOnFailure).compose(makeLoaderSingle(ID_LOADER_GIST, force)).subscribe(result -> {
fillData(result);
setContentShown(true);
supportInvalidateOptionsMenu();
}, this::handleLoadFailure);
}
use of com.meisolsson.githubsdk.service.gists.GistService in project gh4a by slapperwan.
the class GistViewerActivity method loadGist.
private void loadGist(boolean force) {
GistService service = ServiceFactory.get(GistService.class, force);
service.getGist(mGistId).map(ApiHelpers::throwOnFailure).compose(makeLoaderSingle(ID_LOADER_GIST, force)).subscribe(result -> {
mGistOwner = ApiHelpers.getUserLogin(GistViewerActivity.this, result.owner());
mGistFile = result.files().get(mFileName);
onDataReady();
}, this::handleLoadFailure);
}
Aggregations