From e6c8ed227f6558b60a0380d0ce9d75c78d653d50 Mon Sep 17 00:00:00 2001 From: Florens Douwes Date: Mon, 11 Aug 2014 12:59:51 +0200 Subject: [PATCH] Parse spans in chanparser Again --- .../floens/chan/core/loader/ChanParser.java | 59 +++++++++++++++++++ .../org/floens/chan/ui/view/PostView.java | 55 ----------------- .../org/floens/chan/utils/ThemeHelper.java | 6 ++ 3 files changed, 65 insertions(+), 55 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/core/loader/ChanParser.java b/Clover/app/src/main/java/org/floens/chan/core/loader/ChanParser.java index 0826f84c..1afded49 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/loader/ChanParser.java +++ b/Clover/app/src/main/java/org/floens/chan/core/loader/ChanParser.java @@ -18,16 +18,20 @@ package org.floens.chan.core.loader; +import android.content.res.TypedArray; import android.graphics.Typeface; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.AbsoluteSizeSpan; +import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.TypefaceSpan; +import android.text.style.UnderlineSpan; import org.floens.chan.ChanApplication; +import org.floens.chan.R; import org.floens.chan.core.model.Post; import org.floens.chan.core.model.PostLinkable; import org.floens.chan.utils.ThemeHelper; @@ -75,11 +79,66 @@ public class ChanParser { e.printStackTrace(); } + if (!post.parsedSpans) { + TypedArray ta = ThemeHelper.getInstance().getThemedContext().obtainStyledAttributes(null, R.styleable.PostView, R.attr.post_style, 0); + post.parsedSpans = true; + parseSpans(post, ta); + ta.recycle(); + } + if (post.rawComment != null) { post.comment = parseComment(post, post.rawComment); } } + private void parseSpans(Post post, TypedArray ta) { + int detailSize = ta.getDimensionPixelSize(R.styleable.PostView_detail_size, 0); + + if (!TextUtils.isEmpty(post.subject)) { + post.subjectSpan = new SpannableString(post.subject); + post.subjectSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_subject_color, 0)), 0, post.subjectSpan.length(), 0); + } + + if (!TextUtils.isEmpty(post.name)) { + post.nameSpan = new SpannableString(post.name); + post.nameSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_name_color, 0)), 0, post.nameSpan.length(), 0); + if (!TextUtils.isEmpty(post.email)) { + post.nameSpan.setSpan(new UnderlineSpan(), 0, post.nameSpan.length(), 0); + } + } + + if (!TextUtils.isEmpty(post.tripcode)) { + post.tripcodeSpan = new SpannableString(post.tripcode); + post.tripcodeSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_name_color, 0)), 0, post.tripcodeSpan.length(), 0); + post.tripcodeSpan.setSpan(new AbsoluteSizeSpan(detailSize), 0, post.tripcodeSpan.length(), 0); + } + + if (!TextUtils.isEmpty(post.id)) { + post.idSpan = new SpannableString(" ID: " + post.id + " "); + + // Stolen from the 4chan extension + int hash = post.id.hashCode(); + + int r = (hash >> 24) & 0xff; + int g = (hash >> 16) & 0xff; + int b = (hash >> 8) & 0xff; + + int idColor = (0xff << 24) + (r << 16) + (g << 8) + b; + boolean lightColor = (r * 0.299f) + (g * 0.587f) + (b * 0.114f) > 125f; + int idBgColor = lightColor ? ta.getColor(R.styleable.PostView_id_background_light, 0) : ta.getColor(R.styleable.PostView_id_background_dark, 0); + + post.idSpan.setSpan(new ForegroundColorSpan(idColor), 0, post.idSpan.length(), 0); + post.idSpan.setSpan(new BackgroundColorSpan(idBgColor), 0, post.idSpan.length(), 0); + post.idSpan.setSpan(new AbsoluteSizeSpan(detailSize), 0, post.idSpan.length(), 0); + } + + if (!TextUtils.isEmpty(post.capcode)) { + post.capcodeSpan = new SpannableString("Capcode: " + post.capcode); + post.capcodeSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_capcode_color, 0)), 0, post.capcodeSpan.length(), 0); + post.capcodeSpan.setSpan(new AbsoluteSizeSpan(detailSize), 0, post.capcodeSpan.length(), 0); + } + } + private CharSequence parseComment(Post post, String commentRaw) { CharSequence total = new SpannableString(""); diff --git a/Clover/app/src/main/java/org/floens/chan/ui/view/PostView.java b/Clover/app/src/main/java/org/floens/chan/ui/view/PostView.java index 83eddf10..e5965a2b 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/view/PostView.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/view/PostView.java @@ -28,10 +28,8 @@ import android.text.TextUtils; import android.text.format.DateUtils; import android.text.method.LinkMovementMethod; import android.text.style.AbsoluteSizeSpan; -import android.text.style.BackgroundColorSpan; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; -import android.text.style.UnderlineSpan; import android.util.AttributeSet; import android.util.TypedValue; import android.view.MotionEvent; @@ -122,11 +120,6 @@ public class PostView extends LinearLayout implements View.OnClickListener { TypedArray ta = context.obtainStyledAttributes(null, R.styleable.PostView, R.attr.post_style, 0); - if (!post.parsedSpans) { - parseSpans(post, ta); - post.parsedSpans = true; - } - if (!isBuild) { buildView(context, ta); isBuild = true; @@ -272,54 +265,6 @@ public class PostView extends LinearLayout implements View.OnClickListener { } } - private void parseSpans(Post post, TypedArray ta) { - int detailSize = ta.getDimensionPixelSize(R.styleable.PostView_detail_size, 0); - - if (!TextUtils.isEmpty(post.subject)) { - post.subjectSpan = new SpannableString(post.subject); - post.subjectSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_subject_color, 0)), 0, post.subjectSpan.length(), 0); - } - - if (!TextUtils.isEmpty(post.name)) { - post.nameSpan = new SpannableString(post.name); - post.nameSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_name_color, 0)), 0, post.nameSpan.length(), 0); - if (!TextUtils.isEmpty(post.email)) { - post.nameSpan.setSpan(new UnderlineSpan(), 0, post.nameSpan.length(), 0); - } - } - - if (!TextUtils.isEmpty(post.tripcode)) { - post.tripcodeSpan = new SpannableString(post.tripcode); - post.tripcodeSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_name_color, 0)), 0, post.tripcodeSpan.length(), 0); - post.tripcodeSpan.setSpan(new AbsoluteSizeSpan(detailSize), 0, post.tripcodeSpan.length(), 0); - } - - if (!TextUtils.isEmpty(post.id)) { - post.idSpan = new SpannableString(" ID: " + post.id + " "); - - // Stolen from the 4chan extension - int hash = post.id.hashCode(); - - int r = (hash >> 24) & 0xff; - int g = (hash >> 16) & 0xff; - int b = (hash >> 8) & 0xff; - - int idColor = (0xff << 24) + (r << 16) + (g << 8) + b; - boolean lightColor = (r * 0.299f) + (g * 0.587f) + (b * 0.114f) > 125f; - int idBgColor = lightColor ? ta.getColor(R.styleable.PostView_id_background_light, 0) : ta.getColor(R.styleable.PostView_id_background_dark, 0); - - post.idSpan.setSpan(new ForegroundColorSpan(idColor), 0, post.idSpan.length(), 0); - post.idSpan.setSpan(new BackgroundColorSpan(idBgColor), 0, post.idSpan.length(), 0); - post.idSpan.setSpan(new AbsoluteSizeSpan(detailSize), 0, post.idSpan.length(), 0); - } - - if (!TextUtils.isEmpty(post.capcode)) { - post.capcodeSpan = new SpannableString("Capcode: " + post.capcode); - post.capcodeSpan.setSpan(new ForegroundColorSpan(ta.getColor(R.styleable.PostView_capcode_color, 0)), 0, post.capcodeSpan.length(), 0); - post.capcodeSpan.setSpan(new AbsoluteSizeSpan(detailSize), 0, post.capcodeSpan.length(), 0); - } - } - private void buildView(final Context context, TypedArray ta) { int thumbnailBackground = ta.getColor(R.styleable.PostView_thumbnail_background, 0); int replyCountColor = ta.getColor(R.styleable.PostView_reply_count_color, 0); diff --git a/Clover/app/src/main/java/org/floens/chan/utils/ThemeHelper.java b/Clover/app/src/main/java/org/floens/chan/utils/ThemeHelper.java index 0e53497b..cea10ba2 100644 --- a/Clover/app/src/main/java/org/floens/chan/utils/ThemeHelper.java +++ b/Clover/app/src/main/java/org/floens/chan/utils/ThemeHelper.java @@ -42,6 +42,7 @@ public class ThemeHelper { } private static ThemeHelper instance; + private Context context; private int quoteColor; private int linkColor; private int spoilerColor; @@ -82,7 +83,12 @@ public class ThemeHelper { return theme; } + public Context getThemedContext() { + return context; + } + public void reloadPostViewColors(Context context) { + this.context = context; TypedArray ta = context.obtainStyledAttributes(null, R.styleable.PostView, R.attr.post_style, 0); quoteColor = ta.getColor(R.styleable.PostView_quote_color, 0); linkColor = ta.getColor(R.styleable.PostView_link_color, 0);