Search in sources :

Example 16 with FontPreferences

use of me.ccrama.redditslide.Visuals.FontPreferences 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;
}
Also used : RelativeSizeSpan(android.text.style.RelativeSizeSpan) FontPreferences(me.ccrama.redditslide.Visuals.FontPreferences) Bitmap(android.graphics.Bitmap) RoundedBackgroundSpan(me.ccrama.redditslide.Views.RoundedBackgroundSpan) TypedArray(android.content.res.TypedArray) Resources(android.content.res.Resources) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan) TypedValue(android.util.TypedValue)

Example 17 with FontPreferences

use of me.ccrama.redditslide.Visuals.FontPreferences in project Slide by ccrama.

the class CommentOverflow method setViews.

/**
 * Set the text for the corresponding views.
 *
 * @param blocks    list of all blocks to be set
 * @param subreddit
 */
public void setViews(List<String> blocks, String subreddit, OnClickListener click, OnLongClickListener longClick) {
    Context context = getContext();
    int type = new FontPreferences(context).getFontTypeComment().getTypeface();
    if (type >= 0) {
        typeface = RobotoTypefaces.obtainTypeface(context, type);
    } else {
        typeface = Typeface.DEFAULT;
    }
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    theme.resolveAttribute(R.attr.fontColor, typedValue, true);
    textColor = typedValue.data;
    TypedValue fontSizeTypedValue = new TypedValue();
    theme.resolveAttribute(R.attr.font_commentbody, fontSizeTypedValue, true);
    TypedArray a = context.obtainStyledAttributes(null, new int[] { R.attr.font_commentbody }, R.attr.font_commentbody, new FontPreferences(context).getCommentFontStyle().getResId());
    fontSize = a.getDimensionPixelSize(0, -1);
    a.recycle();
    removeAllViews();
    if (!blocks.isEmpty()) {
        setVisibility(View.VISIBLE);
    }
    for (String block : blocks) {
        if (block.startsWith("<table>")) {
            HorizontalScrollView scrollView = new HorizontalScrollView(context);
            scrollView.setScrollbarFadingEnabled(false);
            TableLayout table = formatTable(block, subreddit, click, longClick);
            scrollView.setLayoutParams(MARGIN_PARAMS);
            table.setPaddingRelative(0, 0, 0, Reddit.dpToPxVertical(10));
            scrollView.addView(table);
            addView(scrollView);
        } else if (block.equals("<hr/>")) {
            View line = new View(context);
            line.setLayoutParams(HR_PARAMS);
            line.setBackgroundColor(textColor);
            line.setAlpha(0.6f);
            addView(line);
        } else if (block.startsWith("<pre>")) {
            HorizontalScrollView scrollView = new HorizontalScrollView(context);
            scrollView.setScrollbarFadingEnabled(false);
            SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context);
            newTextView.setTextHtml(block, subreddit);
            setStyle(newTextView, subreddit);
            scrollView.setLayoutParams(MARGIN_PARAMS);
            newTextView.setPaddingRelative(0, 0, 0, Reddit.dpToPxVertical(10));
            scrollView.addView(newTextView);
            if (click != null)
                newTextView.setOnClickListener(click);
            if (longClick != null)
                newTextView.setOnLongClickListener(longClick);
            addView(scrollView);
        } else {
            SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context);
            newTextView.setTextHtml(block, subreddit);
            setStyle(newTextView, subreddit);
            newTextView.setLayoutParams(MARGIN_PARAMS);
            if (click != null)
                newTextView.setOnClickListener(click);
            if (longClick != null)
                newTextView.setOnLongClickListener(longClick);
            addView(newTextView);
        }
    }
}
Also used : Context(android.content.Context) FontPreferences(me.ccrama.redditslide.Visuals.FontPreferences) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TypedArray(android.content.res.TypedArray) Resources(android.content.res.Resources) HorizontalScrollView(android.widget.HorizontalScrollView) TableLayout(android.widget.TableLayout) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TypedValue(android.util.TypedValue)

Example 18 with FontPreferences

use of me.ccrama.redditslide.Visuals.FontPreferences in project Slide by ccrama.

the class RedditItemView method doComment.

public void doComment(Comment comment, View content) {
    ProfileCommentViewHolder holder = new ProfileCommentViewHolder(content);
    String scoreText;
    if (comment.isScoreHidden()) {
        scoreText = "[" + getContext().getString(R.string.misc_score_hidden).toUpperCase() + "]";
    } else {
        scoreText = String.format(Locale.getDefault(), "%d", comment.getScore());
    }
    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", getContext().getResources().getQuantityString(R.plurals.points, comment.getScore())));
    }
    holder.score.setText(score);
    if (Authentication.isLoggedIn) {
        if (ActionStates.getVoteDirection(comment) == VoteDirection.UPVOTE) {
            holder.score.setTextColor(getContext().getResources().getColor(R.color.md_orange_500));
        } else if (ActionStates.getVoteDirection(comment) == VoteDirection.DOWNVOTE) {
            holder.score.setTextColor(getContext().getResources().getColor(R.color.md_blue_500));
        } else {
            holder.score.setTextColor(holder.time.getCurrentTextColor());
        }
    }
    String spacer = getContext().getString(R.string.submission_properties_seperator);
    SpannableStringBuilder titleString = new SpannableStringBuilder();
    String timeAgo = TimeUtils.getTimeAgo(comment.getCreated().getTime(), getContext());
    String time = ((timeAgo == null || timeAgo.isEmpty()) ? "just now" : // some users were crashing here
    timeAgo);
    time = time + (((comment.getEditDate() != null) ? " (edit " + TimeUtils.getTimeAgo(comment.getEditDate().getTime(), getContext()) + ")" : ""));
    titleString.append(time);
    titleString.append(spacer);
    if (comment.getSubredditName() != null) {
        String subname = comment.getSubredditName();
        SpannableStringBuilder subreddit = new SpannableStringBuilder("/r/" + subname);
        if ((SettingValues.colorSubName && Palette.getColor(subname) != Palette.getDefaultColor())) {
            subreddit.setSpan(new ForegroundColorSpan(Palette.getColor(subname)), 0, subreddit.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            subreddit.setSpan(new StyleSpan(Typeface.BOLD), 0, subreddit.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        titleString.append(subreddit);
    }
    holder.time.setText(titleString);
    setViews(comment.getDataNode().get("body_html").asText(), comment.getSubredditName(), holder);
    int type = new FontPreferences(getContext()).getFontTypeComment().getTypeface();
    Typeface typeface;
    if (type >= 0) {
        typeface = RobotoTypefaces.obtainTypeface(getContext(), type);
    } else {
        typeface = Typeface.DEFAULT;
    }
    holder.content.setTypeface(typeface);
    if (comment.getTimesGilded() > 0) {
        final String timesGilded = (comment.getTimesGilded() == 1) ? "" : "\u200Ax" + Integer.toString(comment.getTimesGilded());
        SpannableStringBuilder gilded = new SpannableStringBuilder("\u00A0★" + timesGilded + "\u00A0");
        TypedArray a = getContext().obtainStyledAttributes(new FontPreferences(getContext()).getPostFontStyle().getResId(), R.styleable.FontStyle);
        int fontsize = (int) (a.getDimensionPixelSize(R.styleable.FontStyle_font_cardtitle, -1) * .75);
        a.recycle();
        Bitmap image = BitmapFactory.decodeResource(getContext().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(getContext(), image, ImageSpan.ALIGN_BASELINE), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        gilded.setSpan(new RelativeSizeSpan(0.75f), 3, gilded.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.gild.setVisibility(View.VISIBLE);
        ((TextView) holder.gild).setText(gilded);
    } else if (holder.gild.getVisibility() == View.VISIBLE) {
        holder.gild.setVisibility(View.GONE);
    }
    if (comment.getSubmissionTitle() != null) {
        holder.title.setText(Html.fromHtml(comment.getSubmissionTitle()));
    } else {
        holder.title.setText(Html.fromHtml(comment.getAuthor()));
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) Typeface(android.graphics.Typeface) ProfileCommentViewHolder(me.ccrama.redditslide.Adapters.ProfileCommentViewHolder) RelativeSizeSpan(android.text.style.RelativeSizeSpan) FontPreferences(me.ccrama.redditslide.Visuals.FontPreferences) Bitmap(android.graphics.Bitmap) StyleSpan(android.text.style.StyleSpan) TypedArray(android.content.res.TypedArray) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 19 with FontPreferences

use of me.ccrama.redditslide.Visuals.FontPreferences in project Slide by ccrama.

the class BaseActivity method applyColorTheme.

/**
 * Applies the activity's base color theme. Should be called before inflating any layouts.
 */
protected void applyColorTheme() {
    getTheme().applyStyle(new FontPreferences(this).getCommentFontStyle().getResId(), true);
    getTheme().applyStyle(new FontPreferences(this).getPostFontStyle().getResId(), true);
    getTheme().applyStyle(new ColorPreferences(this).getFontStyle().getBaseId(), true);
}
Also used : FontPreferences(me.ccrama.redditslide.Visuals.FontPreferences) ColorPreferences(me.ccrama.redditslide.ColorPreferences)

Aggregations

FontPreferences (me.ccrama.redditslide.Visuals.FontPreferences)19 View (android.view.View)9 TypedArray (android.content.res.TypedArray)8 Typeface (android.graphics.Typeface)8 RecyclerView (android.support.v7.widget.RecyclerView)7 SpannableStringBuilder (android.text.SpannableStringBuilder)7 RelativeSizeSpan (android.text.style.RelativeSizeSpan)6 TextView (android.widget.TextView)6 ColorPreferences (me.ccrama.redditslide.ColorPreferences)6 Intent (android.content.Intent)5 Bitmap (android.graphics.Bitmap)5 ForegroundColorSpan (android.text.style.ForegroundColorSpan)5 ImageSpan (android.text.style.ImageSpan)5 StyleSpan (android.text.style.StyleSpan)5 ImageView (android.widget.ImageView)5 LinearLayout (android.widget.LinearLayout)5 SpoilerRobotoTextView (me.ccrama.redditslide.SpoilerRobotoTextView)5 Resources (android.content.res.Resources)4 TypedValue (android.util.TypedValue)4 DialogInterface (android.content.DialogInterface)3