Search in sources :

Example 1 with Reactions

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

the class DiffViewerActivity method generateHtml.

@Override
protected String generateHtml(String cssTheme, boolean addTitleHeader) {
    StringBuilder content = new StringBuilder();
    boolean authorized = Gh4Application.get().isAuthorized();
    String title = addTitleHeader ? getDocumentTitle() : null;
    content.append("<html><head><title>");
    if (title != null) {
        content.append(title);
    }
    content.append("</title>");
    writeCssInclude(content, "text", cssTheme);
    writeScriptInclude(content, "codeutils");
    content.append("</head><body");
    int highlightInsertPos = content.length();
    content.append(">");
    if (title != null) {
        content.append("<h2>").append(title).append("</h2>");
    }
    content.append("<pre>");
    mDiffLines = mDiff != null ? mDiff.split("\n") : new String[0];
    int highlightStartLine = -1, highlightEndLine = -1;
    int leftDiffPosition = -1, rightDiffPosition = -1;
    for (int i = 0; i < mDiffLines.length; i++) {
        String line = mDiffLines[i];
        String cssClass = null;
        if (line.startsWith("@@")) {
            int[] lineNumbers = StringUtils.extractDiffHunkLineNumbers(line);
            if (lineNumbers != null) {
                leftDiffPosition = lineNumbers[0];
                rightDiffPosition = lineNumbers[1];
            }
            cssClass = "change";
        } else if (line.startsWith("+")) {
            ++rightDiffPosition;
            cssClass = "add";
        } else if (line.startsWith("-")) {
            ++leftDiffPosition;
            cssClass = "remove";
        } else {
            ++leftDiffPosition;
            ++rightDiffPosition;
        }
        int pos = mHighlightIsRight ? rightDiffPosition : leftDiffPosition;
        if (pos != -1 && pos == mHighlightStartLine && highlightStartLine == -1) {
            highlightStartLine = i;
        }
        if (pos != -1 && pos == mHighlightEndLine && highlightEndLine == -1) {
            highlightEndLine = i;
        }
        content.append("<div id=\"line").append(i).append("\"");
        if (cssClass != null) {
            content.append("class=\"").append(cssClass).append("\"");
        }
        if (authorized) {
            String uri = String.format(Locale.US, COMMENT_ADD_URI_FORMAT, i, leftDiffPosition, rightDiffPosition, line.startsWith("+"));
            content.append(" onclick=\"javascript:location.href='");
            content.append(uri).append("'\"");
        }
        content.append(">").append(TextUtils.htmlEncode(line)).append("</div>");
        List<PositionalCommentBase> comments = mCommitCommentsByPos.get(i);
        if (comments != null) {
            for (PositionalCommentBase comment : comments) {
                long id = comment.id();
                mCommitComments.put(id, new CommitCommentWrapper(comment));
                content.append("<div ").append("id=\"comment").append(id).append("\"");
                content.append(" class=\"comment");
                if (mInitialComment != null && mInitialComment.matches(id, null)) {
                    content.append(" highlighted");
                }
                content.append("\"");
                if (authorized) {
                    String uri = String.format(Locale.US, COMMENT_EDIT_URI_FORMAT, i, leftDiffPosition, rightDiffPosition, line.startsWith("+"), id);
                    content.append(" onclick=\"javascript:location.href='");
                    content.append(uri).append("'\"");
                }
                content.append("><div class=\"change\">");
                content.append(getString(R.string.commit_comment_header, "<b>" + ApiHelpers.getUserLogin(this, comment.user()) + "</b>", StringUtils.formatRelativeTime(DiffViewerActivity.this, comment.createdAt(), true)));
                content.append("</div>").append(comment.bodyHtml());
                Reactions reactions = comment.reactions();
                if (reactions.totalCount() > 0) {
                    content.append("<div>");
                    appendReactionSpan(content, reactions.plusOne(), REACTION_PLUS_ONE_PATH);
                    appendReactionSpan(content, reactions.minusOne(), REACTION_MINUS_ONE_PATH);
                    appendReactionSpan(content, reactions.confused(), REACTION_CONFUSED_PATH);
                    appendReactionSpan(content, reactions.heart(), REACTION_HEART_PATH);
                    appendReactionSpan(content, reactions.laugh(), REACTION_LAUGH_PATH);
                    appendReactionSpan(content, reactions.hooray(), REACTION_HOORAY_PATH);
                    content.append("</div>");
                }
                content.append("</div>");
            }
        }
    }
    if (mInitialLine > 0) {
        content.insert(highlightInsertPos, " onload='scrollToElement(\"line" + mInitialLine + "\")' onresize='scrollToHighlight();'");
    } else if (mInitialComment != null) {
        content.insert(highlightInsertPos, " onload='scrollToElement(\"comment" + mInitialComment.commentId + "\")' onresize='scrollToHighlight();'");
    } else if (highlightStartLine != -1 && highlightEndLine != -1) {
        content.insert(highlightInsertPos, " onload='highlightDiffLines(" + highlightStartLine + "," + highlightEndLine + ")' onresize='scrollToHighlight();'");
    }
    content.append("</pre></body></html>");
    return content.toString();
}
Also used : PositionalCommentBase(com.meisolsson.githubsdk.model.PositionalCommentBase) Reactions(com.meisolsson.githubsdk.model.Reactions)

Aggregations

PositionalCommentBase (com.meisolsson.githubsdk.model.PositionalCommentBase)1 Reactions (com.meisolsson.githubsdk.model.Reactions)1