use of me.ccrama.redditslide.Views.RoundedBackgroundSpan in project Slide by ccrama.
the class CommentAdapterHelper method getScoreString.
public static Spannable getScoreString(Comment comment, Context mContext, CommentViewHolder holder, Submission submission, CommentAdapter adapter) {
final String spacer = " " + mContext.getString(R.string.submission_properties_seperator_comments) + " ";
SpannableStringBuilder titleString = // zero width space to fix first span height
new SpannableStringBuilder("\u200B");
SpannableStringBuilder author = new SpannableStringBuilder(comment.getAuthor());
final int authorcolor = Palette.getFontColorUser(comment.getAuthor());
author.setSpan(new TypefaceSpan("sans-serif-condensed"), 0, author.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
author.setSpan(new StyleSpan(Typeface.BOLD), 0, author.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (comment.getDistinguishedStatus() == DistinguishedStatus.ADMIN) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_red_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (comment.getDistinguishedStatus() == DistinguishedStatus.SPECIAL) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_red_500, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (comment.getDistinguishedStatus() == DistinguishedStatus.MODERATOR) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_green_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (Authentication.name != null && comment.getAuthor().toLowerCase(Locale.ENGLISH).equals(Authentication.name.toLowerCase(Locale.ENGLISH))) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_deep_orange_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (submission != null && comment.getAuthor().toLowerCase(Locale.ENGLISH).equals(submission.getAuthor().toLowerCase(Locale.ENGLISH)) && !comment.getAuthor().equals("[deleted]")) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_blue_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (authorcolor != 0) {
author.setSpan(new ForegroundColorSpan(authorcolor), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
titleString.append(author);
titleString.append(spacer);
int scoreColor;
switch(ActionStates.getVoteDirection(comment)) {
case UPVOTE:
scoreColor = (holder.textColorUp);
break;
case DOWNVOTE:
scoreColor = (holder.textColorDown);
break;
default:
scoreColor = (holder.textColorRegular);
break;
}
String scoreText;
if (comment.isScoreHidden()) {
scoreText = "[" + mContext.getString(R.string.misc_score_hidden).toUpperCase() + "]";
} else {
scoreText = String.format(Locale.getDefault(), "%d", getScoreText(comment));
}
SpannableStringBuilder score = new SpannableStringBuilder(scoreText);
if (score == null || score.toString().isEmpty()) {
score = new SpannableStringBuilder("0");
}
if (!scoreText.contains("[")) {
score.append(String.format(Locale.getDefault(), " %s", mContext.getResources().getQuantityString(R.plurals.points, comment.getScore())));
}
score.setSpan(new ForegroundColorSpan(scoreColor), 0, score.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(score);
titleString.append((comment.isControversial() ? " †" : ""));
titleString.append(spacer);
Long time = comment.getCreated().getTime();
String timeAgo = TimeUtils.getTimeAgo(time, mContext);
SpannableStringBuilder timeSpan = new SpannableStringBuilder().append((timeAgo == null || timeAgo.isEmpty()) ? "just now" : timeAgo);
if (SettingValues.highlightTime && adapter.lastSeen != 0 && adapter.lastSeen < time && !adapter.dataSet.single && SettingValues.commentLastVisit) {
timeSpan.setSpan(new RoundedBackgroundSpan(Color.WHITE, Palette.getColor(comment.getSubredditName()), false, mContext), 0, timeSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
titleString.append(timeSpan);
titleString.append(((comment.getEditDate() != null) ? " (edit " + TimeUtils.getTimeAgo(comment.getEditDate().getTime(), mContext) + ")" : ""));
titleString.append(" ");
if (comment.getDataNode().get("stickied").asBoolean()) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + mContext.getString(R.string.submission_stickied).toUpperCase() + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_green_300, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (comment.getTimesGilded() > 0) {
// if the comment has only been gilded once, don't show a number
final String timesGilded = (comment.getTimesGilded() == 1) ? "" : "\u200Ax" + Integer.toString(comment.getTimesGilded());
SpannableStringBuilder gilded = new SpannableStringBuilder("\u00A0★" + timesGilded + "\u00A0");
TypedArray a = mContext.obtainStyledAttributes(new FontPreferences(mContext).getPostFontStyle().getResId(), R.styleable.FontStyle);
int fontsize = (int) (a.getDimensionPixelSize(R.styleable.FontStyle_font_cardtitle, -1) * .75);
a.recycle();
Bitmap image = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gold);
float aspectRatio = (float) (1.00 * image.getWidth() / image.getHeight());
image = Bitmap.createScaledBitmap(image, (int) Math.ceil(fontsize * aspectRatio), (int) Math.ceil(fontsize), true);
gilded.setSpan(new ImageSpan(mContext, image, ImageSpan.ALIGN_BASELINE), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
gilded.setSpan(new RelativeSizeSpan(0.75f), 3, gilded.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(gilded);
titleString.append(" ");
}
if (UserTags.isUserTagged(comment.getAuthor())) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + UserTags.getUserTag(comment.getAuthor()) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_blue_500, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (UserSubscriptions.friends.contains(comment.getAuthor())) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + mContext.getString(R.string.profile_friend) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_deep_orange_500, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (comment.getAuthorFlair() != null) {
String flairText;
if (comment.getAuthorFlair().getText() != null && !comment.getAuthorFlair().getText().isEmpty()) {
flairText = comment.getAuthorFlair().getText();
} else {
flairText = comment.getAuthorFlair().getCssClass();
}
TypedValue typedValue = new TypedValue();
Resources.Theme theme = mContext.getTheme();
theme.resolveAttribute(R.attr.activity_background, typedValue, true);
int color = typedValue.data;
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + Html.fromHtml(flairText) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(holder.firstTextView.getCurrentTextColor(), color, false, mContext), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (adapter.removed.contains(comment.getFullName()) || (comment.getBannedBy() != null && !adapter.approved.contains(comment.getFullName()))) {
titleString.append(CommentAdapterHelper.createRemovedLine((comment.getBannedBy() == null) ? Authentication.name : comment.getBannedBy(), mContext));
} else if (adapter.approved.contains(comment.getFullName()) || (comment.getApprovedBy() != null && !adapter.removed.contains(comment.getFullName()))) {
titleString.append(CommentAdapterHelper.createApprovedLine((comment.getApprovedBy() == null) ? Authentication.name : comment.getApprovedBy(), mContext));
}
return titleString;
}
use of me.ccrama.redditslide.Views.RoundedBackgroundSpan in project Slide by ccrama.
the class CommentAdapterSearch method doScoreText.
public void doScoreText(CommentViewHolder holder, Comment comment, int offset) {
String spacer = " " + mContext.getString(R.string.submission_properties_seperator_comments) + " ";
SpannableStringBuilder titleString = new SpannableStringBuilder();
SpannableStringBuilder author = new SpannableStringBuilder(comment.getAuthor());
final int authorcolor = Palette.getFontColorUser(comment.getAuthor());
author.setSpan(new TypefaceSpan("sans-serif-condensed"), 0, author.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
author.setSpan(new StyleSpan(Typeface.BOLD), 0, author.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (comment.getDistinguishedStatus() == DistinguishedStatus.ADMIN) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_red_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (comment.getDistinguishedStatus() == DistinguishedStatus.SPECIAL) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_red_500, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (comment.getDistinguishedStatus() == DistinguishedStatus.MODERATOR) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_green_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (Authentication.name != null && comment.getAuthor().toLowerCase(Locale.ENGLISH).equals(Authentication.name.toLowerCase(Locale.ENGLISH))) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_deep_orange_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else /* todoelse if (submission != null && comment.getAuthor()
.toLowerCase(Locale.ENGLISH)
.equals(submission.getAuthor().toLowerCase(Locale.ENGLISH)) && !comment.getAuthor().equals("[deleted]")) {
author.replace(0, author.length(), " " + comment.getAuthor() + " ");
author.setSpan(
new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_blue_300, false),
0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} */
if (authorcolor != 0) {
author.setSpan(new ForegroundColorSpan(authorcolor), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
titleString.append(author);
titleString.append(spacer);
String scoreText;
if (comment.isScoreHidden()) {
scoreText = "[" + mContext.getString(R.string.misc_score_hidden).toUpperCase() + "]";
} else {
scoreText = String.format(Locale.getDefault(), "%d", comment.getScore() + offset);
}
SpannableStringBuilder score = new SpannableStringBuilder(scoreText);
titleString.append(score);
if (!scoreText.contains("[")) {
titleString.append(mContext.getResources().getQuantityString(R.plurals.points, comment.getScore()));
}
titleString.append((comment.isControversial() ? " †" : ""));
titleString.append(spacer);
String timeAgo = TimeUtils.getTimeAgo(comment.getCreated().getTime(), mContext);
titleString.append((timeAgo == null || timeAgo.isEmpty()) ? "just now" : // some users were crashing here
timeAgo);
titleString.append(((comment.getEditDate() != null) ? " (edit " + TimeUtils.getTimeAgo(comment.getEditDate().getTime(), mContext) + ")" : ""));
titleString.append(" ");
if (comment.getDataNode().get("stickied").asBoolean()) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + mContext.getString(R.string.submission_stickied).toUpperCase() + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_green_300, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (UserTags.isUserTagged(comment.getAuthor())) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + UserTags.getUserTag(comment.getAuthor()) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_blue_500, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (comment.getTimesGilded() > 0) {
final String timesGilded = (comment.getTimesGilded() == 1) ? "" : "\u200Ax" + Integer.toString(comment.getTimesGilded());
SpannableStringBuilder gilded = new SpannableStringBuilder("\u00A0★" + timesGilded + "\u00A0");
TypedArray a = mContext.obtainStyledAttributes(new FontPreferences(mContext).getPostFontStyle().getResId(), R.styleable.FontStyle);
int fontsize = (int) (a.getDimensionPixelSize(R.styleable.FontStyle_font_cardtitle, -1) * .75);
a.recycle();
Bitmap image = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gold);
float aspectRatio = (float) (1.00 * image.getWidth() / image.getHeight());
image = Bitmap.createScaledBitmap(image, (int) Math.ceil(fontsize * aspectRatio), (int) Math.ceil(fontsize), true);
gilded.setSpan(new ImageSpan(mContext, image, ImageSpan.ALIGN_BASELINE), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
gilded.setSpan(new RelativeSizeSpan(0.75f), 3, gilded.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(gilded);
titleString.append(" ");
}
if (UserSubscriptions.friends.contains(comment.getAuthor())) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + mContext.getString(R.string.profile_friend) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_deep_orange_500, false), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
if (comment.getAuthorFlair() != null && !comment.getAuthorFlair().getText().isEmpty() && !comment.getAuthorFlair().getText().isEmpty()) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = mContext.getTheme();
theme.resolveAttribute(R.attr.activity_background, typedValue, true);
int color = typedValue.data;
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + Html.fromHtml(comment.getAuthorFlair().getText()) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(holder.firstTextView.getCurrentTextColor(), color, false, mContext), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(pinned);
titleString.append(" ");
}
holder.content.setText(titleString);
}
use of me.ccrama.redditslide.Views.RoundedBackgroundSpan in project Slide by ccrama.
the class SubmissionCache method getTitleSpannable.
private static SpannableStringBuilder getTitleSpannable(Submission submission, String flairOverride, Context mContext) {
SpannableStringBuilder titleString = new SpannableStringBuilder();
titleString.append(Html.fromHtml(submission.getTitle()));
if (submission.isStickied()) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + mContext.getString(R.string.submission_stickied).toUpperCase() + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_green_300, true), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(" ");
titleString.append(pinned);
}
if (submission.getTimesGilded() > 0) {
// if the post has only been gilded once, don't show a number
final String timesGilded = (submission.getTimesGilded() == 1) ? "" : "\u200Ax" + Integer.toString(submission.getTimesGilded());
SpannableStringBuilder gilded = new SpannableStringBuilder("\u00A0★" + timesGilded + "\u00A0");
TypedArray a = mContext.obtainStyledAttributes(new FontPreferences(mContext).getPostFontStyle().getResId(), R.styleable.FontStyle);
int fontsize = (int) (a.getDimensionPixelSize(R.styleable.FontStyle_font_cardtitle, -1) * .75);
a.recycle();
Bitmap image = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gold);
float aspectRatio = (float) (1.00 * image.getWidth() / image.getHeight());
image = Bitmap.createScaledBitmap(image, (int) Math.ceil(fontsize * aspectRatio), (int) Math.ceil(fontsize), true);
gilded.setSpan(new ImageSpan(mContext, image, ImageSpan.ALIGN_BASELINE), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
gilded.setSpan(new RelativeSizeSpan(0.75f), 3, gilded.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(" ");
titleString.append(gilded);
}
if (submission.isNsfw()) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0NSFW\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_red_300, true), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(" ");
titleString.append(pinned);
}
if (submission.getDataNode().get("spoiler").asBoolean()) {
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0SPOILER\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(mContext, R.color.white, R.color.md_grey_600, true), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(" ");
titleString.append(pinned);
}
if (submission.getSubmissionFlair().getText() != null && !submission.getSubmissionFlair().getText().isEmpty() || flairOverride != null || (submission.getSubmissionFlair().getCssClass() != null)) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = mContext.getTheme();
theme.resolveAttribute(R.attr.activity_background, typedValue, false);
int color = typedValue.data;
theme.resolveAttribute(R.attr.fontColor, typedValue, false);
int font = typedValue.data;
String flairString;
if (flairOverride != null) {
flairString = flairOverride;
} else if ((submission.getSubmissionFlair().getText() == null || submission.getSubmissionFlair().getText().isEmpty()) && submission.getSubmissionFlair().getCssClass() != null) {
flairString = submission.getSubmissionFlair().getCssClass();
} else {
flairString = submission.getSubmissionFlair().getText();
}
SpannableStringBuilder pinned = new SpannableStringBuilder("\u00A0" + Html.fromHtml(flairString) + "\u00A0");
pinned.setSpan(new RoundedBackgroundSpan(font, color, true, mContext), 0, pinned.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
titleString.append(" ");
titleString.append(pinned);
}
return titleString;
}
Aggregations