Search in sources :

Example 11 with ReviewComment

use of com.meisolsson.githubsdk.model.ReviewComment in project gh4a by slapperwan.

the class DiffViewHolder method bind.

@Override
public void bind(TimelineItem.Diff item) {
    ReviewComment comment = item.getInitialComment();
    mFileTextView.setTag(item.getInitialTimelineComment());
    mFileTextView.setText(comment.path());
    boolean isOutdated = comment.position() == null;
    mFileTextView.setPaintFlags(isOutdated ? mFileTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG : mFileTextView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
    mFileTextView.setClickable(!isOutdated);
    mFileTextView.setTextColor(isOutdated ? mSecondaryTextColor : mAccentColor);
    String[] lines = comment.diffChunk().split("\n");
    int leftLine = 0;
    int rightLine = 0;
    int[] lineNumbers = StringUtils.extractDiffHunkLineNumbers(lines[0]);
    if (lineNumbers != null) {
        leftLine = lineNumbers[0];
        rightLine = lineNumbers[1];
    }
    int maxLine = Math.max(rightLine, leftLine) + lines.length;
    int maxLineLength = String.valueOf(maxLine).length();
    SpannableStringBuilder builder = new SpannableStringBuilder();
    int start = Math.max(1, lines.length - 4);
    for (int i = 1; i < lines.length; i++) {
        boolean isLeftLine = false;
        boolean isRightLine = false;
        if (lines[i].startsWith("-")) {
            leftLine += 1;
            isLeftLine = true;
        } else if (lines[i].startsWith("+")) {
            rightLine += 1;
            isRightLine = true;
        } else {
            leftLine += 1;
            rightLine += 1;
        }
        if (i < start) {
            continue;
        }
        int spanStart = builder.length();
        String leftLineText = !isRightLine && leftLine > 0 ? String.valueOf(leftLine) : "";
        appendLineNumber(builder, maxLineLength, leftLineText, leftLine, item, false);
        String rightLineText = !isLeftLine && rightLine > 0 ? String.valueOf(rightLine) : "";
        appendLineNumber(builder, maxLineLength, rightLineText, rightLine, item, true);
        // Add additional padding between line numbers and code
        builder.append(" ");
        int lineNumberLength = builder.length() - spanStart;
        builder.append(" ").append(lines[i]).append(" ");
        if (i < lines.length - 1) {
            builder.append("\n");
        }
        int backgroundColor = mDefaultBackgroundColor;
        int lineNumberBackgroundColor = mDefaultLineNumberBackgroundColor;
        if (lines[i].startsWith("+")) {
            backgroundColor = mAddedLineBackgroundColor;
            lineNumberBackgroundColor = mAddedLineNumberBackgroundColor;
        } else if (lines[i].startsWith("-")) {
            backgroundColor = mRemovedLineBackgroundColor;
            lineNumberBackgroundColor = mRemovedLineNumberBackgroundColor;
        }
        DiffLineSpan span = new DiffLineSpan(backgroundColor, lineNumberBackgroundColor, mPadding, i == start, i == lines.length - 1, lineNumberLength);
        builder.setSpan(span, spanStart, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    mDiffHunkTextView.setText(builder);
    mDiffHunkTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mInitialDiffTextSize * getDiffSizeMultiplier());
}
Also used : ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 12 with ReviewComment

use of com.meisolsson.githubsdk.model.ReviewComment in project gh4a by slapperwan.

the class EventAdapter method formatTitle.

private String formatTitle(GitHubEvent event) {
    GitHubEvent.RepoIdentifier eventRepo = event.repo();
    Resources res = mContext.getResources();
    if (hasInvalidPayload(event)) {
        return event.type().toString();
    }
    switch(event.type()) {
        case CommitCommentEvent:
            {
                CommitCommentPayload payload = (CommitCommentPayload) event.payload();
                return res.getString(R.string.event_commit_comment_title, payload.comment().commitId().substring(0, 7), formatFromRepoIdentifier(eventRepo));
            }
        case CreateEvent:
            {
                CreatePayload payload = (CreatePayload) event.payload();
                switch(payload.refType()) {
                    case Repository:
                        return res.getString(R.string.event_create_repo_title);
                    case Branch:
                        return res.getString(R.string.event_create_branch_title, payload.ref(), formatFromRepoIdentifier(eventRepo));
                    case Tag:
                        return res.getString(R.string.event_create_tag_title, payload.ref(), formatFromRepoIdentifier(eventRepo));
                }
                break;
            }
        case DeleteEvent:
            {
                DeletePayload payload = (DeletePayload) event.payload();
                switch(payload.refType()) {
                    case Branch:
                        return res.getString(R.string.event_delete_branch_title, payload.ref(), formatFromRepoIdentifier(eventRepo));
                    case Tag:
                        return res.getString(R.string.event_delete_tag_title, payload.ref(), formatFromRepoIdentifier(eventRepo));
                }
                break;
            }
        case DownloadEvent:
            return res.getString(R.string.event_download_title, formatFromRepoIdentifier(eventRepo));
        case FollowEvent:
            {
                FollowPayload payload = (FollowPayload) event.payload();
                return res.getString(R.string.event_follow_title, ApiHelpers.getUserLogin(mContext, payload.target()));
            }
        case ForkEvent:
            return res.getString(R.string.event_fork_title, formatFromRepoIdentifier(eventRepo));
        case ForkApplyEvent:
            return res.getString(R.string.event_fork_apply_title, formatFromRepoIdentifier(eventRepo));
        case GistEvent:
            {
                GistPayload payload = (GistPayload) event.payload();
                Gist gist = payload.gist();
                String id = gist != null ? gist.id() : mContext.getString(R.string.deleted);
                int resId = payload.action() == GistPayload.Action.Updated ? R.string.event_update_gist_title : R.string.event_create_gist_title;
                return res.getString(resId, id);
            }
        case GollumEvent:
            {
                GollumPayload payload = (GollumPayload) event.payload();
                List<GitHubWikiPage> pages = payload.pages();
                int count = pages == null ? 0 : pages.size();
                return res.getString(R.string.event_gollum_title, res.getQuantityString(R.plurals.page, count, count), formatFromRepoIdentifier(eventRepo));
            }
        case IssueCommentEvent:
            {
                IssueCommentPayload payload = (IssueCommentPayload) event.payload();
                Issue issue = payload.issue();
                if (issue != null) {
                    int formatResId = issue.pullRequest() != null ? R.string.event_pull_request_comment : R.string.event_issue_comment;
                    return res.getString(formatResId, issue.number(), formatFromRepoIdentifier(eventRepo));
                }
                break;
            }
        case IssuesEvent:
            {
                IssuesPayload payload = (IssuesPayload) event.payload();
                final int resId;
                switch(payload.action()) {
                    case Opened:
                        resId = R.string.event_issues_open_title;
                        break;
                    case Closed:
                        resId = R.string.event_issues_close_title;
                        break;
                    case Reopened:
                        resId = R.string.event_issues_reopen_title;
                        break;
                    default:
                        return "";
                }
                return res.getString(resId, payload.issue().number(), formatFromRepoIdentifier(eventRepo));
            }
        case MemberEvent:
            {
                MemberPayload payload = (MemberPayload) event.payload();
                return res.getString(R.string.event_member_title, ApiHelpers.getUserLogin(mContext, payload.member()), formatFromRepoIdentifier(eventRepo));
            }
        case PublicEvent:
            return res.getString(R.string.event_public_title, formatFromRepoIdentifier(eventRepo));
        case PullRequestEvent:
            {
                PullRequestPayload payload = (PullRequestPayload) event.payload();
                PullRequest pr = payload.pullRequest();
                final int resId;
                switch(payload.action()) {
                    case Opened:
                        resId = R.string.event_pr_open_title;
                        break;
                    case Closed:
                        resId = pr.merged() ? R.string.event_pr_merge_title : R.string.event_pr_close_title;
                        break;
                    case Reopened:
                        resId = R.string.event_pr_reopen_title;
                        break;
                    case Synchronized:
                        resId = R.string.event_pr_update_title;
                        break;
                    default:
                        return "";
                }
                return res.getString(resId, payload.number(), formatFromRepoIdentifier(eventRepo));
            }
        case PullRequestReviewCommentEvent:
            {
                PullRequestReviewCommentPayload payload = (PullRequestReviewCommentPayload) event.payload();
                PullRequest pr = payload.pullRequest();
                ReviewComment comment = payload.comment();
                if (pr != null) {
                    return res.getString(R.string.event_pull_request_review_comment_title, pr.number(), formatFromRepoIdentifier(eventRepo));
                } else if (comment != null) {
                    return res.getString(R.string.event_commit_comment_title, comment.commitId().substring(0, 7), formatFromRepoIdentifier(eventRepo));
                }
                break;
            }
        case PushEvent:
            {
                PushPayload payload = (PushPayload) event.payload();
                String ref = payload.ref();
                if (ref.startsWith("refs/heads/")) {
                    ref = ref.substring(11);
                }
                return res.getString(R.string.event_push_title, ref, formatFromRepoIdentifier(eventRepo));
            }
        case ReleaseEvent:
            return res.getString(R.string.event_release_title, formatFromRepoIdentifier(eventRepo));
        case TeamAddEvent:
            {
                TeamAddPayload payload = (TeamAddPayload) event.payload();
                Team team = payload.team();
                if (team != null) {
                    Repository repo = payload.repository();
                    if (repo != null) {
                        return res.getString(R.string.event_team_repo_add, formatToRepoName(repo), team.name());
                    }
                }
                break;
            }
        case WatchEvent:
            return res.getString(R.string.event_watch_title, formatFromRepoIdentifier(eventRepo));
    }
    return "";
}
Also used : GollumPayload(com.meisolsson.githubsdk.model.payload.GollumPayload) PushPayload(com.meisolsson.githubsdk.model.payload.PushPayload) Gist(com.meisolsson.githubsdk.model.Gist) MemberPayload(com.meisolsson.githubsdk.model.payload.MemberPayload) FollowPayload(com.meisolsson.githubsdk.model.payload.FollowPayload) Issue(com.meisolsson.githubsdk.model.Issue) DeletePayload(com.meisolsson.githubsdk.model.payload.DeletePayload) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) IssuesPayload(com.meisolsson.githubsdk.model.payload.IssuesPayload) PullRequest(com.meisolsson.githubsdk.model.PullRequest) GitHubEvent(com.meisolsson.githubsdk.model.GitHubEvent) GistPayload(com.meisolsson.githubsdk.model.payload.GistPayload) IssueCommentPayload(com.meisolsson.githubsdk.model.payload.IssueCommentPayload) PullRequestReviewCommentPayload(com.meisolsson.githubsdk.model.payload.PullRequestReviewCommentPayload) CommitCommentPayload(com.meisolsson.githubsdk.model.payload.CommitCommentPayload) CreatePayload(com.meisolsson.githubsdk.model.payload.CreatePayload) Repository(com.meisolsson.githubsdk.model.Repository) PullRequestPayload(com.meisolsson.githubsdk.model.payload.PullRequestPayload) TeamAddPayload(com.meisolsson.githubsdk.model.payload.TeamAddPayload) List(java.util.List) Team(com.meisolsson.githubsdk.model.Team) Resources(android.content.res.Resources)

Example 13 with ReviewComment

use of com.meisolsson.githubsdk.model.ReviewComment in project gh4a by slapperwan.

the class EventAdapter method formatDescription.

private CharSequence formatDescription(GitHubEvent event, int typefaceValue) {
    GitHubEventType eventType = event.type();
    GitHubEvent.RepoIdentifier eventRepo = event.repo();
    if (hasInvalidPayload(event)) {
        return eventType.toString();
    }
    Resources res = mContext.getResources();
    switch(eventType) {
        case CommitCommentEvent:
            {
                CommitCommentPayload payload = (CommitCommentPayload) event.payload();
                GitComment comment = payload.comment();
                if (comment != null) {
                    return EmojiParser.parseToUnicode(comment.body());
                }
                break;
            }
        case CreateEvent:
            {
                CreatePayload payload = (CreatePayload) event.payload();
                if (payload.refType() == ReferenceType.Repository) {
                    return res.getString(R.string.event_create_repo_desc, eventRepo.repoWithUserName());
                }
                break;
            }
        case DownloadEvent:
            {
                DownloadPayload payload = (DownloadPayload) event.payload();
                if (payload.download() != null) {
                    return payload.download().name();
                }
                break;
            }
        case FollowEvent:
            {
                FollowPayload payload = (FollowPayload) event.payload();
                User target = payload.target();
                if (target != null) {
                    return res.getString(R.string.event_follow_desc, target.login(), target.publicRepos(), target.followers());
                }
                break;
            }
        case ForkEvent:
            {
                ForkPayload payload = (ForkPayload) event.payload();
                return res.getString(R.string.event_fork_desc, formatToRepoName(payload.forkee()));
            }
        case GollumEvent:
            {
                GollumPayload payload = (GollumPayload) event.payload();
                List<GitHubWikiPage> pages = payload.pages();
                if (pages != null && !pages.isEmpty()) {
                    return res.getString(R.string.event_gollum_desc, pages.get(0).pageName());
                }
                break;
            }
        case IssueCommentEvent:
            {
                IssueCommentPayload payload = (IssueCommentPayload) event.payload();
                if (payload != null && payload.comment() != null) {
                    return EmojiParser.parseToUnicode(payload.comment().body());
                }
                break;
            }
        case IssuesEvent:
            {
                IssuesPayload payload = (IssuesPayload) event.payload();
                return payload.issue().title();
            }
        case PublicEvent:
            return null;
        case PullRequestEvent:
            {
                PullRequestPayload payload = (PullRequestPayload) event.payload();
                PullRequest pullRequest = payload.pullRequest();
                if (!StringUtils.isBlank(pullRequest.title())) {
                    return res.getString(R.string.event_pull_request_desc, pullRequest.title(), pullRequest.commits(), pullRequest.additions(), pullRequest.deletions());
                }
                break;
            }
        case PullRequestReviewCommentEvent:
            {
                PullRequestReviewCommentPayload payload = (PullRequestReviewCommentPayload) event.payload();
                ReviewComment comment = payload.comment();
                if (comment != null) {
                    return EmojiParser.parseToUnicode(comment.body());
                }
                break;
            }
        case PushEvent:
            {
                PushPayload payload = (PushPayload) event.payload();
                List<GitCommit> commits = payload.commits();
                if (commits != null && !commits.isEmpty()) {
                    SpannableStringBuilder ssb = new SpannableStringBuilder();
                    float density = mContext.getResources().getDisplayMetrics().density;
                    int bottomMargin = Math.round(2 * /* dp */
                    density);
                    int count = payload.size();
                    int maxLines = mContext.getResources().getInteger(R.integer.event_description_max_lines);
                    int max = Math.min(count > maxLines ? maxLines - 1 : count, commits.size());
                    for (int i = 0; i < max; i++) {
                        GitCommit commit = commits.get(i);
                        if (i != 0) {
                            ssb.append("\n");
                        }
                        int lastLength = ssb.length();
                        String sha = commit.sha().substring(0, 7);
                        ssb.append(sha);
                        ssb.setSpan(new TextAppearanceSpan(mContext, R.style.small_highlighted_sha), ssb.length() - sha.length(), ssb.length(), 0);
                        ssb.append(" ");
                        ssb.append(getFirstLine(EmojiParser.parseToUnicode(commit.message())));
                        ssb.setSpan(new EllipsizeLineSpan(i == (count - 1) ? 0 : bottomMargin), lastLength, ssb.length(), 0);
                    }
                    if (count > maxLines) {
                        String text = res.getString(R.string.event_push_desc, count - max);
                        ssb.append("\n");
                        ssb.append(text);
                        ssb.setSpan(new CustomTypefaceSpan(typefaceValue, Typeface.ITALIC), ssb.length() - text.length(), ssb.length(), 0);
                    }
                    return ssb;
                } else if (eventRepo == null) {
                    return mContext.getString(R.string.deleted);
                }
                break;
            }
        case ReleaseEvent:
            {
                ReleasePayload payload = (ReleasePayload) event.payload();
                Release release = payload.release();
                if (release != null) {
                    if (!TextUtils.isEmpty(release.name())) {
                        return release.name();
                    }
                    return release.tagName();
                }
                break;
            }
        case TeamAddEvent:
            {
                TeamAddPayload payload = (TeamAddPayload) event.payload();
                Team team = payload.team();
                if (team != null) {
                    return res.getString(R.string.event_team_add_desc, team.name(), team.membersCount(), team.reposCount());
                }
                break;
            }
    }
    return null;
}
Also used : ForkPayload(com.meisolsson.githubsdk.model.payload.ForkPayload) GollumPayload(com.meisolsson.githubsdk.model.payload.GollumPayload) ReleasePayload(com.meisolsson.githubsdk.model.payload.ReleasePayload) FollowPayload(com.meisolsson.githubsdk.model.payload.FollowPayload) User(com.meisolsson.githubsdk.model.User) TextAppearanceSpan(android.text.style.TextAppearanceSpan) GitCommit(com.meisolsson.githubsdk.model.git.GitCommit) PullRequest(com.meisolsson.githubsdk.model.PullRequest) GitHubEventType(com.meisolsson.githubsdk.model.GitHubEventType) IssueCommentPayload(com.meisolsson.githubsdk.model.payload.IssueCommentPayload) EllipsizeLineSpan(com.gh4a.widget.EllipsizeLineSpan) PullRequestPayload(com.meisolsson.githubsdk.model.payload.PullRequestPayload) TeamAddPayload(com.meisolsson.githubsdk.model.payload.TeamAddPayload) List(java.util.List) Team(com.meisolsson.githubsdk.model.Team) GitComment(com.meisolsson.githubsdk.model.git.GitComment) Release(com.meisolsson.githubsdk.model.Release) PushPayload(com.meisolsson.githubsdk.model.payload.PushPayload) CustomTypefaceSpan(com.gh4a.widget.CustomTypefaceSpan) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) IssuesPayload(com.meisolsson.githubsdk.model.payload.IssuesPayload) GitHubEvent(com.meisolsson.githubsdk.model.GitHubEvent) PullRequestReviewCommentPayload(com.meisolsson.githubsdk.model.payload.PullRequestReviewCommentPayload) CommitCommentPayload(com.meisolsson.githubsdk.model.payload.CommitCommentPayload) CreatePayload(com.meisolsson.githubsdk.model.payload.CreatePayload) Resources(android.content.res.Resources) DownloadPayload(com.meisolsson.githubsdk.model.payload.DownloadPayload) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

ReviewComment (com.meisolsson.githubsdk.model.ReviewComment)13 Intent (android.content.Intent)7 ApiHelpers (com.gh4a.utils.ApiHelpers)6 List (java.util.List)6 IntentUtils (com.gh4a.utils.IntentUtils)5 Optional (com.gh4a.utils.Optional)5 RxUtils (com.gh4a.utils.RxUtils)5 PullRequest (com.meisolsson.githubsdk.model.PullRequest)5 PullRequestReviewCommentService (com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)5 Single (io.reactivex.Single)5 View (android.view.View)4 ServiceFactory (com.gh4a.ServiceFactory)4 TimelineItem (com.gh4a.model.TimelineItem)4 Review (com.meisolsson.githubsdk.model.Review)4 HashMap (java.util.HashMap)4 Response (retrofit2.Response)4 R (com.gh4a.R)3 GitHubEvent (com.meisolsson.githubsdk.model.GitHubEvent)3 GitHubFile (com.meisolsson.githubsdk.model.GitHubFile)3 Issue (com.meisolsson.githubsdk.model.Issue)3