Search in sources :

Example 1 with EllipsizeLineSpan

use of com.gh4a.widget.EllipsizeLineSpan 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

Resources (android.content.res.Resources)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 TextAppearanceSpan (android.text.style.TextAppearanceSpan)1 CustomTypefaceSpan (com.gh4a.widget.CustomTypefaceSpan)1 EllipsizeLineSpan (com.gh4a.widget.EllipsizeLineSpan)1 GitHubEvent (com.meisolsson.githubsdk.model.GitHubEvent)1 GitHubEventType (com.meisolsson.githubsdk.model.GitHubEventType)1 PullRequest (com.meisolsson.githubsdk.model.PullRequest)1 Release (com.meisolsson.githubsdk.model.Release)1 ReviewComment (com.meisolsson.githubsdk.model.ReviewComment)1 Team (com.meisolsson.githubsdk.model.Team)1 User (com.meisolsson.githubsdk.model.User)1 GitComment (com.meisolsson.githubsdk.model.git.GitComment)1 GitCommit (com.meisolsson.githubsdk.model.git.GitCommit)1 CommitCommentPayload (com.meisolsson.githubsdk.model.payload.CommitCommentPayload)1 CreatePayload (com.meisolsson.githubsdk.model.payload.CreatePayload)1 DownloadPayload (com.meisolsson.githubsdk.model.payload.DownloadPayload)1 FollowPayload (com.meisolsson.githubsdk.model.payload.FollowPayload)1 ForkPayload (com.meisolsson.githubsdk.model.payload.ForkPayload)1 GollumPayload (com.meisolsson.githubsdk.model.payload.GollumPayload)1