Search in sources :

Example 1 with TimestampToastSpan

use of com.gh4a.widget.TimestampToastSpan in project gh4a by slapperwan.

the class EventViewHolder method formatEvent.

private CharSequence formatEvent(final IssueEvent event, final User user, int typefaceValue, boolean isPullRequestEvent) {
    String textBase = null;
    int textResId = 0;
    switch(event.event()) {
        case Closed:
            if (isPullRequestEvent) {
                textResId = event.commitId() != null ? R.string.pull_request_event_closed_with_commit : R.string.pull_request_event_closed;
            } else {
                textResId = event.commitId() != null ? R.string.issue_event_closed_with_commit : R.string.issue_event_closed;
            }
            break;
        case Reopened:
            textResId = isPullRequestEvent ? R.string.pull_request_event_reopened : R.string.issue_event_reopened;
            break;
        case Merged:
            textResId = event.commitId() != null ? R.string.pull_request_event_merged_with_commit : R.string.pull_request_event_merged;
            break;
        case Referenced:
            if (isPullRequestEvent) {
                textResId = event.commitId() != null ? R.string.pull_request_event_referenced_with_commit : R.string.pull_request_event_referenced;
            } else {
                textResId = event.commitId() != null ? R.string.issue_event_referenced_with_commit : R.string.issue_event_referenced;
            }
            break;
        case Assigned:
        case Unassigned:
            {
                boolean isAssign = event.event() == IssueEventType.Assigned;
                String actorLogin = user != null ? user.login() : null;
                String assigneeLogin = event.assignee() != null ? event.assignee().login() : null;
                if (assigneeLogin != null && assigneeLogin.equals(actorLogin)) {
                    if (isAssign) {
                        textResId = isPullRequestEvent ? R.string.pull_request_event_assigned_self : R.string.issue_event_assigned_self;
                    } else {
                        textResId = R.string.issue_event_unassigned_self;
                    }
                } else {
                    textResId = isAssign ? R.string.issue_event_assigned : R.string.issue_event_unassigned;
                    textBase = mContext.getString(textResId, ApiHelpers.getUserLogin(mContext, user), ApiHelpers.getUserLogin(mContext, event.assignee()));
                }
                break;
            }
        case Labeled:
            textResId = R.string.issue_event_labeled;
            break;
        case Unlabeled:
            textResId = R.string.issue_event_unlabeled;
            break;
        case Locked:
            textResId = R.string.issue_event_locked;
            break;
        case Unlocked:
            textResId = R.string.issue_event_unlocked;
            break;
        case Milestoned:
        case Demilestoned:
            textResId = event.event() == IssueEventType.Milestoned ? R.string.issue_event_milestoned : R.string.issue_event_demilestoned;
            textBase = mContext.getString(textResId, ApiHelpers.getUserLogin(mContext, user), event.milestone().title());
            break;
        case Renamed:
            {
                Rename rename = event.rename();
                textBase = mContext.getString(R.string.issue_event_renamed, ApiHelpers.getUserLogin(mContext, user), rename.from(), rename.to());
                break;
            }
        case ReviewRequested:
        case ReviewRequestRemoved:
            {
                final String reviewerNames;
                if (event.requestedReviewers() != null) {
                    ArrayList<String> reviewers = new ArrayList<>();
                    for (User reviewer : event.requestedReviewers()) {
                        reviewers.add(ApiHelpers.getUserLogin(mContext, reviewer));
                    }
                    reviewerNames = TextUtils.join(", ", reviewers);
                } else {
                    reviewerNames = ApiHelpers.getUserLogin(mContext, event.requestedReviewer());
                }
                @StringRes int stringResId = event.event() == IssueEventType.ReviewRequested ? R.string.pull_request_event_review_requested : R.string.pull_request_event_review_request_removed;
                textBase = mContext.getString(stringResId, ApiHelpers.getUserLogin(mContext, event.reviewRequester()), reviewerNames);
                break;
            }
        case HeadRefDeleted:
            textResId = R.string.pull_request_event_ref_deleted;
            break;
        case HeadRefRestored:
            textResId = R.string.pull_request_event_ref_restored;
            break;
        default:
            return null;
    }
    if (textBase == null) {
        textBase = mContext.getString(textResId, ApiHelpers.getUserLogin(mContext, user));
    }
    SpannableStringBuilder text = StringUtils.applyBoldTags(textBase, typefaceValue);
    int pos = text.toString().indexOf("[commit]");
    if (event.commitId() != null && pos >= 0) {
        text.replace(pos, pos + 8, event.commitId().substring(0, 7));
        text.setSpan(new IntentSpan(mContext, context -> {
            // The commit might be in a different repo. The API doesn't provide
            // that information directly, so get it indirectly by parsing the URL
            String repoOwner = mRepoOwner;
            String repoName = mRepoName;
            String url = event.commitUrl();
            if (url != null) {
                Matcher matcher = COMMIT_URL_REPO_NAME_AND_OWNER_PATTERN.matcher(url);
                if (matcher.find()) {
                    repoOwner = matcher.group(1);
                    repoName = matcher.group(2);
                }
            }
            return CommitActivity.makeIntent(context, repoOwner, repoName, event.commitId());
        }), pos, pos + 7, 0);
        text.setSpan(new TypefaceSpan("monospace"), pos, pos + 7, 0);
    }
    pos = text.toString().indexOf("[label]");
    Label label = event.label();
    if (label != null && pos >= 0) {
        int length = label.name().length();
        text.replace(pos, pos + 7, label.name());
        text.setSpan(new IssueLabelSpan(mContext, label, false), pos, pos + length, 0);
    }
    CharSequence time = event.createdAt() != null ? StringUtils.formatRelativeTime(mContext, event.createdAt(), true) : "";
    pos = text.toString().indexOf("[time]");
    if (pos >= 0) {
        text.replace(pos, pos + 6, time);
        if (event.createdAt() != null) {
            text.setSpan(new TimestampToastSpan(event.createdAt()), pos, pos + time.length(), 0);
        }
    }
    return text;
}
Also used : Context(android.content.Context) ImageView(android.widget.ImageView) Intent(android.content.Intent) StringRes(android.support.annotation.StringRes) HashMap(java.util.HashMap) Rename(com.meisolsson.githubsdk.model.Rename) TypefaceSpan(android.text.style.TypefaceSpan) UserActivity(com.gh4a.activities.UserActivity) UiUtils(com.gh4a.utils.UiUtils) ArrayList(java.util.ArrayList) IssueEventType(com.meisolsson.githubsdk.model.IssueEventType) User(com.meisolsson.githubsdk.model.User) SpannableStringBuilder(android.text.SpannableStringBuilder) Matcher(java.util.regex.Matcher) AvatarHandler(com.gh4a.utils.AvatarHandler) Label(com.meisolsson.githubsdk.model.Label) R(com.gh4a.R) View(android.view.View) IntentSpan(com.gh4a.widget.IntentSpan) TimestampToastSpan(com.gh4a.widget.TimestampToastSpan) IssueEvent(com.meisolsson.githubsdk.model.IssueEvent) StringUtils(com.gh4a.utils.StringUtils) ApiHelpers(com.gh4a.utils.ApiHelpers) TextUtils(android.text.TextUtils) StyleableTextView(com.gh4a.widget.StyleableTextView) IssueLabelSpan(com.gh4a.widget.IssueLabelSpan) CommitActivity(com.gh4a.activities.CommitActivity) TimelineItem(com.gh4a.model.TimelineItem) Pattern(java.util.regex.Pattern) User(com.meisolsson.githubsdk.model.User) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Label(com.meisolsson.githubsdk.model.Label) Rename(com.meisolsson.githubsdk.model.Rename) IntentSpan(com.gh4a.widget.IntentSpan) TimestampToastSpan(com.gh4a.widget.TimestampToastSpan) IssueLabelSpan(com.gh4a.widget.IssueLabelSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) TypefaceSpan(android.text.style.TypefaceSpan)

Example 2 with TimestampToastSpan

use of com.gh4a.widget.TimestampToastSpan in project gh4a by slapperwan.

the class ReviewViewHolder method formatTitle.

private void formatTitle(final Review review) {
    int textResId;
    switch(review.state()) {
        case Approved:
            textResId = R.string.pull_request_event_review_approved;
            break;
        case ChangesRequested:
            textResId = R.string.pull_request_event_review_requested_changes;
            break;
        case Dismissed:
        case Commented:
        default:
            textResId = R.string.pull_request_event_review_reviewed;
            break;
        case Pending:
            textResId = R.string.pull_request_event_review_started_review;
            break;
    }
    String login = review.user().login();
    String textBase = mContext.getString(textResId, login);
    SpannableStringBuilder text = StringUtils.applyBoldTags(textBase, mMessageView.getTypefaceValue());
    CharSequence time = review.submittedAt() != null ? StringUtils.formatRelativeTime(mContext, review.submittedAt(), true) : "";
    int pos = text.toString().indexOf("[time]");
    if (pos >= 0) {
        text.replace(pos, pos + 6, time);
        if (review.submittedAt() != null) {
            text.setSpan(new TimestampToastSpan(review.submittedAt()), pos, pos + time.length(), 0);
        }
    }
    mMessageView.setText(text);
}
Also used : TimestampToastSpan(com.gh4a.widget.TimestampToastSpan) Paint(android.graphics.Paint) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)2 TimestampToastSpan (com.gh4a.widget.TimestampToastSpan)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Paint (android.graphics.Paint)1 StringRes (android.support.annotation.StringRes)1 TextUtils (android.text.TextUtils)1 TypefaceSpan (android.text.style.TypefaceSpan)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 R (com.gh4a.R)1 CommitActivity (com.gh4a.activities.CommitActivity)1 UserActivity (com.gh4a.activities.UserActivity)1 TimelineItem (com.gh4a.model.TimelineItem)1 ApiHelpers (com.gh4a.utils.ApiHelpers)1 AvatarHandler (com.gh4a.utils.AvatarHandler)1 StringUtils (com.gh4a.utils.StringUtils)1 UiUtils (com.gh4a.utils.UiUtils)1 IntentSpan (com.gh4a.widget.IntentSpan)1 IssueLabelSpan (com.gh4a.widget.IssueLabelSpan)1