Search in sources :

Example 1 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class ReleaseInfoActivity method loadBody.

private void loadBody() {
    final Single<Optional<String>> htmlSingle;
    if (TextUtils.isEmpty(mRelease.body())) {
        htmlSingle = Single.just(Optional.absent());
    } else {
        MarkdownService service = ServiceFactory.get(MarkdownService.class, false);
        RequestMarkdown request = RequestMarkdown.builder().context(mRepoOwner + "/" + mRepoName).mode("gfm").text(mRelease.body()).build();
        htmlSingle = service.renderMarkdown(request).map(ApiHelpers::throwOnFailure).map(Optional::of);
    }
    mBodySubscription = htmlSingle.compose(makeLoaderSingle(ID_LOADER_BODY, false)).subscribe(this::fillNotes, this::handleLoadFailure);
}
Also used : RequestMarkdown(com.meisolsson.githubsdk.model.request.RequestMarkdown) Optional(com.gh4a.utils.Optional) ApiHelpers(com.gh4a.utils.ApiHelpers) MarkdownService(com.meisolsson.githubsdk.service.misc.MarkdownService)

Example 2 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class CommitCommentLoadTask method load.

public static Single<Optional<Intent>> load(Context context, String repoOwner, String repoName, String commitSha, IntentUtils.InitialCommentMarker marker) {
    RepositoryCommitService commitService = ServiceFactory.get(RepositoryCommitService.class, false);
    RepositoryCommentService commentService = ServiceFactory.get(RepositoryCommentService.class, false);
    Single<Commit> commitSingle = commitService.getCommit(repoOwner, repoName, commitSha).map(ApiHelpers::throwOnFailure);
    Single<List<GitComment>> commentSingle = ApiHelpers.PageIterator.toSingle(page -> commentService.getCommitComments(repoOwner, repoName, commitSha, page)).cache();
    Single<Optional<GitHubFile>> fileSingle = commentSingle.compose(RxUtils.filterAndMapToFirst(c -> marker.matches(c.id(), c.createdAt()))).zipWith(commitSingle, (comment, commit) -> {
        if (comment.isPresent()) {
            for (GitHubFile commitFile : commit.files()) {
                if (commitFile.filename().equals(comment.get().path())) {
                    return Optional.of(commitFile);
                }
            }
        }
        return Optional.absent();
    });
    return Single.zip(commitSingle, commentSingle, fileSingle, (commit, comments, fileOpt) -> {
        GitHubFile file = fileOpt.orNull();
        if (file != null && !FileUtils.isImage(file.filename())) {
            return Optional.of(CommitDiffViewerActivity.makeIntent(context, repoOwner, repoName, commitSha, file.filename(), file.patch(), comments, -1, -1, false, marker));
        } else if (file == null) {
            return Optional.of(CommitActivity.makeIntent(context, repoOwner, repoName, commitSha, marker));
        }
        return Optional.absent();
    });
}
Also used : Context(android.content.Context) FileUtils(com.gh4a.utils.FileUtils) CommitDiffViewerActivity(com.gh4a.activities.CommitDiffViewerActivity) ApiHelpers(com.gh4a.utils.ApiHelpers) RepositoryCommentService(com.meisolsson.githubsdk.service.repositories.RepositoryCommentService) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) Intent(android.content.Intent) Single(io.reactivex.Single) VisibleForTesting(android.support.annotation.VisibleForTesting) Commit(com.meisolsson.githubsdk.model.Commit) GitComment(com.meisolsson.githubsdk.model.git.GitComment) List(java.util.List) RxUtils(com.gh4a.utils.RxUtils) CommitActivity(com.gh4a.activities.CommitActivity) FragmentActivity(android.support.v4.app.FragmentActivity) Optional(com.gh4a.utils.Optional) RepositoryCommitService(com.meisolsson.githubsdk.service.repositories.RepositoryCommitService) ServiceFactory(com.gh4a.ServiceFactory) IntentUtils(com.gh4a.utils.IntentUtils) Commit(com.meisolsson.githubsdk.model.Commit) Optional(com.gh4a.utils.Optional) ApiHelpers(com.gh4a.utils.ApiHelpers) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) List(java.util.List) RepositoryCommentService(com.meisolsson.githubsdk.service.repositories.RepositoryCommentService) RepositoryCommitService(com.meisolsson.githubsdk.service.repositories.RepositoryCommitService)

Example 3 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class PullRequestReviewDiffLoadTask method getSingle.

@Override
protected Single<Optional<Intent>> getSingle() {
    final PullRequestReviewCommentService service = ServiceFactory.get(PullRequestReviewCommentService.class, false);
    final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, false);
    long diffCommentId = Long.parseLong(mDiffId.fileHash);
    return ApiHelpers.PageIterator.toSingle(page -> service.getPullRequestComments(mRepoOwner, mRepoName, mPullRequestNumber, page)).compose(RxUtils.filterAndMapToFirst(c -> c.id() == diffCommentId)).flatMap(commentOpt -> commentOpt.flatMap(comment -> {
        long reviewId = comment.pullRequestReviewId();
        return reviewService.getReview(mRepoOwner, mRepoName, mPullRequestNumber, reviewId).map(ApiHelpers::throwOnFailure);
    })).map(reviewOpt -> reviewOpt.map(review -> ReviewActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, review, new IntentUtils.InitialCommentMarker(diffCommentId))));
}
Also used : PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) VisibleForTesting(android.support.annotation.VisibleForTesting) ReviewActivity(com.gh4a.activities.ReviewActivity) RxUtils(com.gh4a.utils.RxUtils) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) ApiHelpers(com.gh4a.utils.ApiHelpers) FragmentActivity(android.support.v4.app.FragmentActivity) Optional(com.gh4a.utils.Optional) Intent(android.content.Intent) ServiceFactory(com.gh4a.ServiceFactory) Single(io.reactivex.Single) IntentUtils(com.gh4a.utils.IntentUtils) IntentUtils(com.gh4a.utils.IntentUtils) ApiHelpers(com.gh4a.utils.ApiHelpers) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)

Example 4 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class ReactionBar method toggleReaction.

private static Single<Optional<Reaction>> toggleReaction(String content, long id, List<Reaction> existingDetails, Callback callback, Item item, ReactionDetailsCache cache) {
    final Single<Optional<Reaction>> resultSingle;
    if (id == 0) {
        resultSingle = callback.addReaction(item, content).map(Optional::of);
    } else {
        ReactionService service = ServiceFactory.get(ReactionService.class, false);
        resultSingle = service.deleteReaction(id).map(response -> Optional.absent());
    }
    return resultSingle.compose(RxUtils::doInBackground).doOnSuccess(reactionOpt -> {
        if (reactionOpt.isPresent()) {
            existingDetails.add(reactionOpt.get());
        } else {
            for (int i = 0; i < existingDetails.size(); i++) {
                Reaction reaction = existingDetails.get(i);
                if (reaction.id() == id) {
                    existingDetails.remove(i);
                    break;
                }
            }
        }
        cache.putEntry(item, existingDetails);
    });
}
Also used : Context(android.content.Context) LinearLayout(android.widget.LinearLayout) MenuBuilder(android.support.v7.view.menu.MenuBuilder) DrawableCompat(android.support.v4.graphics.drawable.DrawableCompat) ImageView(android.widget.ImageView) HashMap(java.util.HashMap) NonNull(android.support.annotation.NonNull) Drawable(android.graphics.drawable.Drawable) Single(io.reactivex.Single) MenuItem(android.view.MenuItem) UserActivity(com.gh4a.activities.UserActivity) UiUtils(com.gh4a.utils.UiUtils) ArrayList(java.util.ArrayList) User(com.meisolsson.githubsdk.model.User) AttributeSet(android.util.AttributeSet) Reaction(com.meisolsson.githubsdk.model.Reaction) AvatarHandler(com.gh4a.utils.AvatarHandler) MenuPopupHelper(android.support.v7.view.menu.MenuPopupHelper) Menu(android.view.Menu) R(com.gh4a.R) View(android.view.View) Reactions(com.meisolsson.githubsdk.model.Reactions) Log(android.util.Log) Parcelable(android.os.Parcelable) ApiHelpers(com.gh4a.utils.ApiHelpers) ReactionService(com.meisolsson.githubsdk.service.reactions.ReactionService) LayoutInflater(android.view.LayoutInflater) PopupMenu(android.support.v7.widget.PopupMenu) TextUtils(android.text.TextUtils) PorterDuff(android.graphics.PorterDuff) ViewGroup(android.view.ViewGroup) IdRes(android.support.annotation.IdRes) ColorInt(android.support.annotation.ColorInt) List(java.util.List) TextView(android.widget.TextView) RxUtils(com.gh4a.utils.RxUtils) BaseAdapter(android.widget.BaseAdapter) Gh4Application(com.gh4a.Gh4Application) LayoutRes(android.support.annotation.LayoutRes) Optional(com.gh4a.utils.Optional) Nullable(android.support.annotation.Nullable) ServiceFactory(com.gh4a.ServiceFactory) ListPopupWindow(android.support.v7.widget.ListPopupWindow) Optional(com.gh4a.utils.Optional) ReactionService(com.meisolsson.githubsdk.service.reactions.ReactionService) Reaction(com.meisolsson.githubsdk.model.Reaction)

Example 5 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class RepositoryFragment method loadReadme.

private void loadReadme(boolean force) {
    Context context = getActivity();
    Long id = mRepository.id();
    String repoOwner = mRepository.owner().login();
    String repoName = mRepository.name();
    RepositoryContentService service = ServiceFactory.get(RepositoryContentService.class, force);
    service.getReadmeHtml(repoOwner, repoName, mRef).map(ApiHelpers::throwOnFailure).map(Optional::of).compose(RxUtils.mapFailureToValue(HttpURLConnection.HTTP_NOT_FOUND, Optional.<String>absent())).map(htmlOpt -> {
        if (htmlOpt.isPresent()) {
            String html = HtmlUtils.rewriteRelativeUrls(htmlOpt.get(), repoOwner, repoName, mRef != null ? mRef : mRepository.defaultBranch());
            mImageGetter.encode(context, id, html);
            return Optional.of(html);
        }
        return Optional.<String>absent();
    }).compose(makeLoaderSingle(ID_LOADER_README, force)).doOnSubscribe(disposable -> {
        mIsReadmeLoaded = false;
        updateReadmeVisibility();
    }).subscribe(readmeOpt -> {
        if (readmeOpt.isPresent()) {
            mReadmeView.setMovementMethod(UiUtils.CHECKING_LINK_METHOD);
            mImageGetter.bind(mReadmeView, readmeOpt.get(), id);
        } else {
            mReadmeView.setText(R.string.repo_no_readme);
            mReadmeView.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
        }
        mIsReadmeLoaded = true;
        updateReadmeVisibility();
    }, this::handleLoadFailure);
}
Also used : Context(android.content.Context) Typeface(android.graphics.Typeface) ContributorListActivity(com.gh4a.activities.ContributorListActivity) HttpURLConnection(java.net.HttpURLConnection) Context(android.content.Context) LinearLayout(android.widget.LinearLayout) Repository(com.meisolsson.githubsdk.model.Repository) Bundle(android.os.Bundle) Intent(android.content.Intent) UserActivity(com.gh4a.activities.UserActivity) WikiListActivity(com.gh4a.activities.WikiListActivity) UiUtils(com.gh4a.utils.UiUtils) WatcherListActivity(com.gh4a.activities.WatcherListActivity) SpannableStringBuilder(android.text.SpannableStringBuilder) ForkListActivity(com.gh4a.activities.ForkListActivity) EmojiParser(com.vdurmont.emoji.EmojiParser) Permissions(com.meisolsson.githubsdk.model.Permissions) SearchService(com.meisolsson.githubsdk.service.search.SearchService) Locale(java.util.Locale) R(com.gh4a.R) View(android.view.View) IntentSpan(com.gh4a.widget.IntentSpan) HttpImageGetter(com.gh4a.utils.HttpImageGetter) StringUtils(com.gh4a.utils.StringUtils) HtmlUtils(com.gh4a.utils.HtmlUtils) ApiHelpers(com.gh4a.utils.ApiHelpers) LayoutInflater(android.view.LayoutInflater) ReleaseListActivity(com.gh4a.activities.ReleaseListActivity) ViewGroup(android.view.ViewGroup) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService) TextView(android.widget.TextView) RepositoryActivity(com.gh4a.activities.RepositoryActivity) RxUtils(com.gh4a.utils.RxUtils) Optional(com.gh4a.utils.Optional) CollaboratorListActivity(com.gh4a.activities.CollaboratorListActivity) IssueListActivity(com.gh4a.activities.IssueListActivity) ServiceFactory(com.gh4a.ServiceFactory) SearchPage(com.meisolsson.githubsdk.model.SearchPage) OnClickListener(android.view.View.OnClickListener) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService) ApiHelpers(com.gh4a.utils.ApiHelpers)

Aggregations

Optional (com.gh4a.utils.Optional)12 ApiHelpers (com.gh4a.utils.ApiHelpers)11 ServiceFactory (com.gh4a.ServiceFactory)9 RxUtils (com.gh4a.utils.RxUtils)9 Intent (android.content.Intent)8 Single (io.reactivex.Single)8 List (java.util.List)7 IntentUtils (com.gh4a.utils.IntentUtils)6 Context (android.content.Context)5 VisibleForTesting (android.support.annotation.VisibleForTesting)5 FragmentActivity (android.support.v4.app.FragmentActivity)5 LayoutInflater (android.view.LayoutInflater)5 View (android.view.View)5 R (com.gh4a.R)5 ViewGroup (android.view.ViewGroup)4 RepositoryActivity (com.gh4a.activities.RepositoryActivity)4 Repository (com.meisolsson.githubsdk.model.Repository)4 ReviewComment (com.meisolsson.githubsdk.model.ReviewComment)4 Bundle (android.os.Bundle)3 Nullable (android.support.annotation.Nullable)3