diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-03-28 14:03:35 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-03-28 14:03:35 +0000 |
commit | b1d5be17cdfc2c396cf4de40167233f25a1dcda5 (patch) | |
tree | afc7c14269c548565ba0467795fce5b2f8fc6211 /app/src/main | |
parent | 188d58b5dafbb5795a24a57073ff421da113b0a6 (diff) | |
download | infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.tar infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.tar.gz infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.tar.bz2 infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.tar.lz infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.tar.xz infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.tar.zst infinity-for-reddit-b1d5be17cdfc2c396cf4de40167233f25a1dcda5.zip |
Support image post flair.
Diffstat (limited to 'app/src/main')
5 files changed, 45 insertions, 12 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 7b89c940..8a67a44f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -555,9 +555,16 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ((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.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()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index a5504f4a..55fbac6f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -7,6 +7,8 @@ import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.text.Html; +import android.text.Spannable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -66,6 +68,7 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.User.UserDao; +import ml.docilealligator.infinityforreddit.Utils.GlideImageGetter; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; @@ -446,9 +449,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE); } - if (flair != null) { + if (flair != null && !flair.equals("")) { ((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) { @@ -1001,9 +1011,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostCompactViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE); } - if (flair != null) { + if (flair != null && !flair.equals("")) { ((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) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 1d2e2e6f..0974cbdf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -50,10 +50,20 @@ public class ParsePost { boolean archived = data.getBoolean(JSONUtils.ARCHIVED_KEY); boolean locked = data.getBoolean(JSONUtils.LOCKEC_KEY); boolean saved = data.getBoolean(JSONUtils.SAVED_KEY); - String flair = null; - if (!data.isNull(JSONUtils.LINK_FLAIR_TEXT_KEY)) { - flair = data.getString(JSONUtils.LINK_FLAIR_TEXT_KEY); + StringBuilder postFlairHTMLBuilder = new StringBuilder(); + if (data.has(JSONUtils.LINK_FLAIR_RICHTEXT_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)) { voteType = 0; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java index b38f5b52..8e018449 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java @@ -82,7 +82,7 @@ public class GlideImageGetter implements Html.ImageGetter { int drawableWidth = (int) (drawable.getIntrinsicWidth() * density); int drawableHeight = (int) (drawable.getIntrinsicHeight() * density); float ratio = (float) drawableWidth / (float) drawableHeight; - drawableHeight = (int) textSize + 10; + drawableHeight = (int) textSize; drawableWidth = (int) (drawableHeight * ratio); int maxWidth = container.get().getMeasuredWidth(); if ((drawableWidth > maxWidth) || matchParentWidth) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java index 577b16e0..2d7d7919 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java @@ -16,13 +16,12 @@ public class JSONUtils { public static final String SELFTEXT_KEY = "selftext"; public static final String SELFTEXT_HTML_KEY = "selftext_html"; 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 E_KEY = "e"; public static final String T_KEY = "t"; public static final String U_KEY = "u"; 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 LIKES_KEY = "likes"; public static final String NSFW_KEY = "over_18"; |