Search in sources :

Example 36 with ApiHelpers

use of com.gh4a.utils.ApiHelpers 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 37 with ApiHelpers

use of com.gh4a.utils.ApiHelpers 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 38 with ApiHelpers

use of com.gh4a.utils.ApiHelpers 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)

Example 39 with ApiHelpers

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

the class ReviewFragment method addReaction.

@Override
public Single<Reaction> addReaction(GitHubCommentBase comment, String content) {
    final ReactionService service = ServiceFactory.get(ReactionService.class, false);
    final ReactionRequest request = ReactionRequest.builder().content(content).build();
    final Single<Response<Reaction>> responseSingle = comment instanceof ReviewComment ? service.createPullRequestReviewCommentReaction(mRepoOwner, mRepoName, comment.id(), request) : service.createIssueCommentReaction(mRepoOwner, mRepoName, comment.id(), request);
    return responseSingle.map(ApiHelpers::throwOnFailure);
}
Also used : ReactionRequest(com.meisolsson.githubsdk.model.request.ReactionRequest) Response(retrofit2.Response) CreateReviewComment(com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) ReactionService(com.meisolsson.githubsdk.service.reactions.ReactionService) ApiHelpers(com.gh4a.utils.ApiHelpers)

Example 40 with ApiHelpers

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

the class ReviewFragment method onCreateDataSingle.

@Override
protected Single<List<TimelineItem>> onCreateDataSingle(boolean bypassCache) {
    final PullRequestService prService = ServiceFactory.get(PullRequestService.class, bypassCache);
    final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, bypassCache);
    final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, bypassCache);
    Single<TimelineItem.TimelineReview> reviewItemSingle = reviewService.getReview(mRepoOwner, mRepoName, mIssueNumber, mReview.id()).map(ApiHelpers::throwOnFailure).map(TimelineItem.TimelineReview::new);
    Single<List<ReviewComment>> reviewCommentsSingle = ApiHelpers.PageIterator.toSingle(page -> reviewService.getReviewComments(mRepoOwner, mRepoName, mIssueNumber, mReview.id())).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).cache();
    Single<Boolean> hasCommentsSingle = reviewCommentsSingle.map(comments -> !comments.isEmpty());
    Single<Optional<List<GitHubFile>>> filesSingle = hasCommentsSingle.flatMap(hasComments -> {
        if (!hasComments) {
            return Single.just(Optional.absent());
        }
        return ApiHelpers.PageIterator.toSingle(page -> prService.getPullRequestFiles(mRepoOwner, mRepoName, mIssueNumber, page)).map(Optional::of);
    });
    Single<Optional<List<ReviewComment>>> commentsSingle = hasCommentsSingle.flatMap(hasComments -> {
        if (!hasComments) {
            return Single.just(Optional.absent());
        }
        return ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(mRepoOwner, mRepoName, mIssueNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).map(Optional::of);
    });
    return Single.zip(reviewItemSingle, reviewCommentsSingle, filesSingle, commentsSingle, (reviewItem, reviewComments, filesOpt, commentsOpt) -> {
        if (!reviewComments.isEmpty()) {
            HashMap<String, GitHubFile> filesByName = new HashMap<>();
            if (filesOpt.isPresent()) {
                for (GitHubFile file : filesOpt.get()) {
                    filesByName.put(file.filename(), file);
                }
            }
            // Add all of the review comments to the review item creating necessary diff hunks
            for (ReviewComment reviewComment : reviewComments) {
                GitHubFile file = filesByName.get(reviewComment.path());
                reviewItem.addComment(reviewComment, file, true);
            }
            if (commentsOpt.isPresent()) {
                for (ReviewComment commitComment : commentsOpt.get()) {
                    if (reviewComments.contains(commitComment)) {
                        continue;
                    }
                    // Rest of the comments should be added only if they are under the same
                    // diff hunks as the original review comments.
                    GitHubFile file = filesByName.get(commitComment.path());
                    reviewItem.addComment(commitComment, file, false);
                }
            }
        }
        List<TimelineItem> items = new ArrayList<>();
        items.add(reviewItem);
        List<TimelineItem.Diff> diffHunks = new ArrayList<>(reviewItem.getDiffHunks());
        Collections.sort(diffHunks);
        for (TimelineItem.Diff diffHunk : diffHunks) {
            items.add(diffHunk);
            items.addAll(diffHunk.comments);
            if (!diffHunk.isReply()) {
                items.add(new TimelineItem.Reply(diffHunk.getInitialTimelineComment()));
            }
        }
        return items;
    });
}
Also used : CoordinatorLayout(android.support.design.widget.CoordinatorLayout) IssueCommentService(com.meisolsson.githubsdk.service.issues.IssueCommentService) Bundle(android.os.Bundle) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) CreateReviewComment(com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment) FrameLayout(android.widget.FrameLayout) GitHubCommentBase(com.meisolsson.githubsdk.model.GitHubCommentBase) Intent(android.content.Intent) StringRes(android.support.annotation.StringRes) HashMap(java.util.HashMap) Response(retrofit2.Response) Single(io.reactivex.Single) ArrayList(java.util.ArrayList) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) Reaction(com.meisolsson.githubsdk.model.Reaction) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) R(com.gh4a.R) TimelineItemAdapter(com.gh4a.adapter.timeline.TimelineItemAdapter) View(android.view.View) IntentUtils(com.gh4a.utils.IntentUtils) Review(com.meisolsson.githubsdk.model.Review) ApiHelpers(com.gh4a.utils.ApiHelpers) ReactionService(com.meisolsson.githubsdk.service.reactions.ReactionService) LayoutInflater(android.view.LayoutInflater) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) EditPullRequestCommentActivity(com.gh4a.activities.EditPullRequestCommentActivity) ReactionRequest(com.meisolsson.githubsdk.model.request.ReactionRequest) ViewGroup(android.view.ViewGroup) RecyclerView(android.support.v7.widget.RecyclerView) RootAdapter(com.gh4a.adapter.RootAdapter) List(java.util.List) AlertDialog(android.support.v7.app.AlertDialog) RxUtils(com.gh4a.utils.RxUtils) EditorBottomSheet(com.gh4a.widget.EditorBottomSheet) Optional(com.gh4a.utils.Optional) EditIssueCommentActivity(com.gh4a.activities.EditIssueCommentActivity) TimelineItem(com.gh4a.model.TimelineItem) Nullable(android.support.annotation.Nullable) ServiceFactory(com.gh4a.ServiceFactory) Activity(android.app.Activity) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) ArrayList(java.util.ArrayList) List(java.util.List) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) Optional(com.gh4a.utils.Optional) CreateReviewComment(com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) TimelineItem(com.gh4a.model.TimelineItem) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService)

Aggregations

ApiHelpers (com.gh4a.utils.ApiHelpers)68 Response (retrofit2.Response)13 RxUtils (com.gh4a.utils.RxUtils)12 ServiceFactory (com.gh4a.ServiceFactory)11 Intent (android.content.Intent)10 Bundle (android.os.Bundle)10 Optional (com.gh4a.utils.Optional)10 PullRequestReviewCommentService (com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)10 Single (io.reactivex.Single)9 Context (android.content.Context)7 LayoutInflater (android.view.LayoutInflater)7 CommentRequest (com.meisolsson.githubsdk.model.request.CommentRequest)7 List (java.util.List)7 View (android.view.View)6 R (com.gh4a.R)6 IntentUtils (com.gh4a.utils.IntentUtils)6 User (com.meisolsson.githubsdk.model.User)6 ReactionRequest (com.meisolsson.githubsdk.model.request.ReactionRequest)6 CreateReviewComment (com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment)6 ReactionService (com.meisolsson.githubsdk.service.reactions.ReactionService)6