Search in sources :

Example 1 with Gist

use of com.meisolsson.githubsdk.model.Gist in project PocketHub by pockethub.

the class NewsFragment method onListItemClick.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    GitHubEvent event = (GitHubEvent) l.getItemAtPosition(position);
    if (DownloadEvent.equals(event.type())) {
        openDownload(event);
        return;
    }
    if (PushEvent.equals(event.type())) {
        openPush(event);
        return;
    }
    if (CommitCommentEvent.equals(event.type())) {
        openCommitComment(event);
        return;
    }
    Issue issue = issueMatcher.getIssue(event);
    if (issue != null) {
        Repository repo = ConvertUtils.eventRepoToRepo(event.repo());
        viewIssue(issue, repo);
        return;
    }
    Gist gist = gistMatcher.getGist(event);
    if (gist != null) {
        startActivity(GistsViewActivity.createIntent(gist));
        return;
    }
    Repository repo = repoMatcher.getRepository(event);
    if (repo != null) {
        viewRepository(repo);
    }
    UserPair users = userMatcher.getUsers(event);
    if (users != null) {
        viewUser(users);
    }
}
Also used : Repository(com.meisolsson.githubsdk.model.Repository) Gist(com.meisolsson.githubsdk.model.Gist) Issue(com.meisolsson.githubsdk.model.Issue) UserPair(com.github.pockethub.android.core.user.UserEventMatcher.UserPair) GitHubEvent(com.meisolsson.githubsdk.model.GitHubEvent)

Example 2 with Gist

use of com.meisolsson.githubsdk.model.Gist in project PocketHub by pockethub.

the class RefreshGistTask method subscribe.

@Override
public void subscribe(ObservableEmitter<FullGist> emitter) throws Exception {
    try {
        Gist gist = store.refreshGist(id);
        List<GitHubComment> comments;
        if (gist.comments() > 0) {
            comments = ServiceGenerator.createService(context, GistCommentService.class).getGistComments(id, 0).blockingGet().items();
        } else {
            comments = Collections.emptyList();
        }
        for (GitHubComment comment : comments) {
            imageGetter.encode(comment, comment.bodyHtml());
        }
        Response<Boolean> response = ServiceGenerator.createService(context, GistService.class).checkIfGistIsStarred(id).blockingGet();
        boolean starred = response.code() == 204;
        emitter.onNext(new FullGist(gist, starred, comments));
    } catch (IOException e) {
        emitter.onError(e);
    }
}
Also used : Gist(com.meisolsson.githubsdk.model.Gist) GitHubComment(com.meisolsson.githubsdk.model.GitHubComment) IOException(java.io.IOException)

Example 3 with Gist

use of com.meisolsson.githubsdk.model.Gist in project PocketHub by pockethub.

the class GistsViewActivity method createIntent.

/**
     * Create an intent to show gists with an initial selected Gist
     *
     * @param gists
     * @param position
     * @return intent
     */
public static Intent createIntent(List<Gist> gists, int position) {
    String[] ids = new String[gists.size()];
    int index = 0;
    for (Gist gist : gists) {
        ids[index++] = gist.id();
    }
    return new Builder("gists.VIEW").add(EXTRA_GIST_IDS, (Serializable) ids).add(EXTRA_POSITION, position).toIntent();
}
Also used : Gist(com.meisolsson.githubsdk.model.Gist) Builder(com.github.pockethub.android.Intents.Builder)

Example 4 with Gist

use of com.meisolsson.githubsdk.model.Gist in project PocketHub by pockethub.

the class GistFileFragment method loadSource.

private void loadSource() {
    store.refreshGist(gistId).map(gist -> {
        Map<String, GistFile> files = gist.files();
        if (files == null) {
            throw new IOException();
        }
        GistFile loadedFile = files.get(file.filename());
        if (loadedFile == null) {
            throw new IOException();
        }
        return loadedFile;
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(loadedFile -> {
        file = loadedFile;
        getArguments().putParcelable(EXTRA_GIST_FILE, file);
        if (file.content() != null) {
            showSource();
        }
    }, e -> ToastUtils.show(getActivity(), R.string.error_gist_file_load));
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) BaseFragment(com.github.pockethub.android.ui.base.BaseFragment) PreferenceUtils(com.github.pockethub.android.util.PreferenceUtils) OnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener) NonNull(android.support.annotation.NonNull) EXTRA_GIST_FILE(com.github.pockethub.android.Intents.EXTRA_GIST_FILE) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) BindView(butterknife.BindView) Inject(javax.inject.Inject) SuppressLint(android.annotation.SuppressLint) GistStore(com.github.pockethub.android.core.gist.GistStore) MenuInflater(android.view.MenuInflater) Gist(com.meisolsson.githubsdk.model.Gist) Map(java.util.Map) Menu(android.view.Menu) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) ToastUtils(com.github.pockethub.android.util.ToastUtils) WebView(android.webkit.WebView) SourceEditor(com.github.pockethub.android.util.SourceEditor) GistFile(com.meisolsson.githubsdk.model.GistFile) LayoutInflater(android.view.LayoutInflater) IOException(java.io.IOException) WRAP(com.github.pockethub.android.util.PreferenceUtils.WRAP) ViewGroup(android.view.ViewGroup) EXTRA_GIST_ID(com.github.pockethub.android.Intents.EXTRA_GIST_ID) SharedPreferences(android.content.SharedPreferences) R(com.github.pockethub.android.R) IOException(java.io.IOException) GistFile(com.meisolsson.githubsdk.model.GistFile) Map(java.util.Map)

Example 5 with Gist

use of com.meisolsson.githubsdk.model.Gist 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

Gist (com.meisolsson.githubsdk.model.Gist)17 GitHubEvent (com.meisolsson.githubsdk.model.GitHubEvent)4 MenuItem (android.view.MenuItem)3 GistStore (com.github.pockethub.android.core.gist.GistStore)3 Issue (com.meisolsson.githubsdk.model.Issue)3 Repository (com.meisolsson.githubsdk.model.Repository)3 Bundle (android.os.Bundle)2 NonNull (android.support.annotation.NonNull)2 Menu (android.view.Menu)2 MenuInflater (android.view.MenuInflater)2 View (android.view.View)2 Builder (com.github.pockethub.android.Intents.Builder)2 R (com.github.pockethub.android.R)2 UserPair (com.github.pockethub.android.core.user.UserEventMatcher.UserPair)2 ToastUtils (com.github.pockethub.android.util.ToastUtils)2 Single (io.reactivex.Single)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Schedulers (io.reactivex.schedulers.Schedulers)2 IOException (java.io.IOException)2 Inject (javax.inject.Inject)2