From ea779ff74be39ed58589c423035d787764fcb271 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 17 Apr 2020 17:34:04 +0800 Subject: Minor bugs fixed. --- .../Activity/ViewGIFActivity.java | 10 +- .../Activity/ViewImageActivity.java | 6 +- .../Activity/ViewVideoActivity.java | 6 +- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 34 ++--- .../CommentsListingRecyclerViewAdapter.java | 146 ++++++++++++--------- .../Adapter/PostRecyclerViewAdapter.java | 15 +-- .../infinityforreddit/ParseComment.java | 8 +- .../infinityforreddit/ParsePost.java | 10 +- .../infinityforreddit/Utils/Utils.java | 14 ++ app/src/main/res/layout/item_post_detail.xml | 2 +- 10 files changed, 138 insertions(+), 113 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java index ce53e892..323cd7ab 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java @@ -64,7 +64,7 @@ import pl.droidsonroids.gif.GifImageView; public class ViewGIFActivity extends AppCompatActivity { - public static final String IMAGE_URL_KEY = "IUK"; + public static final String GIF_URL_KEY = "GUK"; public static final String FILE_NAME_KEY = "FNK"; public static final String POST_TITLE_KEY = "PTK"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; @@ -120,11 +120,15 @@ public class ViewGIFActivity extends AppCompatActivity { glide = Glide.with(this); Intent intent = getIntent(); - mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); + mImageUrl = intent.getStringExtra(GIF_URL_KEY); mImageFileName = intent.getStringExtra(FILE_NAME_KEY); postTitle = intent.getStringExtra(POST_TITLE_KEY); - setTitle(Html.fromHtml(String.format("%s", postTitle))); + if (postTitle != null) { + setTitle(Html.fromHtml(String.format("%s", postTitle))); + } else { + setTitle(""); + } mLoadErrorLinearLayout.setOnClickListener(view -> { if (!isSwiping) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java index 6b0e0c7d..1ec2de8d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java @@ -129,7 +129,11 @@ public class ViewImageActivity extends AppCompatActivity { mImageFileName = intent.getStringExtra(FILE_NAME_KEY); postTitle = intent.getStringExtra(POST_TITLE_KEY); - setTitle(Html.fromHtml(String.format("%s", postTitle))); + if (postTitle != null) { + setTitle(Html.fromHtml(String.format("%s", postTitle))); + } else { + setTitle(""); + } mLoadErrorLinearLayout.setOnClickListener(view -> { if (!isSwiping) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 2bb35763..11e0d56d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -135,7 +135,11 @@ public class ViewVideoActivity extends AppCompatActivity { videoFileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_ID) + ".mp4"; postTitle = intent.getStringExtra(EXTRA_POST_TITLE); - setTitle(Html.fromHtml(String.format("%s", postTitle))); + if (postTitle != null) { + setTitle(Html.fromHtml(String.format("%s", postTitle))); + } else { + setTitle(""); + } final float pxHeight = getResources().getDisplayMetrics().heightPixels; 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 c003a876..ccbd0e61 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -494,14 +494,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter= android.os.Build.VERSION_CODES.N) { - flairHTML = (Spannable) Html.fromHtml(mPost.getAuthorFlairHTML(), Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null); - } else { - flairHTML = (Spannable) Html.fromHtml(mPost.getAuthorFlairHTML(), glideImageGetter, null); - } - ((PostDetailViewHolder) holder).mAuthorFlairTextView.setText(flairHTML); + Utils.setHTMLWithImageToTextView(((PostDetailViewHolder) holder).mAuthorFlairTextView, mPost.getAuthorFlairHTML()); ((PostDetailViewHolder) holder).mAuthorFlairTextView.setOnClickListener(view -> ((PostDetailViewHolder) holder).mUserTextView.performClick()); } else if (mPost.getAuthorFlair() != null && !mPost.getAuthorFlair().equals("")) { ((PostDetailViewHolder) holder).mAuthorFlairTextView.setVisibility(View.VISIBLE); @@ -587,14 +580,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter= android.os.Build.VERSION_CODES.N) { - awardsHTML = (Spannable) Html.fromHtml(mPost.getAwards(), Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null); - } else { - awardsHTML = (Spannable) Html.fromHtml(mPost.getAwards(), glideImageGetter, null); - } - ((PostDetailViewHolder) holder).mAwardsTextView.setText(awardsHTML); + Utils.setHTMLWithImageToTextView(((PostDetailViewHolder) holder).mAwardsTextView, mPost.getAwards()); } if (mPost.isNSFW()) { @@ -630,6 +616,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter= android.os.Build.VERSION_CODES.N) { - flairHTML = (Spannable) Html.fromHtml(comment.getAuthorFlairHTML(), Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null); - } else { - flairHTML = (Spannable) Html.fromHtml(comment.getAuthorFlairHTML(), glideImageGetter, null); - } - ((CommentViewHolder) holder).authorFlairTextView.setText(flairHTML); + Utils.setHTMLWithImageToTextView(((CommentViewHolder) holder).authorFlairTextView, comment.getAuthorFlairHTML()); ((CommentViewHolder) holder).authorFlairTextView.setOnClickListener(view -> ((CommentViewHolder) holder).authorTextView.performClick()); } else if (comment.getAuthorFlair() != null && !comment.getAuthorFlair().equals("")) { ((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.VISIBLE); @@ -2217,7 +2199,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + ((CommentViewHolder) holder).authorTextView.setText("u/" + comment.getLinkAuthor()); + ((CommentViewHolder) holder).authorTextView.setTextColor(mUsernameColor); + ((CommentViewHolder) holder).authorTextView.setOnClickListener(view -> { Intent intent = new Intent(mContext, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getLinkAuthor()); mContext.startActivity(intent); }); } else { - ((DataViewHolder) holder).authorTextView.setText("r/" + comment.getSubredditName()); - ((DataViewHolder) holder).authorTextView.setTextColor(mSubredditColor); - ((DataViewHolder) holder).authorTextView.setOnClickListener(view -> { + ((CommentViewHolder) holder).authorTextView.setText("r/" + comment.getSubredditName()); + ((CommentViewHolder) holder).authorTextView.setTextColor(mSubredditColor); + ((CommentViewHolder) holder).authorTextView.setOnClickListener(view -> { Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName()); mContext.startActivity(intent); }); } + if (comment.getAuthorFlairHTML() != null && !comment.getAuthorFlairHTML().equals("")) { + ((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.VISIBLE); + Utils.setHTMLWithImageToTextView(((CommentViewHolder) holder).authorFlairTextView, comment.getAuthorFlairHTML()); + } else if (comment.getAuthorFlair() != null && !comment.getAuthorFlair().equals("")) { + ((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.VISIBLE); + ((CommentViewHolder) holder).authorFlairTextView.setText(comment.getAuthorFlair()); + } + if (mShowElapsedTime) { - ((DataViewHolder) holder).commentTimeTextView.setText( + ((CommentViewHolder) holder).commentTimeTextView.setText( Utils.getElapsedTime(mContext, comment.getCommentTimeMillis())); } else { - ((DataViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); + ((CommentViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); + } + + if (comment.getAwards() != null && !comment.getAwards().equals("")) { + Utils.setHTMLWithImageToTextView(((CommentViewHolder) holder).awardsTextView, comment.getAwards()); } - mMarkwon.setMarkdown(((DataViewHolder) holder).commentMarkdownView, comment.getCommentMarkdown()); + mMarkwon.setMarkdown(((CommentViewHolder) holder).commentMarkdownView, comment.getCommentMarkdown()); - ((DataViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, + ((CommentViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, comment.getScore() + comment.getVoteType())); switch (comment.getVoteType()) { case CommentData.VOTE_TYPE_UPVOTE: - ((DataViewHolder) holder).upvoteButton + ((CommentViewHolder) holder).upvoteButton .setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); - ((DataViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); + ((CommentViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); break; case CommentData.VOTE_TYPE_DOWNVOTE: - ((DataViewHolder) holder).downvoteButton + ((CommentViewHolder) holder).downvoteButton .setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); - ((DataViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); + ((CommentViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); break; } - ((DataViewHolder) holder).moreButton.setOnClickListener(view -> { + ((CommentViewHolder) holder).moreButton.setOnClickListener(view -> { Bundle bundle = new Bundle(); if (comment.getAuthor().equals(mAccountName)) { bundle.putString(CommentMoreBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); @@ -214,21 +228,21 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + ((CommentViewHolder) holder).linearLayout.setOnClickListener(view -> { Intent intent = new Intent(mContext, ViewPostDetailActivity.class); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getLinkId()); intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId()); mContext.startActivity(intent); }); - ((DataViewHolder) holder).verticalBlock.setVisibility(View.GONE); + ((CommentViewHolder) holder).verticalBlock.setVisibility(View.GONE); - ((DataViewHolder) holder).commentMarkdownView.setOnClickListener(view -> - ((DataViewHolder) holder).linearLayout.callOnClick()); + ((CommentViewHolder) holder).commentMarkdownView.setOnClickListener(view -> + ((CommentViewHolder) holder).linearLayout.callOnClick()); - ((DataViewHolder) holder).replyButton.setVisibility(View.GONE); + ((CommentViewHolder) holder).replyButton.setVisibility(View.GONE); - ((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> { + ((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> { if (mAccessToken == null) { Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); return; @@ -237,24 +251,24 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + ((CommentViewHolder) holder).downvoteButton.setOnClickListener(view -> { if (mAccessToken == null) { Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); return; @@ -290,23 +304,23 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + ((CommentViewHolder) holder).saveButton.setOnClickListener(view -> { if (comment.isSaved()) { comment.setSaved(false); SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, comment.getFullName(), new SaveThing.SaveThingListener() { @Override public void success() { comment.setSaved(false); - ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); + ((CommentViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); Toast.makeText(mContext, R.string.comment_unsaved_success, Toast.LENGTH_SHORT).show(); } @Override public void failed() { comment.setSaved(true); - ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); + ((CommentViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); Toast.makeText(mContext, R.string.comment_unsaved_failed, Toast.LENGTH_SHORT).show(); } }); @@ -363,14 +377,14 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter= android.os.Build.VERSION_CODES.N) { - awardsHTML = (Spannable) Html.fromHtml(awards, Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null); - } else { - awardsHTML = (Spannable) Html.fromHtml(awards, glideImageGetter, null); - } - ((PostViewHolder) holder).awardsTextView.setText(awardsHTML); + Utils.setHTMLWithImageToTextView(((PostViewHolder) holder).awardsTextView, awards); } switch (voteType) { @@ -553,7 +546,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter"); + authorFlairHTMLBuilder.append(""); } } } @@ -153,10 +153,10 @@ public class ParseComment { JSONArray icons = award.getJSONArray(JSONUtils.RESIZED_ICONS_KEY); if (icons.length() > 4) { String iconUrl = icons.getJSONObject(3).getString(JSONUtils.URL_KEY); - awardingsBuilder.append(" ").append("x").append(count).append(" "); + awardingsBuilder.append(" ").append("x").append(count).append(" "); } else if (icons.length() > 0) { String iconUrl = icons.getJSONObject(icons.length() - 1).getString(JSONUtils.URL_KEY); - awardingsBuilder.append(" ").append("x").append(count).append(" "); + awardingsBuilder.append(" ").append("x").append(count).append(" "); } } int score = singleCommentData.getInt(JSONUtils.SCORE_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 49329844..b8cd4977 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -46,7 +46,7 @@ public class ParsePost { if (e.equals("text")) { authorFlairHTMLBuilder.append(flairObject.getString(JSONUtils.T_KEY)); } else if (e.equals("emoji")) { - authorFlairHTMLBuilder.append(""); + authorFlairHTMLBuilder.append(""); } } } @@ -70,9 +70,9 @@ public class ParsePost { JSONObject flairObject = flairArray.getJSONObject(i); String e = flairObject.getString(JSONUtils.E_KEY); if (e.equals("text")) { - postFlairHTMLBuilder.append(flairObject.getString(JSONUtils.T_KEY)); + postFlairHTMLBuilder.append(Html.escapeHtml(flairObject.getString(JSONUtils.T_KEY))); } else if (e.equals("emoji")) { - postFlairHTMLBuilder.append(""); + postFlairHTMLBuilder.append(""); } } } @@ -85,10 +85,10 @@ public class ParsePost { JSONArray icons = award.getJSONArray(JSONUtils.RESIZED_ICONS_KEY); if (icons.length() > 4) { String iconUrl = icons.getJSONObject(3).getString(JSONUtils.URL_KEY); - awardingsBuilder.append(" ").append("x").append(count).append(" "); + awardingsBuilder.append(" ").append("x").append(count).append(" "); } else if (icons.length() > 0) { String iconUrl = icons.getJSONObject(icons.length() - 1).getString(JSONUtils.URL_KEY); - awardingsBuilder.append(" ").append("x").append(count).append(" "); + awardingsBuilder.append(" ").append("x").append(count).append(" "); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java index fea670d0..f3788ff8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.Utils; import android.content.Context; +import android.text.Html; +import android.text.Spannable; +import android.widget.TextView; import java.util.Locale; @@ -73,4 +76,15 @@ public class Utils { return String.format(Locale.US, "%.1f", (float) votes / 1000) + "K"; } } + + public static void setHTMLWithImageToTextView(TextView textView, String content) { + Spannable html; + GlideImageGetter glideImageGetter = new GlideImageGetter(textView); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + html = (Spannable) Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY, glideImageGetter, null); + } else { + html = (Spannable) Html.fromHtml(content, glideImageGetter, null); + } + textView.setText(html); + } } diff --git a/app/src/main/res/layout/item_post_detail.xml b/app/src/main/res/layout/item_post_detail.xml index dd8b842f..61cf7399 100644 --- a/app/src/main/res/layout/item_post_detail.xml +++ b/app/src/main/res/layout/item_post_detail.xml @@ -58,7 +58,7 @@ android:layout_marginStart="16dp" android:layout_marginEnd="8dp" android:textColor="?attr/username" - android:textSize="?attr/font_default" + android:textSize="?attr/font_12" android:maxLines="2" android:ellipsize="end" android:visibility="gone" -- cgit v1.2.3