Support image post flair.

This commit is contained in:
Alex Ning 2020-03-28 22:03:35 +08:00
parent 188d58b5da
commit b1d5be17cd
5 changed files with 45 additions and 12 deletions

View File

@ -555,9 +555,16 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((PostDetailViewHolder) holder).mSpoilerTextView.setVisibility(View.VISIBLE); ((PostDetailViewHolder) holder).mSpoilerTextView.setVisibility(View.VISIBLE);
} }
if (mPost.getFlair() != null) { if (mPost.getFlair() != null && !mPost.getFlair().equals("")) {
((PostDetailViewHolder) holder).mFlairTextView.setVisibility(View.VISIBLE); ((PostDetailViewHolder) holder).mFlairTextView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).mFlairTextView.setText(mPost.getFlair()); Spannable flairHTML;
GlideImageGetter glideImageGetter = new GlideImageGetter(((PostDetailViewHolder) holder).mFlairTextView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
flairHTML = (Spannable) Html.fromHtml(mPost.getFlair(), Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null);
} else {
flairHTML = (Spannable) Html.fromHtml(mPost.getFlair(), glideImageGetter, null);
}
((PostDetailViewHolder) holder).mFlairTextView.setText(flairHTML);
} }
if (mPost.isNSFW()) { if (mPost.isNSFW()) {

View File

@ -7,6 +7,8 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -66,6 +68,7 @@ import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.SaveThing;
import ml.docilealligator.infinityforreddit.User.UserDao; import ml.docilealligator.infinityforreddit.User.UserDao;
import ml.docilealligator.infinityforreddit.Utils.GlideImageGetter;
import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import ml.docilealligator.infinityforreddit.Utils.RedditUtils;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.Utils.Utils;
@ -446,9 +449,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE); ((PostViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE);
} }
if (flair != null) { if (flair != null && !flair.equals("")) {
((PostViewHolder) holder).flairTextView.setVisibility(View.VISIBLE); ((PostViewHolder) holder).flairTextView.setVisibility(View.VISIBLE);
((PostViewHolder) holder).flairTextView.setText(flair); Spannable flairHTML;
GlideImageGetter glideImageGetter = new GlideImageGetter(((PostViewHolder) holder).flairTextView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
flairHTML = (Spannable) Html.fromHtml(flair, Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null);
} else {
flairHTML = (Spannable) Html.fromHtml(flair, glideImageGetter, null);
}
((PostViewHolder) holder).flairTextView.setText(flairHTML);
} }
switch (voteType) { switch (voteType) {
@ -1001,9 +1011,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostCompactViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE); ((PostCompactViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE);
} }
if (flair != null) { if (flair != null && !flair.equals("")) {
((PostCompactViewHolder) holder).flairTextView.setVisibility(View.VISIBLE); ((PostCompactViewHolder) holder).flairTextView.setVisibility(View.VISIBLE);
((PostCompactViewHolder) holder).flairTextView.setText(flair); Spannable flairHTML;
GlideImageGetter glideImageGetter = new GlideImageGetter(((PostCompactViewHolder) holder).flairTextView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
flairHTML = (Spannable) Html.fromHtml(flair, Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null);
} else {
flairHTML = (Spannable) Html.fromHtml(flair, glideImageGetter, null);
}
((PostCompactViewHolder) holder).flairTextView.setText(flairHTML);
} }
switch (voteType) { switch (voteType) {

View File

@ -50,10 +50,20 @@ public class ParsePost {
boolean archived = data.getBoolean(JSONUtils.ARCHIVED_KEY); boolean archived = data.getBoolean(JSONUtils.ARCHIVED_KEY);
boolean locked = data.getBoolean(JSONUtils.LOCKEC_KEY); boolean locked = data.getBoolean(JSONUtils.LOCKEC_KEY);
boolean saved = data.getBoolean(JSONUtils.SAVED_KEY); boolean saved = data.getBoolean(JSONUtils.SAVED_KEY);
String flair = null; StringBuilder postFlairHTMLBuilder = new StringBuilder();
if (!data.isNull(JSONUtils.LINK_FLAIR_TEXT_KEY)) { if (data.has(JSONUtils.LINK_FLAIR_RICHTEXT_KEY)) {
flair = data.getString(JSONUtils.LINK_FLAIR_TEXT_KEY); JSONArray flairArray = data.getJSONArray(JSONUtils.LINK_FLAIR_RICHTEXT_KEY);
for (int i = 0; i < flairArray.length(); i++) {
JSONObject flairObject = flairArray.getJSONObject(i);
String e = flairObject.getString(JSONUtils.E_KEY);
if (e.equals("text")) {
postFlairHTMLBuilder.append(flairObject.getString(JSONUtils.T_KEY));
} else if (e.equals("emoji")) {
postFlairHTMLBuilder.append("<img src=\"").append(flairObject.getString(JSONUtils.U_KEY)).append("\">");
} }
}
}
String flair = postFlairHTMLBuilder.toString();
if (data.isNull(JSONUtils.LIKES_KEY)) { if (data.isNull(JSONUtils.LIKES_KEY)) {
voteType = 0; voteType = 0;

View File

@ -82,7 +82,7 @@ public class GlideImageGetter implements Html.ImageGetter {
int drawableWidth = (int) (drawable.getIntrinsicWidth() * density); int drawableWidth = (int) (drawable.getIntrinsicWidth() * density);
int drawableHeight = (int) (drawable.getIntrinsicHeight() * density); int drawableHeight = (int) (drawable.getIntrinsicHeight() * density);
float ratio = (float) drawableWidth / (float) drawableHeight; float ratio = (float) drawableWidth / (float) drawableHeight;
drawableHeight = (int) textSize + 10; drawableHeight = (int) textSize;
drawableWidth = (int) (drawableHeight * ratio); drawableWidth = (int) (drawableHeight * ratio);
int maxWidth = container.get().getMeasuredWidth(); int maxWidth = container.get().getMeasuredWidth();
if ((drawableWidth > maxWidth) || matchParentWidth) { if ((drawableWidth > maxWidth) || matchParentWidth) {

View File

@ -16,13 +16,12 @@ public class JSONUtils {
public static final String SELFTEXT_KEY = "selftext"; public static final String SELFTEXT_KEY = "selftext";
public static final String SELFTEXT_HTML_KEY = "selftext_html"; public static final String SELFTEXT_HTML_KEY = "selftext_html";
public static final String AUTHOR_KEY = "author"; public static final String AUTHOR_KEY = "author";
public static final String AUTHOR_FLAIR_TEXT_KEY = "author_flair_text";
public static final String AUTHOR_FLAIR_RICHTEXT_KEY = "author_flair_richtext"; public static final String AUTHOR_FLAIR_RICHTEXT_KEY = "author_flair_richtext";
public static final String E_KEY = "e"; public static final String E_KEY = "e";
public static final String T_KEY = "t"; public static final String T_KEY = "t";
public static final String U_KEY = "u"; public static final String U_KEY = "u";
public static final String LINK_AUTHOR_KEY = "link_author"; public static final String LINK_AUTHOR_KEY = "link_author";
public static final String LINK_FLAIR_TEXT_KEY = "link_flair_text"; public static final String LINK_FLAIR_RICHTEXT_KEY = "link_flair_richtext";
public static final String SCORE_KEY = "score"; public static final String SCORE_KEY = "score";
public static final String LIKES_KEY = "likes"; public static final String LIKES_KEY = "likes";
public static final String NSFW_KEY = "over_18"; public static final String NSFW_KEY = "over_18";