Search in sources :

Example 1 with PositionalCommentBase

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

the class DiffViewerActivity method addCommentsToMap.

private void addCommentsToMap(List<C> comments) {
    mCommitCommentsByPos.clear();
    for (PositionalCommentBase comment : comments) {
        if (!TextUtils.equals(comment.path(), mPath)) {
            continue;
        }
        int position = comment.position();
        List<PositionalCommentBase> commentsByPos = mCommitCommentsByPos.get(position);
        if (commentsByPos == null) {
            commentsByPos = new ArrayList<>();
            mCommitCommentsByPos.put(position, commentsByPos);
        }
        commentsByPos.add(comment);
    }
}
Also used : PositionalCommentBase(com.meisolsson.githubsdk.model.PositionalCommentBase)

Example 2 with PositionalCommentBase

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

the class CommitFragment method fillStats.

protected void fillStats(List<GitHubFile> files, List<? extends PositionalCommentBase> comments) {
    LinearLayout llChanged = mContentView.findViewById(R.id.ll_changed);
    LinearLayout llAdded = mContentView.findViewById(R.id.ll_added);
    LinearLayout llRenamed = mContentView.findViewById(R.id.ll_renamed);
    LinearLayout llDeleted = mContentView.findViewById(R.id.ll_deleted);
    final LayoutInflater inflater = getLayoutInflater();
    int added = 0, changed = 0, renamed = 0, deleted = 0;
    int additions = 0, deletions = 0;
    int count = files != null ? files.size() : 0;
    int highlightColor = UiUtils.resolveColor(getActivity(), android.R.attr.textColorPrimary);
    ForegroundColorSpan addSpan = new ForegroundColorSpan(UiUtils.resolveColor(getActivity(), R.attr.colorCommitAddition));
    ForegroundColorSpan deleteSpan = new ForegroundColorSpan(UiUtils.resolveColor(getActivity(), R.attr.colorCommitDeletion));
    llChanged.removeAllViews();
    llAdded.removeAllViews();
    llRenamed.removeAllViews();
    llDeleted.removeAllViews();
    for (int i = 0; i < count; i++) {
        GitHubFile file = files.get(i);
        final LinearLayout parent;
        switch(file.status()) {
            case "added":
                parent = llAdded;
                added++;
                break;
            case "modified":
                parent = llChanged;
                changed++;
                break;
            case "renamed":
                parent = llRenamed;
                renamed++;
                break;
            case "removed":
                parent = llDeleted;
                deleted++;
                break;
            default:
                continue;
        }
        additions += file.additions();
        deletions += file.deletions();
        int commentCount = 0;
        for (PositionalCommentBase comment : comments) {
            if (TextUtils.equals(file.filename(), comment.path())) {
                commentCount++;
            }
        }
        ViewGroup fileView = (ViewGroup) inflater.inflate(R.layout.commit_filename, parent, false);
        TextView fileNameView = fileView.findViewById(R.id.filename);
        fileNameView.setText(file.filename());
        TextView statsView = fileView.findViewById(R.id.stats);
        if (file.patch() != null) {
            SpannableStringBuilder stats = new SpannableStringBuilder();
            stats.append("+").append(String.valueOf(file.additions()));
            int addLength = stats.length();
            stats.setSpan(addSpan, 0, addLength, 0);
            stats.append("\u00a0\u00a0\u00a0-").append(String.valueOf(file.deletions()));
            stats.setSpan(deleteSpan, addLength, stats.length(), 0);
            statsView.setText(stats);
            statsView.setVisibility(View.VISIBLE);
        } else {
            statsView.setVisibility(View.GONE);
        }
        if (file.patch() != null || (parent != llDeleted && FileUtils.isImage(file.filename()))) {
            fileNameView.setTextColor(highlightColor);
            fileView.setOnClickListener(this);
            fileView.setTag(file);
        }
        if (commentCount > 0) {
            TextView commentView = fileView.findViewById(R.id.comments);
            commentView.setText(String.valueOf(commentCount));
            commentView.setVisibility(View.VISIBLE);
        }
        parent.addView(fileView);
    }
    adjustVisibility(R.id.card_added, added);
    adjustVisibility(R.id.card_changed, changed);
    adjustVisibility(R.id.card_renamed, renamed);
    adjustVisibility(R.id.card_deleted, deleted);
    TextView tvSummary = mContentView.findViewById(R.id.tv_desc);
    tvSummary.setText(getString(R.string.commit_summary, added + changed + renamed + deleted, additions, deletions));
}
Also used : PositionalCommentBase(com.meisolsson.githubsdk.model.PositionalCommentBase) ForegroundColorSpan(android.text.style.ForegroundColorSpan) ViewGroup(android.view.ViewGroup) LayoutInflater(android.view.LayoutInflater) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) StyleableTextView(com.gh4a.widget.StyleableTextView) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 3 with PositionalCommentBase

use of com.meisolsson.githubsdk.model.PositionalCommentBase 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)3 SpannableStringBuilder (android.text.SpannableStringBuilder)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 LayoutInflater (android.view.LayoutInflater)1 ViewGroup (android.view.ViewGroup)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 StyleableTextView (com.gh4a.widget.StyleableTextView)1 GitHubFile (com.meisolsson.githubsdk.model.GitHubFile)1 Reactions (com.meisolsson.githubsdk.model.Reactions)1