Search in sources :

Example 1 with GIST_VIEW

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());
    });
}
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)

Aggregations

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 ICON_TEAM (com.github.pockethub.android.ui.view.OcticonTextView.ICON_TEAM)1 ToastUtils (com.github.pockethub.android.util.ToastUtils)1