From 362634d1c7b81b13c9814c7dfdd6ed35612149d4 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 10 Aug 2018 10:24:36 +0800 Subject: Display the subreddit icon in ViewPostDetailActivity. Fixed a bug when the subreddit had no icon, the default icon was not shown. Save the subreddit icon url to BestPostData. Having done saving the subreddit icon url to the instance of BestPostData, skip loading the icon from the database using AsyncTask and load the icon from the instance of BestPostData instead. --- .../infinityforreddit/BestPostData.java | 11 +++++ .../BestPostRecyclerViewAdapter.java | 41 ++++++++++------ .../LoadSubredditIconAsyncTask.java | 57 ++++++++++++++++++++++ .../infinityforreddit/ViewPostDetailActivity.java | 12 ++++- 4 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java index 8c08680c..ce998cff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java @@ -18,6 +18,7 @@ class BestPostData implements Parcelable { private String id; private String fullName; private String subredditName; + private String subredditIconUrl; private String postTime; private String title; private String selfText; @@ -80,6 +81,7 @@ class BestPostData implements Parcelable { id = in.readString(); fullName = in.readString(); subredditName = in.readString(); + subredditIconUrl = in.readString(); postTime = in.readString(); title = in.readString(); selfText = in.readString(); @@ -120,6 +122,14 @@ class BestPostData implements Parcelable { return subredditName; } + public String getSubredditIconUrl() { + return subredditIconUrl; + } + + public void setSubredditIconUrl(String subredditIconUrl) { + this.subredditIconUrl = subredditIconUrl; + } + public String getPostTime() { return postTime; } @@ -214,6 +224,7 @@ class BestPostData implements Parcelable { parcel.writeString(id); parcel.writeString(fullName); parcel.writeString(subredditName); + parcel.writeString(subredditIconUrl); parcel.writeString(postTime); parcel.writeString(title); parcel.writeString(selfText); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java index 7b70ca08..ed3fd0dd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.customtabs.CustomTabsIntent; @@ -99,8 +98,14 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter { - private Context context; - private CircleImageView iconImageView; + /*private static class LoadSubredditIconAsyncTask extends AsyncTask { + private final WeakReference contextWeakReference; + private final WeakReference circleImageViewWeakReference; + private SubredditDao subredditDao; private String subredditName; private String iconImageUrl; LoadSubredditIconAsyncTask(Context context, CircleImageView iconImageView, SubredditDao subredditDao, String subredditName) { - this.context = context; - this.iconImageView = iconImageView; + contextWeakReference = new WeakReference<>(context); + circleImageViewWeakReference = new WeakReference<>(iconImageView); this.subredditDao = subredditDao; this.subredditName = subredditName; } @@ -556,11 +562,16 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter { + private final WeakReference contextWeakReference; + private final WeakReference circleImageViewWeakReference; + + private SubredditDao subredditDao; + private String subredditName; + private String iconImageUrl; + private BestPostData bestPostData; + + LoadSubredditIconAsyncTask(Context context, CircleImageView iconImageView, + SubredditDao subredditDao, String subredditName, + BestPostData bestPostData) { + contextWeakReference = new WeakReference<>(context); + circleImageViewWeakReference = new WeakReference<>(iconImageView); + this.subredditDao = subredditDao; + this.subredditName = subredditName; + this.bestPostData = bestPostData; + } + + @Override + protected Void doInBackground(Void... voids) { + if(subredditDao.getSubredditData(subredditName) != null) { + iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl(); + } else { + iconImageUrl = ""; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + Context context = contextWeakReference.get(); + CircleImageView circleImageView = circleImageViewWeakReference.get(); + + if(context != null && circleImageView != null) { + if(!iconImageUrl.equals("")) { + Glide.with(context).load(iconImageUrl).into(circleImageView); + } else { + Glide.with(context).load(R.drawable.subreddit_default_icon).into(circleImageView); + } + } + + bestPostData.setSubredditIconUrl(iconImageUrl); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index f0f3dcf5..c65d5ab8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -66,7 +66,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { mCoordinatorLayout = findViewById(R.id.coordinator_layout_view_post_detail); - CircleImageView subredditImageView = findViewById(R.id.subreddit_icon_circle_image_view_view_post_detail); + CircleImageView subredditIconCircleImageView = findViewById(R.id.subreddit_icon_circle_image_view_view_post_detail); TextView postTimeTextView = findViewById(R.id.post_time_text_view_view_post_detail); TextView subredditTextView = findViewById(R.id.subreddit_text_view_view_post_detail); TextView contentTextView = findViewById(R.id.content_text_view_view_post_detail); @@ -86,6 +86,16 @@ public class ViewPostDetailActivity extends AppCompatActivity { mCommentCardView = findViewById(R.id.comment_card_view_view_post_detail); mRecyclerView = findViewById(R.id.recycler_view_view_post_detail); + if(mPostData.getSubredditIconUrl() == null) { + new LoadSubredditIconAsyncTask(this, subredditIconCircleImageView, + SubredditRoomDatabase.getDatabase(this).subredditDao(), mPostData.getSubredditName(), + mPostData).execute(); + } else if(!mPostData.getSubredditIconUrl().equals("")) { + Glide.with(this).load(mPostData.getSubredditIconUrl()).into(subredditIconCircleImageView); + } else { + Glide.with(this).load(R.drawable.subreddit_default_icon).into(subredditIconCircleImageView); + } + mRecyclerView.setNestedScrollingEnabled(false); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); -- cgit v1.2.3