Search in sources :

Example 1 with GistService

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());
    });
}
Also used : Bundle(android.os.Bundle) ICON_PERSON(com.github.pockethub.android.ui.view.OcticonTextView.ICON_PERSON) Random(java.util.Random) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) Inject(javax.inject.Inject) ICON_TEAM(com.github.pockethub.android.ui.view.OcticonTextView.ICON_TEAM) TabPagerFragment(com.github.pockethub.android.ui.TabPagerFragment) GistStore(com.github.pockethub.android.core.gist.GistStore) MenuInflater(android.view.MenuInflater) Gist(com.meisolsson.githubsdk.model.Gist) Page(com.meisolsson.githubsdk.model.Page) Menu(android.view.Menu) View(android.view.View) ServiceGenerator(com.meisolsson.githubsdk.core.ServiceGenerator) Schedulers(io.reactivex.schedulers.Schedulers) ToastUtils(com.github.pockethub.android.util.ToastUtils) Log(android.util.Log) BaseActivity(com.github.pockethub.android.ui.BaseActivity) RxProgress(com.github.pockethub.android.rx.RxProgress) ICON_STAR(com.github.pockethub.android.ui.view.OcticonTextView.ICON_STAR) AutoDisposeUtils(com.github.pockethub.android.rx.AutoDisposeUtils) Nullable(android.support.annotation.Nullable) R(com.github.pockethub.android.R) GIST_VIEW(com.github.pockethub.android.RequestCodes.GIST_VIEW) Activity(android.app.Activity) GistService(com.meisolsson.githubsdk.service.gists.GistService) Gist(com.meisolsson.githubsdk.model.Gist) Page(com.meisolsson.githubsdk.model.Page) GistService(com.meisolsson.githubsdk.service.gists.GistService)

Example 2 with GistService

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);
}
Also used : ApiHelpers(com.gh4a.utils.ApiHelpers) GistService(com.meisolsson.githubsdk.service.gists.GistService)

Example 3 with GistService

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();
    });
}
Also used : Response(retrofit2.Response) ApiHelpers(com.gh4a.utils.ApiHelpers) GistService(com.meisolsson.githubsdk.service.gists.GistService)

Example 4 with GistService

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);
}
Also used : ApiHelpers(com.gh4a.utils.ApiHelpers) GistService(com.meisolsson.githubsdk.service.gists.GistService)

Example 5 with GistService

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);
}
Also used : ApiHelpers(com.gh4a.utils.ApiHelpers) GistService(com.meisolsson.githubsdk.service.gists.GistService)

Aggregations

GistService (com.meisolsson.githubsdk.service.gists.GistService)5 ApiHelpers (com.gh4a.utils.ApiHelpers)4 Activity (android.app.Activity)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Log (android.util.Log)1 Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 R (com.github.pockethub.android.R)1 GIST_VIEW (com.github.pockethub.android.RequestCodes.GIST_VIEW)1 GistStore (com.github.pockethub.android.core.gist.GistStore)1 AutoDisposeUtils (com.github.pockethub.android.rx.AutoDisposeUtils)1 RxProgress (com.github.pockethub.android.rx.RxProgress)1 BaseActivity (com.github.pockethub.android.ui.BaseActivity)1 TabPagerFragment (com.github.pockethub.android.ui.TabPagerFragment)1 ICON_PERSON (com.github.pockethub.android.ui.view.OcticonTextView.ICON_PERSON)1 ICON_STAR (com.github.pockethub.android.ui.view.OcticonTextView.ICON_STAR)1